<template>
    <div class="app-container">
        <div class="head-container">
            <el-row :gutter="20">
                <el-col :span="6">
                    <el-input v-model="query.name" size="small" clearable placeholder="请输入名称或描述" style="width: 100%;"
                        class="filter-item" @keyup.enter.native="getTableData" />
                </el-col>
                <el-col :span="6">
                    <el-select v-model="query.type" placeholder="请选择资源类型">
                        <el-option v-for="item in typeList" :key="item.key" :label="item.name"
                            :value="item.key"></el-option>
                    </el-select>
                </el-col>
                <el-col :span="6">
                    <el-button type="primary" @click="getTableData">查询</el-button>
                    <el-button @click="resetHandle">重置</el-button>
                </el-col>
            </el-row>
        </div>
        <div class="head-container">
            <el-button type="primary" icon="el-icon-plus" @click="$refs.addRef.show()">
                添加资源
            </el-button>
            <add ref="addRef" @success="getTableData" />
        </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="type">
                    <template v-slot="scope">
                        {{ scope.row.type }}
                    </template>
                </el-table-column>
                <el-table-column label="跳转类型" prop="jumpType">
                    <template v-slot="scope">
                        {{ scope.row.jumpType | jumpFilter }}
                    </template>
                </el-table-column>
                <el-table-column label="展示图" prop="coverImg">
                    <template v-slot="scope">
                        <el-image :src="scope.row.coverImg" style="width: 40px;height: 40px;" />
                    </template>
                </el-table-column>
                <el-table-column label="标签前小图标" prop="shareImg">
                    <template v-slot="scope">
                        <el-image :src="scope.row.shareImg" style="width: 40px;height: 40px;" />
                    </template>
                </el-table-column>
                <el-table-column label="背景色" prop="backColor">
                    <template v-slot="scope">
                        <div class="color_wrap">
                            <div class="dot" :style="{ backgroundColor: scope.row.backColor }"></div>
                            <span>{{ scope.row.backColor }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="字体颜色" prop="fontColor">
                    <template v-slot="scope">
                        <div class="color_wrap">
                            <div class="dot" :style="{ backgroundColor: scope.row.fontColor }"></div>
                            <span>{{ scope.row.fontColor }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="相对跳转地址" prop="relUrl"></el-table-column>
                <el-table-column label="绝对跳转地址" prop="absUrl"></el-table-column>
                <el-table-column label="APP端展示" prop="isShowApp">
                    <template v-slot="scope">
                        <el-switch v-model="scope.row.isShowApp" :active-value="1" :inactive-value="0"
                            @change="changeHot($event, scope.row)"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="收银端展示" prop="isShowCash">
                    <template v-slot="scope">
                        <el-switch v-model="scope.row.isShowCash" :active-value="1" :inactive-value="0"
                            @change="changeHot($event, scope.row)"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="小程序端展示" prop="isShowMall">
                    <template v-slot="scope">
                        <el-switch v-model="scope.row.isShowMall" :active-value="1" :inactive-value="0"
                            @change="changeHot($event, scope.row)"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200">
                    <template v-slot="scope">
                        <el-button type="text" size="mini" round icon="el-icon-edit"
                            @click="$refs.addRef.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 enumData from './enumData.js'
import add from "./components/add";
import { tbPlatformDictGet, tbPlatformDictPostPut, tbPlatformDictType } from "@/api/setting";
export default {
    components: {
        add
    },
    data() {
        return {
            enumData,
            typeList: [],
            query: {
                name: "",
                type: ''
            },
            tableData: {
                page: 0,
                size: 10,
                total: 0,
                loading: false,
                list: []
            }
        };
    },
    filters: {
        jumpFilter(t) {
            return t ? enumData.jumpTypeList.find(item => item.value == t).label : ''
        }
    },
    mounted() {
        this.tbPlatformDictType()
    },
    methods: {
        typeFilter(t) {
            let e = t ? this.typeList.find(item => item.value == t).label : t
            return e ? e : t
        },
        // 获取资源类别
        async tbPlatformDictType() {
            try {
                const res = await tbPlatformDictType()
                this.typeList = res.content
                this.getTableData()
            } catch (error) {
                console.log(error);
            }
        },
        // 切换状态
        async changeHot(e, row) {
            try {
                this.tableData.loading = true
                await tbPlatformDictPostPut({ ...row }, 'put')
                this.getTableData()
            } catch (error) {
                console.log(error);
            }
        },
        // 查询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 tbPlatformDictPostPut(ids, "delete");
                this.$notify({
                    title: "成功",
                    message: `删除成功`,
                    type: "success"
                });
                this.getTableData();
            } catch (error) {
                console.log(error);
            }
        },
        // 获取商品列表
        async getTableData() {
            this.tableData.loading = true;
            try {
                const res = await tbPlatformDictGet(
                    {
                        page: this.tableData.page,
                        size: this.tableData.size,
                        ...this.query,
                        sort: 'id'
                    }
                );
                this.tableData.loading = false;
                this.tableData.list = res.content;
                this.tableData.total = res.totalElements;
            } catch (error) {
                console.log(error);
            }
        }
    }
};
</script>

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

    .dot {
        width: 14px;
        height: 14px;
        border-radius: 50%;
        margin-right: 8px;
    }
}
</style>