<!--
 * @Description: 新增/编辑弹框
 * @Author: Sugar.
 * @Date: 2023-11-24 13:55:00
 * @LastEditors: gcz
 * @LastEditTime: 2024-03-14 17:48:20
 * @FilePath: \great_webui\src\views\marketing\coupon\dialog\addAndEdit.vue
 * @Copyright: Copyright (c) 2016~2023 by Sugar., All Rights Reserved.
-->
<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="700px"
    append-to-body
    :close-on-click-modal="false"
    @close="cancel"
  >
    <div class="dialog">
      <el-form :model="form" ref="form" :rules="rules" label-width="120px">
        <el-form-item label="优惠劵名称" prop="couponName" required>
          <el-input
            v-model="form.couponName"
            placeholder="优惠劵名称"
            clearable
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="优惠劵类型" prop="type" required>
          <el-radio v-model="form.type" :label="1">满减卷</el-radio>
          <el-radio v-model="form.type" :label="2">折扣卷</el-radio>
        </el-form-item>
        <el-form-item label="优惠额度" prop="quota" required>
          <el-input placeholder="优惠额度" v-model="form.quota">
            <template slot="append">
              <span v-if="form.type==1">元</span>
              <span v-if="form.type==2">%</span>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="使用对象" prop="useUserType" required>
          <el-radio v-model="form.useUserType" :label="1">所有用户</el-radio>
        </el-form-item>
        <el-form-item label="使用剧目" prop="usePerform" required>
          <el-select v-model="form.usePerform" @change="performChange" placeholder="请选择使用剧目">
                <el-option
                  v-for="dict in performList"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
        </el-form-item>
        <el-form-item label="使用票务" prop="useGood" required>
          <el-select v-model="form.useGood" multiple placeholder="请选择使用票务">
                <el-option
                  v-for="dict in goodsList"
                  :key="dict.goodsId"
                  :label="dict.goodsName"
                  :value="dict.goodsId"
                ></el-option>
              </el-select>
        </el-form-item>
        <el-form-item label="使用门槛" prop="useThresholdType" required>
          <el-radio v-model="form.useThresholdType" :label="1">无条件</el-radio>
          <el-radio v-model="form.useThresholdType" :label="2">指定条件</el-radio>
        </el-form-item>
        <el-form-item v-if="form.useThresholdType==2" label="累计消费满" prop="useThresholdAmount" required>
          <el-input placeholder="" v-model="form.useThresholdAmount">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="发放渠道" prop="channelType" required>
          <el-select v-model="form.channelType" placeholder="请选择发放渠道">
                <el-option
                  v-for="dict in channelTypeList"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
        </el-form-item>
        <el-form-item label="发放数量" prop="issuseNum" required>
          <el-input placeholder="" v-model="form.issuseNum" @change="changeNumber('issuseNum')">
            <template slot="append">张</template>
          </el-input>
        </el-form-item>
        <!-- <el-form-item label="是否叠加" prop="isAdd" required>
          <el-radio v-model="form.isAdd" :label="1">否</el-radio>
          <el-radio v-model="form.isAdd" :label="2">是</el-radio>
        </el-form-item> -->
        <el-form-item label="领取限制" prop="receiveType" required>
          <el-radio v-model="form.receiveType" :label="1">否</el-radio>
          <el-radio v-model="form.receiveType" :label="2">是</el-radio>
        </el-form-item>
        <el-form-item v-if="form.receiveType==2" label="领取限制数量" prop="receiveNum" required>
          <el-input placeholder="" v-model="form.receiveNum" @change="changeNumber('issuseNum')">
            <template slot="append">张</template>
          </el-input>
        </el-form-item>
        <el-form-item label="使用时间" prop="useType" required>
          <el-radio v-model="form.useType" :label="1">指定时间</el-radio>
          <el-radio v-model="form.useType" :label="2">有效天数</el-radio>
        </el-form-item>
        <el-form-item v-if="form.useType==1" label="指定时间" prop="useDate" required>
          <el-date-picker
            @change="useStartDateChange"
            v-model="form.useDate"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="form.useType==2" label="有效天数" prop="useDay" required>
          <el-input placeholder="领取后优惠券有效使用天数" v-model="form.useDay">
            <template slot="append">天</template>
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button
        type="primary"
        @click="submitForm"
        v-loading.fullscreen.lock="loading"
        element-loading-text="提交中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <span v-if="loading">提交中...</span>
        <span v-else>保存</span>
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
// import { updateNoticeMgr } from "@/api/system/noticeMgr";
import { saveAndEdit } from "@/api/coupon/coupon";
import { goodsPageList } from "@/api/programmeMr/programmeMr";
// import { getToken } from "@/utils/auth";
// import { log } from 'mathjs';
export default {
  name: "addAndEdit",
  props: {
    dict: {
      type: Object,
      default: () => [],
    },
  },
  components: {
    
  },
  data() {
    return {
      title: "编辑",
      model: "EDIT",
      activeName: '01',
      open: false,
      loading: false,
      form: {
        id: undefined,
      },
      rules: {
        couponName: [{ required: true, message: "请输入优惠劵名称", trigger: ["change","blur"] }],
        type: [{ required: true, message: "请选择优惠劵类型", trigger: ["change","blur"] }],
        quota: [{ required: true, message: "请输入优惠额度", trigger: ["change","blur"] }],
        useUserType: [{ required: true, message: "请选择使用对象", trigger: ["change","blur"] }],
        usePerform: [
          { required: true, message: "请选择使用剧目", trigger: ["change","blur"] },
        ],
        useGood	: [{ required: true, message: "请选择使用票务", trigger: ["change","blur"] }],
        useThresholdType: [{ required: true, message: "请选择使用门槛", trigger: ["change","blur"] }],
        useThresholdAmount: [{ required: true, message: "请输入累计消费满", trigger: ["change","blur"] }],
        channelType	: [{ required: true, message: "请选择发放渠道", trigger: ["change","blur"] }],
        issuseNum	: [{ required: true, message: "请输入发放数量", trigger: ["change","blur"] }],
        isAdd	: [{ required: true, message: "请选择是否叠加", trigger: ["change","blur"] }],
        receiveType	: [{ required: true, message: "请选择是否限制", trigger: ["change","blur"] }],
        receiveNum	: [{ required: true, message: "请输入领取限制数量", trigger: ["change","blur"] }],
        useType	: [{ required: true, message: "请选择使用时间", trigger: ["change","blur"] }],
        useStartDate	: [{ required: true, message: "请选择指定时间", trigger: ["change","blur"] }],
        useDay	: [{ required: true, message: "请输入有效天数", trigger: ["change","blur"] }],
      },
      channelTypeList:[
        {label:'小程序',value:1},
      ],
      performList:[],
      goodsList:[],
    };
  },
  created() {
    this.getPerformList();
  },
  methods: {
    /**
     * 打开弹框
     * @date 2023-11-22
     * @param {any} obj
     * @returns {any}
     */
    openDialog(title, obj) {
      this.open = true;
      this.reset();
      if (obj){
        this.title = "编辑优惠券";
        this.$nextTick(() => {
          this.$set(this.form, 'id', obj.id);
          this.$set(this.form, 'couponName', obj.couponName);
          this.$set(this.form, 'type', obj.type);
          this.$set(this.form, 'quota', obj.quota);
          this.$set(this.form, 'useUserType', obj.useUserType);
          this.$set(this.form, 'usePerform', obj.usePerform);
          this.$set(this.form, 'useThresholdType', obj.useThresholdType);
          this.$set(this.form, 'useThresholdAmount', obj.useThresholdAmount);
          this.$set(this.form, 'channelType', obj.channelType);
          this.$set(this.form, 'issuseNum', obj.issuseNum);
          this.$set(this.form, 'isAdd', obj.isAdd);
          this.$set(this.form, 'receiveType', obj.receiveType);
          this.$set(this.form, 'receiveNum', obj.receiveNum);
          this.$set(this.form, 'useType', obj.useType);
          this.$set(this.form, 'useStartDate', obj.useStartDate);
          this.$set(this.form, 'useEndDate', obj.useEndDate);
          this.$set(this.form, 'useDay', obj.useDay);
          this.$set(this.form, 'useDate', [obj.useStartDate, obj.useEndDate]);
          this.performChange(obj.usePerform);
          this.$set(this.form, 'useGood', obj.useGood);
          this.form.useGood= this.getGoodsIds(this.form.useGood);
        });
      }else{
        this.title = "新增优惠券";
        
      }
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    getGoodsIds(data, type) {
        if (type == "array") {
            let array = data.join(",")
            return array

        } else {
            let string = data.split(",");
            return string
        }
    },
    changeNumber(key) {
      if(this.form[key] * 1 < 1){
        this.$message.error("输入需大于或等于1!");
        this.$set(this.form, key, '');
        return false
      }
    },
    /**
     * 保存
     * @date 2023-11-22
     * @returns {any}
     */
    submitForm() {
      delete this.form.useDate;
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          try {
            this.loading = true;
            let params = JSON.parse(JSON.stringify(this.form))
            params.useGood = this.getGoodsIds(params.useGood, "array")
            const { code } = await saveAndEdit({ ...params });
            if (code === 200) {
              this.$message.success("操作成功!");
              this.$emit("getList");
              this.cancel();
            }
          } catch (error) {
          } finally {
            this.loading = false;
          }
        }
      });
    },
    /**
     * 重置
     * @date 2023-11-22
     * @returns {any}
     */
    reset() {
      this.$set(this.form, 'id', '');
      this.$set(this.form, 'couponName', '');
      this.$set(this.form, 'type', '');
      this.$set(this.form, 'quota', '');
      this.$set(this.form, 'useUserType', 1);
      this.$set(this.form, 'usePerform', '');
      this.$set(this.form, 'useGood', '');
      this.$set(this.form, 'useThresholdType', '');
      this.$set(this.form, 'useThresholdAmount', '');
      this.$set(this.form, 'channelType', 1);
      this.$set(this.form, 'issuseNum', '');
      this.$set(this.form, 'isAdd', '');
      this.$set(this.form, 'receiveType', '');
      this.$set(this.form, 'receiveNum', '');
      this.$set(this.form, 'useType', '');
      this.$set(this.form, 'useStartDate', '');
      this.$set(this.form, 'useEndDate', '');
      this.$set(this.form, 'useDay', '');
      this.$set(this.form, 'useDate', '');
    },
    /**
     * 关闭弹框
     * @date 2023-11-22
     * @returns {any}
     */
    cancel() {
      this.reset();
      this.open = false;
    },
    /** 查询剧目列表 */
    getPerformList() {
      goodsPageList({status:1,pageNum:1,pageSize:999})
      .then(response => {
          this.performList = response.data.rows.map(item => {return {label:item.name,value:item.id,goodsList:item.goodsList}});
        }
      );
    },
    performChange(e){
      // console.log('performChange',e);
      // console.log('this.performList',this.performList);
      // this.$set(this.form, 'useGood', '');
      // console.log('performChange',e);
      let goodsList = this.performList.filter(item => {return item.value == e})[0].goodsList;
      this.goodsList = goodsList;
      // console.log('goodsList',this.goodsList);
      // console.log('useGood',this.form.useGood);
      this.$set(this.form, 'useGood', null);

    },
    useStartDateChange(e){
      // console.log('useStartDateChange',e);
      // console.log('useStartDate',this.form.useDate);
      this.form.useStartDate  = e[0];
      this.form.useEndDate = e[1];
      console.log(' this.form.useStartDate', this.form.useStartDate);
    }
  },
};
</script>

<style lang="scss" scoped>
.dialog {
  padding: 0 30px;
  max-height: 65vh;
  overflow-y: auto;
}
.dialog {
  padding: 0 30px;
  .upload-btn {
    width: 100px;
    height: 100px;
    background-color: #fbfdff;
    border: dashed 1px #c0ccda;
    border-radius: 5px;
    i {
      font-size: 30px;
      margin-top: 20px;
    }
    &-text {
      margin-top: -10px;
    }
  }
  .avatar {
    cursor: pointer;
  }
}
</style>
