<template>
    <div class="menuManagement">
        <div class="menuBtn">
            <p class="titleSty">菜单管理</p>
            <div style="display:flex;justify-content: space-between;padding: 10px;">
                <el-row :gutter="10" class="regionalBtn">
                    <el-col :span="1.5">
                        <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        size="mini"
                        @click="handleAdd"
                        >新增</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                        type="success"
                        plain
                        icon="Edit"
                        size="mini"
                        @click="handleUpdate"
                        >修改</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        size="mini"
                        @click="handleDelete"
                        >删除</el-button>
                    </el-col>
                </el-row>
            </div>
            <div class="menuTableData">
                <el-table :data="menuTableData" border style="width: 100%" @row-click="rowClick">
                    <el-table-column label="操作" width="60" align="center">
                        <template #default="scope">
                            <el-radio v-model="menuradio" :label="scope.row"></el-radio>
                        </template>
                    </el-table-column>
                    <el-table-column label="菜单名称" prop="menuName">
                    </el-table-column>
                    <!-- <el-table-column label="菜单层级">
                        <template #default="scope">
                            <span>{{showMenuLeavel(scope.row.parentId)}}</span>
                        </template>
                    </el-table-column> -->
                    <el-table-column label="创建时间" prop="createTime">
                    </el-table-column>
                </el-table>
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="page.currentPage"
                    :page-size="page.pagesize"
                    layout="total, prev, pager, next, jumper"
                    :total="page.total">
                </el-pagination>
            </div>
        </div>
        <el-dialog :title="title" v-model="dialogVisible" width="600px" :close-on-click-modal="false" @close="closeDialog">
            <div class="windframFromsxx">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="菜单名称" prop="menus">
                        <el-select v-model="ruleForm.menus" placeholder="请选择菜单名称">
                            <el-option
                            v-for="item in menuNameOptions"
                            :key="item.label"
                            :label="item.label"
                            :value="item.label">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <!-- <el-form-item label="菜单编号" prop="menuNum">
                        <el-select v-model="ruleForm.menuNum" placeholder="请选择菜单编号">
                            <el-option
                            v-for="item in menuOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item> -->
                </el-form>
            </div>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="resetForm" v-if="!single">重 置</el-button>
                    <el-button @click="dialogVisible = false" v-else>取 消</el-button>
                    <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import {apiGetgetMenuList, apiGetStation, apiGetgetaddmenuMsg, apiGetgetuploadmenuMsg, apiGetgetdeletemenuMsg} from '../../api/api'
