import _ from "lodash"
import commonDeclare from "./commonDeclare"
import commonApi from "./commonApi"
import Utils from "~/utils"

export default {
  mixins: [commonApi,commonDeclare],
  data: function () {
    return {
      defFlag:true,
      // 弹框回填
      promptData: {
        title: '',
        columnStr: '',
        data: [],
        rulePath: ''
      }
    }
  },
  created() {
  },
  mounted() {
    if(!this.isInDisplay){
      // this.ruleWatcher()
      this.ruleCheck()
    }
  },
  methods: {
    openWatch(flag){
      this.defFlag = !!flag
    },
    ruleWatcher() {
      if(!this.defaultRules)
        return
      const that = this;
      Object.keys(that.defaultRules).forEach(key => {
        let func = function(){
          if(that.defFlag){
            that.defaultRules[key].apply(that)
          }
        }
        that.$watch("model." + key, _.debounce(func, 1000))
      })
    },
    ruleCheck() {
      if(!this.pattern)
        return
      const keySet = new Set(Object.keys(this.pattern).concat(Object.keys(this.checkRules).concat(Object.keys(this.defaultRules))))
      const res = {};
      const that = this;
      for (let key of keySet.keys()) {
        const rule = []
        if(that.pattern[key]){
          rule.push(...that.pattern[key])
        }
        const triggerType = that.getTriggerType(key)
        if(that.checkRules[key]){
          if (Array.isArray(that.checkRules[key])) {
            for (let j = 0; j < that.checkRules[key].length; j++) {
              const check = that.checkRules[key][j];
              rule.push({
                validator: check.bind(that),
                trigger: triggerType
              })
            }
          } else {
            rule.push({
              validator: that.checkRules[key].bind(that),
              trigger: triggerType
            })
          }
        }
        if (that.defaultRules[key]) {
          rule.push({
            validator: that.defaultRules[key].bind(that),
            trigger: triggerType
          })
        }
        if(rule.length > 0) {
          res[key] = rule;
        }
      }
      that.rules = res;
    },
    getTriggerType(prop) {
      const fields = this.$refs.modelForm.fields;
      for (let i = 0; i < fields.length; i++) {
        const field = fields[i];
        if (field.prop === prop) {
          // select、checkbox使用change触发
          if (field.$children[1].$children[0].$el.className.startsWith("el-select") || field.$children[1].$children[0].$el.className.startsWith("el-checkbox")) {
            return "change";
          }
          return "blur";
        }
      }
      return "blur";
    },
    /**
     * 更新Model
     * @param {any} data model数据
     */
    updateModel(data) {
      Utils.copyValueFromVO(this.model, data);
    },
    showGridPromptDialog(rulePath) {
      this.executeRule(rulePath).then((res) => {
        if (res.respCode = SUCCESS){
          if (res.data.params) {
              Utils.copyValueFromVO(this.model, res.data);  
          } else {
              this.root.$refs.etyDialog.show = true
              this.root.promptData = {
                title: res.data.title,
                columnStr: res.data.columns,
                data: res.data.vals.rows,
                rulePath: rulePath,
              }
          }
        }
      })
    },
    selectEty(val, rulePath) {
      const props = rulePath.replaceAll(".", "_")
      const obj = {}
      obj[props] = val;
      Utils.copyValueFromVO(this.model, obj);
      this.executeRule(rulePath).then((res) => {
        if (res.respCode = SUCCESS){
          Utils.copyValueFromVO(this.model, res.data);
        }
      });
    }
  },
  computed:{
    isInDisplay(){
      return this.$store.state.Status.mode === 'display'
    }
  }
}