var a = 'handler/StaffAdminHandler.ashx';
var i = "Modules/html/StaffForm.htm";
$(function() {
    var W = {
        width: $(window).width(),
        height: $(window).height()
    };
    b.init(W);
    x.init();
    autoResize({
        dataGrid: '#staffGird',
        gridType: 'datagrid',
        callback: e.databind,
        height: 36,
        width: 230
    });
    $('#btnAddStaff').click(K.AddStaff);
    $('#btnEditStaff').click(K.EditStaff);
    $('#btnDeleteStaff').click(K.DeleteStaff);
    $('#btnMoveTo').click(K.MoveTo);
    $('#btnExport').click(K.Export);
    $('#btnRefresh').click(function() {
        e.reload()
    });
    $(window).resize(function() {
        W = {
            width: $(window).width(),
            height: $(window).height()
        };
        b.resize(W)
    })
});
var b = {
    init: function(W) {
        $('#layout').width(W.width - 4).height(W.height - 4).layout();
        var X = $('#layout').layout('panel', 'center');
        X.panel({
            onResize: function(Y, Z) {
                $('#staffGird').datagrid('resize', {
                    width: Y - 6,
                    height: Z - 36
                })
            }
        })
    },
    resize: function(W) {
        b.init(W);
        $('#layout').layout('resize')
    }
};
var x = {
    init: function() {
        $('#organizeTree').tree({
            lines: true,
            url: 'handler/OrganizeAdminHander.ashx?action=treedata',
            animate: true,
            onLoadSuccess: function(W, X) {
                $('body').data('depData', X)
            },
            onClick: function(W) {
                var X = W.id;
                $('#staffGird').datagrid('load', {
                    organizeId: X
                })
            }
        })
    },
    data: function(W) {
        var X = JSON.stringify($('body').data('depData'));
        if (W === '1') {
            X = '[{"id":0,"text":"请选择组织机构"},' + X.substr(1)
        };
        return JSON.parse(X)
    }
};
var e = {
    databind: function(W) {
        $('#staffGird').datagrid({
            url: "handler/StaffAdminHandler.ashx",
            title: "员工（职员）列表",
            loadMsg: "正在加载员工（职员）数据，请稍等...",
            width: W.width,
            height: W.height,
            idfield: 'ID',
            singleSelect: true,
            striped: true,
            rownumbers: true,
            rowStyler: function(X, Y) {
                if (Y.ENABLED <= 0 && Y.USERID == "") {
                    return 'color:#999;'
                };
                if (Y.ENABLED <= 0 && Y.USERID != "") {
                    return 'color:#999;font-weight:bold;'
                };
                if (Y.USERID != "") {
                    return 'font-weight:bold;'
                }
            },
            columns: [[{
                field: 'ck',
                checkbox: true,
                rowspan: 2
            },
            {
                title: '主键',
                field: 'ID',
                hidden: true,
                rowspan: 2
            },
            {
                title: '编号',
                field: 'CODE',
                width: 100,
                rowspan: 2
            },
            {
                title: '姓名',
                field: 'REALNAME',
                width: 100,
                rowspan: 2
            },
            {
                title: '性别',
                field: 'GENDER',
                width: 50,
                rowspan: 2,
                formatter: function(X, Y, Z) {
                    if (Y.GENDER === '男') {
                        return '<img src="/css/icon/user_b.png" alt="男" title="男" />'
                    } else if (Y.GENDER === '女') {
                        return '<img src="/css/icon/user_green.png" alt="女" title="女" />'
                    } else {
                        return '<img src="/css/icon/question_button.png" alt="性别未知" title="未设置性别" />'
                    }
                }
            },
            {
                title: '联系方式',
                colspan: 4
            },
            {
                title: '有效',
                field: 'ENABLED',
                width: 50,
                align: 'center',
                formatter: imgcheckbox,
                rowspan: 2
            },
            {
                title: '描述',
                field: 'DESCRIPTION',
                width: 260,
                rowspan: 2
            },
            {
                title: 'UserId',
                field: 'USERID',
                hidden: true,
                rowspan: 2
            }], [{
                title: '出生日期',
                field: 'BIRTHDAY',
                align: "center",
                width: 90,
                rowspan: 1
            },
            {
                title: '手机号码',
                field: 'MOBILE',
                width: 120,
                rowspan: 1
            },
            {
                title: '办公电话',
                field: 'OFFICEPHONE',
                width: 120,
                rowspan: 1
            },
            {
                title: '邮箱地址',
                field: 'EMAIL',
                width: 150,
                rowspan: 1
            }]],
            onLoadSuccess: function(X) {
                if (X.rows.length > 0) {
                    $('#staffGird').datagrid("selectRow", 0)
                }
            }
        })
    },
    reload: function() {
        $('#staffGird').datagrid('clearSelections').datagrid('reload')
    },
    selectRow: function() {
        return $('#staffGird').datagrid('getSelected')
    }
};
var imgcheckbox = function(W, X, Y) {
    return W ? '<img src="/css/icon/bullet_tick.png" alt="正常" title="正常" />': '<img src="/css/icon/bullet_minus.png" alt="禁用" title="禁用" />'
};
var K = {
    initData: function(W) {
        top.$('#txt_Education,#txt_Degree,#txt_Title,#txt_TitleLevel,#txt_WorkingProperty,#txt_Party,#txt_Gender').combobox({
            panelHeight: 'auto'
        });
        top.$('#txt_Birthday,#txt_TitleDate,#txt_WorkingDate,#txt_DimissionDate,#txt_JoinInDate').datebox({
            formatter: function(Y) {
                return Y.getFullYear() + '-' + (Y.getMonth() + 1) + '-' + Y.getDate()
            },
            arser: function(Y) {
                return new Date(Date.parse(Y.replace(/-/g, "/")))
            }
        });
        var X = W || 0;
        top.$('#txt_OrganizeId').combotree({
            data: x.data('1'),
            valuefield: 'ID',
            textField: 'text',
            value: X
        });
        s.bindCategory('txt_Gender', 'Gender');
        s.bindCategory('txt_Education', 'Education');
        s.bindCategory('txt_WorkingProperty', 'WorkingProperty');
        s.bindCategory('txt_Degree', 'Degree');
        s.bindCategory('txt_Gender', 'Gender');
        s.bindCategory('txt_Title', 'Title');
        s.bindCategory('txt_TitleLevel', 'TitleLevel');
        s.bindCategory('txt_Nationality', 'Nationality');
        s.bindCategory('txt_Party', 'PoliticalStatus');
        top.$('#staffTab').tabs({
            onSelect: function() {
                top.$('.validatebox-tip').remove()
            }
        });
        top.$('#txt_passSalt').val(randomString())
    },
    AddStaff: function() {
        if ($(this).linkbutton('options').disabled == true) {
            return false
        };
        var W = top.$.hDialog({
            href: i + '?v=' + Math.random(),
            width: 680,
            height: 410,
            title: '新增员工（职员）',
            iconCls: 'icon-vcard_add',
            onLoad: function() {
                var X = $('#organizeTree').tree('getSelected');
                var Y = 0;
                if (X) {
                    Y = X.id || 0
                };
                top.$('#chk_Enabled').attr("checked", true);
                K.initData(Y)
            },
            closed: false,
            submit: function() {
                var X = top.$('#staffTab').tabs('getSelected');
                var Y = top.$('#staffTab').tabs('getTabIndex', X);
                if (top.$('#uiform').form('validate')) {
                    var Z = top.$('#txt_OrganizeId').combobox('getValue');
                    var a0 = 'action=AddStaff&vOrganizeId=' + Z + '&' + top.$('#uiform').serialize();
                    $.ajaxjson(a, a0, 
                    function(a1) {
                        if (a1.Success) {
                            msg.ok('添加成功');
                            e.reload();
                            W.dialog('close')
                        } else {
                            if (a1.Data == -2) {
                                msg.error('用户名已存在，请更改用户名。');
                                if (Y > 0) {
                                    top.$('#staffTab').tabs('select', 0)
                                };
                                top.$('#txt_username').select()
                            } else {
                                MessageOrRedirect(a1)
                            }
                        }
                    })
                } else {
                    if (Y > 0) top.$('#staffTab').tabs('select', 0)
                }
            }
        });
        return false
    },
    EditStaff: function() {
        if ($(this).linkbutton('options').disabled == true) {
            return false
        };
        var W = e.selectRow();
        if (W) {
            var X = top.$.hDialog({
                href: i + '?v=' + Math.random(),
                title: '修改员工（职工）',
                iconCls: 'icon-vcard_edit',
                width: 680,
                height: 410,
                onLoad: function() {
                    var Y = $('#organizeTree').tree('getSelected');
                    var Z = 0;
                    if (Y) {
                        Z = Y.id || 0
                    };
                    K.initData(Z);
                    var a0 = 'action=GetEntity&KeyId=' + W.ID;
                    $.ajaxjson(a, a0, 
                    function(a1) {
                        if (a1) {
                            top.$('#txt_Code').val(a1.Code);
                            top.$('#txt_RealName').val(a1.RealName);
                            top.$('#txt_Birthday').datebox('setValue', a1.Birthday);
                            top.$('#txt_Gender').combobox('setValue', a1.Gender);
                            top.$('#txt_Age').val(a1.Age);
                            top.$('#txt_Major').val(a1.Major);
                            top.$('#txt_School').val(a1.School);
                            top.$('#txt_Education').combobox('setValue', a1.Education);
                            top.$('#txt_Degree').combobox('setValue', a1.Degree);
                            top.$('#txt_Title').combobox('setValue', a1.Title);
                            top.$('#txt_TitleLevel').combobox('setValue', a1.TitleLevel);
                            top.$('#txt_TitleDate').datebox('setValue', a1.TitleDate);
                            top.$('#txt_WorkingProperty').combobox('setValue', a1.WorkingProperty);
                            top.$('#txt_WorkingDate').datebox('setValue', a1.WorkingDate);
                            top.$('#txt_IdentificationCode').val(a1.IdentificationCode);
                            top.$('#txt_BankCode').val(a1.BankCode);
                            top.$('#txt_JoinInDate').datebox('setValue', a1.JoinInDate);
                            top.$('#txt_Email').val(a1.Email);
                            top.$('#txt_Mobile').val(a1.Mobile);
                            top.$('#txt_ShortNumber').val(a1.ShortNumber);
                            top.$('#txt_QICQ').val(a1.QICQ);
                            top.$('#txt_OfficeZipCode').val(a1.OfficeZipCode);
                            top.$('#txt_OfficePhone').val(a1.OfficePhone);
                            top.$('#txt_OfficeFax').val(a1.OfficeFax);
                            top.$('#txt_OfficeAddress').val(a1.OfficeAddress);
                            top.$('#txt_NativePlace').val(a1.NativePlace);
                            top.$('#txt_HomeZipCode').val(a1.HomeZipCode);
                            top.$('#txt_HomeFax').val(a1.HomeFax);
                            top.$('#txt_Party').combobox('setValue', a1.Party);
                            top.$('#txt_Nation').val(a1.Nation);
                            top.$('#txt_Nationality').combobox('setValue', a1.Nationality);
                            top.$('#txt_HomePhone').val(a1.HomePhone);
                            top.$('#txt_Telphone').val(a1.Telphone);
                            top.$('#txt_HomeAddress').val(a1.HomeAddress);
                            top.$('#txt_DimissionDate').datebox('setValue', a1.DimissionDate);
                            top.$('#txt_DimissionWhither').val(a1.DimissionWhither);
                            top.$('#chk_IsDimission').attr('checked', a1.IsDimission == "1");
                            top.$('#txt_DimissionCause').val(a1.DimissionCause);
                            top.$('#chk_Enabled').attr('checked', a1.Enabled == "1");
                            top.$('#txt_Description').val(a1.Description)
                        }
                    })
                },
                submit: function() {
                    if (top.$('#uiform').validate().form()) {
                        var Y = top.$('#txt_OrganizeId').combobox('getValue');
                        var Z = 'action=EditStaff&vOrganizeId=' + Y + '&KeyId=' + W.ID + '&' + top.$('#uiform').serialize();
                        $.ajaxjson(a, Z, 
                        function(a0) {
                            if (a0.Success) {
                                msg.ok(a0.Message);
                                X.dialog('close');
                                e.reload()
                            } else {
                                MessageOrRedirect(a0)
                            }
                        })
                    }
                }
            })
        } else {
            msg.warning('请选择待修改的员工（职工）!');
            return false
        };
        return false
    },
    DeleteStaff: function() {
        if ($(this).linkbutton('options').disabled == true) {
            return false
        };
        var W = e.selectRow();
        if (W != null) {
            var X = 'action=DeleteStaff&KeyId=' + W.ID;
            $.messager.confirm('询问提示', '确定要删除选中的员工（职员）吗？', 
            function(Y) {
                if (Y) {
                    $.ajaxjson(a, X, 
                    function(Z) {
                        if (Z.Success) {
                            msg.ok(Z.Message);
                            e.reload()
                        } else {
                            MessageOrRedirect(Z)
                        }
                    })
                } else {
                    return false
                }
            })
        } else {
            msg.warning('请选择要删除的员工（职员）!');
            return false
        };
        return false
    },
    MoveTo: function() {
        if ($(this).linkbutton('options').disabled == true) {
            return false
        };
        var W = e.selectRow();
        if (W != null) {
            var X = top.$.hDialog({
                max: false,
                width: 300,
                height: 500,
                title: '移动员工（职员） ━ ' + W.REALNAME,
                iconCls: 'icon-arrow_nsew',
                content: '<ul id="orgTree"></ul>',
                submit: function() {
                    var Y = top.$('#orgTree').tree('getSelected');
                    if (Y) {
                        $.ajaxtext(a, 'action=MoveTo&staffId=' + W.ID + '&organizeId=' + Y.id, 
                        function(Z) {
                            if (Z > 0) {
                                msg.ok('移动成功！');
                                e.reload();
                                X.dialog('close')
                            } else if (Z == 0) {
                                msg.warning('移动失败！')
                            } else {
                                msg.error('移动出错！')
                            }
                        })
                    } else {
                        msg.warning('请选择要移动的节点！')
                    }
                }
            });
            top.$(X).hLoading();
            top.$('#orgTree').tree({
                data: x.data('0'),
                valuefield: 'ID',
                textField: 'text',
                animate: true,
                lines: true,
                onLoadSuccess: function(Y, Z) {
                    top.$.hLoading.hide();
                },
                onClick: function(Y) {
                    var Z = Y.id
                }
            })
        } else {
            msg.warning('请选择要移动的员工（职员）!');
            return false
        };
        return false
    },
    Export: function() {
        if ($(this).linkbutton('options').disabled == true) {
            return false
        };
        var W = new ExportExcel('staffGird');
        W.go('PISTAFF', 'SORTCODE');
        return false
    }
};
var s = {
    bindCategory: function(W, X) {
        if (W == '' || X == '') {
            return false
        };
        top.$('#' + W).combobox({
            url: 'Modules/handler/DataItemAdminHandler.ashx?action=GetCategory&categorycode=' + X,
            method: 'get',
            valueField: 'ITEMVALUE',
            textField: 'ITEMNAME',
            editable: false,
            panelHeight: 'auto'
        });
        return false
    }
}