<template>
  <div id="form-1">
    <el-row>
      <el-col :span="24" class="main animate__animated animate__backInRight">
        <el-col class="top-btn">
          <el-button type="primary" size="mini" @click="toBack()">返回</el-button>
        </el-col>
        <el-col :span="12">
          <el-form :model="form" :rules="rules" ref="form" label-width="180px">
            <el-col :span="24">
              <el-form-item label="发行方" prop="issue">
                <el-select v-model="form.issue" clearable filterable placeholder="请选择发行方" size="small" style="width: 100%">
                  <el-option v-for="i in issueList" :key="i.label" :label="i.label" :value="i.value"> </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="店铺" prop="shop" style="display: none">
                <el-select
                  v-model="form.shop"
                  filterable
                  remote
                  reserve-keyword
                  placeholder="请输入店铺名称"
                  :remote-method="querySearch"
                  :loading="loading"
                  @change="handleSelect"
                >
                  <el-option v-for="item in shopList" :key="item.id" :label="item.name" :value="item.id"> </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="优惠券名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入优惠券名称" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="优惠券数量" prop="num">
                <el-input v-model="form.num" placeholder="请输入优惠券数量" type="number" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="是否使用" prop="status">
                <el-select v-model="form.status" clearable filterable placeholder="请选择是否使用" size="small" style="width: 100%">
                  <el-option v-for="i in statusList" :key="i.label" :label="i.label" :value="i.value"> </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- 减免方式 -->
            <el-col :span="24">
              <el-form-item label="减免方式" prop="discount_type">
                <el-select v-model="form.discount_type" clearable filterable placeholder="请选择减免方式" size="small" style="width: 100%">
                  <el-option v-for="i in discount_typeList" :key="i.label" :label="i.label" :value="i.value"> </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="form.discount_type == 'min' && form.discount_type">
              <el-form-item label="订单最低消费金额" prop="limit">
                <el-input v-model="form.limit" placeholder="请输入订单最低消费金额" type="number" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="form.discount_type == 'min' && form.discount_type">
              <el-form-item label="满足条件后优惠的金额" prop="min">
                <el-input v-model="form.min" placeholder="请输入满足条件后优惠的金额" type="number" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="form.discount_type == 'discount' && form.discount_type">
              <el-form-item label="订单最低消费金额" prop="limit">
                <el-input v-model="form.limit" placeholder="请输入订单最低消费金额" type="number" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="form.discount_type == 'discount' && form.discount_type">
              <el-form-item label="几折" prop="min">
                <el-input v-model="form.min" placeholder="请输入X折的X" type="number" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="form.discount_type == 'discount' && form.discount_type">
              <el-form-item label="折扣减免的最大金额" prop="discount_type_max">
                <el-input v-model="form.discount_type_max" placeholder="请输入折扣减免的最大金额" type="number" size="small"></el-input>
              </el-form-item>
            </el-col>
            <!-- 使用限制 -->
            <el-col :span="24">
              <el-form-item label="使用限制" prop="use_limit">
                <el-select v-model="form.use_limit" clearable filterable placeholder="请选择使用限制" size="small" style="width: 100%">
                  <el-option v-for="i in use_limitList" :key="i.label" :label="i.label" :value="i.value"> </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="form.use_limit == 'tags' && form.use_limit">
              <el-form-item label="商品类型" prop="tags">
                <el-cascader
                  v-model="form.tags"
                  :options="tagsList"
                  :props="props"
                  clearable
                  filterable
                  placeholder="请选择商品类型"
                  :show-all-levels="false"
                ></el-cascader>
              </el-form-item>
            </el-col>
            <!-- 领取限制 -->
            <el-col :span="24">
              <el-form-item label="领取限制" prop="get_limit">
                <el-select v-model="form.get_limit" clearable filterable placeholder="请选择领取限制" size="small" style="width: 100%">
                  <el-option v-for="i in get_limitList" :key="i.label" :label="i.label" :value="i.value"> </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="form.get_limit == 'max' && form.get_limit">
              <el-form-item label="每人领取最大限制" prop="get_limit_max">
                <el-input v-model="form.get_limit_max" placeholder="请输入每人领取最大限制" type="number" size="small"></el-input>
              </el-form-item>
            </el-col>
            <!-- 失效方式 -->
            <el-col :span="24">
              <el-form-item label="失效方式" prop="expire_type">
                <el-select v-model="form.expire_type" clearable filterable placeholder="请选择失效方式" size="small" style="width: 100%">
                  <el-option v-for="i in expire_typeList" :key="i.label" :label="i.label" :value="i.value"> </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="form.expire_type == 'fixed' && form.expire_type">
              <el-form-item label="失效配置" prop="time">
                <el-date-picker
                  v-model="form.time"
                  type="datetimerange"
                  range-separator="至"
                  start-placeholder="生效开始日期"
                  end-placeholder="生效结束日期"
                  format="yyyy-MM-dd HH:mm:ss"
                  value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="form.expire_type == 'days' && form.expire_type">
              <el-form-item label="失效配置" prop="days">
                <el-input v-model="form.days" placeholder="请输入生效几天后过期" type="number" size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24" class="btn">
              <el-button type="primary" size="mini" @click="onSubmit('form')">提交</el-button>
              <el-button type="danger" size="mini" @click="toBack()">取消</el-button>
            </el-col>
          </el-form>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
