<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      size="small"
      @submit.native.prevent
    >
      <el-form-item label="券包名称" prop="title">
         <el-input
          v-model="queryParams.title"
          placeholder="请输入券包名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </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="10">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="$router.push({ name: 'CouponPkgAdd' })"
          v-hasPermi="['business:couponPkg:add']"
          >生成券包</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="券包编号" prop="id" width="80" align="center" />
      <el-table-column label="券包名称" prop="title" align="center" />
      <el-table-column label="券数量" prop="couponNum" />
      <el-table-column label="总面值">
        <template slot-scope="{ row }">
          ¥{{ $numberFormat(row.facePrice) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status">
        <template slot-scope="{ row }">
          <el-tag v-if="row.status == 0" type="info">待上架</el-tag>
          <el-tag v-else-if="row.status == 1" type="success">上架</el-tag>
          <el-tag v-else-if="row.status == 2" type="info">下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" width="230">
        <template slot-scope="{ row }">
          <el-button
            v-hasPermi="['business:couponPkg:query']"
            type="text"
            @click="
              $router.push({
                name: 'CouponPkgQuery',
                query: { id: row.id },
              })
            "
            >查看</el-button
          >
          <el-button
            v-if="row.status === 0"
            v-hasPermi="['business:couponPkg:edit']"
            type="text"
            @click="
              $router.push({
                name: 'CouponPkgEdit',
                query: { id: row.id },
              })
            "
            >编辑</el-button
          >
          <el-button
            v-if="row.status === 0 || row.status === 2"
            v-hasPermi="['business:couponPkg:on']"
            type="text"
            @click="setStatus(row, 1)"
            >上架</el-button
          >
          <el-button
            v-if="row.status === 1"
            v-hasPermi="['business:couponPkg:off']"
            type="text"
            @click="setStatus(row, 2)"
            >下架</el-button
          >
          <el-button
            v-if="row.status === 0"
            v-hasPermi="['business:couponPkg:remove']"
            class="del"
            type="text"
            @click="del(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="pageParams.pageNum"
      :limit.sync="pageParams.pageSize"
      @pagination="getList()"
    />
  </div>
</template>
<script>
import {
  getCouponList,
  delCoupon,
  setCouponPkgStatus,
} from "@/api/business/couponPkg";
export default {
  name: 'ListCouponPkg',
  data() {
    return {
      loading: false,
      showSearch: true,
      queryParams: {
        title: "",
      },
      // 分页
      pageParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 总条数
      total: 0,
      list: [],
    };
  },
  watch: {
    $route: {
      handler: function (val, oldVal) {
        if (val.name == "ListCouponPkg") {
          this.getList();
        }
      },
    },
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getCouponList(
        "pageNum=" +
          this.pageParams.pageNum +
          "&pageSize=" +
          this.pageParams.pageSize +
          "&",
        this.queryParams
      )
        .then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.list = res.rows;
            this.total = res.total;
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },

     //搜索
    handleQuery() {
      this.pageParams.pageNum = 1;
      this.getList();
    },

    // 重置
    resetQuery() {
      this.resetForm("queryForm");
      this.pageParams.pageNum = 1;
      this.getList();
    },

    setStatus(item, status) {
      this.$confirm(
        `确认${status === "on" ? "上架" : "下架"}券包 “${item.title}” 吗？`,
        `${status === "on" ? "上架" : "下架"}券包`,
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        setCouponPkgStatus({
          id: item.id,
          status,
        }).then((res) => {
          if (res.code === 0) {
            this.$message.success("操作已完成！");
            this.getList();
          }
        });
      });
    },

    del(item) {
      this.$confirm(`确认删除券包 “${item.title}” 吗？`, "删除券", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delCoupon(item.id).then((res) => {
          if (res.code === 0) {
            this.$message.success("操作已完成！");
            this.getList();
          }
        });
      });
    },
  },
};
</script>
<style>
</style>
