"use strict";
import {base} from './base.js'
import { util } from '../../util/util.js'

class inputCheckBox extends base {
    constructor(data) {
        super(data);
        this.type='checkbox';
    }
    render(parentObjID) {
        let that=this;
        let data=that.data
        if(data.kind==undefined||data.kind=='default'){
            that.renderDefault(parentObjID);
        }
        if(data.kind=='custom'){
            that.renderCustom(parentObjID);
        }
    }
    setDisabled(){
        let that=this;
        let eleObj = document.getElementsByName(this.getEleID());
        let value=that.getValue();
        for (let i = 0; i < eleObj.length; i++) {
            /*if(value.indexOf(','+eleObj[i].value+',')==-1){
                eleObj[i].parentNode.style.display='none';
            }else{
                eleObj[i].parentNode.style.display='';
            }*/
            eleObj[i].disabled=true;
        }
    }
    removeDisabled(){
        let eleObj = document.getElementsByName(this.getEleID());
        for (let i = 0; i < eleObj.length; i++) {
            eleObj[i].disabled=false;
            //eleObj[i].parentNode.style.display='';
        }
    }
    setEnable(){
        let eleObj = document.getElementsByName(this.getEleID());
        for (let i = 0; i < eleObj.length; i++) {
            eleObj[i].disabled=false;
        }
    }
    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){
            checkBoxValue=','+checkBoxValue;//前后都保留逗号，可做模糊查询
        }
        return checkBoxValue;
    }
    setValue(value) {
        let that=this;
        value=','+value+','
        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;
            }
        }
        if(that.data.is_read_only=='Y'){
            that.setDisabled();
        }else{
            that.removeDisabled();
        }
    }
    //value自定义value转换showValue，可以为空
    getShowValue(value='') {//获取展示的值，主要是将用到字典的值转换
        let that=this;
        if(util.isNull(value)){
            value=that.getValue();
        }
        let showValueStr='';
        if(util.isNull(value)){
            return showValueStr;
        }
        let valueArray = value.split(',')
        for (let i = 0; i < valueArray.length; i++) {
            if(!util.isNull(valueArray[i])){
                showValueStr+=that.dictMap.get(valueArray[i])+'，';
            }
        }
        showValueStr=showValueStr.substring(0,showValueStr.length-1);
        return showValueStr;
    }
    hide(){
        document.getElementById(this.objID).setAttribute('style', 'display:none');//隐藏
    }
    show(){
        document.getElementById(this.objID).setAttribute('style', '');//展示
    }
    renderDefault(parentObjID){
        let that=this;
        let data = that.data
        let dictDataArray=that.data.dict_code.data;
        if(util.isNull(dictDataArray)){
            if(util.isNull(that.data.dict_code_json)){
                dictDataArray=[];
            }else{
                try {
                    if (typeof that.data.dict_code_json == 'string') {
                        dictDataArray=JSON.parse(that.data.dict_code_json)
                        dictDataArray.length;
                    }else{
                        dictDataArray=that.data.dict_code_json
                    }
                } catch (error) {
                }
            }
        }
        for(let i=0;i<dictDataArray.length;i++){
            that.dictMap.set(dictDataArray[i].key.toString(),dictDataArray[i].value);
        }
        let defaultValue=data.default_value;
        let canNull=data.can_null;
        let labelName=data.label_name;
        that.initFormCol(that);
        let checkboxHtmlTemplate=that.getCheckboxHtml();
        let questionCircleHtml=that.getQuestionCircleIconHtml();
        let layoutType=util.isNull(data.field_layout_type)?'horizontal':data.field_layout_type
        let htmlTemplate=`
        <div class="form-group ${layoutType=='horizontal'?'row':''} col-sm-${data.col}" id="${that.objID}" style="display: none">
        <label class="col-form-label ${layoutType=='horizontal'?`col-sm-${that.labelCol}`:''} text-sm-right" style="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="${layoutType=='horizontal'?`col-sm-${that.inputCol}`:''}" id="${that.eleID}showInputDiv"  style="top:8px;vertical-align: middle;">
        ${checkboxHtmlTemplate}
        <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);
        if (!util.isNull(defaultValue)) {
            that.setValue(defaultValue);
        }
    }
    refreshInputHtml() {
        let that=this;
        let checkboxHtmlTemplate=that.getCheckboxHtml();
        document.getElementById(that.eleID+'showInputDiv').innerHTML=checkboxHtmlTemplate;
    }

    setShowDictKeyArrayStr(keyArrayStr){
        let that=this;
        let data = that.data
        that.showDictKey=keyArrayStr.split(',');
        let dictDataArray=data.dict_code.data;
        if(util.isNull(dictDataArray)){
            dictDataArray=[];
        }
        for (let i = 0; i < dictDataArray.length; i++) {
            if(that.showDictKey.includes(dictDataArray[i].key)){
                dictDataArray[i].isShow='Y'//js可对字典设置isShow属性来控制是否渲染
            }else{
                dictDataArray[i].isShow='N'
            }
        }
        that.refreshInputHtml();
    }

    getCheckboxHtml(){
        let that=this;
        let data = that.data
        let checkboxHtmlTemplate=``;
        let dictDataArray=that.data.dict_code.data;
        if(util.isNull(dictDataArray)){
            if(util.isNull(that.data.dict_code_json)){
                dictDataArray=[];
            }else{
                try {
                    if (typeof that.data.dict_code_json == 'string') {
                        dictDataArray=JSON.parse(that.data.dict_code_json)
                        dictDataArray.length;
                    }else{
                        dictDataArray=that.data.dict_code_json
                    }
                } catch (error) {
                }
            }
        }
        for (let i = 0; i < dictDataArray.length; i++) {
            checkboxHtmlTemplate+=`
            <label class="form-check form-check-inline" style="${!util.isNull(dictDataArray[i].isShow)&&dictDataArray[i].isShow=='N'?'display:none':''}">
            <input class="form-check-input" type="checkbox" name="${that.eleID}" value="${dictDataArray[i].key}" ${dictDataArray=='Y'?'disabled':''}>
            <span class="form-check-label">${dictDataArray[i].value}</span>
            </label>
            `;
        }
        return checkboxHtmlTemplate;
    }
    renderCustom(parentObjID){
        let that=this;
        let data=that.data
        let defaultValue=data.default_value;
        let obj=document.getElementById(parentObjID).querySelectorAll('[name="' + data.name + '"]');
        //console.log('show my layout name：'+data.name+"   value："+obj.value+"    eleID："+this.eleID)
        for (let i = 0; i < obj.length; i++) {
            obj[i].setAttribute("name", that.eleID);
        }
        if (!util.isNull(defaultValue)) {
            that.setValue(defaultValue);
        }
    }
}
export { inputCheckBox };
