<template>
    <div class="app-container">
        <el-row :gutter="10" class="mb8">
            <el-col :span="2">
                <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="2">
                <el-button type="primary" plain icon="el-icon-upload" @click="handleAddMenu">同步菜单</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" fit highlight-current-row border stripe :data="menuList" row-key="wxMenuId" default-expand-all :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
            <el-table-column label="菜单名称" align="center" prop="wxMenuName" />
            <el-table-column label="排序" align="center" prop="sort" />
            <el-table-column label="菜单类型" align="center" prop="wxMenuType">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.wx_menu_type" :value="scope.row.wxMenuType" />
                </template>
            </el-table-column>
            <el-table-column label="内容" align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.wxMenuType === '1'">
                        {{ scope.row.keyWords }}
                    </span>
                    <span v-if="scope.row.wxMenuType === '2'">
                        {{ scope.row.picTextUrl }}
                    </span>
                    <span v-if="scope.row.wxMenuType === '3'">
                        <dict-tag :options="dict.type.wx_menu_extend_type" :value="scope.row.extendMenu" />
                    </span>
                </template>
            </el-table-column>
            <!--      <el-table-column label="父级菜单id" align="center" prop="parentId" />-->
            <!--      <el-table-column label="关键词" align="center" prop="keyWords" />-->

            <!--      <el-table-column label="状态 0启用  1未启用" align="center" prop="status">-->
            <!--        <template slot-scope="scope">-->
            <!--          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>-->
            <!--        </template>-->
            <!--      </el-table-column>-->
            <!--      <el-table-column label="扩展菜单" align="center" prop="extendMenu">-->
            <!--        <template slot-scope="scope">-->
            <!--          <dict-tag :options="dict.type.wx_menu_extend_type" :value="scope.row.extendMenu"/>-->
            <!--        </template>-->
            <!--      </el-table-column>-->
            <!--      <el-table-column label="图文外链网址" align="center" prop="picTextUrl" />-->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.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" />

        <!-- 添加或修改微信菜单管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px" size="medium">
                <el-form-item label="菜单名称" prop="wxMenuName">
                    <el-input v-model="form.wxMenuName" placeholder="请输入菜单名称" :style="{ width: '80%' }" maxLength="10" />
                </el-form-item>
                <el-form-item label="是否父级菜单" prop="isParent">
                    <el-radio-group v-model="form.isParent" @input="handleRules">
                        <el-radio-button label="0">是</el-radio-button>
                        <el-radio-button label="1">否</el-radio-button>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="菜单类型" prop="wxMenuType">
                    <el-select v-model="form.wxMenuType" placeholder="请选择菜单类型" :style="{ width: '80%' }" clearable>
                        <el-option v-for="dict in dict.type.wx_menu_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="父级菜单" prop="parentId" v-if="this.form.isParent === '1'">
                    <el-select v-model="form.parentId" placeholder="请选择菜单" :style="{ width: '80%' }">
                        <el-option v-for="dict in this.wxMenuTypeVO" :key="dict.wxMenuId" :label="dict.wxMenuName" :value="dict.wxMenuId"></el-option>
                    </el-select>
                </el-form-item>
                <template v-if="form.wxMenuType === '1'">
                    <el-form-item label="关键词" prop="keyWords">
                        <el-input v-model="form.keyWords" placeholder="请输入关键词" :style="{ width: '80%' }" />
                    </el-form-item>
                </template>
                <template v-if="form.wxMenuType === '2'">
                    <el-form-item label="外链网址" prop="picTextUrl">
                        <el-input v-model="form.picTextUrl" placeholder="请输入图文外链网址" :style="{ width: '80%' }" />
                    </el-form-item>
                </template>
                <template v-if="form.wxMenuType === '3'">
                    <el-form-item label="扩展菜单" prop="extendMenu">
                        <el-select v-model="form.extendMenu" placeholder="请选择扩展菜单" :style="{ width: '80%' }">
                            <el-option v-for="dict in dict.type.wx_menu_extend_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                        </el-select>
                    </el-form-item>
                </template>
                <el-form-item label="排序" prop="sort" maxLength="20">
                    <el-input v-model.number="form.sort" placeholder="请输入排序" :style="{ width: '80%' }" maxLength="5" />
                </el-form-item>
                <!--        <el-form-item label="状态">-->
                <!--          <el-radio-group v-model="form.status">-->
                <!--            <el-radio-->
                <!--              v-for="dict in dict.type.sys_normal_disable"-->
                <!--              :key="dict.value"-->
                <!--              :label="dict.value"-->
                <!--            >{{dict.label}}</el-radio>-->
                <!--          </el-radio-group>-->
                <!--        </el-form-item>-->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listMenu, getMenu, delMenu, addMenu, updateMenu, getMenuTypeList, createMenu } from '@/api/wxManage/menu'

