<#include "/incloud/header.ftl"/>
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<@header></@header>

<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

    <@top></@top>
    <@left></@left>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                菜单管理
                <small>Optional description</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> Level</a></li>
                <li class="active">Here</li>
            </ol>
        </section>
        <!-- Main content -->
        <section class="content container-fluid" id="app">
            <div class="box-body">
                <!--功能按钮区-->
                    <div class="buttonDiv">
                    <el-button
                            size="mini" type="primary" class="pagination-but"
                            @click="loadPids()">新增
                    </el-button>
                </div>
                <!--表格区-->
                <el-table
                        :data="tableData"
                        style="width: 100%;margin-bottom: 20px;"
                        border
                        row-key="id">
                    <el-table-column
                            prop="name"
                            label="菜单名称"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="url"
                            label="链接地址"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="seq"
                            label="排序号">
                    </el-table-column>
                    <el-table-column
                            prop="icon"
                            label="图标">
                    </el-table-column>

                    <el-table-column label="操作" min-width="300px">
                        <template slot-scope="scope">
                            <el-button
                                    v-if="scope.row.lever!=3"
                                    size="mini"
                                    type="primary"
                                    @click="table1Add(scope.$index, scope.row)">新增
                            </el-button>
                            <el-button
                                    size="mini"
                                    type="warning"
                                    @click="table1Edit(scope.$index, scope.row)">修改
                            </el-button>
                            <el-button
                                    size="mini" type="danger"
                                    @click="table1Del(scope.$index, scope.row)">删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--分页-->
                <!--<div class="block">
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="con.currentPage"
                            :page-sizes="[1,10, 50, 200, 500]"
                            :page-size="con.pageSize"
                            layout="total, sizes, prev, pager, next, jumper, slot"
                            :total="con.total">
                    </el-pagination>
                </div>-->
                <!--对话框1 新增-->
                <el-dialog title="新增" :visible.sync="dialog1Visible" :custom-class="'ele-dialog'" :center="true">
                    <el-form :model="addData" status-icon :rules="rules" ref="dialog1">
                        <el-form-item label="菜单名称" :label-width="formLabelWidth" prop="name">
                            <el-input v-model="addData.name" placeholder="请输入菜单名称"></el-input>
                        </el-form-item>
                        <el-form-item label="链接地址" :label-width="formLabelWidth" prop="url">
                            <el-input v-model="addData.url" placeholder="请输入链接地址"></el-input>
                        </el-form-item>
                        <el-form-item label="排序号" :label-width="formLabelWidth" prop="seq">
                            <el-input type="number" v-model="addData.seq" placeholder="请输入排序号"></el-input>
                        </el-form-item>
                        <el-form-item label="图标" :label-width="formLabelWidth" prop="icon">
                            <el-input v-model="addData.icon" placeholder="请输入图标"></el-input>
                        </el-form-item>
                        <!--<el-form-item label="父级菜单" :label-width="formLabelWidth" prop="pid">
                            <el-select v-model="addData.pid" placeholder="请选择父级菜单">
                                <el-option
                                        v-for="item in pids"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>-->
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button type="primary" @click="dialog1Sub('dialog1')">提 交</el-button>
                        <el-button @click="dialog1Visible = false">取 消</el-button>
                    </div>
                </el-dialog>

                <!--对话框2 编辑-->
                <el-dialog title="编辑" :visible.sync="dialog2Visible" :custom-class="'ele-dialog'" :center="true">
                    <el-form :model="editData" status-icon :rules="rules" ref="dialog2">
                        <el-form-item label="菜单名称" :label-width="formLabelWidth" prop="name">
                            <el-input v-model="editData.name" placeholder="请输入菜单名称"></el-input>
                        </el-form-item>
                        <el-form-item label="链接地址" :label-width="formLabelWidth" prop="url">
                            <el-input v-model="editData.url" placeholder="请输入链接地址"></el-input>
                        </el-form-item>
                        <el-form-item label="排序号" :label-width="formLabelWidth" prop="seq">
                            <el-input type="number" v-model="editData.seq" placeholder="请输入排序号"></el-input>
                        </el-form-item>
                        <el-form-item label="图标" :label-width="formLabelWidth" prop="icon">
                            <el-input v-model="editData.icon" placeholder="请输入图标"></el-input>
                        </el-form-item>
                        <!--<el-form-item label="父级菜单" :label-width="formLabelWidth" prop="pid">
                            <el-select v-model="editData.pid" placeholder="请选择父级菜单">
                                <el-option
                                        v-for="item in pids"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>-->
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button type="primary" @click="dialog2Sub('dialog2')">提 交</el-button>
                        <el-button @click="dialog2Visible = false">取 消</el-button>
                    </div>
                </el-dialog>

                <!--对话框3 新增-->
                <el-dialog title="新增" :visible.sync="dialog3Visible" :custom-class="'ele-dialog'" :center="true">
                    <el-form :model="addData" status-icon :rules="rules" ref="dialog3">
                        <el-form-item label="菜单名称" :label-width="formLabelWidth" prop="name">
                            <el-input v-model="addData.name" placeholder="请输入菜单名称"></el-input>
                        </el-form-item>
                        <el-form-item label="链接地址" :label-width="formLabelWidth" prop="url">
                            <el-input v-model="addData.url" placeholder="请输入链接地址"></el-input>
                        </el-form-item>
                        <el-form-item label="排序号" :label-width="formLabelWidth" prop="seq">
                            <el-input type="number" v-model="addData.seq" placeholder="请输入排序号"></el-input>
                        </el-form-item>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button type="primary" @click="dialog1Sub('dialog3')">提 交</el-button>
                        <el-button @click="dialog3Visible = false">取 消</el-button>
                    </div>
                </el-dialog>
            </div>
        </section>
        <!-- /.content -->
    </div>
    <!-- Main Footer -->
    <@footer></@footer>
