/**
 * Created by ly on 2019/4/25.
 */
var pageNumber = 1;
var form = '';
$(function(){
    $('#myModal').modal('hide');
    //先销毁表格
    $('#bootstrapTable').bootstrapTable({
        //表格高度
        height: getHeight(),
        method : 'get',
        url : path+ "/role/pageList",//请求路径
        striped : true, //是否显示行间隔色
        pageNumber : 1, //初始化加载第一页
        pagination : true,//是否分页
        sidePagination : 'server',//server:服务器端分页|client：前端分页
        pageSize : 10,//单页记录数
        pageList : [ 5, 10, 20, 30 ],//可选择单页记录数
        cache: false,
        paginationPreText : '上一页',
        paginationNextText : '下一页',
        queryParams : function(params) {//上传服务器的参数
            var temp = {//如果是在服务器端实现分页，limit、offset这两个参数是必须的
                limit : params.limit, // 每页显示数量
                offset : params.offset, // SQL语句起始索引
                page : (params.offset / params.limit) + 1, //当前页码
                roleId:$("#role_id").val(),
                roleName:$("#role_name").val(),
                effective : $("#effective option:selected").val(),
                creater:$("#creater").val(),
            };
            return temp;
        },
        columns : [{
            checkbox:true
            },
            {
            title:'序号',
            field:'no',
            formatter: function (value, row, index) {
                //获取每页显示的数量
                var pageSize = $('#bootstrapTable').bootstrapTable('getOptions').pageSize;
                //获取当前是第几页
                if(pageNumber == 1){
                    pageNumber = $('#bootstrapTable').bootstrapTable('getOptions').pageNumber;
                }
                //返回序号，注意index是从0开始的，所以要加上1
                return pageSize * (pageNumber - 1) + index + 1;
            }
        },
            {
                title:'角色名',
                field:'roleName',
            },
            {
                title:'备注',
                field:'remark',
                formatter:function (value,row,index) {
                    if(value ==null){
                        return "";
                    }else{
                        return value;
                    }
                }
            },
            {
                title:'是否有效',
                field:'effective',
                formatter: function (value, row, index) {
                    if(value ==1){
                        return '是'
                    }else if(value ==0){
                        return '否'
                    }
                }
            },
            {
                title:'创建时间',
                field:'createDate',
            },
            {
                title:'创建人',
                field:'creater',
            },

            {
                title:'操作',
                field:'roleId', formatter: function(value,row,index){
                    var editanddrop  = '';
                    if(row.isUpdate == 1){
                        editanddrop += '<button type="button" onclick="edit('+row.roleId+')" class="btn btn-info operBtns btn-sm" >编辑</button>';
                    }
                    if(row.isDelete == 1){
                        editanddrop += '<button type="button" onclick="drop('+row.roleId+')" class="btn btn-danger operBtns btn-sm">删除</button>';
                    }
                    return editanddrop;
                }
            }
        ],
        onLoadSuccess: function(){  //加载成功时执行
            $(".page-list").show();
            $("th").css({'text-align':'center','vertical-align':'middle'})
            $("td").css({'text-align':'center','vertical-align':'middle'})
        },
        //监听分页点击事件
        onPageChange: function(num, type) {
            pageNumber = num;
        },
        //选中单个复选框
        onCheck:function(row){
            var checks = $("#checks").val();
            $("#checks").val(checks+=row.roleId + ",");
        },
        //取消单个复选框
        onUncheck:function(row){
            var checks = $("#checks").val();
            checks = checks.replace(row.roleId + ",","");
            $("#checks").val(checks);
        },
        //全选
        onCheckAll:function(rows){
            $("#checks").val("");
            var checks = '';
            for(var i=0;i<rows.length;i++)
            {
                checks += rows[i].roleId + ","
            }
            $("#checks").val(checks);
        },
        //全不选
        onUncheckAll: function (rows) {
            $("#checks").val("");
        }
    });
});

