"use strict";
import {base} from './base.js'
import { util } from '../../util/util.js'
import {khcAlert} from "../alert/khcAlert.js";
class inputSwitch extends base {
    constructor(data) {
        super(data);
        this.type='switch';
    }
    render(renderParentID) {
        let that=this;
        this.initFunc();
        let data=that.data
        if(data.kind==undefined||data.kind=='default'){
            that.renderDefault(renderParentID);
        }
        if(data.kind=='custom'){
            that.renderCustom(renderParentID);
        }
        if(data.kind=='text_detail'){
            that.renderTextDetail(renderParentID);
        }
    }
    renderDefault(renderParentID){
        let that=this;
        that.initFunc()
        let data = that.data
        that.switchYesKey=data.switch_yes_key;//选中的key
        that.switchNoKey=data.switch_no_key;//未选中的key
        if(that.switchYesKey.length==0||that.switchNoKey.length==0){
            khcAlert.showSystemError(data.label_name+' 类型是：'+that.type+' 需设置选中的key和未选中的key')
            return ;
        }
        let objID = that.objID;
        let eleID = that.eleID;
        let defaultValue=data.default_value;
        let canNull=data.can_null;
        let labelName=data.label_name;
        let dict=data.dict_code.data;
        that.dictMap=new Map();
        if(util.isNull(dict)){
            if(util.isNull(that.data.dict_code_json)){
                dict=[];
            }else{
                try {
                    if (typeof that.data.dict_code_json == 'string') {
                        dict=JSON.parse(that.data.dict_code_json)
                        dict.length;
                    }else{
                        dict=that.data.dict_code_json
                    }
                } catch (error) {
                }
            }
        }
        for (let i = 0; i < dict.length; i++) {
            that.dictMap.set(dict[i].key.toString(),dict[i].value);
        }
        that.initFormCol(that);
        let questionCircleHtml=that.getQuestionCircleIconHtml();
        let htmlTemplate=`
        <div class="form-group row col-sm-${that.data.col}" id="${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}
        ${questionCircleHtml}
        </label>
        <div class="col-sm-${that.inputCol}"  style="top:10px;vertical-align: middle;">
        <label class="switcher">
            <input type="checkbox" class="switcher-input" name="${eleID}" ${data.is_read_only=='Y'?'disabled':''}>
            <span class="switcher-indicator">
              <span class="switcher-yes">${that.dictMap.get(that.switchYesKey)}</span>
              <span class="switcher-no">${that.dictMap.get(that.switchNoKey)}</span>
            </span>
        </label>
        <small class="form-text text-muted"><font style="display:none;"  validate="validate"  color="red">12412345123</font></small>
        </div>
        </div>
        `;
        document.getElementById(renderParentID).insertAdjacentHTML("beforeEnd", htmlTemplate);
        if (!util.isNull(defaultValue)) {
            that.setValue(defaultValue);
        }
        that.bindFunc();
    }
    getValue() {
        let that=this;
        let eleObj = document.getElementsByName(that.getEleID());
        let checkBoxValue='';
        for (let i = 0; i < eleObj.length; i++) {
            if (eleObj[i].checked == true) {
                checkBoxValue+=eleObj[i].value+",";
            }
        }
        if(checkBoxValue.length>0){
            return that.switchYesKey
        }else{
            return that.switchNoKey
        }

    }
    setValue(value) {
        let that=this;
        if(that.data.kind=='text_detail'){
            document.getElementById(that.eleID).innerHTML=that.dictMap.get(value);
            return
        }
        if(value.length>0&&value==that.switchYesKey){
            value=',on,'
        }
        let eleObj = document.getElementsByName(that.getEleID());
        for (let i = 0; i < eleObj.length; i++) {
            if (value.indexOf(','+eleObj[i].value+',')>=0) {
                eleObj[i].checked = true;
            }else{
                eleObj[i].checked = false;
            }
        }
       
    }
    setDisabled(){
        let eleObj = document.getElementsByName(this.getEleID());
        for (let i = 0; i < eleObj.length; i++) {
            eleObj[i].disabled=true;
        }
    }
    removeDisabled(){
        let eleObj = document.getElementsByName(this.getEleID());
        for (let i = 0; i < eleObj.length; i++) {
            eleObj[i].disabled=false;
        }
    }
    setEnable(){
        let eleObj = document.getElementsByName(this.getEleID());
        for (let i = 0; i < eleObj.length; i++) {
            eleObj[i].disabled=false;
        }
    }
    hide(){
        document.getElementById(this.objID).setAttribute('style', 'display:none');//隐藏
    }
    show(){
        let that=this;
        if(that.data.kind=='text_detail')return
        if(that.switchYesKey.length==0||that.switchNoKey.length==0)return ;
        document.getElementById(that.objID).setAttribute('style', '');//展示
    }
    initFunc(){
        let that=this;
        that.bindValueValidate= ()=>{
            that.validateFunc= ()=>{
                that.validate();
            }
            let eleObj=document.getElementsByName(that.eleID);
            for (let i = 0; i < eleObj.length; i++) {
                util.addEventListener(eleObj[i],'click',that.validateFunc)//点击后校验
            }
        }
    }
    bindFunc(){
        let that=this;
        that.bindValueValidate();//绑定校验函数
    }
    renderTextDetail(renderParentID){
        let that=this;
        let data=that.data;
        let dict=data.dict_code.data;
        that.dictMap=new Map();
        if(util.isNull(dict)){
            dict=[];
        }
        for (let i = 0; i < dict.length; i++) {
            that.dictMap.set(dict[i].key.toString(),dict[i].value);
        }
        let html=`<tr>
                                <th>${data.label_name}</th>
                                <td name="${data.name}" id="${that.eleID}"></td>
                            </tr>`
        document.getElementById(renderParentID).insertAdjacentHTML("beforeEnd",html);
    }
}
export { inputSwitch };

