<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.id" clearable size="mini"
        class="filter-item" style="width: 200px;" placeholder="请输入代金券ID"
      />
      <el-input
        v-model="listQuery.orderSn" clearable size="mini" class="filter-item"
        style="width: 200px;" placeholder="请输入订单编号"
      />
      <el-select v-model="listQuery.status" size="mini" class="filter-item" style="width: 200px;" placeholder="请选择状态">
        <el-option :value="0" label="未付款" />
        <el-option :value="1" label="取消" />
        <el-option :value="2" label="已付款" />
        <el-option :value="3" label="代金券未发放" />
        <el-option :value="4" label="代金券已发放" />
        <el-option :value="5" label="支付失败" />
      </el-select>
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查找
      </el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading" :data="listData" size="small" element-loading-text="正在查询中。。。"
      border fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" prop="id" sortable />
      <el-table-column align="center" label="订单编号" prop="orderSn" />
      <el-table-column align="center" label="购买者id" prop="shopId" />
      <el-table-column align="center" label="购买数量" prop="number" />
      <el-table-column align="center" label="状态" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0">未付款</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="info">取消</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="success">已付款</el-tag>
          <el-tag v-else-if="scope.row.status === 3" type="danger">代金券未发放</el-tag>
          <el-tag v-else-if="scope.row.status === 4" type="success">代金券已发放</el-tag>
          <el-tag v-else-if="scope.row.status === 5" type="warning">支付失败</el-tag>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="微信订单编号" prop="payId" />
      <el-table-column align="center" label="消费金额" prop="payGrade" />
      <el-table-column align="center" label="支付时间" prop="payTime" />
      <el-table-column align="center" label="创建时间" prop="createTime" />
      <el-table-column align="center" label="支付ID" prop="trxId" />
      <el-table-column align="center" fixed="right" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button :disabled="scope.row.status !== 3" type="primary" size="mini" @click="handleGrant(scope.row)">
            发放
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <Pagination
      v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { getAllVoucherOrder } from '@/api/voucherManagement/rechargeRecord'
import Pagination from '@/components/Pagination'

export default {
  name: 'RechargeRecord',
  components: { Pagination },
  data() {
    return {
      listLoading: true,
      listData: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 20,
        id: '',
        orderSn: '',
        status: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      getAllVoucherOrder(this.listQuery)
        .then((res) => {
          this.listData = res.data.list || []
          this.total = res.data.total || 0
          this.listLoading = false
        })
        .catch(() => {
          this.listData = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleGrant(row) {
      if (row.status === 3) {
        this.$confirm('进行代金券发放, 是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            grant({ id: row.id })
              .then((response) => {
                this.$message({ type: 'success', message: response.data })
              })
              .catch(() => {
                this.$message({ type: 'error', message: '代金券发放失败' })
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })
      }
    }
  }
}
</script>

<style scoped></style>
