<template>
    <div class="box">
        <div class="inquire">
            <span>TabID:</span>
            <el-input v-model="formData.id" placeholder="" class="input" />
        </div>
        <div class="inquire">
            <span>类型</span>
            <el-select v-model="formData.useVip" placeholder="请选择">
                <el-option label="全部" :value="-1"></el-option>
                <el-option label="普通" :value="0"></el-option>
                <el-option label="贵族" :value="1"></el-option>
            </el-select>
        </div>
        <div class="inquire">
            <span>状态</span>
            <el-select v-model="formData.status" placeholder="请选择">
                <el-option label="全部" :value="-1"></el-option>
                <el-option label="不展示" :value="0"></el-option>
                <el-option label="展示" :value="1"></el-option>
            </el-select>
        </div>
        <el-button style="" type="primary" @click="getData()">查询</el-button>
        <el-button style="" type="primary" @click="resetFormData()">重置</el-button>
        <el-button style="" type="primary" @click="addDialog = true; resetaddFormData()">新增</el-button>
        <!-- 表格数据 -->
        <el-table v-loading="tableData.loading" :data="tableData.data" ref="multipleTable"
            @selection-change="handleSelectionChange" border style="width: 100%; margin-top: 25px">
            <el-table-column prop="id" align="center" label="ID" />
            <el-table-column prop="tabName" align="center" label="Tab名字" />
            <el-table-column prop="tabUrl" align="center" label="Tab封面">
                <template v-slot="scope">
                    <el-image style="width: 100px; height: 100px" :src="scope.row.tabUrl" :zoom-rate="1.1"
                        :preview-src-list="[scope.row.tabUrl]" fit="scale-down" preview-teleported="true"
                        hide-on-click-modal="true" />
                </template>
            </el-table-column>
            <el-table-column prop="useVip" align="center" label="表情类型">
                <template v-slot="scope">
                    {{ scope.row.useVip == 1 ? '贵族' : '普通' }}
                </template>
            </el-table-column>
            <el-table-column prop="status" align="center" label="状态">
                <template v-slot="scope">
                    {{ scope.row.status == 1 ? '展示' : '不展示' }}
                </template>
            </el-table-column>
            <el-table-column prop="tabSeq" align="center" label="排序" />
            <el-table-column align="center" label="操作" width="300">
                <template v-slot="scope">
                    <el-button class="primary" type="primary" @click="
                        detailPageFun(scope.row);
                    " size="default">编辑</el-button>
                    <!-- <el-button class="primary" type="danger" @click="
                        deletePageFun(scope.row);
                    " size="default">删除</el-button> -->
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination style="margin-top: 10px" class="paginationClass" :current-page="formData.pageNo"
            :page-size="formData.pageSize" :page-sizes="[10, 20, 50, 100, 200]" layout="sizes, prev, pager, next"
            :total="tableData.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        <el-dialog v-model="addDialog" :title="addFormData.id == '' ? '新增' : '编辑'" width="28%" center>
            <!-- <div style="margin-bottom: 25px" v-if="addFormData.id != ''">
                <span style="display: inline-block; margin-right: 20px; width: 100px"
                    class="col-sm-2 control-label">表情TabID</span>
                <el-input v-model="addFormData.id" style="width: 50%" class="input" disabled></el-input>
            </div> -->
            <div style="margin-bottom: 25px">
                <span style="display: inline-block; margin-right: 20px; width: 100px"
                    class="col-sm-2 control-label">表情Tab名字</span>
                <el-input v-model="addFormData.tabName" style="width: 50%" class="input"></el-input>
            </div>
            <div style="margin-bottom: 25px">
                <span style="display: inline-block; margin-right: 20px; width: 100px"
                    class="col-sm-2 control-label">表情类型</span>
                <el-select v-model="addFormData.useVip" placeholder="请选择">
                    <el-option label="普通" :value="0"></el-option>
                    <el-option label="贵族" :value="1"></el-option>
                </el-select>
            </div>
            <div class="selectBox selectBoxImg">
                <span class="left" style="margin-right: 75px;padding-left: 15px;">Tab图</span>
                <!-- action="/admin/tencent/cos/upload/file" -->
                <el-upload class="avatar-uploader" action="/admin/tencent/cos/upload/file" :show-file-list="false"
                    :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload" :on-error="handleAvatarError">
                    <img v-if="addFormData.tabUrl" :src="addFormData.tabUrl" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon">
                        <Plus />
                    </el-icon>
                </el-upload>
            </div>
            <div style="margin-bottom: 25px">
                <span style="display: inline-block; margin-right: 20px; width: 100px"
                    class="col-sm-2 control-label">排序</span>
                <el-input v-model="addFormData.tabSeq" style="width: 50%" class="input"></el-input>
            </div>
            <div style="margin-bottom: 25px">
                <span style="display: inline-block; margin-right: 20px; width: 100px"
                    class="col-sm-2 control-label">状态</span>
                <el-select v-model="addFormData.status" placeholder="请选择">
                    <el-option label="不展示" :value="0"></el-option>
                    <el-option label="展示" :value="1"></el-option>
                </el-select>
            </div>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="addDialog = false">取消</el-button>
                    <el-button type="primary" @click="addFun()"> 确认 </el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { ref, onMounted, reactive, computed } from 'vue'
