<template>
    <div class="list-view">
        <page-title>
            <el-button @click="$router.go(-1)"  type="primary"  icon="el-icon-back">返回</el-button>
            
            
            <el-button
                @click="download('/userCoupon/excel')"
                icon="el-icon-upload2"
                :loading="downloading"
                :disabled="fetchingData"
                class="filter-item"
            >
                导出
            </el-button>
        </page-title>
        <div class="filters-container">
            <el-form :model="formData" :inline="true" @keyup.enter.native="initData">
                <el-form-item label="创建时间">
                    <created-at-picker v-model="formData.createdAt" @input="initData" name="创建"></created-at-picker>
                </el-form-item>
                 <el-form-item label="兑换券ID">
                    <el-input v-model.number="formData.couponId" placeholder="请输入兑换券ID" clearable ></el-input>
                </el-form-item>
                <el-form-item label="用户">
                    <user-search v-model="formData.userId"></user-search>
                </el-form-item>
                <el-form-item label="到期时间">
                    <created-at-picker v-model="expiration" @input="initData" name="到期"></created-at-picker>
                </el-form-item>
            </el-form>
            
            <el-input
                placeholder="搜索..."
                v-model="search"
                clearable
                class="filter-item search"
                @keyup.enter.native="initData"
            >
                <el-button @click="initData" slot="append" icon="el-icon-search"> </el-button>
            </el-input>
        </div>
        <el-table
            :data="tableData"
            row-key="id"
            ref="table"
            header-row-class-name="table-header-row"
            header-cell-class-name="table-header-cell"
            row-class-name="table-row"
            cell-class-name="table-cell"
            :height="tableHeight"
            v-loading="fetchingData"
        >
            <el-table-column v-if="multipleMode" align="center" type="selection" width="50"> </el-table-column>
            <el-table-column prop="id" label="ID" width="100"> </el-table-column>
            <el-table-column prop="userId" label="用户ID"> </el-table-column>
            <el-table-column prop="couponId" label="兑换卷ID"> </el-table-column>
            <el-table-column prop="name" label="名称" min-width="120" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="remark" label="备注" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="limited" label="指定藏品">
                <template v-slot="{ row }">
                    <el-tag v-if="row.limited" type="success">是</el-tag>
                    <el-tag v-else type="info">否</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="needGas" label="需要支付GAS">
                <template v-slot="{ row }">
                    <el-tag v-if="row.needGas" type="success">是</el-tag>
                    <el-tag v-else type="info">否</el-tag>
                </template>
            </el-table-column>
            
            <el-table-column prop="collectionIds" label="可用藏品ID"> </el-table-column>
            <el-table-column prop="createdAt" label="创建时间"  min-width="150" > </el-table-column>
            <el-table-column prop="expiration" label="到期时间"  min-width="150" > </el-table-column>
             <el-table-column prop="used" label="是否已使用">
                <template v-slot="{ row }">
                    <el-tag v-if="row.used" type="success">是</el-tag>
                    <el-tag v-else type="info">否</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="useTime" label="使用时间"  min-width="150" > </el-table-column>
        </el-table>
        <div class="pagination-wrapper">
            <el-pagination
                background
                @size-change="onSizeChange"
                @current-change="onCurrentChange"
                :current-page="page"
                :page-sizes="[10, 20, 30, 40, 50]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totalElements"
            >
            </el-pagination>
        </div>
    </div>
</template>
<script>
import pageableTable from '@/mixins/pageableTable';

export default {
    name: 'UserCouponList',
    mixins: [pageableTable],
    data() {
        return {
            multipleMode: false,
            search: '',
            url: '/userCoupon/all',
            downloading: false,
            formData: {
                couponId:this.$route.query.couponId,
                createdAt: '',
                userId: '',
                expiration:'',
            },
        };
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        beforeGetData() {
            return { search: this.search, query: { del: false, ...this.formData } };
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },

        deleteRow(row) {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/userCoupon/del/${row.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.getData();
                })
                .catch(e => {
                    this.$message.error(e.error);
                });
        }
    }
};
</script>
<style lang="less" scoped></style>
