<template>
    <div class="common-container common-container">
        <TitleBar></TitleBar>
        <div class="main-container">
            <div class="search-container TableSelectMain">
                <el-form class="table-select-main" :model="searchForm" label-position="left">
                    <el-row :gutter="15">
                        <el-col :span="4">
                            <el-form-item>
                                <el-select placeholder="请选择卡券类型" v-model="searchForm.type">
                                    <el-option label="折扣卷" :value="1"></el-option>
                                    <el-option label="红包" :value="2"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-button type="primary" round @click="querySearch">查询</el-button>
                                <el-button type="primary" plain round @click="reset">重置</el-button>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="24">
                            <el-form-item>
                                <el-button
                                    type="primary"
                                    round
                                    icon="el-icon-plus"
                                    @click="showAddEditDialog(null)"
                                >新建</el-button>
                                <el-button type="primary" plain round @click="openBatch()">批量启用</el-button>
                                <el-button type="primary" plain round @click="deleteData(null)">批量删除</el-button>
                            </el-form-item>
                        </el-col> -->
                    </el-row>
                </el-form>
            </div>

            <div class="SearchTitle">
                已选择
                <span class="subject-color">4</span> 项
            </div>

            <div class="MaintenanceTable">
                <el-table
                    ref="multipleTable"
                    :data="tableData"
                    style="width: 100%"
                    tooltip-effect="dark"
                    @selection-change="selectColumn"
                    class="OutTable"
                    border
                    v-loading="listLoading"
                >
                    <el-table-column type="selection"></el-table-column>
                    <el-table-column prop="name" label="名称"></el-table-column>
                    <el-table-column prop="type" label="类型">
                        <template slot-scope="scope">
                            <span>{{scope.row.type | couponTypePipe}}</span>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column prop="scenario" label="使用范围">
                        <template slot-scope="scope">
                            <span>{{scope.row.scenario | couponUsePipe}}</span>
                        </template>
                    </el-table-column>-->
                    <el-table-column label="折扣/面值">
                        <template slot-scope="scope">
                            <span v-if="scope.row.type ===1">{{scope.row.discount }}</span>
                            <span v-if="scope.row.type ===2">{{scope.row.price }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="满减" prop="fullReduction"></el-table-column>
                    <el-table-column prop="validity" label="截止时间">
                        <template slot-scope="scope">
                            <span>{{scope.row.validity | formatTimePipe }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="checkFalg" label="审核状态">
                        <template slot-scope="scope">
                            <span class="success" v-show="scope.row.checkFalg == 1">通过</span>
                            <span class="warn" v-show="scope.row.checkFalg == 2">待审核</span>
                            <span class="danger" v-show="scope.row.checkFalg == 0">驳回</span>
                            <span class="warn" v-show="scope.row.checkFalg == 3">等待财务审核</span>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column prop="statuz" label="启用状态" min-width="110px;">
                        <template slot-scope="scope">
                            <el-switch
                                v-model="scope.row.statuz"
                                active-text="开"
                                inactive-text="关"
                                :active-value="1"
                                :inactive-value="0"
                                @change="changeSwitch(scope.row)"
                            ></el-switch>
                        </template>
                    </el-table-column>-->
                    <el-table-column label="操作" min-width="180px;">
                        <template slot-scope="scope">
                            <!-- <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="deleteData(scope.row)"
                            >删除</el-button> -->
                            <!-- <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="showAddEditDialog(scope.row)"
                            >编辑</el-button> -->
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="audit(scope.row,1)"
                            >通过</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                :disabled="scope.row.checkFalg === 0"
                                @click="audit(scope.row,0)"
                            >驳回</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagenation">
                    <PageComponent
                        :total="total"
                        @pagination="handlePagination"
                        :pageSize.sync="page.pageSize"
                        :pageNum.sync="page.pageNum"
                    ></PageComponent>
                </div>
            </div>
        </div>
        <!-- 添加与编辑弹层 -->
        <AddEditDialog ref="addEditDialog" @queryListData="queryListData"></AddEditDialog>
        <RejectionDialog ref="rejectionDialog" @queryListData="queryListData"></RejectionDialog>
    </div>
</template>
<script>
import TitleBar from '@/apps/mobile/components/common/TitleBar';
import AddEditDialog from '../components/addEditDialog';
import RejectionDialog from '../components/rejectionDialog';
import {
    queryCouponsList,
    updateCouponsStatus,
    deleteCouponSingle,
    deleteCouponBatch,
    openCouponBatch,
    couponsExamine,
} from '@/apps/mobile/api/cardbank/index';
export default {
    components: {
        TitleBar,
        AddEditDialog,
        RejectionDialog,
    },
    data() {
        return {
            searchForm: {},
            tableData: [],
            page: {
                pageNum: 1,
                pageSize: 5,
            },
            total: 10,
            listLoading: false,
            tableSelection: [],
        };
    },
    created() {
        this.queryListData();
    },
    methods: {
        queryListData() {
            this.listLoading = true;
            let obj = {
                ...this.page,
                ...this.searchForm,
                checkFalg: 2,
                sort: {
                    order: 'desc',
                    orderBy: 'create_time',
                },
            };
            queryCouponsList(obj)
                .then(res => {
                    console.log(res);
                    if(res.statusCode == "10"){
                        this.$router.replace("/login");
                    }
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                    this.listLoading = false;
                })
                .catch();
        },
        changeSwitch(data) {
            console.log(data);
            let obj = {
                statuz: data.statuz ? 1 : 0,
                id: data.id,
            };
            console.log(obj);
            updateCouponsStatus(obj)
                .then(res => {
                    if (res.statusCode === '1') {
                        this.$message.success(res.message);
                    } else {
                        this.$message.warning(res.message);
                    }
                    this.queryListData();
                })
                .catch();
        },
        deleteData(data) {
            if (data) {
                this.$confirm('确定要删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(() => {
                        deleteCouponSingle(data.id)
                            .then(res => {
                                console.log(res);
                                if (res.statusCode === '1') {
                                    this.$message.success(res.message);
                                } else {
                                    this.$message.warning(res.message);
                                }
                                this.queryListData();
                            })
                            .catch();
                    })
                    .catch(() => {});
            } else {
                if (!this.tableSelection.length) {
                    this.$message({
                        type: 'warning',
                        message: '请先选择一项！',
                    });
                    return false;
                }
                this.$confirm('确定要删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    closeOnClickModal: false,
                })
                    .then(() => {
                        let ids = this.tableSelection.map(item => {
                            return item.id;
                        });
                        console.log(ids);
                        deleteCouponBatch(ids)
                            .then(res => {
                                if (res.statusCode === '1') {
                                    this.$message.success(res.message);
                                } else {
                                    this.$message.warning(res.message);
                                }
                                this.queryListData();
                            })
                            .catch();
                    })
                    .catch(() => {});
            }
        },
        openBatch() {
            if (!this.tableSelection.length) {
                this.$message({
                    type: 'warning',
                    message: '请先选择一项！',
                });
                return false;
            }
            this.$confirm('确定要启用吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                closeOnClickModal: false,
            })
                .then(() => {
                    let ids = this.tableSelection.map(item => {
                        return item.id;
                    });
                    console.log(ids);
                    openCouponBatch(ids)
                        .then(res => {
                            if (res.statusCode === '1') {
                                this.$message.success(res.message);
                            } else {
                                this.$message.warning(res.message);
                            }
                            this.queryListData();
                        })
                        .catch();
                })
                .catch(() => {});
        },
        audit(data, status) {
            console.log(data);
            if (status) {
                this.$confirm('确定要审核通过吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(() => {
                        let obj = {
                            checkFalg: status,
                            id: data.id,
                        };
                        console.log(obj);
                        couponsExamine(obj)
                            .then(res => {
                                if (res.statusCode === '1') {
                                    this.$message.success(res.message);
                                } else {
                                    this.$message.warning(res.message);
                                }
                                this.queryListData();
                            })
                            .catch();
                    })
                    .catch(() => {});
            } else {
                this.$refs.rejectionDialog.open(data);
            }
        },
        querySearch() {
            this.page.pageNum = 1;
            this.queryListData();
        },
        reset() {
            this.page.pageNum = 1;
            this.searchForm = {};
            this.queryListData();
        },
        selectColumn(selection) {
            this.tableSelection = selection;
        },
        // 页码变化时
        handlePagination(val) {
            this.page = val;
            this.queryListData();
        },
        showAddEditDialog(data) {
            if (data) {
                this.$refs.addEditDialog.open('edit', data);
            } else {
                this.$refs.addEditDialog.open('add');
            }
        },
    },
};
</script>
<style lang="stylus" scoped>
.TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
    width: 95%;
}
</style>
