<template>
    <div>
        <div class="coentent">
            <div class="contents-area">
                <div class="areas">
                    <div class="headers">
                        <div class="headers-left">
                            <div class="headers-name">菜单列表</div>
                        </div>
                        <div class="headers-right">
                            <div class="filter-item">
                                <span class="filter-label">选择菜单</span>
                                <el-select v-model="queryForm.menuType" style="border: 1px solid #ededed;" placeholder="全部" class="filter-select" size="small">
                                    <el-option v-for="item in menuTypeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
                                </el-select>
                            </div>
                            <div class="filter-item">
                                <span class="filter-label">选择状态</span>
                                <el-select v-model="queryForm.status" style="border: 1px solid #ededed;" placeholder="全部" class="filter-select" size="small">
                                    <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
                                </el-select>
                            </div>
                            <el-input placeholder="输入菜单名称" style="border: 1px solid #ededed;" v-model="queryForm.keyword" class="search-input" size="small" clearable>
                                <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                            </el-input>
                        </div>
                    </div>

                    <div class="content-table">
                        <el-table
                            :data="pagedData"
                            border
                            v-loading="loading"
                            :header-cell-style="{ background: '#EDEFFE', color: '#363636', textAlign: 'center' }"
                            :cell-style="cellStyle"
                            :row-class-name="rowClassName"
                            style="width: 100%">
                            <el-table-column type="index" label="序号" width="70" align="center" :index="formatIndex"></el-table-column>
                            <el-table-column prop="title" label="菜单名称" min-width="200"></el-table-column>
                            <el-table-column prop="weigh" label="排序" width="100" align="center"></el-table-column>
                            <el-table-column prop="pid" label="上级" min-width="160">
                                <template #default="scope">
                                    <span v-if="scope.row.parent" style="display: flex;justify-content: center;">{{ scope.row.parent.title }}</span>
                                    <span v-else style="display: flex;justify-content: center;">顶级</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="status" label="状态" width="120" align="center">
                                <template #default="scope">
                                    <span :class="['status-tag', scope.row.status === 1 || scope.row.status === '正常' ? 'status-active' : 'status-inactive']">
                                        {{ scope.row.status === 1 || scope.row.status === '正常' ? '正常' : '禁用' }}
                                    </span>
                                </template>
                            </el-table-column>
                            <el-table-column label="操作" width="120" align="center">
                                <template #default="scope">
                                    <el-button type="text" class="link-btn" @click="handleEdit(scope.row)">修改</el-button>
                                </template>
                            </el-table-column>
                        </el-table>

                        <div class="footer">
                            <el-pagination
                                @size-change="handleSizeChange"
                                @current-change="handleCurrentChange"
                                :current-page="currentPage"
                                :page-sizes="[10, 20, 30, 50]"
                                :page-size="pageSize"
                                layout="total, sizes, prev, pager, next, jumper"
                                :total="total">
                            </el-pagination>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <el-dialog
            :visible.sync="editDialogVisible"
            width="840px"
            class="menu-edit-dialog"
            :close-on-click-modal="false"
            :destroy-on-close="true"
            @closed="resetEditDialog">
            <template #title>
                <div class="edit-dialog-title">菜单修改</div>
            </template>
            <div class="edit-dialog-content">
                <div class="edit-grid">
                    <div class="grid-row">
                        <div class="grid-label">菜单名称</div>
                        <div class="grid-value">
                            <el-input v-model="editForm.menuName" size="small"></el-input>
                        </div>
                        <div class="grid-label">排序</div>
                        <div class="grid-value">
                            <el-input v-model="editForm.order" size="small"></el-input>
                        </div>
                    </div>
                    <div class="grid-row">
                        <div class="grid-label">状态</div>
                        <div class="grid-value">
                            <el-select v-model="editForm.status" size="small">
                                <el-option label="正常" :value="1"></el-option>
                                <el-option label="禁用" :value="0"></el-option>
                            </el-select>
                        </div>
                        <div class="grid-label">上级（不可编辑）</div>
                        <div class="grid-value">
                            <el-input v-model="editForm.parentName" size="small" disabled></el-input>
                        </div>
                    </div>
                    <div class="grid-row">
                        <div class="grid-label">默认状态图标</div>
                        <div class="grid-value icon-uploader">
                            <div class="icon-placeholder">
                                <i class="el-icon-picture"></i>
                            </div>
                            <el-upload
                                class="upload-link"
                                action="#"
                                :auto-upload="false">
                                <span class="upload-text">上传</span>
                            </el-upload>
                        </div>
                        <div class="grid-label">选中状态图标</div>
                        <div class="grid-value icon-uploader">
                            <div class="icon-placeholder">
                                <i class="el-icon-picture"></i>
                            </div>
                            <el-upload
                                class="upload-link"
                                action="#"
                                :auto-upload="false">
                                <span class="upload-text">上传</span>
                            </el-upload>
                        </div>
                    </div>
                </div>
            </div>
            <template #footer>
                <div class="edit-dialog-footer">
                    <el-button size="small" @click="cancelEdit">取消</el-button>
                    <el-button type="primary" size="small" @click="submitEdit">保存</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import {menuList} from '@/api/system/Configuration/index'
