<template>
  <div class="p20" v-loading="loading">
    <el-form :model="ruleForm" ref="ruleForm" :rules="rules" label-width="160px">
      <h3 class="pb20">自提点信息</h3>
      <el-form-item label="自提点类型">
        <el-radio v-model="ruleForm.type" :label="1">从门店中选择</el-radio>
      </el-form-item>
      <el-form-item label="门店选择器">
        ？？
      </el-form-item>
      <h3 class="pt20 pb20">自提点信息</h3>
      <form-content :rule-form.sync="ruleForm"></form-content>
      <el-form-item class="mt20">
        <el-button @click="handleSubmit()" type="primary">{{ isAdd ? '确认新建' : '保存' }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import fetch from '@/api/gift.js';
const { insertPickupStore, getPickupDetails } = fetch;
import formContent from './module/pickup-form-content';

export default {
  name: 'PickupForm',
  components: {
    formContent
  },
  data() {
    return {
      loading: false,
      integralMallPickUpPointId: '', // 主键
      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: {},
      isAdd: this.$route.meta.type === 'add',
      isEdit: this.$route.meta.type === 'edit',
    };
  },
  created() {
    this.$emit('updateBread', [  { breadName: '福利中心'  }, { breadName: '通用设置', breadPath: '/card-package/list' }, { breadName: '自提点设置', breadPath: '/setting/pickup-list' }, { breadName: this.isAdd ? '新建自提点' : '编辑自提点', breadPath: '' } ]);
    this.integralMallPickUpPointId = this.$route.query.integralMallPickUpPointId;
  },
  methods: {
    getInfo() {
      this.loading = true;
      getPickupDetails().then(() => {
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
    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
          };
          // 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[0]).getTime(), endTime: new Date(v[0]).getTime() };
            });
          }
          if(this.isEdit) {
            params.integralMallPickUpPointId = this.integralMallPickUpPointId;
          }
          this.loading = true;
          insertPickupStore(params).then(res => {
            this.loading = false;
            this.$router.push('/setting/pickup-list');
            this.$tips({ message: `${this.isAdd ? '新建' : '编辑'}成功`, type: 'success' });
          }).catch(() => {
            this.loading = false;
          });
        }
      });
    }
  },
};
</script>