</div>
</body>
<script src="/js/util/dyAjax.js"></script>
<script>
    $(function () {
        var option = {
            mixins: [dyMixin],
            data() {
                return {
                    // con: {currentPage: 1, pageSize: 10, total: 0},//查询条件
                    tableData: [],
                    dialog1Visible: false,//对话框隐藏
                    addData: {
                        name: '',
                        url: '',
                        seq: '',
                        icon: '',
                        lever:'',
                        pid: ''
                    },//新增数据模板
                    editData: {},//编辑数据
                    pids: [],
                    dialog2Visible: false,//编辑弹框隐藏
                    dialog3Visible: false,//弹框隐藏
                    rules: {
                        name: [
                            {required: true, message: '菜单名称', trigger: 'blur'}
                        ],
                        url: [
                            {required: true,validator: (rule, value, callback) => {
                                if (!(/^[a-zA-Z0-9/#]+$/.test(value))) {
                                    callback(new Error('请输入正确的链接地址'));
                                } else if(value==''){
                                    callback(new Error('请输入链接地址'));
                                }else{
                                    callback();
                                }
                            }, trigger: 'blur'}
                        ],
                        seq: [
                            {required: true, message: '请输入排序号', trigger: 'change'}
                        ],
                    },//对话框1验证规则

                }
            },
            mounted: function () {
                this.loadTable1();
            },
            methods: {
                //新增加载父级菜单
                loadPids() {
                    let _this = this;
                    /*$.dyAjax('/permission/getParent', {}, function (data) {
                        console.log(data);
                        _this.pids = data;
                        _this.dialog1Visible = true;
                    }, 'get');*/
                    _this.addData.lever=1;
                    _this.addData.pid=0;
                    _this.dialog1Visible = true;
                },
                //表内新增
                table1Add(index, row) {

                    let _this = this;
                    var lever=row.lever;
                    _this.addData.pid=row.id;
                    if(lever==1){
                        _this.addData.lever=2;
                        _this.dialog1Visible = true;
                    }else if(lever==2){
                        _this.addData.lever=3;
                        _this.dialog3Visible = true;
                    }
                },
                //编辑
                table1Edit(index, row) {
                    let _this = this;
                    /*$.dyAjax('/permission/getParent', {}, function (data) {
                        console.log(data);
                        _this.pids = data;
                    }, 'get');*/
                    $.dyAjax('/permission/get/' + row.id, {}, function (data) {
                        _this.editData = data.data;
//                        _this.editData.password2 = '';
                        _this.dialog2Visible = true;
                        console.log(data);
                    }, 'get');
                },
                /*//翻页大小改变事件
                handleSizeChange(val) {
                    var _this = this;
                    _this.con.pageSize = val;
                    _this.loadTable1();
                },
                //翻页翻页事件
                handleCurrentChange(val) {
                    var _this = this;
                    _this.con.currentPage = val;
                    _this.loadTable1();
                },*/
                //对话框1提交事件
                dialog1Sub: function (formName) {
                    var _this = this;
                    console.log(this.addData);
                    this.$refs[formName].validate((valid) => {
                        if(valid) {
                            $.dyAjax('/permission/add', _this.addData, function (data) {
                                console.log(data);
                                if (data.status === 200) {
                                    _this.loadTable1(function () {
                                        _this.$message({
                                            message: '成功',
                                            type: 'success'
                                        });
                                        _this.dialog1Visible = false;
                                        _this.dialog3Visible = false;
                                        _this.jsonClear(_this.addData);
                                    });
                                } else {
                                    _this.loadTable1(function () {
                                        _this.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                        _this.dialog1Visible = false;
                                        _this.dialog3Visible = false;
                                        _this.jsonClear(_this.addData);
                                    });
                                }

                            });
                        } else {
                            return false;
                }
                })
                    ;
                },
                //加载表数据
                loadTable1: function (callback) {
                    var _this = this;
                    $.dyAjax('/permission/tree', {}, function (data) {
                        callback && callback(data);
                        console.log(data)
                        _this.tableData = data;
                        // _this.con.total = data.total;
                        // _this.con.currentPage = data.pageNum;
                        // _this.con.pageSize = data.pageSize;
                    }, 'get');
                },
                //删除
                table1Del: function (index, row) {
                    let _this = this;
                    console.log(row);
                    this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        $.dyAjax('/permission/del', {id: row.id}, function (data) {
                        if (data.status == 200) {
                            _this.loadTable1(function () {
                                _this.$message({
                                    message: '成功',
                                    type: 'success'
                                });
                                _this.dialog1Visible = false;
                                _this.jsonClear(_this.addData);
                            });
                        } else {
                            _this.loadTable1(function () {
                                _this.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                                _this.dialog1Visible = false;
                                _this.jsonClear(_this.addData);
                            });
                        }
                    });
                }).
                    catch(() => {}
                )
                    ;
                },
                //对话框2 编辑提交事件
                dialog2Sub: function (formName) {
                    var _this = this;
                    console.log(this.editData);
                    this.$refs[formName].validate((valid) => {
                        if(valid) {
                            console.log(_this.editData);
                            $.dyAjax('/permission/update', _this.editData, function (data) {
                                _this.loadTable1(function () {
                                    _this.$message({
                                        message: '成功',
                                        type: 'success'
                                    });
                                    _this.dialog2Visible = false;
                                    _this.jsonClear(_this.editData);
                                });
                            });
                        } else {
                            return false;
                }
                })
                    ;
                },
            }
        };
        var app = Vue.extend(option);
        new app().$mount('#app');
    });
</script>
</html>