import {util} from "../../../util/util.js";
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 {inputAutocomplete} from "../../input/inputAutocomplete.js";
import {inputTag} from "../../input/inputTag.js";
import {tree} from "./tree.js";
import {inputHidden} from "../../input/inputHidden.js";
import {inputSelectCheckBox} from "../../input/inputSelectCheckBox.js";
import {inputSelectSearch} from "../../input/inputSelectSearch.js";

class queryUtil {
    constructor(data) {
        this.queryObj=data;
    }
    /**
     * 由于组件越来越多，持续的写 if else 导致分支太多 不易于后续开发运维
     * 优化方案，使用映射的方式来优化
     * 数组里面有两项，第一项是条件，第二项是满足后要执行的内容。
     */
    _render(inputData){
        let that=this;
        const map=
            [[
                ()=>inputData.show_type=='text',()=>{
                    that._renderText(inputData)
                }],[
                ()=>inputData.show_type=='select',()=>{
                    that._renderSelect(inputData)
                }],[
                ()=>inputData.show_type=='selectCheckbox',()=>{
                    that._renderSelectCheckbox(inputData)
                }],[
                ()=>inputData.show_type=='selectSearch',()=>{
                    that._renderSelectSearch(inputData)
                }],[
                ()=>inputData.show_type=='checkbox',()=>{
                    that._renderCheckbox(inputData)
                }],[
                ()=>inputData.show_type=='radio',()=>{
                    that._renderRadio(inputData)
                }],[
                ()=>inputData.show_type=='date',()=>{
                    that._renderDate(inputData)
                }],[
                ()=>inputData.show_type=='autocomplete',()=>{
                    that._renderAutocomplete(inputData)
                }],[
                ()=>inputData.show_type=='tag',()=>{
                    that._renderTag(inputData)
                }],[
                ()=>inputData.show_type=='tree',()=>{
                    that._renderTree(inputData)
                }],[
                ()=>inputData.show_type=='hidden',()=>{
                    that._renderHidden(inputData)
                }]
            ]
        let target=map.find((m)=>m[0]());//find()是es6新加的方法，具体可以看ES6文档
        if(target){
            that.queryObj.showNum++;
            return target[1]()
        }else{
            console.log( '未加入渲染的表单类型：'+inputData.show_type)
        }
    }
    renderInput(){
        let that=this;
        return new Promise((resolve, reject) => {
            that.queryObj.eleParentObjID = that.queryObj.getObjID();
            for (let i = 0; i < that.queryObj.queryData.length; i++) {
                let inputData = that.queryObj.queryData[i].data;
                inputData.componentType = 'query';
                inputData.placeholder = '';
                //给每个元素定位，如果有布局文件，在此将domid，设定为布局文件里各个元素位置的domid
                if (inputData.is_query == 'Y') {
                    that._render(inputData)
                    resolve();
                }
            }
            resolve();
        })

    }
    _renderText(inputData){
        let that=this
        that.queryObj.isShowQuery=true;
        let element = new inputText(inputData);//初始化
        element.render(that.queryObj.eleParentObjID);//渲染
        that.queryObj.queryMap.set(element.getData().name, element);
    }

    _renderSelect(inputData){
        let that=this;
        that.queryObj.isShowQuery=true;
        let element = new inputSelect(inputData);//初始化
        element.render(that.queryObj.eleParentObjID);//渲染
        that.queryObj.queryMap.set(element.getData().name, element);
    }

    _renderSelectCheckbox(inputData){
        let that=this;
        that.queryObj.isShowQuery=true;
        let element = new inputSelectCheckBox(inputData);//初始化
        element.render(that.queryObj.eleParentObjID);//渲染
        that.queryObj.queryMap.set(element.getData().name, element);
    }

    _renderSelectSearch(inputData){
        let that=this;
        that.queryObj.isShowQuery=true;
        let element = new inputSelectSearch(inputData);//初始化
        element.render(that.queryObj.eleParentObjID);//渲染
        that.queryObj.queryMap.set(element.getData().name, element);
    }

    _renderCheckbox(inputData){
        let that=this;
        that.queryObj.isShowQuery=true;
        let element = new inputCheckBox(inputData);//初始化
        element.render(that.queryObj.eleParentObjID);//渲染
        that.queryObj.queryMap.set(element.getData().name, element);
    }

    _renderRadio(inputData){
        let that=this;
        if (inputData.show_type == 'radio') {
            that.queryObj.isShowQuery=true;
            let element = new inputRadio(inputData);//初始化
            element.render(that.queryObj.eleParentObjID);//渲染
            that.queryObj.queryMap.set(element.getData().name, element);
        }
    }
    _renderDate(inputData){
        let that=this;
        that.queryObj.isShowQuery=true;
        inputData.is_read_only = 'Y'
        if (inputData.query_type == '0') {
            let element = new inputDate(inputData);//初始化
            element.render(that.queryObj.eleParentObjID);//渲染
            that.queryObj.queryMap.set(element.getData().name, element);
        } else if (inputData.query_type == '1') {
            let element = new inputDate(inputData);//初始化
            element.render(that.queryObj.eleParentObjID);//渲染
            that.queryObj.queryMap.set(element.getData().name, element);
        } else if (inputData.query_type == '2') {
            if ((inputData.name.indexOf('start') == 0) && (that.queryObj.showNum % that.queryObj.cols != 0)) {
                let divId = util.getUUID() + 'div';
                let divHtml = '<div forrows style="margin-bottom: -0.5rem;" class="form-group row col-sm-12" id="' + divId + '"></div>'
                document.getElementById(that.queryObj.getObjID()).insertAdjacentHTML("beforeEnd", divHtml);
                that.queryObj.eleParentObjID = divId;
            }
            let element = new inputDate(inputData);//初始化
            element.render(that.queryObj.eleParentObjID);//渲染
            that.queryObj.queryMap.set(element.getData().name, element);
        }
    }
    _renderAutocomplete(inputData){
        let that=this;
        that.queryObj.isShowQuery=true;
        let element=new inputAutocomplete(inputData);
        element.render(that.queryObj.eleParentObjID);//渲染
        that.queryObj.queryMap.set(inputData.name.toString(), element);
    }
    _renderTag(inputData){
        let that=this;
        that.queryObj.isShowQuery=true;
        let element = new inputTag(inputData);//初始化
        element.render(that.queryObj.eleParentObjID);//渲染
        that.queryObj.queryMap.set(inputData.name.toString(), element);
    }
    _renderTree(inputData){
        let that=this;
        inputData['parentDomID']=util.getUUID()+'div'
        that.queryObj.treeView=new tree(inputData);
        that.queryObj.treeView.openLevel(inputData.tree_open_level)
        that.queryObj.treeView.onClick(()=>{
            that.queryObj.getTableObj().page=0;
            that.queryObj.getTableObj().refresh();
        })
        that.queryObj.haveTree=true;
        that.queryObj.queryMap.set(inputData.name, that.queryObj.treeView);
    }
    _renderHidden(inputData){
        let that=this;
        let element=new inputHidden(inputData);
        element.render(that.queryObj.eleParentObjID);//渲染
        that.queryObj.queryMap.set(inputData.name.toString(), element);
    }

}
export { queryUtil }