<template>
  <el-dialog title="批量设置" :visible.sync="dialogVisible" width="700px" @close="close">
    <el-form :model="ruleForm" ref="ruleForm" :rules="rules" label-width="160px">
      <form-content :rule-form.sync="ruleForm"></form-content>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="handleSubmit()">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { isEmpty } from '@/utils/index';
import fetch from '@/api/gift.js';
const { updatePickupStore } = fetch;
import formContent from './pickup-form-content';
export default {
  name: 'PickupUpdateDialog',
  components: {
    formContent
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default() {
        return {};
      }
    },
    ids: {
      // 批量删除的ids
      type: Array,
      default: () => []
    }
  },
  data() {
    const validDynamicType = (rule, value, callback) => {
      const { dynamicType, dynamicH, dynamicM, dynamicD } = this.ruleForm;
      if (dynamicType == 1) {
        if(isEmpty(dynamicH) || isEmpty(dynamicM)) {
          callback(new Error('填写不完整'));
          return;
        }
      } else {
        if(isEmpty(dynamicD)) {
          callback(new Error('填写不完整'));
          return;
        }
      }
      callback();
    };
    const validTime = (rule, value, callback) => {
      const { fixedTime } = this.ruleForm;
      if(fixedTime.length == 0) {
        callback(new Error('请配置固定日期范围'));
        return;
      }
      let flag = false;
      fixedTime.forEach(v => {
        if(!(v.dataRange && v.dataRange.length && v.dataRange[0] && v.dataRange[1])) {
          flag = true;
        }
      });
      if(flag) {
        callback(new Error('请配置固定日期范围'));
        return;
      }
      callback();
    };
    return {
      ruleForm: {
        type: 1,
        chooseTimeFlag: 1,
        dateType: 1, // 日期类型 1、动态日期 2、固定日期
        fixedTime: [ { dataRange: '' } ], // 固定日期
        // 动态日期start
        dynamicType: 1, // 动态日期-类型
        dynamicH: '', // 动态日期-小时
        dynamicM: '', // 动态日期-分钟
        dynamicD: '', // 动态日期-天
        dynamicStop: '', // 停止自提
        // 动态日期end
        storeWidgetId: '', // 门店id
        timeoutDealType: 1, // 超期处理方式：1自动完成核销 2自动关闭发货单
      },
      rules: {
        storeWidgetId: { required: true, message: '请选择门店', trigger: 'change' },
        dynamicType: { required: true, validator: validDynamicType, trigger: 'blur' },
        dynamicStop: { required: true, message: '请填写提货有效期', trigger: 'change' },
        fixedTime: { required: true, validator: validTime, trigger: 'change' },
      },
    };
  },
  methods: {
    close() {
      this.$emit('update:dialogVisible', false);
      this.$refs.ruleForm.resetFields();
    },
    handleSubmit() {
      if(this.loading) {
        return;
      }
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          PublicMethods.jumpToFormError(); // eslint-disable-line
          return;
        } else {
          // 组装数据
          const { chooseTimeFlag, dateType, storeWidgetId, timeoutDealType, dynamicType, dynamicH, dynamicM, dynamicD, dynamicStop, fixedTime } = this.ruleForm;
          let params = {
            chooseTimeFlag, dateType, storeWidgetId, timeoutDealType
          };
          if(chooseTimeFlag) {
            // pickupDateInfo: 动态日期 {"setType":1,"time":45,"validite":2}；固定日期 json数组[{"endTime":1587168000000,"startTime":1587168000000}]
            if(params.dateType == 1) {
              // 动态日期
              params.pickupDateInfo = {
                setType: dynamicType,
                time: dynamicType == 1 ? dynamicH * 60 + dynamicM : dynamicD * 24 * 60, // 转换为分钟
                validite: dynamicStop
              };
            } else {
              // 固定日期
              params.pickupDateInfo = fixedTime.map(v => {
                return { startTime: new Date(v.dataRange[0]).getTime(), endTime: new Date(v.dataRange[0]).getTime() };
              });
            }
            params.pickupDateInfo = JSON.stringify(params.pickupDateInfo);
          }
          params.ids = this.ids.join(',');
          this.loading = true;
          updatePickupStore(params).then(res => {
            this.loading = false;
            this.$tips({ message: '修改成功', type: 'success' });
            this.close();
            this.$emit('mutiUpdate');
          }).catch(() => {
            this.loading = false;
          });
        }
      });
    }
  }
};
</script>