<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="60%" v-dialogDrag append-to-body>
      <span style="color: red">请输入充值金额：</span><el-input-number v-model="topUpAmount" :precision="2"  ></el-input-number>
      <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 label="名称" prop="name" align="left"/>
            <el-table-column label="充值金额" prop="rechAmount" align="right"/>
            <el-table-column label="赠送金额" prop="giveAmount" align="right"/>
          </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'],
      topUpAmount:0.00,
      // 表单参数
      formData: {
        id: undefined,
        name: undefined,
        type: undefined,
        beginTime: undefined,
        endTime: undefined,
        remark: undefined,
        ruleList: [{
          name:'',
          rechAmount: '',
          giveAmount: '',
        }],
      },
      //客户数据
      cusRow:{},
      // 表单校验
      formRules: {},
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id,cusRow) {
      this.dialogVisible = true;
      this.cusRow = cusRow
      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 = {
          cusId:this.cusRow.id,
          activeId:this.formData.id,
          topUpAmount:this.topUpAmount
        }
        await ActiveMainApi.attendActive(data);
        this.$modal.msgSuccess("参与成功");
        this.dialogVisible = false
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },





    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        name: undefined,
        type: undefined,
        beginTime: undefined,
        endTime: undefined,
        remark: undefined,
        ruleList: [{
          name:'',
          rechAmount: '',
          giveAmount: '',
        }],
      };
    }
  }
};
</script>
