"use strict";
import { base } from './base.js'
import { util } from '../../util/util.js'
import { tree } from '../page/query/tree.js'
import {khcModal} from '../alert/khcModal.js'
import {khcToastr} from "../alert/khcToastr.js";
import {config} from "../../config.js";
import {httpUtil} from "../../util/httpUtil.js";
class inputTree extends base {
    constructor(data) {
        super(data);
        this.type = 'tree';

    }
    render(renderParentID) {
        let that=this;
        that.initFunc();
        let data=that.data
        if(data.kind==undefined||data.kind=='default'){
            this.renderHtml(renderParentID).then(()=>{
                util.addEventListener(that.eleID+'showvalue','click',that.formBindFunc)
            })
        }
        if(data.kind=='custom'){
            that.renderCustom(renderParentID);
        }
        if(data.kind=='text_detail'){
            that.renderTextDetail(renderParentID);
        }

    }
    renderHtml(parentObjID) {
        let that=this;
        return new Promise((resolve, reject) => {
            let data = that.data
            let dictDataArray=that.data.dict_code.data;
            if(util.isNull(dictDataArray)){
                dictDataArray=[];
            }
            for(let i=0;i<dictDataArray.length;i++){
                that.dictMap.set(dictDataArray[i].id.toString(),dictDataArray[i]);
            }
            let labelName = data.label_name;
            let eleName = that.data.name;
            let placeholder = data.placeholder;
            let defaultValue = data.default_value;
            let canNull = data.can_null;
            that.initFormCol(that);
            let questionCircleHtml=that.getQuestionCircleIconHtml();
            let htmlTemplate=`
            <div class="form-group row col-sm-${data.col}"  id="${that.objID}" style="display: none">
            <label class="col-form-label col-sm-${that.labelCol} text-sm-right" style="line-height:25px;vertical-align: middle;">
            <font id="${that.objID}CanNull" style="font-size: 22px;vertical-align: middle;${canNull!='Y'?'display:none;':''}" color="red">*</font>
            ${labelName}</label>
            <div class="col-sm-${that.inputCol}">
                <div class="input-group-append">
                    <input type=hidden id="${that.eleID}">
                    <input type="text" id="${that.eleID}showvalue" readonly="readonly" name="${eleName}showvalue"
                    class="form-control" placeholder="${placeholder}">
                    ${data.is_show_input_button=='Y'?`<span class="input-group-append">
                        <button class="btn btn-secondary" type="button" id="${that.eleID}_input_button">${data.input_button_name}</button></span>`:''}
                </div>
                <small class="form-text text-muted"><font style="display:none;"  validate="validate"  color="red">12412345123</font></small>
            </div></div>`;
            document.getElementById(parentObjID).insertAdjacentHTML("beforeEnd", htmlTemplate);
            that.bindFunc();
            resolve();
        })
    }
    initFunc(){
        let that=this;
        that.formBindFunc= ()=>{
            that.renderTree();
        }
        that.clickCallback= ()=>{
            let selectNodes=that.treeView.getSelectedNodes();
            let idValue=selectNodes[0].id;
            let showValue=selectNodes[0].titleName;
            let node = selectNodes[0].getParentNode();
            that.setValue(idValue);
            that.validate();
            that.khcModalForTree.isDestory=true;
            that.khcModalForTree.hide();
        }
        that.getFullShowValue=(id,showValue)=>{
            let treeSplit=!util.isNull(that.data.tree_show_full_split)?that.data.tree_show_full_split:'';
            let thisDict=that.dictMap.get(id.toString())
            if(!util.isNull(thisDict)&&parseInt(thisDict.level)!=0){
                if(!util.isNull(showValue))showValue=treeSplit+showValue
                return that.getFullShowValue(thisDict.pId,thisDict.name+showValue);
            }else{
                return showValue;
            }
        }
        that.bindInputButtonFunc=()=>{
            that.inputButtonFunc();
        }
        that.inputButtonFunc=()=>{//较复杂的代码逻辑请重写此方法
            let myselfFuncStr=util.toOldChar(that.data.input_button_func);
            util.executeDynamicCode(myselfFuncStr,{eleObj:that});
        }
    }
    bindFunc(){
        let that=this;
        if(that.data.is_show_input_button=='Y'){
            document.getElementById(that.eleID+'_input_button').addEventListener('click',that.bindInputButtonFunc);
        }
    }
    renderTree() {
        let that=this;
        if(that.data.is_read_only=='N'){
            let khcModalForTree = new khcModal({ modalAreaID: config.getConfigJson().modalAreaID });
            that.khcModalForTree=khcModalForTree;
            let treeData=util.clone(that.data);
            khcModalForTree.parentModal=that.parentObj.khcModalForForm;
            khcModalForTree.render().then(() => {
                treeData['parentDomID']=khcModalForTree.eleID;
                document.getElementById(khcModalForTree.eleID).setAttribute('class','ztree');
                that.treeView = new tree(treeData);
                that.treeView.openLevel(that.data.tree_open_level)
                that.treeView.onClick((event, treeId, treeNode) => {
                    if(!util.isNull(that.data.is_tree_select_parent)){
                        if(treeNode.isParent) return khcToastr.showErrorMessage('请选择子节点！');
                    }
                    that.clickCallback();
                })
                that.treeView.render();
            });
            khcModalForTree.show();
        }
    }
    setValue(value) {
        let that=this;
        document.getElementById(that.eleID).value = value;
        let showValue='';
        if(!util.isNull(value)&&that.data.tree_show_value_type=='full'){
            if(value!='')showValue=that.getFullShowValue(value,'');
        }else if(!util.isNull(value)&&that.dictMap.get(value.toString())!=undefined){
            showValue= that.dictMap.get(value.toString()).name;
        }
        if(util.isNull(showValue))showValue='';
        if(that.data.kind=='custom'){
            let showObj=document.getElementById(that.eleID+'showvalue');
            if(showObj!=null){
                showObj.innerHTML = showValue;
            }
        }else if(that.data.kind=='text_detail'){
            document.getElementById(that.eleID).innerHTML = showValue;
        }else{
            document.getElementById(that.eleID+'showvalue').value =showValue;
        }
    }
    setDictData(dictData){
        let that=this;
        if(util.isNull(dictData)){
            return ;
        }
        if(util.isNull(that.data.dict_code)){
            that.data.dict_code={data:dictData}
        }else{
            that.data.dict_code.data=dictData;
        }
        let dictDataArray=that.data.dict_code.data;
        if(util.isNull(dictDataArray)){
            dictDataArray=[];
        }
        for(let i=0;i<dictDataArray.length;i++){
            that.dictMap.set(dictDataArray[i].id.toString(),dictDataArray[i]);
        }
    }
    hide(){
        if(this.data.kind=='text_detail')return
        document.getElementById(this.objID).setAttribute('style', 'display:none');//隐藏
    }
    show(){
        if(this.data.kind=='text_detail')return
        document.getElementById(this.objID).setAttribute('style', '');//展示
    }
    //刷新tree字典
    refreshTreeValue(){
        let that=this;
        let treeCode=that.data.dict_code.code;
        if(util.isNull(treeCode)){//如果自定义tree字典，可以将code置为空，再通过setDictData自定义传入即可
            return ;//没有字典直接返回
        }
        let params={treeCode:treeCode,queryParam:{pId:""}}//pId为空则为查询所有
        let obj={url:config.getConfigJson().queryTreeUrl,params:params}
        httpUtil.post(obj, (response)=>{
            that.dictData=response.data.data.treeData;
            that.data.dict_code.data=response.data.data.treeData;
            let dictDataArray=that.data.dict_code.data;
            if(util.isNull(dictDataArray)){
                dictDataArray=[];
            }
            for(let i=0;i<dictDataArray.length;i++){
                that.dictMap.set(dictDataArray[i].id.toString(),dictDataArray[i]);
            }
        })
    }
    renderTextDetail(renderParentID){
        let that=this;
        let data=that.data
        let dictDataArray=that.data.dict_code.data;
        if(util.isNull(dictDataArray)){
            dictDataArray=[];
        }
        for(let i=0;i<dictDataArray.length;i++){
            that.dictMap.set(dictDataArray[i].id.toString(),dictDataArray[i]);
        }
        let html=`<tr>
                                <th>${data.label_name}</th>
                                <td name="${data.name}" id="${that.eleID}"></td>
                            </tr>`
        document.getElementById(renderParentID).insertAdjacentHTML("beforeEnd",html);
    }
}
export { inputTree }