<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" 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-table :data="formData.ruleList" border size="small" style="width: 100%">
            <el-table-column prop="name" align="left">
              <template slot="header">
                <i style="color: #409EFF;cursor:pointer" class="el-icon-circle-plus-outline" @click="addItem"/>
                名称
              </template>
              <template v-slot="scope">
                <el-input v-model="scope.row.name" ></el-input>
              </template>
            </el-table-column>

            <el-table-column prop="rechAmount" align="center" label="充值金额">
              <template v-slot="scope">
                <el-input-number v-model="scope.row.rechAmount" :precision="2" :step="0.1" ></el-input-number>
              </template>
            </el-table-column>

            <el-table-column prop="giveAmount" align="center" label="赠送金额">
              <template v-slot="scope">
                <el-input-number v-model="scope.row.giveAmount" :precision="2" :step="0.1" ></el-input-number>
              </template>
            </el-table-column>

            <el-table-column align="center" width="100" label="操作">
              <template v-slot="scope">
                <el-button v-if="formData.ruleList.length > 1" size="mini" type="text" icon="el-icon-delete"
                           @click="deleteItem(scope.$index, formData.ruleList)">删除
                </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>
  </div>
</template>

<script>
import * as ActiveMainApi from '@/api/member/active';

export default {
  name: "ActiveRuleForm",
  components: {},
  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',
        ruleList: [{
          name:'',
          rechAmount: '',
          giveAmount: '',
        }],
      },
      // 表单校验
      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;
      }
    },

    /**
     * 新增规则
     */
    addItem() {
      const item = {
        name:'',
        rechAmount: '',
        giveAmount: '',
      }
      this.formData.ruleList.push(item)
    },

    /**
     * 移除账户内容
     */
    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',
        ruleList: [{
          name:'',
          rechAmount: '',
          giveAmount: '',
        }],
      };
    }
  }
};
</script>