import { ElMessage, ElMessageBox } from "element-plus";
import { getFaceTabNewList, saveOrUpdateFaceTabNew, deleteFaceTabNew } from '@/api/EmojiManagement/EmojiManagement'
export default {
    name: 'EmojiTabManagement',
    setup() {
        const formData = reactive({
            pageNo: 1,
            pageSize: 10,
            useVip: -1,
            status: -1,
            id: ''
        })
        const addFormData = reactive({
            id: '',
            useVip: 0,
            tabUrl: '',
            status: 1,
            tabSeq: '',
            tabName: ''
        })
        const tableData = reactive({
            data: [],
            total: 0,
            loading: false,
        })
        const addDialog = ref(false)
        const getData = () => {
            tableData.loading = true;
            getFaceTabNewList(formData).then(res => {
                if (res.code == 200) {
                    tableData.data = res.data.records
                    tableData.loading = false;
                    tableData.total = res.data.total

                } else {
                    tableData.loading = false;
                    ElMessage.error(res.message);
                }
            })
        };
        // 增加
        const addFun = () => {
            saveOrUpdateFaceTabNew(addFormData).then(res => {
                if (res.code == 200) {
                    ElMessage.success('添加成功');
                    Object.assign(addFormData, {
                        id: '',
                        useVip: 0,
                        tabUrl: '',
                        status: 1,
                        tabSeq: '',
                        tabName: ''
                    });
                    addDialog.value = false
                    getData()
                } else {
                    ElMessage.error(res.message);
                    addDialog.value = false
                }
            })
        }
        // 编辑
        const detailPageFun = (row) => {
            addFormData.id = row.id;
            addFormData.useVip = row.useVip;
            addFormData.tabUrl = row.tabUrl;
            addFormData.status = row.status;
            addFormData.tabSeq = row.tabSeq;
            addFormData.tabName = row.tabName || '';
            addDialog.value = true
        }
        // 删除
        const deletePageFun = (row) => {
            ElMessageBox.confirm('确定要删除吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteFaceTabNew({ id: row.id }).then(res => {
                    if (res.code == 200) {
                        ElMessage.success('删除成功')
                        getData()
                    } else {
                        ElMessage.error(res.message)
                    }
                })
            })
        }
        //重置
        const resetFormData = () => {
            Object.assign(formData, {
                pageNo: 1,
                pageSize: 10,
                useVip: -1,
                status: -1,
                id: ''
            });

        };
        const resetaddFormData = () => {
            Object.assign(addFormData, {
                id: '',
                useVip: 0,
                tabUrl: '',
                status: 1,
                tabSeq: '',
                tabName: ''
            });

        };
        const beforeAvatarUpload = () => {
            ElMessage({
                showClose: true,
                message: "上传中~",
                type: "warning",
            });
        };
        const handleAvatarError = () => {
            ElMessage({
                showClose: true,
                message: "上传失败！",
                type: "error",
            });
        };
        const handleAvatarSuccess = (res, file) => {
            addFormData.tabUrl = file.response.data;
            ElMessage({
                showClose: true,
                message: "上传成功！",
                type: "success",
            });
        };
        const handleSizeChange = (val) => {
            formData.pageSize = val;
            getData();
        };
        const handleCurrentChange = (val) => {
            formData.pageNo = val;
            getData();
        };
        onMounted(() => {
            getData();

        })
        return {
            formData,
            addFormData,
            tableData,
            addDialog,
            getData,
            addFun,
            resetFormData,
            handleSizeChange,
            handleCurrentChange,
            beforeAvatarUpload,
            handleAvatarError,
            handleAvatarSuccess,
            detailPageFun,
            deletePageFun,
            resetaddFormData
        }
    },
}
</script>

<style lang="less" scoped>
.box {
    padding-top: 20px;
    background: #ecf0f5;

    .inquire {
        display: inline-block;
        margin-right: 20px;

        span {
            margin-right: 10px;
        }

        .input {
            width: 180px;
            margin-right: 10px;
        }
    }

    .dialogTableVisibleBut {
        display: block;
        margin: 30px 0 0 830px;
    }

    .paginationClass {
        margin: 15px 0 5px 0px;
    }
}

.selectBox {
    display: flex;
    height: 35px;
    line-height: 35px;
    margin-bottom: 20px;
}

.selectBoxImg {
    height: 150px;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>