import {util} from "../../../util/util.js";
import {inputText} from "../../input/inputText.js";
import {inputSelect} from "../../input/inputSelect.js";
import {inputCheckBox} from "../../input/inputCheckBox.js";
import {inputRadio} from "../../input/inputRadio.js";
import {inputDate} from "../../input/inputDate.js";
import {inputAutocomplete} from "../../input/inputAutocomplete.js";
import {inputTag} from "../../input/inputTag.js";
import {inputHidden} from "../../input/inputHidden.js";
import {inputFile} from "../../input/inputFile.js";
import {inputImage} from "../../input/inputImage.js";
import {vCutChar} from "../../input/vCutChar.js";
import {vCutCharFold} from "../../input/vCutCharFold.js";
import {vTabs} from "../../input/vTabs.js";
import {inputTextArea} from "../../input/inputTextArea.js";
import {vChildPage} from "../../input/vChildPage.js";
import {inputDualList} from "../../input/inputDualList.js";
import {inputEditor} from "../../input/inputEditor.js";
import {inputMarkdown} from "../../input/inputMarkdown.js";
import {vOpinion} from "../../input/vOpinion.js";
import {inputTree} from "../../input/inputTree.js";
import {inputMultiPicker} from "../../input/inputMultiPicker.js";
import {inputSwitch} from "../../input/inputSwitch.js";
import {inputTreeCheckBox} from "../../input/inputTreeCheckBox.js";
import {inputColor} from "../../input/inputColor.js";
import {vCutCharTitle} from "../../input/vCutCharTitle.js";
import {vPage} from "../../input/vPage.js";
import {inputSelectPage} from "../../input/inputSelectPage.js";
import {inputSelectCheckBox} from "../../input/inputSelectCheckBox.js";
import {inputSelectSearch} from "../../input/inputSelectSearch.js";
import {vCutSteps} from "../../input/vCutSteps.js";
import {khcToastr} from "../../alert/khcToastr.js";
import {inputHtmlView} from "../../input/inputHtmlView.js";
import {inputJson} from "../../input/inputJson.js";
import {inputHtmlEdit} from "../../input/inputHtmlEdit.js";
import {vCutRowChar} from "../../input/vCutRowChar.js";
import {inputKV} from "../../input/inputKV.js";
import {inputSegmentedProgressBar} from "../../input/inputSegmentedProgressBar.js";