const moment = require('moment');
import { mapState, mapGetters, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('coupon');
const { mapActions: dictData } = createNamespacedHelpers('dictData');
const { mapActions: goodsTags } = createNamespacedHelpers('goodsTags');
const { mapActions: shop } = createNamespacedHelpers('shop');
export default {
  name: 'form-1',
  props: {},
  components: {},
  data: function () {
    return {
      form: {},
      rules: {
        issue: [{ required: true, message: '请选择发行方', trigger: 'change' }],
      },
      // 发行方
      issueList: [],
      // 失效方式
      expire_typeList: [],
      // 减免方式
      discount_typeList: [],
      // 领取限制
      get_limitList: [],
      // 使用限制
      use_limitList: [],
      // 商品类型
      tagsList: [],
      props: { multiple: true, label: 'label', value: 'code' },
      // 是否使用
      statusList: [],
      // 商铺
      shopList: [],
      // 店铺远程搜索加载
      loading: false,
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...dictData({ dictQuery: 'query' }),
    ...goodsTags(['tree']),
    ...shop({ shopQuery: 'query', shopFetch: 'fetch' }),
    ...mapActions(['fetch', 'create', 'update']),
    // 查询
    async search() {
      if (this.id) {
        let res = await this.fetch(this.id);
        if (this.$checkRes(res)) {
          let data = res.data;
          data.get_limit_max = _.get(data, 'get_limit_config.max');
          data.discount_type_max = _.get(data, 'discount_config.max');
          data.limit = _.get(data, 'discount_config.limit');
          data.min = _.get(data, 'discount_config.min');
          data.tags = _.get(data, 'use_limit_config.tags');
          data.time = _.get(data, 'expire_config.fixed[0]');
          data.days = _.get(data, 'expire_config.days');
          // if (data.shop) {
          //   let res = await this.shopFetch(data.shop);
          //   if (this.$checkRes(res)) {
          //     data.shop = res.data.name;
          //   }
          // }
          this.$set(this, `form`, data);
        }
      } else {
        this.$set(this, `form`, {});
      }
    },
    // 店铺名称远程查询
    async querySearch(value) {
      this.loading = true;
      let res = await this.shopQuery({ name: value });
      if (this.$checkRes(res)) {
        this.$set(this, 'shopList', res.data);
      }
      this.loading = false;
    },
    // 店铺远程搜索多选
    handleSelect(value) {
      this.$set(this.form, `shop`, value);
    },
    // 提交
    onSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let data = this.form;
          let discount_config = { limit: data.limit, min: data.min, max: data.discount_type_max }; //减免配置
          let use_limit_config = { tags: data.tags }; //使用配置
          let get_limit_config = { max: data.get_limit_max }; //领取配置
          let expire_config = { fixed: [data.time], days: data.days }; //失效配置
          data = { ...data, discount_config, use_limit_config, get_limit_config, expire_config };
          let res;
          if (data._id) res = await this.update(data);
          else res = await this.create(data);
          if (this.$checkRes(res, '维护信息成功', `${res.errmsg}`)) this.toBack();
        } else {
          return false;
        }
      });
    },
    // 查询其他信息
    async searchOther() {
      let res;
      // 发行方
      res = await this.dictQuery({ code: 'coupon_issue', value: '0' });
      if (this.$checkRes(res)) {
        this.$set(this, `issueList`, res.data);
      }
      // 失效方式
      res = await this.dictQuery({ code: 'coupon_expire_type' });
      if (this.$checkRes(res)) {
        this.$set(this, `expire_typeList`, res.data);
      }
      // 减免方式
      res = await this.dictQuery({ code: 'coupon_discount_type' });
      if (this.$checkRes(res)) {
        this.$set(this, `discount_typeList`, res.data);
      }
      // 使用限制
      res = await this.dictQuery({ code: 'coupon_use_limit' });
      if (this.$checkRes(res)) {
        this.$set(this, `use_limitList`, res.data);
      }
      // 领取限制
      res = await this.dictQuery({ code: 'coupon_get_limit' });
      if (this.$checkRes(res)) {
        this.$set(this, `get_limitList`, res.data);
      }
      // 商品类型
      res = await this.tree();
      if (this.$checkRes(res)) this.$set(this, `tagsList`, res.data);
      // 使用状态
      res = await this.dictQuery({ code: 'use' });
      if (this.$checkRes(res)) {
        this.$set(this, `statusList`, res.data);
      }
    },
    // 返回
    toBack() {
      window.history.go('-1');
    },
  },
  computed: {
    id() {
      return this.$route.query.id;
    },
  },
  metaform() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    font-size: 20px;
    margin: 10px 0 0 20%;
    padding: 5px;
  }
  .btn {
    text-align: center;
  }
}
</style>
