"use strict";
import { util } from '../../util/util.js'
import { verify } from '../../util/verify.js'
import {khcAlert} from "../alert/khcAlert.js";
import {fullMap} from "../../fullMap.js";
import {khcToastr} from "../alert/khcToastr.js";
import {config} from "../../config.js";
class base {
    constructor(data) {
        //构造text的数据
        this.data = data;
        //给当前对象创建一个id，同时该id也是对象的domid
        this.eleID = util.getUUID();
        this.objID = this.eleID + 'div';
        this.rows='12';
        this.disabled=false;
        this.isValue=true;//是否支持setValue getValue 默认都支持，如果不支持则在写组件时设置为false即可，如虚拟字段这些就可以设置为false
        this.parentObj=fullMap.getObj(data.parentObjID);//query组件的则为undefined
        this.util=util;//将util存储到text对象里，在自定义js可调用
        this.config=config;//将config存储到对象里，在自定义js可调用
        this.verify=verify;
        this.khcToastr=khcToastr;
        this.dictMap=new Map();//存储字典Map
    }
    getObjID() {
        return this.objID;
    }
    setRows(rows){
        this.rows=rows;
    }
    getRows(){
        return this.rows;
    }
    //返回元素的domID
    getEleID() {
        return this.eleID;
    }
    addEventListener(event, func) {//这里需调整一下，应该使用util类里的方法绑定（防止重复绑定）
        document.getElementById(this.eleID).addEventListener(event, func);
    }
    getData() {
        return this.data;
    }
    getValue() {//获取存储到数据库的值
        if(this.isValue){
            return document.getElementById(this.eleID).value;
        }
    }
    setValue(value) {
        if(this.isValue){
            if(this.data.kind=='text_detail'){
                let ele=document.getElementById(this.eleID)
                if(ele!=null){
                    value=util.toOldChar(value)
                    value=value.replace(/\n/g, '<br>');
                    document.getElementById(this.eleID).innerHTML = value;
                }
            }else{
                document.getElementById(this.eleID).value = util.toOldChar(value);
            }
        }
    }
    getShowValue() {//获取展示的值，主要是将用到字典的值转换（如：select，radio，checkbox等等）
        if(this.isValue){
            return document.getElementById(this.eleID).value;
        }
    }
    copyValue(){
        let that=this;
        let copyValue=that.getValue();
        new Clipboard('.btn',{
            text:function(){//.btn 这个元素在就行，这里使用的是自定义复制文本内容，更多使用示例阅读官方文档：https://it.cha138.com/ios/show-45625.html
                return copyValue;
            }
        })
    }

