$(function () {
    $("#jqGrid").jqGrid({
        url: '../sys/user/list',
        datatype: "json",
        colModel: [
            {label: '用户ID', name: 'userId', index: "user_id", key: true, hidden: true},
            {label: '用户名', name: 'username', width: 75},
            {label: '所属商户', name: 'merchName', width: 75,
                formatter: function (value) {
                    if (value == '' || value == null) {
                        return '-';
                    }else {
                        return value;
                    }
                }
            },
            {label: '所属第三方商户', name: 'thirdPartyMerchCode', width: 75,
                formatter: function (value) {
                    if (value == '' || value == null) {
                        return '-';
                    }else {
                        return value;
                    }
                }
            },
            {label: '所属门店', name: 'storeName', width: 75,
                formatter: function (value) {
                    if (value == '' || value == null) {
                        return '-';
                    }else {
                        return value;
                    }
                }
            },
            {label: '所属角色', name: 'roleName', width: 75},
            {label: '角色类型', name: 'roleType', width: 75,
                formatter: function (value) {
                    if (value == '1') {
                        return '操作员';
                    }else if (value == '2') {
                        return '店员';
                    }else if (value == '3') {
                        return '一级商户';
                    }else if (value == '4') {
                        return '第三方商户';
                    }else {
                        return value;
                    }
                }
            },
            {label: '邮箱', name: 'email', width: 90},
            {label: '手机号', name: 'mobile', width: 100},
            {
                label: '状态', name: 'status', width: 80, formatter: function (value) {
                return value === 0 ?
                    '<span class="label label-danger">禁用</span>' :
                    '<span class="label label-success">正常</span>';
            }
            },
            {
                label: '创建时间', name: 'createTime', index: "create_time", width: 80, formatter: function (value) {
                return transDate(value);
            }
            }],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            username: null
        },
        showList: true,
        title: null,
        roleList: {},
        user: {
            status: 1,
            deptName: '',
            roleIdList: []
        },
        ruleValidate: {
            /*username: [
                {required: true, message: '姓名不能为空', trigger: 'blur'}
            ],
            email: [
                {required: true, message: '邮箱不能为空', trigger: 'blur'},
                {type: 'email', message: '邮箱格式不正确', trigger: 'blur'}
            ],
            mobile: [
                {required: true, message: '手机号不能为空', trigger: 'blur'}
            ]*/
        },
        storeList: [],
        merchList: [],
        thirdMerchantBizList: [],
        isMerchShow:false,
        isStoreShow:false,
        isThirdMerchShow:false
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增(默认密码：111111)";
            vm.roleList = {};
            vm.user = {status: 1, roleIdList: [], deptId: '', deptName: '', storeId: '', roleType: 2,merchSn:''};
            vm.storeList = [];
            vm.merchList = [];
            vm.thirdMerchantBizList = [];
            //获取角色信息
            this.getRoleList();
            // vm.getDept();
            //vm.getStoreList();
            vm.changeRole(2);
            vm.getMerchList();
        },
        changeRole:function (opt) {
           var value = opt;
           if(value == 1){
               vm.isMerchShow = false;
               vm.isStoreShow = false;
               vm.isThirdMerchShow = false;
               vm.user.merchSn = '';
               vm.user.storeId = '';
               vm.user.thirdPartyMerchCode = '';
           }
           if(value == 2){//店员
               vm.isMerchShow = true;
               vm.isThirdMerchShow = true;
               vm.isStoreShow = true;
           }
            if(value == 3){//一级商户
                vm.isMerchShow = true;
                vm.isThirdMerchShow = false;
                vm.isStoreShow = false;
                vm.user.storeId = '';
                vm.user.thirdPartyMerchCode = '';
            }
            if(value == 4){//第三方商户
                vm.isMerchShow = true;
                vm.isThirdMerchShow = true;
                vm.isStoreShow = false;
                vm.user.storeId = '';
            }
        },
        getStoreList: function() {
            $.get("../store/queryAll", function (r) {
                vm.storeList = r.list;
            });
        },
        getStoresByMerch: function (opt) {
            var value = opt.value;
            vm.getThirdMerchantBizList(value);
        },
        getMerchList: function() {
            $.get("../merch/queryAll", function (r) {
                vm.merchList = r.list;
            });
        },
        getDept: function () {
            //加载部门树
            $.get("../sys/dept/list", function (r) {
                ztree = $.fn.zTree.init($("#deptTree"), setting, r.list);
                var node = ztree.getNodeByParam("deptId", vm.user.deptId);
                if (node != null) {
                    ztree.selectNode(node);
                    vm.user.deptName = node.name;
                }
            })
        },
        update: function () {
            var userId = getSelectedRow();
            if (userId == null) {
                return;
            }

            vm.showList = false;
            vm.title = "修改";
            vm.thirdMerchantBizList = [];

            $.get("../sys/user/info/" + userId, function (r) {
                vm.user = r.user;
                //获取角色信息
                // vm.getStoreList();
                vm.getStoresByMerchAndUpd(r.user.merchSn);
                vm.getRoleList();
                vm.changeRole(r.user.roleType);
                vm.getMerchList();
                // vm.getDept();
            });

        },
        getStoresByMerchAndUpd: function (merchSn) {
            $.get("../store/getStoresByMerch?merchSn=" + merchSn, function (r) {
                vm.storeList = r.list;
            });
        },
        getStoresByThirdMerch:function (opt) {
            var value = opt.value;
            $.get("../store/getStoresByThirdMerch?thirdPartyMerchCode=" + value, function (r) {
                vm.storeList = r.list;
            });
        },
        getThirdMerchantBizList: function(merchSn) {
            $.get("../thirdmerchantbiz/queryAll?merchSn=" + merchSn, function (r) {
                vm.thirdMerchantBizList = r.list;
            });
        },
        del: function () {
            var userIds = getSelectedRows();
            if (userIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../sys/user/delete",
                    contentType: "application/json",
                    data: JSON.stringify(userIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function (event) {
            var url = vm.user.userId == null ? "../sys/user/save" : "../sys/user/update";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.user),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        getRoleList: function () {
            $.get("../sys/role/select", function (r) {
                vm.roleList = r.list;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'username': vm.q.username},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        deptTree: function () {
            openWindow({
                title: "选择部门",
                area: ['300px', '450px'],
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.user.deptId = node[0].deptId;
                    vm.user.deptName = node[0].name;

                    layer.close(index);
                }
            });
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});