/*****************************************************************************************
**  Author:COCO 2022
*****************************************************************************************/
//此js文件是用来自定义扩展业务代码，可以扩展一些自定义页面或者重新配置生成的代码
import * as dateUtil from "../../../uitils/dateFormatUtil.js";
let extension = {
  components: {
    //查询界面扩展组件
    gridHeader: '',
    gridBody: '',
    gridFooter: '',
    //新建、编辑弹出框扩展组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  data() {
    return {
      prefixVal: "",
      submitTimeVal: "",
      submitTimeText: "",
      serialNumberVal: "",
      serialNumberNine: "",
    };
  },
  tableAction: '', //指定某张表的权限(这里填写表名,默认不用填写)
  buttons: { view: [], box: [], detail: [] }, //扩展的按钮
  methods: {
    getFormOption(field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        })
      })
      return option;
    },
    //下面这些方法可以保留也可以删除
    onInit() { 
      this.columnIndex = true;
      //示例：设置修改新建、编辑弹出框字段标签的长度
      this.boxOptions.labelWidth = 120;
      this.editFormOptions[this.editFormOptions.length - 1].push({ //往新数组对象中添加新的属性 属性名对应属性值
        title: "编号预览",
        required: false,
        field: "NumberPreview",
        disabled: true,
        type: "label",
        inputStyle: { 'color': 'blue', 'font-size': '26px' }
      });
      //当订单类型select改变值时，如果选中的是发货(对应字典编号为2)，emark,SellNo隐藏，否则显示出来
      var numReg = /^[0-9]*$/
      var numRe = new RegExp(numReg);
      var prefix = this.getFormOption('Prefix');
      var submitTime = this.getFormOption('SubmitTime');
      var serialNumber = this.getFormOption('SerialNumber');
      this.prefixVal = "";
      this.submitTimeVal = "";
      this.submitTimeText = "";
      this.serialNumberVal = "";
      this.serialNumberNine = "";
      prefix.onKeyPress = (val) => {
        if (val.toString() != '[object KeyboardEvent]') {
          this.prefixVal = val;
          if (this.currentAction == "update") {
            this.submitTimeVal = dateUtil.formatTimeStamp(Date.now(), this.editFormFields.SubmitTime);
            submitTime.data.forEach((kv) => {
              //根据字典的值判断
              if (kv.key == this.editFormFields.SubmitTime) {
                this.submitTimeText = kv.value
              }
            });
            this.serialNumberVal = "";
            this.serialNumberNine = ""
            for (var i = 0; i < this.editFormFields.SerialNumber; i++) {
              if ((i + 1) == this.editFormFields.SerialNumber) {
                this.serialNumberVal += "1"
              }
              else {
                this.serialNumberVal += "0"
              }
              this.serialNumberNine += "9"
            }
            serialNumber.extra = {
              text: "例：" + this.serialNumberNine,//显示文本
              style: "color:blue;",//指定样式
            }
          }
          this.editFormFields.NumberPreview = this.prefixVal + this.submitTimeVal + this.serialNumberVal;
          this.editFormFields.GenerativeRule = this.prefixVal + this.submitTimeText + this.serialNumberVal + "~" + this.serialNumberNine;
        }
      };
      submitTime.onChange = (val, item) => {
        this.submitTimeVal = dateUtil.formatTimeStamp(Date.now(), val);
        submitTime.data.forEach((kv) => {
          //根据字典的值判断
          if (kv.key == val) {
            this.submitTimeText = kv.value
          }
        });
        if (this.currentAction == "update") {
          this.prefixVal = this.editFormFields.Prefix;
          this.serialNumberVal = "";
          this.serialNumberNine = ""
          for (var i = 0; i < this.editFormFields.SerialNumber; i++) {
            if ((i + 1) == this.editFormFields.SerialNumber) {
              this.serialNumberVal += "1"
            }
            else {
              this.serialNumberVal += "0"
            }
            this.serialNumberNine += "9"
          }
          serialNumber.extra = {
            text: "例：" + this.serialNumberNine,//显示文本
            style: "color:blue;",//指定样式
          }
        };
        this.editFormFields.NumberPreview = this.prefixVal + this.submitTimeVal + this.serialNumberVal;
        this.editFormFields.GenerativeRule = this.prefixVal + this.submitTimeText + this.serialNumberVal + "~" + this.serialNumberNine;
      };

      serialNumber.onKeyPress = (val) => {
        this.serialNumberVal = "";
        this.serialNumberNine = "";
        if (val.toString() != '[object KeyboardEvent]') {
          if (numRe.test(val)) {
            if (val > 10) {
              this.$Message.info('流水号不能大于10');
              return;
            }
            for (var i = 0; i < val; i++) {
              if ((i + 1) == val) {
                this.serialNumberVal += "1"
              }
              else {
                this.serialNumberVal += "0"
              }
              this.serialNumberNine += "9"
            }
          }
          else {
            this.$Message.error('请输入数字');
          }
        }
        serialNumber.extra = {
          text: "例：" + this.serialNumberNine,//显示文本
          style: "color:blue;",//指定样式
        }
        if (this.currentAction == "update") {
          this.prefixVal = this.editFormFields.Prefix;
          this.submitTimeVal = dateUtil.formatTimeStamp(Date.now(), this.editFormFields.SubmitTime);
          submitTime.data.forEach((kv) => {
            //根据字典的值判断
            if (kv.key == this.editFormFields.SubmitTime) {
              this.submitTimeText = kv.value
            }
          });
        }
        this.editFormFields.NumberPreview = this.prefixVal + this.submitTimeVal + this.serialNumberVal;
        this.editFormFields.GenerativeRule = this.prefixVal + this.submitTimeText + this.serialNumberVal + "~" + this.serialNumberNine;
      };
    },
    onInited() {
      //框架初始化配置后
      //如果要配置明细表,在此方法操作
      //this.detailOptions.columns.forEach(column=>{ });
    },
    searchBefore(param) {
      //界面查询前,可以给param.wheres添加查询参数
      //返回false，则不会执行查询
      return true;
    },
    searchAfter(result) {
      //查询后，result返回的查询数据,可以在显示到表格前处理表格的值
      return true;
    },
    addBefore(formData) {
      //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
      return true;
    },
    updateBefore(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      return true;
    },
    rowClick({ row, column, event }) {
      //查询界面点击行事件
      // this.$refs.table.$refs.table.toggleRowSelection(row); //单击行时选中当前行;
    },
    modelOpenBefore(row) {
      this.prefixVal = "";
      this.submitTimeVal = "";
      this.submitTimeText = "";
      this.serialNumberVal = "";
      this.serialNumberNine = "";
    },
    modelOpenAfter(row) {
      //点击编辑、新建按钮弹出框后，可以在此处写逻辑，如，从后台获取数据
      //(1)判断是编辑还是新建操作： this.currentAction=='Add';
      //(2)给弹出框设置默认值
      //(3)this.editFormFields.字段='xxx';
      //如果需要给下拉框设置默认值，请遍历this.editFormOptions找到字段配置对应data属性的key值
      //看不懂就把输出看：console.log(this.editFormOptions)
      //编辑保存前formData为对象，包括明细表、删除行的Id
      // formData.prefix.onChange();
      if (this.currentAction == 'update') {
        var prefixVal = "";
        var submitTimeVal = "";
        var submitTimeText = "";
        var serialNumberVal = "";
        var serialNumberNine = "";
        var submitTime = this.getFormOption('SubmitTime');
        var serialNumber = this.getFormOption('SerialNumber');
        prefixVal = row.Prefix;
        submitTimeVal = dateUtil.formatTimeStamp(Date.now(), row.SubmitTime);
        submitTime.data.forEach((kv) => {
          //根据字典的值判断
          if (kv.key == row.submitTime) {
            submitTimeText = kv.value
          }
        });
        for (var i = 0; i < row.SerialNumber; i++) {
          if ((i + 1) == row.SerialNumber) {
            serialNumberVal += "1"
          }
          else {
            serialNumberVal += "0"
          }
          serialNumberNine += "9"
        }
        serialNumber.extra = {
          text: "例：" + serialNumberNine,//显示文本
          style: "color:blue;",//指定样式
        }
        prefixVal = prefixVal == null ? "" : prefixVal;
        this.editFormFields.NumberPreview = prefixVal + submitTimeVal + serialNumberVal;
      }
    }
  }
};
export default extension;