export default {
    data() {
        return {
            loading: false,
            queryForm: {
                keyword: '',
                menuType: 'all',
                status: 'all'
            },
            currentPage: 1,
            pageSize: 10,
            total: 0,
            menuTypeOptions: [
                { value: 'all', label: '全部' },
                { value: 'top', label: '顶级菜单' },
                { value: 'system', label: '系统管理' },
                { value: 'business', label: '业务模块' }
            ],
            statusOptions: [
                { value: 'all', label: '全部' },
                { value: 'enabled', label: '启用' },
                { value: 'disabled', label: '禁用' }
            ],
            editDialogVisible: false,
            editForm: {
                id: '',
                menuName: '',
                order: '',
                status: 1,
                parentName: '',
                defaultIcon: '',
                activeIcon: ''
            },
            tableData: [
                { id: 1, menuName: '企业总览', order: 1, parentName: '顶级', status: '启用' },
                { id: 2, menuName: '资源资料', order: 1, parentName: '顶级', status: '禁用' },
                { id: 3, menuName: '研发项目', order: 1, parentName: '顶级', status: '启用' },
                { id: 4, menuName: '经营项目', order: 1, parentName: '顶级', status: '启用' },
                { id: 5, menuName: '申报中心', order: 1, parentName: '顶级', status: '启用' },
                { id: 6, menuName: '高企认定', order: 1, parentName: '顶级', status: '启用' },
                { id: 7, menuName: '专精特新', order: 1, parentName: '顶级', status: '启用' },
                { id: 8, menuName: '加计扣除', order: 1, parentName: '顶级', status: '启用' },
                { id: 9, menuName: '科创汇', order: 1, parentName: '顶级', status: '启用' },
                { id: 10, menuName: '系统管理', order: 1, parentName: '顶级', status: '启用' },
                { id: 11, menuName: '权限管理', order: 1, parentName: '系统管理', status: '启用' },
                { id: 12, menuName: '菜单配置', order: 2, parentName: '系统管理', status: '启用' }
            ]
        }
    },
    computed: {
        pagedData() {
            // 后端分页，直接返回 tableData
            return this.tableData
        }
    },
    mounted() {
        this.getMenuList()
    },
    methods: {
        // 获取菜单列表
        getMenuList() {
            this.loading = true
            const params = {
                page: this.currentPage,
                limit: this.pageSize
            }
            // 添加筛选条件
            if (this.queryForm.keyword) {
                params.keyword = this.queryForm.keyword.trim()
            }
            if (this.queryForm.menuType !== 'all') {
                params.menuType = this.queryForm.menuType
            }
            if (this.queryForm.status !== 'all') {
                params.status = this.queryForm.status === 'enabled' ? 1 : 0
            }
            menuList(params).then((res) => {
                if (res && res.code === 1 && res.data) {
                    const list = res.data.data || res.data.list || res.data.records || []
                    // 更新总数
                    this.total = res.data.total || res.data.count || list.length || 0
                    // 更新表格数据
                    this.tableData=res.data.data
                } else {
                    this.tableData = []
                    this.total = 0
                }
            }).catch(error => {
                console.error('获取菜单列表失败:', error)
                this.tableData = []
                this.total = 0
                // 错误信息已经在 request.js 的拦截器中处理并显示
            }).finally(() => {
                this.loading = false
            })
        },
        handleSearch() {
            this.currentPage = 1
            this.getMenuList()
        },
        handleSizeChange(val) {
            this.pageSize = val
            this.currentPage = 1
            this.getMenuList()
        },
        handleCurrentChange(val) {
            this.currentPage = val
            this.getMenuList()
        },
        formatIndex(index) {
            return (this.currentPage - 1) * this.pageSize + index + 1
        },
        rowClassName() {
            return 'menu-row'
        },
        cellStyle({ column }) {
            const leftAlignedProps = ['title', 'menuName', 'pid', 'parentName']
            return {
                textAlign: leftAlignedProps.includes(column.property) ? 'left' : 'center',
                color: '#363636'
            }
        },
        handleEdit(row) {
            this.editForm = {
                id: row.id,
                menuName: row.title || row.menuName,
                order: row.weigh || row.order,
                status: row.status === '正常' ? 1 : (row.status === '禁用' ? 0 : row.status),
                parentName: row.pid || row.parentName || '',
                defaultIcon: row.icon || '',
                activeIcon: row.activeIcon || ''
            }
            this.editDialogVisible = true
        },
        cancelEdit() {
            this.editDialogVisible = false
        },
        submitEdit() {
            this.$message.success('保存成功')
            this.editDialogVisible = false
        },
        resetEditDialog() {
            this.editForm = {
                id: '',
                menuName: '',
                order: '',
                status: 1,
                parentName: '',
                defaultIcon: '',
                activeIcon: ''
            }
        }
    }
}
</script>

