"use strict";
import {buildText} from "./input/buildText.js";
import {util} from "../../../../util/util.js";
import {buildTextarea} from "./input/buildTextarea.js";
import {buildRadio} from "./input/buildRadio.js";
import {buildCheckbox} from "./input/buildCheckbox.js";
import {buildSelect} from "./input/buildSelect.js";
import {buildDate} from "./input/buildDate.js";
import {buildSwitch} from "./input/buildSwitch.js";
import {buildDualList} from "./input/buildDualList.js";
import {buildTag} from "./input/buildTag.js";
import {buildColor} from "./input/buildColor.js";

class formBuild {
    constructor(data) {
        this.data = data;
        //给当前对象创建一个id，同时该id也是对象的domid
        this.eleID = util.getUUID();
        this.objID = this.eleID + 'div';
        this.formDataMap = new Map();
        this.fieldIndex=1;
        //默认的操作按钮
        this.defaultListManager = [
            {
                title: '复制',
                areaName: 'copy',
                iconClass: 'fas fa-copy d-block text-info',
                iconCss: 'color:grey'
            },
            {
                title: '删除',
                areaName: 'remove',
                iconClass: 'far fa-trash-alt d-block text-danger',
                iconCss: 'color:grey'
            }]
    }
    remove() {
        $('#' + this.objID).remove();
    }
    getData() {
        return this.data;
    }
    getObjID() {
        return this.objID;
    }
    getTitle() {
        return this.myTitle;
    }
    setTitle(title) {
        this.myTitle = title;
    }
    initEventFormBuild() {
        let that=this;
        $(`#${that.data.buildFormTitleID}`).attr("contenteditable","true");
        $(`#${that.data.buildFormTitleID}`).attr("style","height: 35px;line-height: 35px;");
    }
    getInputData(key) {
        let that=this;
        // 存储所有可能的 input 初始配置数据
        const data = {
            text: {
                inputData: {
                    show_type: 'text',
                    label_name: '单行文本',
                    placeholder: '这里输入文本呦',
                    default_value: '',
                    is_read_only: '',
                    can_null: '',
                    col: '12',
                    field_layout_type: 'horizontal',
                    rows: 3,
                    dict_code:'',
                    dict_code_json: []
                },
                sort: 'auto',
                showSort: 'Y',
                funcName: 'addText',
                parentDomId: that.data.buildFormStartID,
                parentObj: that,
            },

            textarea: {
                inputData: {
                    show_type: 'textarea',
                    label_name: '多行文本',
                    placeholder: '这里输入文本呦',
                    default_value: '',
                    is_read_only: '',
                    can_null: '',
                    col: '12',
                    field_layout_type: 'horizontal',
                    rows: 3,
                    dict_code:'',
                    dict_code_json: []
                },
                sort: 'auto',
                showSort: 'Y',
                funcName: 'addTextarea',
                parentDomId: that.data.buildFormStartID,
                parentObj: that,
            },

            radio: {
                inputData: {
                    show_type: 'radio',
                    label_name: '单选框',
                    placeholder: '',
                    default_value: '',
                    is_read_only: '',
                    can_null: '',
                    col: '12',
                    field_layout_type: 'horizontal',
                    rows: 3,
                    dict_code:'',
                    dict_code_json: [{key: '1', value: '选项1'}, {key: '2', value: '选项2'}]
                },
                sort: 'auto', showSort: 'Y', funcName: 'addRadio',
                parentDomId: that.data.buildFormStartID,
                parentObj: that
            },

            checkbox: {
                inputData: {
                    show_type: 'checkbox',
                    label_name: '复选框',
                    placeholder: '',
                    default_value: '',
                    is_read_only: '',
                    can_null: '',
                    col: '12',
                    field_layout_type: 'horizontal',
                    rows: 3,
                    dict_code:'',
                    dict_code_json: [{key: '1', value: '选项1'}, {key: '2', value: '选项2'}]
                },
                sort: 'auto', showSort: 'Y', funcName: 'addCheckbox',
                parentDomId: that.data.buildFormStartID,
                parentObj: that
            },

            select: {
                inputData: {
                    show_type: 'select',
                    label_name: '下拉框',
                    placeholder: '',
                    default_value: '',
                    is_read_only: '',
                    can_null: '',
                    col: '12',
                    field_layout_type: 'horizontal',
                    rows: 3,
                    dict_code:'',
                    dict_code_json: [{key: '1', value: '选项1'}, {key: '2', value: '选项2'}]
                },
                sort: 'auto', showSort: 'Y',
                funcName: 'addSelect',
                parentDomId: that.data.buildFormStartID,
                parentObj: that
            },

            dualList: {
                inputData: {
                    show_type: 'dualList',
                    label_name: '穿梭框',
                    placeholder: '',
                    default_value: '',
                    is_read_only: '',
                    can_null: '',
                    col: '12',
                    field_layout_type: 'horizontal',
                    rows: 3,
                    dict_code:'',
                    dict_code_json: [{key: '1', value: '选项1'}, {key: '2', value: '选项2'}]
                },
                sort: 'auto', showSort: 'Y',
                funcName: 'addDualList',
                parentDomId: that.data.buildFormStartID,
                parentObj: that
            },

            date: {
                inputData: {
                    show_type: 'date',
                    label_name: '日期选择',
                    placeholder: '选择日期',
                    default_value: '',
                    is_read_only: '',
                    can_null: '',
                    col: '12',
                    field_layout_type: 'horizontal',
                    rows: 3,
                    dict_code:'',
                    dict_code_json: []
                },
                sort: 'auto', showSort: 'Y',
                funcName: 'addDate',
                parentDomId: that.data.buildFormStartID,
                parentObj: that
            },

            switch: {
                inputData: {
                    show_type: 'switch',
                    label_name: '开关控件',
                    placeholder: '',
                    default_value: '',
                    is_read_only: '',
                    can_null: '',
                    col: '12',
                    field_layout_type: 'horizontal',
                    rows: 3,
                    dict_code:'',
                    dict_code_json: [{key: 'Y', value: '开'}, {key: 'N', value: '关'}],
                    switch_yes_key: 'Y',
                    switch_no_key: 'N'
                },
                sort: 'auto', showSort: 'Y', funcName: 'addSwitch',
                parentDomId: that.data.buildFormStartID,
                parentObj: that
            },

            tag: {
                inputData: {
                    show_type: 'tag',
                    label_name: '标签',
                    placeholder: '',
                    default_value: '',
                    is_read_only: '',
                    can_null: '',
                    col: '12',
                    field_layout_type: 'horizontal',
                    rows: 3,
                    dict_code:'',
                    dict_code_json: []
                },
                sort: 'auto',
                showSort: 'Y',
                funcName: 'addTag',
                parentDomId: that.data.buildFormStartID,
                parentObj: that,
            },

            color: {
                inputData: {
                    show_type: 'color',
                    label_name: '颜色选择',
                    placeholder: '',
                    default_value: '',
                    is_read_only: '',
                    can_null: '',
                    col: '12',
                    field_layout_type: 'horizontal',
                    rows: 3,
                    dict_code:'',
                    dict_code_json: []
                },
                sort: 'auto',
                showSort: 'Y',
                funcName: 'addColor',
                parentDomId: that.data.buildFormStartID,
                parentObj: that,
            },


        };

        // 返回对应的 data，若没有匹配的 key，返回 null
        const result = data[key] || null;

        if(result==null){
            return result;
        }

        // 如果匹配到了 key，但是没有 listManager，则使用默认 listManager
        if (result && !result.listManager) {
            result.listManager = that.defaultListManager;
        }
        result.inputData.name='field'+this.fieldIndex
        this.fieldIndex++;

        return result;
    }
    createInputComponent(currentType, inputData) {
        switch (currentType) {
            case 'text':
                return new buildText(inputData);
            case 'textarea':
                return new buildTextarea(inputData);
            case 'radio':
                return new buildRadio(inputData);
            case 'checkbox':
                return new buildCheckbox(inputData);
            case 'select':
                return new buildSelect(inputData);
            case 'dualList':
                return new buildDualList(inputData);
            case 'date':
                return new buildDate(inputData);
            case 'switch':
                return new buildSwitch(inputData);
            case 'tag':
                return new buildTag(inputData);
            case 'color':
                return new buildColor(inputData);
            default:
                throw new Error(`不支持的输入类型: ${currentType}`);
        }
    }
    toggleInputComponent(currentType,oldInputComponent) {
        let that = this;
        that.removeFormSort(oldInputComponent);
        // 获取当前组件数据
        let inputData = that.getInputData(currentType);
        inputData.sort=oldInputComponent.sort
        inputData.uuid=oldInputComponent.uuid
        // 判断当前类型，决定切换到哪种组件
        let inputComponent=that.createInputComponent(currentType,inputData)

        // 渲染新组件
        inputComponent.reRender();
        // 显示组件并设置新的输入组件
        that.setFormSort(inputComponent);
        this.data.componentFieldListObj.showDiv();
        this.data.componentFieldListObj.setInputComponent(inputComponent);
    }
    createImportJsonInputComponent(inputData) {
        let that = this;
        let newInputData = that.getInputData(inputData.show_type);
        if(newInputData==null){
            console.log( '未加入渲染的表单类型：'+inputData.show_type)
            return ;
        }
        newInputData.inputData=inputData;
        let inputComponent=that.createInputComponent(inputData.show_type,newInputData)
        // 渲染新组件
        inputComponent.render();
        // 显示组件并设置新的输入组件
        that.setFormSort(inputComponent);
        this.data.componentFieldListObj.showDiv();
        this.data.componentFieldListObj.setInputComponent(inputComponent);
    }
    addText(inputData){
        let that=this;
        if(util.isNull(inputData)){
            inputData = that.getInputData('text');
        }
        let inputComponent=new buildText(inputData);
        inputComponent.render();
        inputComponent.scrollToElement()
        that.setFormSort(inputComponent);
        this.data.componentFieldListObj.showDiv();
        this.data.componentFieldListObj.setInputComponent(inputComponent)
    }
    addTextarea(inputData){
        let that=this;
        if(util.isNull(inputData)){
            inputData = that.getInputData('textarea');
        }
        let inputComponent=new buildTextarea(inputData);
        inputComponent.render();
        inputComponent.scrollToElement()
        that.setFormSort(inputComponent);
        this.data.componentFieldListObj.showDiv();
        this.data.componentFieldListObj.setInputComponent(inputComponent)
    }
    addRadio(inputData){
        let that=this;
        if(util.isNull(inputData)){
            inputData = that.getInputData('radio');
        }
        let inputComponent=new buildRadio(inputData);
        inputComponent.render();
        inputComponent.scrollToElement()
        that.setFormSort(inputComponent);
        this.data.componentFieldListObj.showDiv();
        this.data.componentFieldListObj.setInputComponent(inputComponent)
    }
    addCheckbox(inputData){
        let that=this;
        if(util.isNull(inputData)){
            inputData = that.getInputData('checkbox');
        }
        let inputComponent=new buildCheckbox(inputData);
        inputComponent.render();
        inputComponent.scrollToElement()
        that.setFormSort(inputComponent);
        this.data.componentFieldListObj.showDiv();
        this.data.componentFieldListObj.setInputComponent(inputComponent)
    }
    addSelect(inputData){
        let that=this;
        if(util.isNull(inputData)){
            inputData = that.getInputData('select');
        }
        let inputComponent=new buildSelect(inputData);
        inputComponent.render();
        inputComponent.scrollToElement()
        that.setFormSort(inputComponent);
        this.data.componentFieldListObj.showDiv();
        this.data.componentFieldListObj.setInputComponent(inputComponent)
    }
    addDualList(inputData){
        let that=this;
        if(util.isNull(inputData)){
            inputData = that.getInputData('dualList');
        }
        let inputComponent=new buildDualList(inputData);
        inputComponent.render();
        inputComponent.scrollToElement()
        that.setFormSort(inputComponent);
        this.data.componentFieldListObj.showDiv();
        this.data.componentFieldListObj.setInputComponent(inputComponent)
    }
    addDate(inputData){
        let that=this;
        if(util.isNull(inputData)){
            inputData = that.getInputData('date');
        }
        let inputComponent=new buildDate(inputData);
        inputComponent.render();
        inputComponent.scrollToElement()
        that.setFormSort(inputComponent);
        this.data.componentFieldListObj.showDiv();
        this.data.componentFieldListObj.setInputComponent(inputComponent)
    }
    addSwitch(inputData){
        let that=this;
        if(util.isNull(inputData)){
            inputData = that.getInputData('switch');
        }
        let inputComponent=new buildSwitch(inputData);
        inputComponent.render();
        inputComponent.scrollToElement()
        that.setFormSort(inputComponent);
        this.data.componentFieldListObj.showDiv();
        this.data.componentFieldListObj.setInputComponent(inputComponent)
    }
    addTag(inputData){
        let that=this;
        if(util.isNull(inputData)){
            inputData = that.getInputData('tag');
        }
        let inputComponent=new buildTag(inputData);
        inputComponent.render();
        inputComponent.scrollToElement()
        that.setFormSort(inputComponent);
        this.data.componentFieldListObj.showDiv();
        this.data.componentFieldListObj.setInputComponent(inputComponent)
    }
    addColor(inputData){
        let that=this;
        if(util.isNull(inputData)){
            inputData = that.getInputData('color');
        }
        let inputComponent=new buildColor(inputData);
        inputComponent.render();
        inputComponent.scrollToElement()
        that.setFormSort(inputComponent);
        this.data.componentFieldListObj.showDiv();
        this.data.componentFieldListObj.setInputComponent(inputComponent)
    }
    removeFormSort(component){
        let that=this;
        that.formDataMap.delete(component.getObjID())
    }
    setFormSort(component) {
        let that=this;
        if (component.data.sort == 'auto') {
            if (that.formDataMap.size == 0) {
                component.data.sort = 10;
            } else {
                let arr = []; //遍历json数组，把所有組件的sort存到arr这个空数组当中
                that.formDataMap.forEach(function (value, key) {
                    arr.push(value.data.sort);
                });
                let maxSort = Math.max.apply(null, arr);
                component.data.sort = maxSort + (10 - maxSort % 10);
            }
        }
        that.formDataMap.set(component.getObjID(), component)
        //refreshSort()
    }
}
export { formBuild }