<#include "/incloud/header.ftl"/>
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html >
<@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="query">
                    <el-form :inline="true" :model="con" class="demo-form-inline">
                        <el-form-item label="姓名" :label-width="queryLabelWidth">
                            <el-input v-model="con.name" placeholder="姓名" maxlength="4" minlength="2"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="loadTable1()">查询</el-button>
                        </el-form-item>
                    </el-form>
                </div>
                <!--功能按钮区-->
                <div class="buttonDiv">
                    <el-button
                            size="mini" type="primary" class="pagination-but"
                            @click="dialog1Visible = true">新增
                    </el-button>
                </div>
                <!--表格区-->
                <el-table border
                          :data="tableData"
                          style="width: 100%">
                    <el-table-column min-width="150px"
                                     label="姓名"
                                     prop="name">
                    </el-table-column>
                    <el-table-column min-width="150px"
                                     label="账号"
                                     prop="account">
                    </el-table-column>
                    <el-table-column min-width="150px"
                                     label="电话"
                                     prop="phone">
                    </el-table-column>
                    <el-table-column min-width="150px"
                                     label="状态">
                        <template slot-scope="scope">
                            {{scope.row.status|getEnumVal(MANAGE_STATUS)}}
                        </template>
                    </el-table-column>
                    <el-table-column min-width="150px"
                                     label="创建时间"
                                     prop="createTime">
                    </el-table-column>
                    <el-table-column min-width="150px"
                                     label="修改时间"
                                     prop="updateTime">
                    </el-table-column>
                    <el-table-column label="操作" min-width="300px">
                        <template slot-scope="scope">
                            <el-button
                                    size="mini"
                                    type="primary"
                                    @click="table1Edit(scope.$index, scope.row)">修改
                            </el-button>
                            <el-button
                                    size="mini" type="danger"
                                    @click="table1Del(scope.$index, scope.row)">删除
                            </el-button>
                            <!--<el-button
                                    size="mini" type="primary"
                                    @click="table1Role(scope.$index, scope.row)">分配角色
                            </el-button>-->
                            <el-button v-if="scope.row.status===MANAGE_STATUS.正常" size="mini" type="warning"
                                    @click="table1FreezeUnfreeza(scope.row.id,MANAGE_STATUS.无效)">冻结
                            </el-button>
                            <el-button v-if="scope.row.status===MANAGE_STATUS.无效" size="mini" type="primary"
                                    @click="table1FreezeUnfreeza(scope.row.id,MANAGE_STATUS.正常)">解冻
                            </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"
                           @close="closeAddDialog1">
                    <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="phone">
                            <el-input v-model="addData.phone" placeholder="请输入电话"></el-input>
                        </el-form-item>
                        <el-form-item label="账号" :label-width="formLabelWidth" prop="account">
                            <el-input v-model="addData.account" placeholder="请输入账号"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
                            <el-input type="password" v-model="addData.password" placeholder="请输入密码"></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码" :label-width="formLabelWidth" prop="password2">
                            <el-input type="password" v-model="addData.password2" placeholder="请再次输入密码"></el-input>
                        </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"
                           @close="closeEditDialog2">
                    <el-form :model="editData" status-icon :rules="rules2" 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="phone">
                            <el-input v-model="editData.phone" placeholder="请输入电话"></el-input>
                        </el-form-item>
                        <el-form-item label="账号" :label-width="formLabelWidth" prop="account">
                            <el-input v-model="editData.account" placeholder="请输入账号"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
                            <el-input type="password" v-model="editData.password" placeholder="请输入密码,留空不作修改"></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码" :label-width="formLabelWidth" prop="password2">
                            <el-input type="password" v-model="editData.password2" placeholder="请再次输入密码,留空不作修改"></el-input>
                        </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="roleData" status-icon :rules="rules3" ref="dialog3">
                        <el-form-item :label-width="formLabelWidth" prop="id">
                            <el-input type="hidden" v-model="roleData.id"></el-input>
                        </el-form-item>
                        <el-form-item label="角色" :label-width="formLabelWidth" prop="roles">
                            <el-select v-model="roleData.roles" placeholder="请选择角色" multiple>
                                <el-option
                                        v-for="item in roleIds"
                                        :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="dialog3Sub('dialog3')">提 交</el-button>
                        <el-button @click="dialog3Visible = false">取 消</el-button>
                    </div>
                </el-dialog>
            </div>
        </section>
        <!-- /.content -->
    </div>
    <!-- Main Footer -->
    <!--<@footer></@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: '',
                        phone: '',
                        account: '',
                        password: '',
                        password2: ''
                    },//新增数据模板
                    editData: {},//编辑数据
                    roleData: {roles:[]},//角色数据
                    roleIds:[],
                    dialog2Visible: false,//编辑弹框隐藏
                    dialog3Visible: false,//角色弹框隐藏
                    rules: {
                        name: [
                            {required: true,validator: (rule, value, callback) => {
                                if (!(/^[\u4E00-\u9FA5\uf900-\ufa2d]{2,4}$/.test(value))) {
                                    callback(new Error('请输入正确的姓名,只能输入汉字,长度在2-4个字符之间'));
                                } else {
                                    callback();
                                }
                            }, trigger: 'blur'}
                        ],
                        phone: [
                            {required: true,validator: (rule, value, callback) => {
                                if (!(/^1[34578]\d{9}$/.test(value))) {
                                    callback(new Error('请输入正确的联系方式'));
                                } else {
                                    callback();
                                }
                            }, trigger: 'blur'}
                        ],
                        account: [
                            {required: true,
                                validator: (rule, value, callback) => {
                                    if (!(/^[a-zA-Z0-9]+$/.test(value))) {
                                        callback(new Error('请输入字母或数字'));
                                    } else {
                                        callback();
                                    }
                                }, trigger: 'blur'},
                            { min: 6, max: 12, message: '长度在6-12个字符', trigger: 'blur' }
                        ],
                        password: [
                            {
                                required: true,
                                validator: (rule, value, callback) => {
                                    if (!(/^.{6,12}$/.test(value))) {
                                        callback(new Error('请输入6-12位密码'));
                                    } else {
                                        if (this.addData.password !== '') {
                                            this.$refs.dialog1.validateField('password2');
                                        }
                                        callback();
                                    }
                                }, trigger: 'blur'
                            }
                        ],
                        password2: [
                            {
                                required: true, validator: (rule, value, callback) => {
                                if (value === '') {
                                    callback(new Error('请再次输入密码'));
                                } else if (value !== this.addData.password) {
                                    callback(new Error('两次输入密码不一致!'));
                                } else {
                                    callback();
                                }
                            }, trigger: 'blur'
                            }
                        ]
                    },//对话框1验证规则
                    rules2: {
                        name: [
                            {required: true,validator: (rule, value, callback) => {
                                if (!(/^[\u4E00-\u9FA5\uf900-\ufa2d]{2,4}$/.test(value))) {
                                    callback(new Error('请输入正确的姓名'));
                                } else {
                                    callback();
                                }
                            }, trigger: 'blur'}
                        ],
                        phone: [
                            {required: true,validator: (rule, value, callback) => {
                                if (!(/^1[34578]\d{9}$/.test(value))) {
                                    callback(new Error('请输入正确的联系方式'));
                                } else {
                                    callback();
                                }
                            }, trigger: 'blur'}
                        ],
                        account: [
                            {required: true,
                                validator: (rule, value, callback) => {
                                    if (!(/^[a-zA-Z0-9]+$/.test(value))) {
                                        callback(new Error('请输入字母或数字'));
                                    } else {
                                        callback();
                                    }
                                }, trigger: 'blur'},
                            { min: 6, max: 12, message: '长度在6-12个字符', trigger: 'blur' }
                        ],
                        password: [
                            {
                                required: false,
                                validator: (rule, value, callback) => {
                                // console.log(value===null)
                                    if(value !== null && value !==''){
                                        if (!(/^.{6,12}$/.test(value))) {
                                            callback(new Error('请输入6-12位密码'));
                                        } else {
                                            if (this.editData.password !== '') {
                                                this.$refs.dialog2.validateField('password2');
                                            }
                                            callback();
                                        }
                                    }else{
                                        callback();
                                    }

                                }, trigger: 'blur'
                            }
                        ],
                        password2: [
                            {
                                required: false, validator: (rule, value, callback) => {
                                // if (value === '') {
                                //     callback(new Error('请再次输入密码'));
                                // } else
                                console.log(value,typeof(value),typeof(value) !== 'undefined',"b:"+this.editData.password)
                                console.log(this.editData.password)
                                if((this.editData.password !== null && this.editData.password !== '') || typeof(value) !== 'undefined' && value !==  null){
                                    if ( value !== this.editData.password) {
                                        callback(new Error('两次输入密码不一致!'));
                                    } else {
                                        callback();
                                    }
                                }else{
                                    callback();
                                }

                            }, trigger: 'blur'
                            }
                        ]
                    },//对话框2验证规则
                    rules3: {
                        roles: [
                            {required: true, message: '请选择角色', trigger: 'change'}
                        ]
                    },//对话框3验证规则
                    MANAGE_STATUS:{'正常':"1",'无效':"0"},//账户状态
                }
            },
            mounted: function () {
                this.loadTable1();
            },
            methods: {
//                表1 冻结 解结
                table1FreezeUnfreeza(id,stutes){
                    let _this = this;
                    let isFreeze = stutes === _this.MANAGE_STATUS.无效;
                    this.$confirm(isFreeze ? '确定冻结' : '确定解冻?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        $.dyAjax('/manage/freezeUnfreeza', {id: id, status: stutes}, function (data) {
                            _this.$message({message: '操作成功', type: 'success'});
                            _this.loadTable1();
                        });
                    }).catch(() => {
                    });
                },
                //编辑
                table1Edit(index, row) {
                    let _this = this;
                    $.dyAjax('/manage/get/' + row.id, {}, function (data) {
                        _this.editData = 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('/manage/addManage', _this.addData, 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);
                                    });
                                }

                            });
                        } else {
                            return false;
                        }
                    });
                },
                //加载表数据
                loadTable1: function (callback) {
                    var _this = this;
                    $.dyAjax('/manage/page1', this.con, function (data) {
                        callback && callback(data);
                        console.log(data)
                        _this.tableData = data.list;
                        _this.con.total = data.total;
                        _this.con.currentPage = data.pageNum;
                        _this.con.pageSize = data.pageSize;
                    });
                },
                //删除
                table1Del: function (index, row) {
                    let _this = this;
                    console.log(row);
                    this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        $.dyAjax('/manage/del', {id: row.id}, function (data) {
                            _this.loadTable1(function () {
                                _this.$message({
                                    type: 'success',
                                    message: '删除成功!'
                                });
                            });
                        });
                    }).catch(() => {
                    });
                },
                //分配角色
                table1Role: function (index, row) {
                    let _this = this;
                    $.dyAjax('/role/getAll', {}, function (data) {
                        var list=row.roleList;
                        ids=[];
                        for(var i=0;i<list.length;i++){
                            ids.push(list[i].id);
                        }
                        console.log(ids);
                        _this.roleIds=data;
                        _this.roleData.roles=ids;
                        _this.roleData.id=row.id;
                        console.log(_this.roleData.id);
                        _this.dialog3Visible = true;
                    }, 'get');
                },
                //对话框2 编辑提交事件
                dialog2Sub: function (formName) {
                    var _this = this;
                    console.log(this.editData);
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            $.dyAjax('/manage/update', _this.editData, function (data) {
                                _this.loadTable1(function () {
                                    _this.$message({
                                        message: '成功',
                                        type: 'success'
                                    });
                                    _this.dialog2Visible = false;
                                    _this.jsonClear(_this.editData);
                                });
                            });
                        } else {
                            return false;
                        }
                    });
                },
                //对话框3 编辑提交事件
                dialog3Sub: function (formName) {
                    var _this = this;
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            console.log(_this.roleData);
                            $.dyAjax('/manage/saveRole', {id:_this.roleData.id,roles:_this.roleData.roles.join(',')}, function (data) {
                                _this.loadTable1(function () {
                                    _this.$message({
                                        message: '成功',
                                        type: 'success'
                                    });
                                    _this.dialog3Visible = false;
                                    _this.jsonClear(_this.roleData.roles);
                                    _this.jsonClear(_this.roleData.id);
                                },'post');
                            });
                        } else {
                            return false;
                        }
                    });
                },

                closeAddDialog1: function(){
                    this.$refs['dialog1'].resetFields()
                },

                closeEditDialog2 :function () {
                    this.$refs['dialog2'].resetFields()
                },



            },
        };
        var app = Vue.extend(option);
        new app().$mount('#app');
    });
</script>
</html>