"use strict";
import { inputText } from '../../input/inputText.js';
import { inputDate } from '../../input/inputDate.js';
import { inputSelect } from '../../input/inputSelect.js';
import { inputRadio } from '../../input/inputRadio.js';
import { inputCheckBox } from '../../input/inputCheckBox.js';
import { queryButtons } from './queryButtons.js';
import { table } from './table/table.js';
import { fullMap } from '../../../fullMap.js';
import { util } from '../../../util/util.js'
import { khcAlert } from '../../alert/khcAlert.js'
import {tree} from './tree.js'
import {inputAutocomplete} from "../../input/inputAutocomplete.js";
import {config} from "../../../config.js";
import {inputHidden} from "../../input/inputHidden.js";
import {inputTag} from "../../input/inputTag.js";
import {httpUtil} from "../../../util/httpUtil.js";
import {khcMask} from "../../alert/khcMask.js";
import {queryUtil} from "./queryUtil.js";
import {simpleCard} from "../../simplecard/simpleCard.js";



class query {
    constructor(data) {
        this.eleID = util.getUUID();
        this._objID = util.getUUID() + 'div';
        this.tableView;//对应的表格组件
        this.parentObjID = data.parentObjID;
        this.code=data.code;
        this.pageType=data.pageType;
        this.title = '';
        this.pkFieldName = '';//主键字段，给表格用的
        this.myjs = '';//子定义js
        this.queryData = {};//查询视图的定义数据
        this.queryMap = new Map();//所生成的查询字段，给表格获取查询条件的值使用
        this.querySimpleCardMap = new Map();//所生成的simpleCard对象使用
        this.pageID=data.pageID
        this.haveTree=false;
        this.queryInfo=null;
    }
    getCode(){
        return this.code;
    }
    getObjID(){
        return this._objID;
    }
    getQueryMap(){
        return this.queryMap;
    }
    getTableObj(){
        return this.tableView;
    }
    init() {
        let code = this.code;
        let that = this;
        let obj={url:config.getConfigJson().metaUrl,params:code,isClientCache:true}
        return httpUtil.post(obj, (response)=>{
            that.pkFieldName = response.data.data.ds.ds_pk_id;
            that.queryData = response.data.data.query.queryData;
            that.queryInfo=response.data.data.query.queryInfo;
        });
    }
    render(){
        let that=this;
        that.init().then(() => {
            that.runQueryRenderBefore();
            that.renderDetail().then(() => {
                that.renderSimpleCard().then(()=>{
                    that.runQueryRenderAfter();
                })
            })
        }).then(()=>{
            this.renderTable();
        });
    }
    renderDetail() {
        return new Promise((resolve, reject) => {
            this.queryMap = new Map();
            let that=this;
            let titleFontSize=util.getFontSizeByLevel(6);
            let htmlTemplate=`
            <div class="row" id="${that.eleID}_header_simple_card"></div>
            <div class="card mb-4" id="${that.eleID}">
            ${this.pageType=='pageTabs'?'':`<h6 class="card-header card-header pb-2 pt-2" style="font-size: ${titleFontSize}">${that.queryInfo.query_name}</h6>`}
            <div class="card-body pb-2 pt-3" >
            <div forrows style="margin-bottom: -0.5rem;" class="form-group row col-sm-12" id="${that.getObjID()}"></div>
            <div forrows id="${that.getObjID()}manager"></div>
            </div></div>`;
            document.getElementById(that.parentObjID).insertAdjacentHTML("beforeEnd", htmlTemplate);
            let qUtil=new queryUtil(that);
            //renderInput()方法是渲染表单的各元素
            qUtil.renderInput().then(()=>{
                let eleMap=that.getQueryMap();
                eleMap.forEach( (obj,key)=>{//默认渲染的表单是隐藏的，需要遍历
                    if(obj.data.is_query == 'Y'){
                        if(obj.data.show_type!='hidden'){
                            obj.show();
                        }
                    }
                })
                //is_show_query是query组件的配置
                //isShowQuery变量在queryUtil赋予状态
                if(!that.queryInfo.is_show_query=='Y'||!that.isShowQuery){
                    that.hide();
                }
                if(that.queryInfo.is_show_table_search=='Y'){
                    that.hide();
                }
                let queryShowButtons = new queryButtons({parentObjID:that.getObjID(),queryButtonArray:that.queryInfo.queryButtonArray});
                queryShowButtons.render();
                this.queryMap.set('buttons', queryShowButtons);

                let queryButtonDivClass = document.getElementById(queryShowButtons.getObjID()).getAttribute("class");
                let newClass = queryButtonDivClass.concat(" pull-right");
                document.getElementById(queryShowButtons.getObjID()).setAttribute("class", newClass);
                resolve();
            })
        })
    }
    renderSimpleCard(){
        let that=this;
        return new Promise((resolve,reject)=>{
            let querySimpleCardArray=that.queryInfo['querySimpleCardArray'];
            if(util.isNull(querySimpleCardArray)||querySimpleCardArray.length==0){
                resolve();
                return ;
            }
            for (let i = 0; i < querySimpleCardArray.length; i++) {
                let code={ version: '1.0.0',simpleCardCode:querySimpleCardArray[i].simple_card_code,simpleCardData:querySimpleCardArray[i],col: querySimpleCardArray[i].col};
                let simpleCardCode = { parentObjID: util.getUUID() + 'div', type: 'simpleCard', code: code }
                let simpleCardConfig = { parentObjID: `${that.eleID}_header_simple_card`};
                simpleCardConfig['simpleCardCode']=simpleCardCode
                let thisSimpleCard = new simpleCard(simpleCardConfig);
                thisSimpleCard.render();
                fullMap.push(thisSimpleCard.objID, thisSimpleCard);
                that[querySimpleCardArray[i]['simple_card_code']]=thisSimpleCard;
                that.querySimpleCardMap.set(querySimpleCardArray[i]['simple_card_code'], thisSimpleCard);
            }
            resolve();
        })
    }
    renderTable(){
        let that=this;
        return new Promise((resolve,reject)=>{
            let tableData={parentObjID:that.parentObjID,code:that.code,queryMap:that.queryMap,queryObjID:that.getObjID()};
            tableData['queryInfo']=that.queryInfo;
            if(that.haveTree){
                tableData['haveTree']=true;
                tableData['treeView']=that.treeView;
            }
            let tableView=new table(tableData);
            tableView.render();
            that.tableView=tableView;
            resolve();
        })

    }
    hide(){
        document.getElementById(this.eleID).style.display='none'
    }
    show(){
        document.getElementById(this.eleID).style.display=''
    }
    toggle() {
        const el = document.getElementById(this.eleID);
        el.style.display = el.style.display === 'none' ? '' : 'none';
    }
    runQueryRenderAfter(){
        let that=this;
        if(!util.isNull(that.queryInfo.js_file)&&!util.isNull(that.queryInfo.query_render_after)){
            fullMap.push(that.getObjID(),that);
            let jsonConfig={objID:that.getObjID(),button_method:that.queryInfo.query_render_after}
            util.import2023(that.queryInfo.js_file,jsonConfig);
        }
    }
    runQueryRenderBefore(){
        let that=this;
        if(!util.isNull(that.queryInfo.js_file)&&!util.isNull(that.queryInfo.query_render_before)){
            fullMap.push(that.getObjID(),that);
            let jsonConfig={objID:that.getObjID(),button_method:that.queryInfo.query_render_before}
            util.import2023(that.queryInfo.js_file,jsonConfig);
        }
    }
    search(){
        //增加遮罩，防止暴力点击请求
        fullMap.getObj('mask').show('正在查询...');
        this.getTableObj().page=0;
        this.getTableObj().refresh().then(()=>{
            fullMap.getObj('mask').hide(500)
        });
    }
}
export { query };