<template>
    <div class="app-container">
        <div class="page-container">
            <el-form :model="queryParams" :inline="true" label-width="90">
                <el-form-item label="话题名称">
                    <el-input v-model="queryParams.name" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="话题来源">
                    <el-select v-model="queryParams.source" placeholder="请选择">
                        <el-option label="轻享广场" value="轻享广场"></el-option>
                        <el-option label="后台管理" value="后台管理"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleQuery">查询</el-button>
                    <el-button @click="handleReset">重置</el-button>
                </el-form-item>
            </el-form>
            <el-row class="mb10">
                <el-col :span="2">
                    <el-button type="primary" @click="handleAdd">新增话题</el-button>
                </el-col>
            </el-row>
            <el-table :data="tableList">
                <el-table-column label="序号" width="55" type="index" align="center"></el-table-column>
                <el-table-column label="话题名称" prop="name" align="center"></el-table-column>
                <el-table-column label="来源" prop="source" align="center"></el-table-column>
                <el-table-column label="排序" prop="sort" align="center"></el-table-column>
                <el-table-column label="置顶" prop="isTop" align="center">
                    <template #default="{ row }">
                        <span v-if="row.isTop == 1" style="color: #007BFF;">是</span>
                        <span v-if="row.isTop == 0" style="color: #FF645F;">否</span>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template #default="{ row }">
                        <el-button type="text" @click="handleUpdate(row)">修改</el-button>
                        <el-button type="text" @click="handleDelete(row)">删除</el-button>
                        <el-button type="text" v-if="row.isTop == '0'" @click="handleSetTop(row)">置顶</el-button>
                        <el-button type="text" v-if="row.isTop == '1'" @click="handleSetTop(row)">取消置顶</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>

        <!-- 表单 -->
        <form-dialog ref="formDialogRef" @handleOk="handleReset"></form-dialog>
    </div>
</template>

<script>
import { listData, setTop ,delData} from '@/api/topic';
import FormDialog from './FormDialog.vue';
export default {
    name: 'Topic',
    data() {
        return {
            queryParams: {
                pageNum: 1,
                pageSize: 10
            },
            tableList: [],
            total: 0
        }
    },
    components: {
        FormDialog
    },
    created() {

    },
    mounted() {
        this.getList()
    },
    methods: {
        // 列表数据
        getList() {
            listData(this.queryParams).then(res => {
                console.log(res)
                this.tableList = res.rows;
                this.total = res.total
            })
        },
        // 新增
        handleAdd() {
            this.$refs.formDialogRef.openModal()
        },
        // 修改
        handleUpdate(row) {
            this.$refs.formDialogRef.openModal(row)
        },
        // 置顶
        handleSetTop(row) {
            let data = {
                id: row.id,
                isTop: row.isTop == 0 ? 1 : 0
            }
            let text = row.isTop == 0 ? '置顶' : '取消置顶'
            this.$modal.confirm('确认要' + text + '话题名称为：' + row.name + '的数据吗？').then(function () {
                return setTop(data);
            }).then(() => {
                this.$modal.msgSuccess(text + "成功");
                this.getList()
            })
        },
        // 删除
        handleDelete(row) {
            this.$modal.confirm('确认要删除话题名称为：' + row.name + '的数据吗？').then(function () {
                return delData(row.id);
            }).then(() => {
                this.$modal.msgSuccess("删除成功");
                this.getList()
            })
        },
        // 查询
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList()
        },
        // 重置
        handleReset() {
            this.queryParams = {
                pageNum: 1,
                pageSize: 10
            }
            this.getList()
        }
    }
}
</script>

<style lang="scss" scoped>
.app-container {
    background-color: rgba(241, 242, 247, 1);
    height: calc(100vh - 50px);

    .page-container {
        background-color: white;
        height: 100%;
        border-radius: 6px;
        padding: 20px 30px;
        box-sizing: border-box;
        overflow: auto;
    }
}
</style>