<template>
    <el-dialog title="选择团购券分类" :visible.sync="dialogVisible" @open="resetHandle()">
        <el-form :model="searhForm" inline>
            <el-form-item>
                <el-input v-model="searhForm.name" placeholder="分类名称"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getTableData">查询</el-button>
                <el-button @click="resetHandle">重置</el-button>
            </el-form-item>
        </el-form>
        <div class="head-container">
            <el-table ref="table" :data="tableData.list" v-loading="tableData.loading">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column label="名称" prop="name"></el-table-column>
                <el-table-column label="状态" prop="status">
                    <template v-slot="scope">
                        <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0"
                            disabled></el-switch>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-pagination :total="tableData.total" :current-page="tableData.page + 1" :page-size="tableData.size"
            @current-change="paginationChange" @size-change="sizeChange"
            layout="total, sizes, prev, pager, next, jumper"></el-pagination>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirmHandle">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { tbCouponCategoryGet } from "@/api/setting";
export default {
    data() {
        return {
            dialogVisible: false,
            searhForm: {
                name: ''
            },
            tableData: {
                page: 0,
                size: 10,
                total: 0,
                loading: false,
                list: []
            },
            goods: []
        }
    },
    methods: {
        // 确定选商品
        confirmHandle(row) {
            let res = this.$refs.table.selection
            this.$emit('success', res)
            this.close()
        },
        // 重置查询
        resetHandle() {
            this.searhForm.name = ''
            this.tableData.page = 0
            this.tableData.size = 10
            this.tableData.list = []
            this.getTableData()
        },
        // 分页大小改变
        sizeChange(e) {
            this.tableData.size = e
            this.getTableData()
        },
        // 分页回调
        paginationChange(e) {
            this.tableData.page = e - 1
            this.getTableData()
        },
        // 商品列表
        async getTableData() {
            this.tableData.loading = true
            try {
                const res = await tbCouponCategoryGet({
                    page: this.tableData.page,
                    size: this.tableData.size,
                    name: this.searhForm.name,
                    sort: 'id',
                })
                this.tableData.list = res.content
                this.tableData.total = res.totalElements

                if (this.goods.length) {
                    this.$nextTick(() => {
                        this.selection()
                    })
                }
                setTimeout(() => {
                    this.tableData.loading = false
                }, 500);
            } catch (error) {
                console.log(error)
            }
        },
        show(goods) {
            this.dialogVisible = true
            if (goods && goods.length) {
                this.goods = [...goods]
            } else {
                this.goods = []
            }
            this.resetHandle()
            this.getTableData()
        },
        close() {
            this.dialogVisible = false
        },
        selection() {
            this.goods.forEach(row => {
                this.tableData.list.forEach((item, index) => {
                    if (row.id == item.id) {
                        this.$refs.table.toggleRowSelection(this.tableData.list[index]);
                    }
                })
            });
        }
    }
}
</script>

<style scoped lang="scss">
.shop_info {
    display: flex;
    align-items: center;

    span {
        margin-left: 10px;
    }
}
</style>