<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="优惠券名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入优惠券名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="优惠券类型" prop="couponType">
        <el-select v-model="queryParams.couponType" placeholder="请选择" clearable size="small">
          <el-option v-for="dict in couponTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable size="small">
          <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @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="handleAdd" v-hasPermi="['store:storeCoupon:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['store:storeCoupon:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>
    <el-table border v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="优惠券名称" align="center" prop="name" width="180" show-overflow-tooltip />
      <el-table-column label="类型" align="center" prop="couponType" width="100">
        <template slot-scope="scope"><dict-tag :options="couponTypeOptions" :value="scope.row.couponType" /></template>
      </el-table-column>
      <el-table-column label="面值/折扣" align="center" prop="couponValue" width="100">
        <template slot-scope="scope"><span>{{ scope.row.couponType === '1' ? '¥' + scope.row.couponValue : scope.row.couponValue + '折' }}</span></template>
      </el-table-column>
      <el-table-column label="最低消费" align="center" prop="minAmount" width="100">
        <template slot-scope="scope"><span>¥{{ scope.row.minAmount || 0 }}</span></template>
      </el-table-column>
      <el-table-column label="发行量" align="center" prop="totalCount" width="80" />
      <el-table-column label="已领取" align="center" prop="usedCount" width="80" />
      <el-table-column label="有效期" align="center" width="200">
        <template slot-scope="scope"><span>{{ scope.row.startTime }} ~ {{ scope.row.endTime }}</span></template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope"><dict-tag :options="statusOptions" :value="scope.row.status" /></template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['store:storeCoupon:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['store:storeCoupon:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { getRequest, delRequest } from "@/api/common";
export default {
  name: "StoreCoupon",
  data() {
    return {
      loading: true, exportLoading: false, ids: [], showSearch: true, total: 0, list: [],
      couponTypeOptions: [], statusOptions: [],
      queryParams: { pageNum: 1, pageSize: 10, name: undefined, couponType: undefined, status: undefined }
    };
  },
  created() {
    this.getDicts("coupon_type").then(res => { this.couponTypeOptions = res.data; });
    this.getDicts("sys_normal_disable").then(res => { this.statusOptions = res.data; });
    this.getList();
  },
  methods: {
    getList() { this.loading = true; getRequest("/store/storeCoupon/list", this.queryParams).then(res => { this.list = res.rows; this.total = res.total; this.loading = false; }); },
    handleQuery() { this.queryParams.pageNum = 1; this.getList(); },
    resetQuery() { this.resetForm("queryForm"); this.handleQuery(); },
    handleSelectionChange(selection) { this.ids = selection.map(item => item.id); },
    handleAdd() { this.$router.push({ path: "/storeCouponEdit" }); },
    handleUpdate(row) { this.$router.push({ path: "/storeCouponEdit", query: { id: row.id } }); },
    handleDelete(row) { const ids = row.id || this.ids; this.$modal.confirm('确认删除优惠券吗？').then(() => delRequest("/store/storeCoupon/" + ids)).then(() => { this.getList(); this.$modal.msgSuccess("删除成功"); }).catch(() => {}); },
    handleExport() { this.$modal.confirm("确认导出所有优惠券数据吗？").then(() => { this.exportLoading = true; getRequest("/store/storeCoupon/export", this.queryParams).then(res => { this.download(res.msg); this.exportLoading = false; }); }).catch(() => {}); }
  }
};
</script>