class formUtil {
    constructor(data) {
        this.formObj=data;
    }
    /**
     * 由于组件越来越多，持续的写 if else 导致分支太多 不易于后续开发运维
     * 优化方案，使用映射的方式来优化
     * 数组里面有两项，第一项是条件，第二项是满足后要执行的内容。
     */
    _render(inputData){
        let that=this;
        const map=
            [[
                ()=>inputData.show_type=='text',()=>{
                    that._renderText(inputData)
                }],[
                ()=>inputData.show_type=='textarea',()=>{
                    that._renderTextarea(inputData)
                }],[
                ()=>inputData.show_type=='select',()=>{
                    that._renderSelect(inputData)
                }],[
                ()=>inputData.show_type=='selectCheckbox',()=>{
                    that._renderSelectCheckbox(inputData)
                }],[
                ()=>inputData.show_type=='selectPage',()=>{
                    that._renderSelectPage(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=='switch',()=>{
                    that._renderSwitch(inputData)
                }],[
                ()=>inputData.show_type=='multiPicker',()=>{
                    that._renderMultiPicker(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=='treeCheckBox',()=>{
                    that._renderTreeCheckBox(inputData)
                }],[
                ()=>inputData.show_type=='hidden',()=>{
                    that._renderHidden(inputData)
                }],[
                ()=>inputData.show_type=='color',()=>{
                    that._renderColor(inputData)
                }],[
                ()=>inputData.show_type=='file',()=>{
                    that._renderFile(inputData)
                }],[
                ()=>inputData.show_type=='image',()=>{
                    that._renderImage(inputData)
                }],[
                ()=>inputData.show_type=='json',()=>{
                    that._renderJson(inputData)
                }],[
                ()=>inputData.show_type=='kv',()=>{
                    that._renderKV(inputData)
                }],[
                ()=>inputData.show_type=='htmlEdit',()=>{
                    that._renderHtmlEdit(inputData)
                }],[
                ()=>inputData.show_type=='htmlView',()=>{
                    that._renderHtmlView(inputData)
                }],[
                ()=>inputData.show_type=='cutChar',()=>{
                    that._renderCutChar(inputData)
                }],[
                ()=>inputData.show_type=='cutRowChar',()=>{
                    that._renderCutRowChar(inputData)
                }],[
                ()=>inputData.show_type=='cutCharFold',()=>{
                    that._renderCutCharFold(inputData)
                }],[
                ()=>inputData.show_type=='cutCharTitle',()=>{
                    that._renderCutCharTitle(inputData)
                }],[
                ()=>inputData.show_type=='cutSteps',()=>{
                    that._renderCutSteps(inputData)
                }],[
                ()=>inputData.show_type=='tabs',()=>{
                    that._renderTabs(inputData)
                }],[
                ()=>inputData.show_type=='childPage',()=>{
                    that._renderChildPage(inputData)
                }],[
                ()=>inputData.show_type=='page',()=>{
                    that._renderPage(inputData)
                }],[
                ()=>inputData.show_type=='dualList',()=>{
                    that._renderDualList(inputData)
                }],[
                ()=>inputData.show_type=='editor',()=>{
                    that._renderEditor(inputData)
                }],[
                ()=>inputData.show_type=='segmentedProgressBar',()=>{
                    that._renderSegmentedProgressBar(inputData)
                }],[
                ()=>inputData.show_type=='markdown',()=>{
                    that._renderMarkdown(inputData)
                }],[
                ()=>inputData.show_type=='opinion',()=>{
                    that._renderOption(inputData)
                }]
            ]
        let target=map.find((m)=>m[0]());//find()是es6新加的方法，具体可以看ES6文档
        if(target){
            return target[1]()
        }else{
            console.log( '未加入渲染的表单类型：'+inputData.show_type)
        }
    }
    renderInput(){
        let that=this;
        return new Promise((resolve, reject) => {
            that.formObj.rows=Math.floor(12 / that.formObj.cols).toString();
            that.formObj.firstParentObjID=that.formObj.eleParentObjID;//标记第一个id
            for (let i = 0; i < that.formObj.formStructureData.formData.length; i++) {
                let inputData = that.formObj.formStructureData.formData[i].data;
                //给每个元素定位，如果有布局文件，在此将domId，设定为布局文件里各个元素位置的domId
                inputData.parentObjID = that.formObj.getObjID();
                inputData.componentType = 'form';
                //如果第一个不是cutChar，就补齐card-body
                if(that.formObj.kind!='text_detail'){
                    //如果第一个不是cutChar，就补齐card-body
                    if (i == 0&&inputData.show_type != 'cutChar') {
                        let classAtr = document.getElementById(that.formObj.getObjID()).getAttribute("class");
                        let newClass = classAtr.concat("card-body");
                        document.getElementById(that.formObj.getObjID()).setAttribute("class", newClass);
                        let divId = util.getUUID() + 'div';
                        let divHtml = '<div forrows style="margin-bottom: -0.5rem;" class="form-group row col-sm-12 pt-2" id="' + divId + '"></div>'
                        document.getElementById(that.formObj.firstParentObjID).insertAdjacentHTML("beforeEnd", divHtml);
                        that.formObj.eleParentObjID=divId;
                    }
                }else if(i == 0){
                    let divId = util.getUUID() + 'div';
                    let divHtml = `<table class="table_khc2_show_detail" id="tableDetail" border="0" cellpadding="0" cellspacing="0" width="900" id="${divId}_table">
                            <tbody id="${divId}">
                            </tbody>
                        </table>`
                    document.getElementById(that.formObj.firstParentObjID).insertAdjacentHTML("beforeEnd", divHtml);
                    that.formObj.eleParentObjID=divId;
                }
                if(that.formObj.kind=='custom'){//自定义布局
                    inputData.kind='custom';
                    that.formObj.eleParentObjID=that.formObj.parentObjID;
                }else if (that.formObj.kind=='text_detail'){
                    inputData.kind='text_detail';
                }else{
                    inputData.renderParentObjID=that.formObj.vInputParentObjID;
                    if(!util.isNull(that.formObj.vInputParentObjID)){
                        that.formObj.eleParentObjID=that.formObj.vInputParentObjID;
                    }
                }
                that._render(inputData)
            }
            if(that.formObj.formStructureData.formInfo.is_bpm_model == 'Y'){
                let dataJson={"field_sort":999,"can_null":"N","create_time":"2022-08-08 16:27:25","validate_code":"none","form_id":49,"default_value":"","field_unit":"","type":"String","rows":5,"dict_code":"","uuid":"fcdafb4bd5884fe4a3548726781abce6","show_type":"opinion","field_show_type":"editShow","update_time":"2022-08-08 16:53:35","update_user":"廖家辉辉辉辉辉","user_area":"","name":"work_order_opinion","is_read_only":"N","id":930,"placeholder":"","label_name":"填写意见","status":"0"}
                that._render(dataJson)
            }
            if(that.formObj.isCutSteps){
                that.formObj.initStepsFunc();
            }
            resolve();
        })

    }
    _renderText(inputData){
        let that=this
        let element = new inputText(inputData);//初始化
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderTextarea(inputData){
        let that=this
        let element = new inputTextArea(inputData);//初始化
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderSelect(inputData){
        let that=this;
        let element = new inputSelect(inputData);//初始化
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }

    _renderSelectCheckbox(inputData){
        let that=this;
        let element = new inputSelectCheckBox(inputData);//初始化
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }

    _renderSelectPage(inputData){
        let that=this;
        let element = new inputSelectPage(inputData);//初始化
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }

    _renderSelectSearch(inputData){
        let that=this;
        let element = new inputSelectSearch(inputData);//初始化
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }

    _renderCheckbox(inputData){
        let that=this;
        let element = new inputCheckBox(inputData);//初始化
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }

    _renderRadio(inputData){
        let that=this;
        let element = new inputRadio(inputData);//初始化
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderSwitch(inputData){
        let that=this;
        let element = new inputSwitch(inputData);//初始化
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderMultiPicker(inputData){
        let that=this;
        let element = new inputMultiPicker(inputData);//初始化
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderDate(inputData){
        let that=this;
        let element = new inputDate(inputData);//初始化
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderAutocomplete(inputData){
        let that=this;
        let element=new inputAutocomplete(inputData);
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderTag(inputData){
        let that=this;
        let element = new inputTag(inputData);//初始化
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderTree(inputData){
        let that=this;
        let element = new inputTree(inputData);//初始化
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderTreeCheckBox(inputData){
        let that=this;
        let element = new inputTreeCheckBox(inputData);//初始化
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderHidden(inputData){
        let that=this;
        let element=new inputHidden(inputData);
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderColor(inputData){
        let that=this;
        let element=new inputColor(inputData);
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderFile(inputData){
        let that=this;
        let element=new inputFile(inputData);
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderImage(inputData){
        let that=this;
        let element=new inputImage(inputData);
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderJson(inputData){
        let that=this;
        let element=new inputJson(inputData);
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderKV(inputData){
        let that=this;
        let element=new inputKV(inputData);
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderHtmlEdit(inputData){
        let that=this;
        let element=new inputHtmlEdit(inputData);
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderHtmlView(inputData){
        let that=this;
        let element=new inputHtmlView(inputData);
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderCutChar(inputData){
        let that=this;
        let element = new vCutChar(inputData);//初始化
        element.render(that.formObj.parentObjID);//渲染
        if(inputData.kind!='custom'){
            that.formObj.vInputParentObjID = element.getObjID();
            that.formObj.renderParentObjID = element.getObjID();
        }
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderCutRowChar(inputData){
        let that=this;
        let element = new vCutRowChar(inputData);//初始化
        element.render(that.formObj.parentObjID);//渲染
        if(inputData.kind!='custom'){
            that.formObj.vInputParentObjID = element.getObjID();
            that.formObj.renderParentObjID = element.getObjID();
        }
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderCutCharFold(inputData){
        let that=this;
        let element = new vCutCharFold(inputData);//初始化
        element.render(that.formObj.parentObjID);//渲染
        if(inputData.kind!='custom'){
            that.formObj.vInputParentObjID = element.getObjID();
            that.formObj.renderParentObjID = element.getObjID();
        }
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderCutCharTitle(inputData){
        let that=this;
        let element=new vCutCharTitle(inputData);//注：这个没有拿组件id做父级渲染id
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderCutSteps(inputData){
        let that=this;
        let element = new vCutSteps(inputData);//初始化
        element.render(that.formObj.parentObjID);//渲染
        that.formObj.cutStepsMap.set(element.getObjID(),element);
        if(inputData.kind!='custom'){
            that.formObj.vInputParentObjID = element.getObjID();
            that.formObj.renderParentObjID = element.getObjID();
        }
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderTabs(inputData){
        let that=this;
        let element = new vTabs(inputData);//初始化
        element.render(that.formObj.parentObjID);//渲染
        that.formObj.tabsMap.set(element.getObjID(),element);
        if(inputData.kind!='custom'){
            that.formObj.vInputParentObjID = element.getObjID();
            that.formObj.renderParentObjID = element.getObjID();
        }
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderChildPage(inputData){
        let that=this;
        inputData.formObjID=that.formObj.getObjID()
        let childPageUuid=`${that.formObj.eleParentObjID}_child_page`;
        let childPageDivHtml=`<div id="${childPageUuid}" class="col-12"></div>`;//子表渲染比较慢由于异步的原因总会是在最后渲染，单独置一个div
        document.getElementById(that.formObj.eleParentObjID).insertAdjacentHTML("beforeEnd", childPageDivHtml);
        let element=new vChildPage(inputData);
        element.render(childPageUuid);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderPage(inputData){
        let that=this;
        inputData.formObjID=that.formObj.getObjID()
        let element=new vPage(inputData);
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderDualList(inputData){
        let that=this;
        let element=new inputDualList(inputData);
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderEditor(inputData){
        let that=this;
        let element=new inputEditor(inputData);
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderSegmentedProgressBar(inputData){
        let that=this;
        let element=new inputSegmentedProgressBar(inputData);
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderMarkdown(inputData){
        let that=this;
        let element=new inputMarkdown(inputData);
        element.render(that.formObj.eleParentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }
    _renderOption(inputData){
        let that=this;
        let element=new vOpinion(inputData);
        element.render(that.formObj.parentObjID);//渲染
        that.formObj.formMap.set(inputData.name.toString(), element);
    }

}
export {formUtil}