//验证角色名不能重复
$("#re_roleName").blur(function(){
    var roleName = $("#re_roleName").val();
    var roleId = $("#re_roleId").val();
    if(roleId == '' && roleName != ''){
        $.ajax({
            type:'get',
            url:path+'/role/checkRoleName',
            data:{roleName:roleName},
            dataType:'json',
            success:function(data){
                if(data.code == 200){
                    toastr.warning(data.msg);
                    $("#re_roleName").val("");
                    $("#re_roleName").focus();
                }
            }
        })
    }
})
//编辑框回显
function edit(id){
    $("#myModalLabel").text("编辑");
    $.ajax({
        type: "post",
        url:path+ "/role/selectRole",
        data:{
            roleId:id
        },
        dataType:"json",
        success: function(data){
            $("#re_roleId").val(data.roleId);
            $("#re_roleName").val(data.roleName);
            $("#re_effective").find("option[value='"+data.effective+"']").attr("selected",true);
            $("#re_remark").val(data.remark);
        }
    });
    $('#myModal').modal('show')
}

//删除
function drop(id) {
    Common.confirm({
        title: "提示",
        message: "确定是否删除这条记录",
        operate: function (reselt) {
            if (reselt) {
                $.ajax({
                    type: "post",
                    url: path + "/role/delete",
                    data: {
                        roleId: id
                    },
                    async: false,
                    success: function (data) {
                        if ("success" == data.msg) {
                            toastr.success("删除成功!");
                            $("#checks").val("");
                            backToPage();
                        }
                    },
                    error: function () {
                        window.confirm("删除失败");
                    }
                })
            }
        }
    })
}
//新增框
function add() {
    initable();
    $("#myModalLabel").text('增加');
    $('#myModal').modal('show')
}
//提交更改
$('#btn_submit').click(function () {
    var roleName = $("#re_roleName").val();
    if(roleName != ''){
        var roleId = $("#re_roleId").val();
        var url = '';
        if(roleId == ''){
            url = path+"/role/add";
        }else{
            url = path+"/role/update";
        }
        $.ajax({
            type: "post",
            url:url,
            data:$("#updateaddform").serialize(),
            dataType:"json",
            success: function(data){
                if("success"==data.msg){
                    toastr.success('保存成功!');
                    $('#myModal').modal('hide');
                    backToPage();
                }else{
                    toastr.warning(data.msg);
                }
            }
        })
    }else{
        toastr.warning("角色名不能为空!");
    }
})
//初始化模态框
function initable(){
    $("#updateaddform")[0].reset();
    $("#re_roleId").val("");
}
//搜索
$('#queryBtn').click(function () {
    $("#checks").val("");
    refresh();
})
//获取table的高度
function getHeight() {
    return $(window).height() - 190;
}
//刷新表格
function refresh() {
    $('#bootstrapTable').bootstrapTable('refresh',{
        url : path+'/role/pageList'
    })
}
//清空
function clearForm(){
    $("#updateaddform")[0].reset();
}

//导出excel功能
function exportExcel(){
    var checks = $("#checks").val();
    if(checks != '') {
        checks = checks.substring(0, checks.length - 1);
        var url = path+"/role/export?roleName="+$("#role_name").val()+"&effective="+$("#effective").val()+"&creater="+$("#creater").val()+"&checks="+checks;
        window.location.href = url;
    }else{
        Common.confirm({
            title: "提示",
            message: "没有选中，您确定要按搜索栏条件导出?",
            operate: function (reselt) {
                if (reselt) {
                    var url = path + "/role/export?roleName=" + $("#role_name").val() + "&effective=" + $("#effective").val() + "&creater=" + $("#creater").val() + "&checks=" + checks;
                    window.open(url);
                }
            }
        })
    }
}

//监听关闭模态框刷新事件
$('#myModal1').on('hide.bs.modal', function () {
    window.location.reload();
});