<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-select
        v-model="listQuery.status" clearable size="mini" class="filter-item"
        style="width: 200px;" placeholder="请选择库存状态"
      >
        <el-option label="全部" value="" />
        <el-option label="未付款" :value="0" />
        <el-option label="取消" :value="1" />
        <el-option label="已付款" :value="2" />
        <el-option label="支付失败" :value="3" />
      </el-select>
      <el-button
        size="mini" class="filter-item" type="primary" icon="el-icon-search"
        style="margin-left:10px;"
        @click="handleSearch"
      >
        查找
      </el-button>
    </div>

    <!-- 查询结果 -->
    <div v-tableHeight>
      <el-table
        v-loading="listLoading" height="100%" element-loading-text="正在查询中。。。" :data="list"
        v-bind="{ stripe: true, size: 'small', border: true, fit: true, highlightCurrentRow: true }"
      >
        <el-table-column align="center" width="100" label="ID" prop="id" fixed="left" />
        <el-table-column align="center" min-width="150" label="充值订单编号" prop="orderFormid" show-overflow-tooltip />
        <el-table-column align="center" width="100" label="购买者ID" prop="shopId" show-overflow-tooltip />
        <el-table-column align="center" width="100" label="购买的代金券ID" prop="voucherId" show-overflow-tooltip />
        <el-table-column align="center" width="100" label="购买数量" prop="number" show-overflow-tooltip />
        <el-table-column align="center" label="状态" prop="status">
          <template slot-scope="{ row }">
            <el-tag v-if="row.status === 0" effect="plain" type="info">未付款</el-tag>
            <el-tag v-else-if="row.status === 1" effect="plain" type="success">取消</el-tag>
            <el-tag v-else-if="row.status === 2" effect="plain" type="danger">已付款</el-tag>
            <el-tag v-else-if="row.status === 3" effect="plain" type="danger">代金券未发放</el-tag>
            <el-tag v-else-if="row.status === 4" effect="plain" type="danger">代金券已发放</el-tag>
            <el-tag v-else-if="row.status === 5" effect="plain" type="danger">支付失败</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" label="消费金额" prop="payGrade" show-overflow-tooltip />
        <el-table-column align="center" min-width="150" label="第三方支付ID" prop="trxId" show-overflow-tooltip />
        <el-table-column align="center" width="150" label="支付时间" prop="payTime" />
        <el-table-column align="center" width="150" label="创建时间" prop="createTime" />
        <el-table-column align="center" label="操作" width="300" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{ row }">
            <el-button type="warning" size="mini" @click="handleDetail(row)">
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div>
      <el-pagination
        :current-page="listQuery.page" :page-sizes="[10, 20, 50, 100]" :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="(val) => ((listQuery.pageSize = val) && getList())"
        @current-change="(val) => ((listQuery.page = val) && getList())"
      />
    </div>

    <!-- 查看详情 -->
    <DetailModal ref="DetailModal" />
  </div>
</template>

<script>
import DetailModal from './components/DetailModal'
import { getAllShopVoucherEntryRecord } from '@/api/voucherManagement/voucherOrder'

export default {
  name: 'VoucherOrder',
  components: {
    DetailModal
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        status: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await getAllShopVoucherEntryRecord(this.listQuery)
        this.list = res.data.list
        this.total = res.data.total
      } finally {
        this.listLoading = false
      }
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDetail(row) {
      this.$refs.DetailModal && this.$refs.DetailModal.handleOpen(row)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
	padding: 20px;
	display: flex;
	flex-direction: column;

	.filter-container {
		.filter-item {
			display: inline-block;
			vertical-align: middle;
			margin-bottom: 10px;
		}
	}

	.small-padding {
		.cell {
			padding-left: 5px;
			padding-right: 5px;
		}
	}

	.fixed-width {
		.el-button--mini {
			padding: 7px 10px;
		}
	}
}
</style>