export default {
    name: 'Menu',
    dicts: ['wx_menu_extend_type', 'wx_menu_type', 'sys_normal_disable'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 微信菜单管理表格数据
            menuList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                wxMenuName: null,
                wxMenuType: null,
                parentId: null,
                keyWords: null,
                sort: null,
                status: null,
                extendMenu: null,
                picTextUrl: null,
                isParent: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                wxMenuName: [{ required: true, message: '菜单名称不能为空', trigger: 'blur' }],
                keyWords: [{ required: true, message: '关键字不能为空', trigger: 'blur' }],
                picTextUrl: [{ required: true, message: '外链地址不能为空', trigger: 'blur' }],
                extendMenu: [{ required: true, message: '扩展菜单不能为空', trigger: 'blur' }],
                sort: [
                    { required: true, message: '排序不能为空', trigger: 'blur' },
                    { type: 'number', message: '请输入数字' }
                ],
                status: [{ required: true, message: '回复类型不能为空', trigger: 'blur' }],
                parentId: [{ required: true, message: '父级菜单不能为空', trigger: 'blur' }],
                wxMenuType: [{ required: true, message: '菜单类型不能为空', trigger: 'blur' }]
            },
            wxMenuTypeVO: {}
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询微信菜单管理列表 */
        getList() {
            this.loading = true
            listMenu(this.queryParams).then((response) => {
                this.menuList = this.handleTree(response.data, 'wxMenuId')
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                wxMenuId: null,
                wxMenuName: null,
                wxMenuType: null,
                parentId: null,
                keyWords: null,
                sort: null,
                status: '0',
                extendMenu: null,
                picTextUrl: null,
                createTime: null
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.wxMenuId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加微信菜单'
            getMenuTypeList().then((res) => {
                this.wxMenuTypeVO = res.data
            })
        },
        handleRules() {
            this.rules.wxMenuType[0].required = this.form.isParent == '1'
            this.$forceUpdate()
        },
        handleAddMenu() {
            createMenu().then((res) => {
                this.$message('操作成功')
            })
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const wxMenuId = row.wxMenuId || this.ids
            getMenu(wxMenuId).then((response) => {
                this.form = response.data
                this.form.sort = parseInt(this.form.sort)
                this.open = true
                this.title = '修改微信菜单'
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    // if (this.form.parentId === null){
                    //   this.form.parentId = 0;
                    // }
                    this.form.status = 0
                    if (this.form.wxMenuId != null) {
                        updateMenu(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addMenu(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const wxMenuIds = row.wxMenuId || this.ids
            const menuName = row.wxMenuName
            this.$modal
                .confirm('是否确认删除"' + menuName + '"该项菜单？')
                .then(function () {
                    return delMenu(wxMenuIds)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'wxManage/menu/export',
                {
                    ...this.queryParams
                },
                `menu_${new Date().getTime()}.xlsx`
            )
        }
        // handleRules(){
        //   if (this.form.parentId != null){
        //       this.rules.wxMenuType = { required: true, message: "菜单类型不能为空", trigger: "blur" }
        //   }else {
        //       this.rules.wxMenuType = { required: false, message: "菜单类型不能为空", trigger: "blur" }
        //   }
        // }
    }
}
</script>