<style scoped>
.coentent {
    width: 100%;
    height: 100%;
    background: white
}

.contents-area {
    width: 100%;
    margin-top: 20px;
}

.areas {
    width: 97%;
    margin: 0 auto;
}

.headers {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    flex-wrap: wrap;
    gap: 16px;
    padding-top: 18px;
}

.headers-left {
    display: flex;
    align-items: center;
    gap: 20px;
}

.headers-name {
    font-size: 15px;
    color: #17192C;
}

.headers-right {
    display: flex;
    align-items: center;
    gap: 16px;
    flex-wrap: wrap;
}

.search-input {
    width: 220px;
}

.filter-item {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: #6A6E8C;
}

.filter-label {
    min-width: 64px;
}

.filter-select {
    width: 150px;
}

.content-table {
    background: #fff;
    border-radius: 8px;
}

.link-btn {
    color: #5165F7;
}

.footer {
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
}

.status-tag {
    display: inline-block;
    padding: 2px 12px;
    border-radius: 12px;
    font-size: 12px;
}

.status-active {
    background: #E5F7ED;
    color: #19B66A;
}

.status-inactive {
    background: #FDEBEB;
    color: #F56C6C;
}

::v-deep .menu-row td {
    color: #363636;
}

::v-deep .el-table__body tr:hover > td {
    background-color: #F7F8FF !important;
}

.menu-edit-dialog ::v-deep .el-dialog {
    border-radius: 12px;
}

.menu-edit-dialog ::v-deep .el-dialog__header {
    padding: 18px 24px 10px;
    border-bottom: 1px solid #EBEEF5;
}

.menu-edit-dialog ::v-deep .el-dialog__body {
    padding: 20px 40px 10px;
}

.menu-edit-dialog ::v-deep .el-dialog__footer {
    padding: 20px 40px 30px;
    border-top: none;
}

.edit-dialog-title {
    font-size: 18px;
    font-weight: 600;
    color: #17192C;
    text-align: center;
}

.edit-grid {
    border: 1px solid #E0E3F5;
    border-radius: 8px;
    overflow: hidden;
}

.grid-row {
    display: grid;
    grid-template-columns: 160px 1fr 160px 1fr;
    border-bottom: 1px solid #E0E3F5;
}

.grid-row:last-child {
    border-bottom: none;
}

.grid-label {
    background: #F5F6FE;
    padding: 16px;
    font-size: 14px;
    color: #363636;
    display: flex;
    align-items: center;
}

.grid-value {
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
}

.icon-uploader {
    gap: 16px;
}

.icon-placeholder {
    width: 38px;
    height: 38px;
    border: 1px solid #E0E3F5;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #B2B6D4;
    background: #F8F9FF;
}

.upload-link {
    cursor: pointer;
}

.upload-text {
    color: #5165F7;
    font-size: 14px;
}

.edit-dialog-footer {
    text-align: center;
}

.edit-dialog-footer .el-button + .el-button {
    margin-left: 16px;
}
</style>