import moment from "moment";
export default {
  props: {
    templateData: {
      type: Object,
      default: () => {},
    },
  },
  watch: {
		templateData: {
			immediate: true,
			handler(v) {
        if(v){
          let n = {...v};
          if(v.bdnr){
            this.formData = JSON.parse(v.bdnr);
          }
          this.templateDetail = n;
          this.setTemplateData(n);
        }
			}
		}
	},
  data() {
    return {
      formData: {},
      templateDetail:{}
    }
  },
  mounted() {

        setTimeout(() => {
          this.setConditionOptions(
            [
              { label: "条件5", value: "1" },
              { label: "条件6", value: "2" },
              { label: "条件7", value: "3" },
              { label: "其他", value: "-1" },
            ]
          )
        }, 1000);
        this.setTemplateStatus("preFill");
        // this.setTemplateStatus(this.fillType)
   
  },
  unmounted() {
    this.setTemplateStatus("");
    this.setTemplateData({});
  },
  methods: {
    setTemplateStatus(status) {
      this.$store.commit('template/SET_TEMPLATE_STATUS', status)
    },
    setConditionOptions(options) {
      this.$store.commit('template/SET_CONDITION_OPTIONS', options)
    },
    //统一处理blur事件，因为有效周期和过期日期是相关的，所以需要在有效周期失焦时更新过期日期
    onHandleBlur(fields){
			const {key ,effectivePeriodUnit,effectivePeriod,codeSTD} = fields;
			const {createTime} = this.formData;
			if(key ==="effectivePeriod"){//统一处理有效周期失焦，计算失效事件，保证字段名不能变
				const start = moment(createTime);
				const end = start.add(Number(effectivePeriod), effectivePeriodUnit).format("YYYY-MM-DD HH:mm:ss");
				this.$refs.stepFormPackageRef.updateFormField("expireDate", end);
			}else if(key === "codeSTD"){//起始编号STD失焦时，更新stepDataSource
        const arr = Array.from({length:codeSTD},(item,index) => ({
         subTargetStartSolutionPrecision:3,//小数点精度默认为3
         targetSolutionCode:`STD${Number(codeSTD)-index}`
        }));
        this.$refs.stepTable.updateDataSource(arr);
      }
		}

  },

}
