<template>
  <div class="app-container coupon-list">
    <el-form v-show="showSearch" :model="queryParams" ref="queryForm" :inline="true" size="small">
      <el-form-item label="券名称">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入券名称"
          clearable
          @keyup.enter.native="queryParams.pageNum = 1;getList()"
        />
      </el-form-item>
      <el-form-item label="使用场景">
        <el-select v-model="queryParams.type" placeholder="请选择券状态" clearable @change="queryParams.pageNum = 1;getList()">
          <el-option label="全部" value="" />
          <el-option label="盲票购买" :value="1" />
          <el-option label="门店消费" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" placeholder="请选择券状态" clearable @change="queryParams.pageNum = 1;getList()">
          <el-option label="全部" value="" />
          <el-option label="待上架" value="init" />
          <el-option label="已上架" value="on" />
          <el-option label="已下架" value="off" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="queryParams.pageNum = 1;getList()">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="getList(true)">重置</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('/coupon/add')"
          v-hasPermi="['business:coupon:add']"
        >生成优惠券</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="券ID" prop="couponId" width="80" />
      <el-table-column label="券名称" prop="title" />
      <el-table-column label="券金额" prop="discount">
        <template slot-scope="{row}">
          ¥{{$numberFormat(row.discount)}}
        </template>
      </el-table-column>
      <el-table-column label="使用场景" prop="type">
        <template slot-scope="{row}">
          {{ JSON.parse(row.type).desc }}
        </template>
      </el-table-column>
      <el-table-column label="已领取数量" prop="distributeQty" />
      <el-table-column label="有效期限(天)" prop="dueDays">
        <template slot-scope="{row}">
          领取后{{ row.dueDays }}天内有效
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status">
        <template slot-scope="{row}">
          <el-tag :type="JSON.parse(row.status).value === 'on' ? 'success' : 'info'">{{ JSON.parse(row.status).desc }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="right" label="操作" width="150">
        <template slot-scope="{row}">
          <el-button v-hasPermi="['business:coupon:query']" type="text" @click="$router.push('/coupon/view?id=' + row.couponId)">查看</el-button>
          <el-button v-if="JSON.parse(row.status).value === 'init'" v-hasPermi="['business:coupon:edit']" type="text" @click="$router.push('/coupon/edit?id=' + row.couponId)">编辑</el-button>
          <el-button v-if="JSON.parse(row.status).value === 'off' || JSON.parse(row.status).value === 'init'" v-hasPermi="['business:coupon:on']" type="text" @click="setStatus(row, 'on')">上架</el-button>
          <el-button v-if="JSON.parse(row.status).value === 'on'" v-hasPermi="['business:coupon:off']" type="text" @click="setStatus(row, 'off')">下架</el-button>
          <el-button v-if="JSON.parse(row.status).value === 'init'" v-hasPermi="['business:coupon:remove']" class="del" type="text" @click="del(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList()" />
  </div>
</template>
<script>
import { getCouponList, delCoupon, setCouponStatus } from '@/api/business/coupon'
import { accDiv } from '@/utils/util'
export default {
  name: 'CouponList',
  data() {
    return {
      loading: false,
      showSearch: true,
      tableData: [],
      queryParams: {},
      total: 0
    }
  },
  created() {
    this.getList(true)
  },
  methods: {
    getList(reset) {
      if (this.loading) {
        return
      }
      this.loading = true
      if (reset) {
        this.queryParams = { pageNum: 1, pageSize: 20 }
      }
      getCouponList('pageNum='+this.queryParams.pageNum + '&pageSize='+this.queryParams.pageSize+'&', this.queryParams).then(res => {
        this.loading = false
        if (res.code === 0) {
          this.tableData = res.rows
          this.total = res.total
        }
      }).catch(() => {
        this.loading = false
      })
    },
    setStatus(item, status) {
      this.$confirm(`确认${status === 'on' ? '上架' : '下架'}券 “${item.title}” 吗？`, `${status === 'on' ? '上架' : '下架'}券`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        setCouponStatus({
          couponId: item.couponId,
          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.couponId).then(res => {
          if (res.code === 0) {
            this.$message.success('操作已完成！')
            this.getList()
          }
        })
      })
    }
  }
}
</script>
