<template>
    <div class="app-container">
        <div class="head-container">
            <el-button type="primary" icon="el-icon-plus" @click="$refs.addClassifyRef.show()">添加分类</el-button>
            <addClassify ref="addClassifyRef" @success="getTableData" />
        </div>
        <div class="head-container">
            <el-table :data="tableData.list" v-loading="tableData.loading" row-key="id"
                :tree-props="{ children: 'childrenList' }">
                <el-table-column label="排序" prop="sort" sortable width="100"></el-table-column>
                <el-table-column label="分类名称" prop="name"></el-table-column>
                <el-table-column label="分类图片">
                    <template v-slot="scope">
                        <el-image :src="scope.row.pic"
                            style="width: 50px;height: 50px;border-radius: 4px;background-color: #efefef;">
                            <div class="img_error" slot="error">
                                <i class="icon el-icon-document-delete"></i>
                            </div>
                        </el-image>
                    </template>
                </el-table-column>
                <el-table-column label="状态">
                    <template v-slot="scope">
                        <el-switch v-model="scope.row.isShow" :active-value="1" :inactive-value="0"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="颜色">
                    <template v-slot="scope">
                        <div
                            :style="{ width: '20px', height: '20px', borderRadius: '50%', backgroundColor: scope.row.style || '#efefef' }">
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="300">
                    <template v-slot="scope">
                        <el-button type="text" size="mini" round icon="el-icon-plus"
                            @click="$refs.addClassifyRef.show({ pid: scope.row.id })"
                            v-if="!scope.row.pid">添加子分类</el-button>
                        <el-button type="text" size="mini" round icon="el-icon-edit"
                            @click="$refs.addClassifyRef.show(scope.row)">编辑</el-button>
                        <el-popconfirm title="确定删除吗？" @confirm="delHandle([scope.row.id])">
                            <el-button type="text" size="mini" round icon="el-icon-delete" slot="reference">
                                删除
                            </el-button>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="head-container">
            <el-pagination :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>
    </div>
</template>

<script>
import addClassify from './components/addClassify'
import { tbShopCategoryGet, tbShopCategoryDelete } from '@/api/shop'
export default {
    components: {
        addClassify
    },
    data() {
        return {
            tableData: {
                page: 0,
                size: 10,
                total: 0,
                loading: false,
                list: []
            }
        }
    },
    mounted() {
        this.getTableData()
    },
    methods: {
        // 添加子分类
        addChildGatgory(row) {

        },
        // 查询table
        toQuery() {
            this.getTableData()
        },// 重置查询
        resetHandle() {
            this.tableData.page = 0;
            this.query.blurry = ''
            this.getTableData()
        },
        // 分页回调
        paginationChange(e) {
            this.tableData.page = e -1;
            this.getTableData()
        },
        // 删除
        async delHandle(ids) {
            try {
                await tbShopCategoryDelete(ids)
                this.$notify({
                    title: '成功',
                    message: `删除成功`,
                    type: 'success'
                });
                this.getTableData()
            } catch (error) {
                console.log(error)
            }
        },
        // 获取商品列表
        async getTableData() {
            this.tableData.loading = true
            try {
                const res = await tbShopCategoryGet({
                    page: this.tableData.page,
                    size: this.tableData.size,
                    sort: 'id,desc',
                    shopId: localStorage.getItem('shopId')
                })
                this.tableData.loading = false
                this.tableData.list = res.content
                this.tableData.total = res.totalElements
            } catch (error) {
                console.log(error)
            }
        }
    }
}
</script>