export default {
    data() {
        return {
            title:'',
            menuTableData:[],
            menuradio: {},
            single: false,
            dialogVisible:false,
            roleName: '',
            typeWh: '',
            menuOptions: [],
            menuNameOptions: [],
            page:{
                pagesize: 10,
                currentPage: 1,
                total: 0
            },
            ruleForm: {
                menus: '',
                // menuNum: null
            },
            rules: {
                menus: [
                    { required: true, message: '请选择菜单名称', trigger: 'change' }
                ],
                // menuNum: [
                //     { required: true, message: '请选择菜单编号', trigger: 'change' }
                // ]
            }
        }
    },
    created() {
        this.menuOptions = [
            {
                label: '一级菜单',
                value: 0
            },
            // {
            //     label: '二级菜单',
            //     value: 1
            // },
            // {
            //     label: '三级菜单',
            //     value: 2
            // }
        ]
        this.menuNameOptions = [
            {
                label: '概要'
            },
            {
                label: '全景功率'
            },
            {
                label: '功率预测'
            },
            {
                label: '电量预测'
            },
            {
                label: '功率管控'
            },
            {
                label: '天气预报'
            },
            {
                label: '统计汇总'
            },
            {
                label: '智能营销'
            }
        ]
        this.getAllStations()
    },
    mounted() {
        this.getPersonListFn()
    },
    methods:{
        // 获取所有场站
        getAllStations() {
            apiGetStation().then(datas =>{
                if (datas && datas.data && datas.data.list) {
                    datas.data.list.forEach(item =>{
                        let obj = {
                            label: item.stationName
                        }
                        this.menuNameOptions.push(obj)
                    })
                }
            })
        },
        getSeachData() {
            this.getPersonListFn('seach')
        },
        reset() {
            this.roleName = ''
            this.page = {
                pagesize: 10,
                currentPage: 1,
                total: 0
            }
            this.getPersonListFn()
        },
        showMenuLeavel(id) {
            let str = ''
            this.menuOptions.forEach(item =>{
                if (item.value === id) {
                    str = item.label
                }
            })
            return str
        },
        handleSizeChange(val){
            this.page.pagesize = val
            this.getPersonListFn('seach')
        },
        handleCurrentChange(val){
            this.page.currentPage =val
            this.getPersonListFn('seach')
        },
        handleChange(file, fileList) {
            this.ruleForm.fileList = fileList.slice(-3);
        },
        closeDialog() {
            this.$refs['ruleForm'].clearValidate()
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let that = this
                    that.addOrUpdatemenuDate()
                }
            });
        },
        // 新增/修改风电场配置
        addOrUpdatemenuDate() {
            let that = this
            let params = {
                menuName: that.ruleForm.menus,
                // parentId: that.ruleForm.menuNum
                parentId: 0
            }
            if (!that.single) {
                apiGetgetaddmenuMsg(params).then(datas =>{
                    that.changeshowData(datas)
                })
            } else {
                params.menuId = this.menuradio.menuId
                apiGetgetuploadmenuMsg(params).then(datas =>{
                    that.changeshowData(datas)
                })
            }
        },
        changeshowData(datas) {
            if (datas.msg === '操作成功') {
                this.dialogVisible = false
                if (!this.single) {
                    this.$message({
                        message: '菜单新增成功',
                        type: 'success'
                    });
                } else {
                    this.$message({
                        message: '菜单修改成功',
                        type: 'success'
                    });
                }
                this.single = false
                this.reset()
            }
        },
        resetForm() {
            this.ruleForm = {
                menus: '',
                // menuNum: null
            }
        },
        // 新增区域信息
        handleAdd() {
            this.dialogVisible = true
            this.single = false
            this.title = '新增菜单'
            this.$nextTick(() =>{
                this.resetForm()
                this.$refs['ruleForm'].clearValidate()
            })
        },
        // 修改区域信息
        handleUpdate() {
            this.dialogVisible = true
            this.single = true
            this.title = '修改菜单'
            this.ruleForm = {
                menus: this.menuradio.menuName,
                // menuNum: this.menuradio.parentId
            }
        },
        rowClick(row, column, event) {
            this.menuradio = row
        },
        // 删除区域信息
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let params = {
                    menuId: that.menuradio.menuId
                }
                apiGetgetdeletemenuMsg(params).then(datas =>{
                    if (datas.msg === '操作成功') {
                        that.getPersonListFn()
                    }
                })
                if (that.menuTableData.length>0) {
                    that.menuradio = that.menuTableData[0]
                }
                that.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            })
        },
        //获取风电场列表数据
        getPersonListFn(type) {
            let that = this
            let params = {
                pageNum: that.page.currentPage,
                pageSize: that.page.pagesize
            }
            apiGetgetMenuList(params).then(datas =>{
                if (datas && datas.rows) {
                    that.menuTableData = datas.rows
                    that.menuradio = datas.rows[0]
                    that.page.total = datas.total
                }
            })
        }
    }
}
</script>

<style lang="less">
.menuManagement{
    .menuBtn{
        background: #fff;
        padding: 10px 20px 0 20px;
        .titleSty{
            border-left: 4px solid #2996da;
            padding-left: 5px;
            font-size: 14px;
            margin-left:20px;
        }
        .periodSeach{
            display: flex;
            margin:20px;
                .exceed{
                    display: flex;
                    margin-right:20px;
                    .el-input{
                        width: 200px;
                        margin-right:10px;
                        .el-input__inner{
                            height:30px;
                        }
                        .el-input__suffix{
                            .el-select__caret{
                                line-height:30px;
                            }
                        }
                    }
                }
        }
        span{
            font-size:14px;
        }
        .regionalBtn{
            padding-left: 10px;
        }

        .exceed{
            display: flex;
            margin-right:20px;
            .exTime{
                width: 240px;
                .el-select__tags{
                    left: 1px;
                    background:#fff;
                }
            }
        }
        .el-select{
            // width: 160px;
            margin-right:10px;
            .el-input__inner{
                height:30px;
            }
            .el-input__suffix{
                .el-select__caret{
                    line-height:30px;
                }
            }
        }
        .el-button{
            height: 30px;
            // width:100px;
            padding: 0 30px ;
            // padding-top: 8px;
            span{
                margin:0;
            }
        }
    }
    .menuTableData{
        padding: 10px 20px;
        .el-table{
        .el-table__body-wrapper{
            .el-table__empty-block{
            height: 550px !important;
            }
        }
        .el-input__inner{
            height: 30px !important;
        }
        .el-radio__label{
            display: none;
        }
        }
        .el-pagination{
            margin-top: 20px;
            text-align: end;
            position: relative;
        }
    }
    .el-overlay{
        .el-dialog{
            .el-dialog__body{
                padding: 30px 60px 30px 20px !important;
                .windframFromsxx{
                    padding-right: 20px !important;
                    .el-select{
                        width: 100%;
                    }
                }
            }
        }
    }
    
}
</style>