<template>
    <div class="app-container">
        <div class="head-container">
            <el-button type="primary" icon="el-icon-plus" @click="$refs.addStaff.show()">
                添加员工
            </el-button>
        </div>
        <div class="head-container">
            <el-table :data="tableData.list" v-loading="tableData.loading">
                <el-table-column label="员工名称" prop="name"></el-table-column>
                <el-table-column label="员工编号" prop="code"></el-table-column>
                <el-table-column label="员工账号" prop="account"></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"
                            @change="statusChange($event, scope.row)"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="120">
                    <template v-slot="scope">
                        <div class="btn_rows" style="display: flex;gap: 10px;">
                            <el-button type="text" icon="el-icon-edit"
                                @click="$refs.addStaff.show(scope.row)">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="delTableHandle([scope.row.id])">
                                <el-button type="text" icon="el-icon-delete" slot="reference">删除</el-button>
                            </el-popconfirm>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="head-container">
            <el-pagination @size-change="paginationSizeChange" :total="tableData.total"
                :current-page="tableData.page + 1" :page-size="tableData.size" @current-change="paginationChange"
                layout="total, sizes, prev, pager, next, jumper"></el-pagination>
        </div>
        <addStaff ref="addStaff" @success="resetHandle" />
    </div>
</template>

<script>
import addStaff from './components/addStaff.vue'
import { tbPlussShopStaffGet, updateStatus, shopStaffDelete } from '@/api/shop.js'
export default {
    components: {
        addStaff
    },
    data() {
        return {
            query: {},
            tableData: {
                page: 0,
                size: 30,
                total: 0,
                loading: false,
                list: []
            }
        }
    },
    mounted() {
        this.getTableData()
    },
    methods: {
        // 删除
        async delTableHandle(ids) {
            try {
                const res = await shopStaffDelete(ids)
                this.$notify({
                    title: '成功',
                    message: `已删除`,
                    type: 'success'
                });
                this.getTableData()
            } catch (error) {
                console.log(error);
            }
        },
        // 修改员工状态
        async statusChange(e, row) {
            try {
                const res = await updateStatus({
                    id: row.id,
                    status: e
                })
                this.getTableData()
            } catch (error) {
                console.log(error);
            }
        },
        // 重置查询
        resetHandle() {
            this.tableData.page = 0
            this.getTableData()
        },
        // 每页条数改变是回调
        paginationSizeChange(e) {
            this.tableData.size = e
            this.tableData.page = 0
            this.getTableData()
        },
        // 分页回调
        paginationChange(e) {
            this.tableData.page = e - 1
            this.getTableData()
        },
        // 获取员工列表
        async getTableData() {
            try {
                this.tableData.loading = true
                const res = await tbPlussShopStaffGet({
                    page: this.tableData.page,
                    size: this.tableData.size
                })
                this.tableData.loading = false
                this.tableData.list = res.content
                this.tableData.total = res.totalElements
            } catch (error) {
                console.log(error)
            }
        }
    }
}
</script>