<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="60%" v-dialogDrag append-to-body>
      <el-collapse v-model="activeNames">
        <!--   供应商基本信息     -->
        <el-collapse-item name="1">
          <template slot="title">
            <div style="font-weight: bold">活动内容</div>
          </template>
          <el-descriptions class="margin-top" :column="2" border>

            <el-descriptions-item>
              <template slot="label">
                活动名称
              </template>
              {{ formData.name }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">
                活动类型
              </template>
              <dict-tag :type="DICT_TYPE.MEMBER_ACTIVE" :value="formData.type"/>
            </el-descriptions-item>

            <el-descriptions-item>
              <template slot="label">
                开始时间
              </template>
              {{parseTime(formData.beginTime)}}
            </el-descriptions-item>

            <el-descriptions-item>
              <template slot="label">
                结束时间
              </template>
              {{parseTime(formData.endTime)}}
            </el-descriptions-item>

            <el-descriptions-item>
              <template slot="label">
                活动内容
              </template>
              {{formData.remark}}
            </el-descriptions-item>
          </el-descriptions>
        </el-collapse-item>

        <el-collapse-item title="关联卡券" name="2">
          <template slot="title">
            <div style="font-weight: bold">关联卡券</div>
          </template>
          <el-button @click="openChooseCoupon" size="mini" type="primary" plain>选择卡券</el-button>
          <el-table height="250" :data="formData.couponList" border size="small" style="width: 100%">
            <el-table-column label="卡券名称" width="250" align="left" prop="name"/>
            <el-table-column label="卡券类型" width="80" align="left" prop="type">
              <template v-slot="scope">
                <dict-tag :type="DICT_TYPE.MEMBER_COUPON_TYPE" :value="scope.row.type"/>
              </template>
            </el-table-column>
            <el-table-column label="开始有效期" align="center" prop="beginTime" width="150">
              <template v-slot="scope">
                <span>{{ parseTime(scope.row.beginTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="结束有效期" align="center" prop="endTime" width="150">
              <template v-slot="scope">
                <span>{{ parseTime(scope.row.endTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="面额" width="100" align="right" prop="amount"/>
            <el-table-column label="备注"  show-overflow-tooltip align="left" prop="remark"/>
            <el-table-column align="center" width="100" label="操作">
              <template v-slot="scope">
                <el-button size="mini" type="text" icon="el-icon-delete"
                           @click="deleteItem(scope.$index, formData.couponList)">删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-collapse-item>
      </el-collapse>


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
    <ChooseCouponDraw @success="chooseCoupon"  ref="chooseCouponForm"/>
  </div>
</template>

<script>
import * as ActiveMainApi from '@/api/member/active';
import ChooseCouponDraw from '@/views/member/active/ChooseCouponDraw.vue';

export default {
  name: "ActiveRuleCouponForm",
  components: {ChooseCouponDraw},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      //折叠面板默认展开
      activeNames: ['1', '2'],
      // 表单参数
      formData: {
        id: undefined,
        name: undefined,
        type: undefined,
        beginTime: undefined,
        endTime: undefined,
        // status: '01',
        remark: undefined,
        // isRechRule: '0',
        // isPack:'0',
        // isCoupon:'0',
        couponList:[]
      },
      // 表单校验
      formRules: {},
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await ActiveMainApi.getActiveMain(id);
          this.formData = res.data;
          this.title = "修改营销活动";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增营销活动";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await ActiveMainApi.updateActiveMain(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await ActiveMainApi.createActiveMain(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },

    /**选择卡券*/
    openChooseCoupon(){
      this.$refs["chooseCouponForm"].open(this.formData.couponList);
    },

    /**
     * 选中车辆
     */
    chooseCoupon(chooseCoupon){
      this.formData.couponList = chooseCoupon
    },

    /**
     * 移除账户内容
     */
    deleteItem(index, rows) {
      rows.splice(index, 1);
    },

    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        name: undefined,
        type: undefined,
        beginTime: undefined,
        endTime: undefined,
        // status: '01',
        remark: undefined,
        // isRechRule: '0',
        // isPack:'0',
        // isCoupon:'0',
        couponList:[]
      };
    }
  }
};
</script>
