"use strict";
import {base} from './base.js'
import { util } from '../../util/util.js'
class inputRadio extends base {
    constructor(data) {
        super(data);
        this.type='radio';
    }
    render(renderParentID) {
        this.initFunc();
        let data=this.data
        if(data.kind==undefined||data.kind=='default'){
            this.renderDefault(renderParentID);
        }
        if(data.kind=='custom'){
            this.renderCustom(renderParentID);
        }
    }
    renderDefault(renderParentID){
        let that=this;
        that.initFunc()
        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 objID = that.objID;
        let eleID = that.eleID;
        let defaultValue=data.default_value;
        let canNull=data.can_null;
        let labelName=data.label_name;
        let dict=dictDataArray;
        that.initFormCol(that);
        let radioHtmlTemplate=``;
        if(util.isNull(dictDataArray)){
            dict=[];
        }
        for (let i = 0; i < dict.length; i++) {
            radioHtmlTemplate+=`
            <label class="form-check form-check-inline">
            <input class="form-check-input" type="radio" id="${eleID}" name="${eleID}" value="${dict[i].key}">
            <span class="form-check-label">${dict[i].value}</span>
            </label>
            `;
        }
        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-${that.data.col}" id="${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}`:''}"  style="top:8px;vertical-align: middle;">
        ${radioHtmlTemplate}
        <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 eleObj = document.getElementsByName(this.getEleID());
        let radioValue='';
        
        for (let i = 0; i < eleObj.length; i++) {
           
            if (eleObj[i].checked == true) {
                radioValue=eleObj[i].value;
                break;
            }
        }
        return radioValue;
        
    }
    setValue(value) {
        let that=this;
        let eleObj = document.getElementsByName(that.getEleID());
        for (let i = 0; i < eleObj.length; i++) {
           
            if (eleObj[i].value == value) {
                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();
        }
        if(util.isNull(value)){
            return '';
        }
        return that.dictMap.get(value);
    }
    setDisabled(){
        let that=this;
        let eleObj = document.getElementsByName(that.getEleID());
        for (let i = 0; i < eleObj.length; i++) {
            /*if(eleObj[i].value!=that.getValue()){
                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;
        }
    }
    hide(){
        document.getElementById(this.objID).setAttribute('style', 'display:none');//隐藏
    }
    show(){
        document.getElementById(this.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();//绑定校验函数
    }
}
export { inputRadio };

