import base from './baseMixin';
import util from './util.js';
import NumUtils from './numUtils.js';
export default {
    data() {
        return {
          value:null,
          promptType:"none",
          message:"",
          state:"",
          isSet:false,
          isContent:false,
          textAlign:false,
          textColor:false,
          customStyle:{},
          dyConditions:[],
          controlDisplay:true
        }
    },
    inject: ['viewData'],
    methods: {
        setValue(value) {
            this.isSet = true;
            this.value = value;
            this.isSet = false;
            this.formatFieldValue&&this.formatFieldValue();
        },
        onBlur() {
            this.context.bill.changeTempformulaType(this.define.config.field, 2);
            if(this.define.config.formulas&&this.define.config.formulas.confirm){
                let _message = this.context.bill.getControlState(`confirm`,this.define.name);
                if(_message){
                    this.$Modal.confirm({
                        title: '提示',
                        content: _message,
                        onOk: () => {
                            this.setDataObjectValue(this.value);
                        },
                        onCancel: () => {
                            this.value = this.context.dataObject.getValue(this.define.config.field.split('.')[1]);
                        }
                    })
                }else{
                    this.setDataObjectValue(this.value);
                }
            }else{
                this.setDataObjectValue(this.value);
            }
            
        },
        setDataObjectValue(valueChange){
            this.context.dataObject.setValue(this.define.config.field, valueChange);
        },
        getContentStatus(){
          this.isContent = false;
          if(this.define.config.content){
              this.isContent = true;
          }
      },
      getValue(key,format){
          let val = {};
          if(this.context.dataObject){
              val = this.context.dataObject.getValue(this.define.config.field);
              if(val){
                  val = this.formatValue(val,format);
              }
              if(key){
                  if(val){
                      return val[key];
                  }else{
                      return '';
                  }
              }else if(val instanceof Object){
                  return val.title?val.title:val.name;
              }else{
                  return val;
              }
          }else if(this.context.card){
              let _data = this.context.card.getGlobalParam('gams_card_cardData');
              let _key = this.define.config.field.split('.').length > 1?this.define.config.field.split('.')[1]:this.define.config.field;
              val = _data[_key];
              val = this.formatValue(val,format);
              return val;
          }
      },
      formatValue(val,format){
          if(this.define.config.format){
               
              //TODO
          }else if(format){
              return this[`${format}Format`](val);
          }else if(this.define.type === 'NumberControl'){
            return this.numberFormat(val);
          }else{
            return val;
          }
      },
      dateFormat(val){
          return GAMS.Common.getDateStrs(val);
      },
      numberFormat(val){
          if(this.define.config.precision){
              val = GAMS.Common.roundIng(val,this.define.config.precision);
              return NumUtils.splitThousand(val,this.define.config.precision);
          }else{
              val = GAMS.Common.roundIng(val,2);
              return NumUtils.splitThousand(val,2);
          }
      },
      getConditions(){
           
          this.context.bill.runGetFilterConditionsFormula(this.define.config.field);
      },
      dealWithCondition(conditions,key){
          return conditions[key];
      },
      getTextAlign(style){
          this.deleteCustomStyleAttibute('center');
          this.deleteCustomStyleAttibute('left');
          this.deleteCustomStyleAttibute('right');
          if(this.define.layout&&this.define.layout.textAlign){
              this.customStyle[`input-${this.define.layout.textAlign}`] = true;
              style['textAlign'] = this.define.layout.textAlign;
          }
      },
      getTextColor(style){
          if(this.define.layout&&this.define.layout.color){
              style['color'] = this.define.layout.color;
          }
      },
      getInputHeight(style){
          this.deleteCustomStyleAttibute('height');
          if(this.define.layout&&this.define.layout.height){
              this.customStyle['input-height'] = true;
              style['height'] = util.analyUnit(this.define.layout.height);
          }
      },
      deleteCustomStyleAttibute(key){
          delete this.customStyle[`input-${key}`];
      },
      getInputWidth(style){
          if(this.define.layout&&this.define.layout.width){
              style['width'] = util.analyUnit(this.define.layout.width);
          }
      },
      setInputCorlor(){
          if(this.$refs.setfocus&&this.$refs.setfocus.$el.children&&this.$refs.setfocus.$el.children[1]){
              if(this.define.layout.color){
                  this.$refs.setfocus.$el.children[1].style['color'] = util.analyColor(this.define.layout.color);
              }
              if(this.define.layout.fontSize){
                  let _fz = util.analyUnit(this.define.layout.fontSize);
                  this.$refs.setfocus.$el.children[1].style.setProperty('font-size', _fz, 'important');
              }
          }
      },
        getControlHideStatus(name){
            this.context.bill.getControlHideStatus(name,'runtime');
        },
        addControlDisplayListener(){
            this.hideRemove = this.context.bill.addControlHideListener(this.define.name,{
                hideControl:function(type){
                    if(type){
                        this.controlDisplay = true;
                    }else{
                        this.controlDisplay = false;
                    }
                }.bind(this)
            });
        },
        addControlStateListener(){
            this.controlState = this.context.bill.addControlStateListener(this.define.name,{
                display:function(type){
                    if(type){
                        this.controlDisplay = true;
                    }else{
                        this.controlDisplay = false;
                    }
                }.bind(this),
                editable:function(nv){
                    this.state = nv;
                }.bind(this),
                enable:function(){

                }.bind(this),
                confirm:function(msg){
                    /**
                     * 主动调用
                     */
                    // if(msg){
                    //     this.showTipMessage(msg);
                    //     return false;
                    // }
                }.bind(this)
            });
        },
        getControlState(config){
            if(config&&config.formulas){
                for (const key in config.formulas) {
                    if(config.formulas[key] === "false"){
                        this[`setControl${key}`]&&this[`setControl${key}`]();
                    }else{
                        this.context.bill.getControlState(key,this.define.name);
                    }
                }
            }
        },
        setControldisplay(){
            this.controlDisplay = false;
        },
        setControleditable(){
            this.state = 'readOnly';
        },
        showTipMessage(message){
            this.$Modal.confirm({
                title: '提示',
                content: message,
                onOk: () => {
                    
                },
                onCancel: () => {
                    if(this.context.dataObject){
                      this.context.dataObject.resetFieldValue();
                    }
                    return ;
                }
            })
        }
    },
    mixins:[base],
    mounted() {
        this.$nextTick(function () {
            let obj = this.context.dataObject;
            if (this.define.config.field) {
                // 暂时注释
                this.remove = obj.addListener(this.define.config.field, {
                    messageChange: function (nm, om) {
                        this.promptType = nm.promptType;
                        this.message = nm.message;
                    }.bind(this),
                    valueChange: function (nv) {
                        this.setValue(nv);
                    }.bind(this),
                    stateChange: function (nv) {
                        this.state = nv;
                    }.bind(this),
                });
                if (obj.getValue) {
                    this.setValue(obj.getValue(this.define.config.field, this.define.config.detailTableName||'master'));
                } else {
                    let field = this.define.config.field.split('.')[1]
                    this.setValue(obj[field])
                }
                
                if(this.define.config&&this.define.config.formulaName){
                    this.addControlDisplayListener();
                    this.getControlHideStatus(this.define.config.formulaName);
                }else{
                    this.context.bill.setGlobalParam(this.define.name,this.define.config.field);
                    this.addControlStateListener();
                }

                if(!this.context.bill.getGlobalParam('_old_action_status')){
                    this.getControlState(this.define.config);
                }
            }
        }.bind(this));

        if(this.define.config.field == 'gams_inspectionentry.inspector'){
            console.log(this.define,'this.define')
        }
    },
    created(){
      this.getContentStatus();
    },
    props: {
        define: {
            type: Object,
            default: () => { }
        },
        context: {
            type: Object,
            default: () => { }
        },
        tableStyle: {
            type: Boolean,
            default: false
        }
    },
    watch: {
        value: function (nv, ov) {
            if (this.isSet || nv == ov) {
                return;
            }
        },
        viewData:{
            handler: function(nv,ov){
                let editableObj = this.define.config["editable"];
                if(editableObj && editableObj.type && editableObj.type.toLowerCase&&editableObj.type.toLowerCase() == "viewdata"){
                  if(nv[editableObj.attr]){
                    this.state = 'readOnly';
                  }else{
                    this.state = 'edit';
                  }
                }
            },
            deep: true,
            immediate: true
        }
    },
    beforeDestroy() {
        this.remove();
        this.hideRemove&&this.hideRemove();
        this.controlState&&this.controlState();
    },
    computed:{
      getInputStyle(){
          let style = {};
          this.getTextAlign(style);
          this.getTextColor(style);
          this.getInputHeight(style);
          this.getInputWidth(style);
          return style;
      }
  }
}