<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="80%" v-dialogDrag append-to-body>
      <!-- 搜索工作栏 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="卡券名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入卡券名称" clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="卡券类型" prop="type">
          <el-select v-model="queryParams.type" placeholder="请选择卡券类型" @keyup.enter.native="handleQuery">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.MEMBER_COUPON_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="核销规则" prop="outRule">
          <el-select v-model="queryParams.type" placeholder="请选择核销规则" @keyup.enter.native="handleQuery">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.MEMBER_COUPON_OUT_RULE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 操作工具栏 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openForm(undefined)"
                     v-hasPermi="['member:coupon:create']">新增
          </el-button>
        </el-col>

        <right-toolbar :showSearch.sync="showSearch" @queryTable="queryList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="卡券名称" width="200" align="left" prop="name"/>
        <el-table-column label="卡券类型" width="100" 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="核销规则" width="100" align="center" prop="outRule">
          <template v-slot="scope">
            <dict-tag :type="DICT_TYPE.MEMBER_COUPON_OUT_RULE" :value="scope.row.outRule"/>
          </template>
        </el-table-column>
        <el-table-column label="开始有效期" align="center" prop="beginTime" width="180">
          <template v-slot="scope">
            <span>{{ parseTime(scope.row.beginTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束有效期" align="center" prop="endTime" width="180">
          <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 label="操作" align="center" class-name="small-padding fixed-width">
          <template v-slot="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="openForm(scope.row.id)"
                       v-hasPermi="['member:coupon:update']">修改
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                       v-hasPermi="['member:coupon:delete']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                  @pagination="queryList"/>
      <!-- 对话框(添加 / 修改) -->
      <CouponForm ref="couponRefForm" @success="queryList"/>
    </el-dialog>
  </div>
</template>

<script>
import * as CouponApi from '@/api/member/coupon';
import CouponForm from './CouponForm.vue';

export default {
  name: "ChildCouponForm",
  components: {
    CouponForm,
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 基础卡券列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        parentId:null,
        name: null,
        type: null,
        carModel: null,
        outRule: null,
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(parentId) {
      this.dialogVisible = true;
      this.queryParams.parentId = parentId
      this.queryList()
    },

    /** 查询列表 */
    async queryList() {
      try {
        const res = await CouponApi.getCouponPage(this.queryParams);
        this.list = res.data.records;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.queryList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["couponRefForm"].open(id,this.queryParams.parentId);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除基础卡券名称为"' + row.name + '"的数据项?')
      try {
        await CouponApi.deleteCoupon(id);
        await this.queryList();
        this.$modal.msgSuccess("删除成功");
      } catch {
      }
    },
  }
};
</script>