    validate() {
        if(this.data.query_type!=undefined) {
            return ;//是query组件就不校验，直接return
        }
        let returnResult = '';
        let returnValue = true;
        if(util.isNull(this.data.validate_code)||this.data.validate_code=='none'){
            this.data.validateType='none'
        }else if(this.data.validate_code=='myselfFunc'){
            this.data.validateType='myself'
        }else{
            this.data.validateType='func'
        }
        if (this.data.validateType == 'myself') {
            //自定义验证函数，要求函数体内变量的值是elementValue，并写上labelName
            let myselfFuncStr=util.toOldChar(this.data.myself_func);
            returnResult=util.executeDynamicCode(myselfFuncStr,{eleObj:this});
            if (returnResult.isPass == 'false') {
                this.validateText({isShow:true,type:'default',text:returnResult.message});
                returnValue = false;
            } else {
                this.validateText({isShow:false,type:'default'});
            }
        } else if (this.data.validateType == 'func') {
            returnResult = verify[this.data.validate_code](this.getValue(),this)
            // this.data.kind (default：默认水平),(formRow：表格行),(custom：自定义模板)
            if(this.data.kind==undefined){
                if (returnResult.isPass == 'false') {
                    this.validateText({isShow:true,type:'default',text:returnResult.message});
                    returnValue = false;
                } else {
                    this.validateText({isShow:false,type:'default'});
                }
            }else if(this.data.kind=='custom'){
                if (returnResult.isPass == 'false') {
                    this.validateText({isShow:true,type:'custom',text:returnResult.message});
                    returnValue = false;
                }else{
                    this.validateText({isShow:false,type:'custom'});
                }
            }
        } else if (this.data.can_null == 'Y') {
            if (this.data.show_type != 'hidden'&& this.data.show_type !='childPage') {
                let str = this.getValue();
                if(this.data.kind==undefined||this.data.kind=='default'){//默认水平展示的表单
                    if (!(str != undefined && str != null && str.length > 0)) {
                        this.validateText({isShow:true,type:'default',text:'此项为必填项！'});
                        returnValue = false;
                    } else {
                        this.validateText({isShow:false,type:'default'});
                        returnValue = true;
                    }
                }else if(this.data.kind=='custom'){//自定义模板表单
                    if(this.data.can_null=='Y'){
                        if (!(str != undefined && str != null && str.length > 0)) {
                            this.validateText({isShow:true,type:'custom',text:'此项为必填项！'});
                            returnValue = false;
                        } else {
                            this.validateText({isShow:false,type:'custom'});
                            returnValue = true;
                        }
                    }
                }
            }else if(this.data.show_type =='childPage'){//子表必填
                let tableView=this.pageObj.queryComponent.tableView;
                if(tableView.allItemNum==0){
                    returnValue = false;
                }
            }
        }
        return returnValue;
    }
    //validate 会在保存时调用校验value填写是否符合规范，如果不符合规范会有相应的提示
    //此时用户看到相应的提示，修正后，不符合规范的提示还在
    //bindValueValidate就是来做这个的，当保存时不符合规范就会绑定此事件，由各个组件重写此方法绑定校验事件
    //文本框就是失去焦点时处发校验，如果值符合规范就会关闭提示信息，下拉框select就是change后处发，其他表单类型类似
    bindValueValidate(){

    }
    setDisabled(){
        if(this.data.kind=='text_detail')return
        let eleObj = document.getElementById(this.getEleID());

            eleObj.disabled=true;
            this.disabled=true;

    }
    removeDisabled(){
        let eleObj = document.getElementById(this.getEleID());

        eleObj.disabled=false;
        this.disabled=false;

    }
    setEnable(){
        let eleObj = document.getElementById(this.getEleID());

            eleObj.disabled=false;
            this.disabled=false;

    }
    refreshCanNull(){//只在page生效,modals因为是每次都请求最新表单信息，需要放缓存里，modals渲染时去取出来校验即可
        let eleObj = document.getElementById(this.getObjID()+'CanNull');//星号必填需要在objId找
        let that=this;
        if(that.data.can_null=='Y'){
            eleObj.style.display='';
        }else{
            eleObj.style.display='none';
        }
    }
    hideDiv(){
        let eleObj = document.getElementById(this.getObjID());
        eleObj.style.display='none';
    }
    showDiv(){
        let eleObj = document.getElementById(this.getObjID());
        eleObj.style.display='';
    }
    //初始化input所有属性
    init(){

    }
    initFormCol(obj) {
        let that=this;
        let form=fullMap.getObj(obj.data.parentObjID)
        //undefined是查询输入框，两个输入框占一行，所以比例要调整一下
        //modals是弹出模态框，模态框本身就小，比例也要调整一下
        if(form==undefined||form.formShowType=='modals'){
            if(that.data.componentType=='form'){
                if(that.data.col==12){
                    obj.labelCol=3;
                    obj.inputCol=9;
                }else{
                    obj.labelCol=5;
                    obj.inputCol=7;
                }
            }else{
                obj.labelCol=3;
                obj.inputCol=9;
            }
        }else{
            if(that.data.col>=9){
                obj.labelCol=2;
                obj.inputCol=10;
            }else if(that.data.col>=6){
                obj.labelCol=4;
                obj.inputCol=8;
            }else{
                obj.labelCol=6;
                obj.inputCol=6;
            }
        }
    }
    /**
     * 展示或隐藏提示信息
     * @param obj.isShow 不能为空 是否展示
     * @param obj.text 允许为空 隐藏是可以不传此参数（obj.isShow=Y）
     * @param obj.type 标识是什么表单模板，default,custom（默认,自定义）
     * @param obj.eleObj 允许为空 在自定义提示函数可用此对象
     */
    validateText(obj){
        this.initValidateTextFunc();
        let that=this;
        if(util.isNull(obj.type)||obj.type=='default'){
            that.defaultValidateTextFunc(obj);
        }
        if(obj.type=='custom'){//自定义模板也有默认提示
            that.customValidateTextFunc(obj);
        }
    }
    //初始化隐藏和展示提示函数
    initValidateTextFunc(){
        let that=this;
        if(that.defaultValidateTextFunc==undefined) {
            that.defaultValidateTextFunc= (obj)=>{
                let thisObjDiv=document.getElementById(that.objID);
                if(thisObjDiv!=null){
                    let validateShowObj = thisObjDiv.querySelector("font[validate]");
                    if(!util.isNull(validateShowObj)){
                        if(obj.isShow){
                            validateShowObj.innerHTML = obj.text
                            validateShowObj.setAttribute('style', 'display:block');
                        }else{
                            validateShowObj.setAttribute('style', 'display:none');
                        }
                    }
                }
            }
        }
        if(that.customValidateTextFunc==undefined){//如果不想使用自定义提示 就重写后赋给validateTextFunc即可
            that.customValidateTextFunc= (obj)=>{
                if(obj.isShow){
                    if(document.getElementById(that.eleID).className.indexOf("is-invalid")==-1){
                        document.getElementById(that.eleID).className+=' is-invalid'
                    }
                    document.getElementById(that.eleID).setAttribute('data-toggle','tooltip');
                    document.getElementById(that.eleID).setAttribute('data-placement','bottom');
                    document.getElementById(that.eleID).setAttribute('data-state','danger');
                    document.getElementById(that.eleID).setAttribute('data-original-title',obj.text);
                    document.getElementById(that.eleID).setAttribute('aria-describedby','tooltip');
                    $('[data-toggle="tooltip"]').tooltip();
                    //$('#'+that.eleID).tooltip('show');//手动展示（跳转其他菜单时，手动展示的还会在新页面）建议默认即可（默认鼠标悬浮再展示）
                }else{
                    document.getElementById(that.eleID).classList.remove('is-invalid')
                    document.getElementById(that.eleID).removeAttribute('data-toggle');
                    document.getElementById(that.eleID).removeAttribute('data-placement');
                    document.getElementById(that.eleID).removeAttribute('data-state');
                    document.getElementById(that.eleID).removeAttribute('data-original-title');
                    $('#'+that.eleID).tooltip('hide');//手动隐藏
                }

            }
        }
    }
    getQuestionCircleIconHtml(){
        let that=this;
        let data=that.data
        let questionCircleHtml=``;
        if(data['is_question_circle']!='Y')return '';
        if(util.isNull(data['question_circle_type'])||data['question_circle_type']=='default'){//非弹出框，鼠标悬浮显示(icon是空心的)
            questionCircleHtml=`<i class="far fa-question-circle" style="font-size: 15px;cursor:pointer;" data-toggle="tooltip" data-placement="top" title="" data-original-title="${data['question_circle_text']}"></i>`;
        }else if(data['question_circle_type']=='popovers'){//弹出框(icon是实心的)
            questionCircleHtml=`<i class="fas fa-question-circle" style="font-size: 15px;cursor:pointer;"  data-toggle="popover" data-placement="bottom" data-content="${data['question_circle_text']}" data-original-title="${data['label_name']}"></i>`;
        }
        return questionCircleHtml;
    }
    renderCustom(renderParentID){
        let that=this;
        let data=that.data
        let obj=document.getElementById(renderParentID).querySelector('[name="' + data.name + '"]');
        if(obj==null){
            let hiddenFieldsDiv=document.getElementById(renderParentID).querySelector('div[name="hiddenFields"]');
            let hiddenHtmlTemplate=`<input type="hidden" name="${data.name}" id="${that.eleID}" value="">`;
            hiddenFieldsDiv.insertAdjacentHTML("beforeEnd", hiddenHtmlTemplate)
        }else{
            obj.setAttribute("id", that.eleID);
        }
    }
}
export { base }