
layui.use(['table', 'jquery'], function () {
    var table = layui.table;
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer;
        //$ = layui.jquery;
    //监听表格复选框选择
    table.on('checkbox(demo)', function (obj) {
        console.log(obj) 
    });
    //监听工具条
    table.on('tool(demo)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            layer.open({
                type: 2,
                anim: 4,
                scrollbar: false,
                content: '/HrEmployeeBase/EmployeeBaseEdit/?employeeId=' + data.F_EmployeeId + "&editType=2",
                area: ['800px', '430px'],
                id: 'EmployeeBaseEdit',
                title: '编辑通讯录'
                , end: function () {
                    refushTreeAndData();
                }
            });
        }
        else if (obj.event === 'delete') {
            //layer.msg('ID：' + data.ProjectKey + ' 的查看操作');
            layer.confirm('确认要删除该行数据么？', function (index) {
                $.ajax({
                    url: "/HrEmployeeBase/DeletehremployData",
                    type: "get",
                    async: false,//同步请求
                    contentType: "application/json",
                    dataType: "text",
                    cache: false,
                    data: "employeeId=" + data.F_EmployeeId,
                    success: function (thisResult, result) {

                        if (thisResult === "True") {
                            layer.open({
                                content: '删除成功'
                                 , icon: 1
                                 ,offset: '40%'
                                 , btn: ['确定']
                                 , yes: function (index, layero) {

                                     layer.close(index);
                                     refushTreeAndData();
                                 }
                            });
                        } else {
                            layui.layer.alert("删除失败!", {
                                icon: 2
                                ,offset: '40%'
                            });

                        }
                    },
                    error: function (error) {
                        return false;
                    }
                });
                layer.close(index);
            });
        }
    });
    var $ = layui.$, active = {
        //查询按钮
        searchInfo: function () {
            var realname = $('#realname');
            var phone = $('#phone');
            //执行重载
            table.reload('recordtable', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {

                    realname: realname.val(),
                    phone: phone.val()

                }
            });
        }
        ,
        //新增
        rowAdd: function () {
            var parentId = $("#selectTypeId").val();
            if (parentId !== null && parentId !== "") {
                layer.open({
                    type: 2,
                    anim: 4,
                    scrollbar: false,
                    content: '/HrEmployeeBase/EmployeeBaseEdit/?deptId=' + parentId + "&editType=1",
                    area: ['800px', '430px'],
                    id: 'EmployeeBaseEdit',
                    title: '新增通讯录'
                    , end: function () {

                        refushTreeAndData();

                    }
                });
            }
            else {
                layui.layer.alert("请先选择左侧树节点!", {
                    icon: 2
                    ,offset: '40%'
                });
            }
        }
        //保存Excel
        , SaveExcel: function () {
            layer.open({
                type: 2,
                anim: 4,
                scrollbar: false,
                content: '/HrEmployeeBase/EmployyFromExcel',
                area: ['800px', '430px'],
                id: 'EmployeeBaseEdit',
                title: '保存文件'
                , end: function () {

                    refushTreeAndData();

                }
            });
        }
        //删除
        , rowDelete: function () {

            var checkStatus = table.checkStatus('recordtable')
                , data = checkStatus.data;
            if (data.length === 0) {
                layui.layer.alert("请先选中要操作的数据行！", {
                    icon: 3
                    ,offset: '40%'
                });
            }
            else {

                layer.confirm('确认要删除该行数据么？', function (index) {

                    var strId = "";
                    for (var i = 0; i < data.length; i++) {
                        if (i === 0) {
                            strId += +data[i].F_EmployeeId;
                        }
                        else {
                            strId += "," + data[i].F_EmployeeId;
                        }
                    }

                    $.ajax({
                        url: "/HrEmployeeBase/DeleteEmployeeData",
                        type: "get",
                        async: false,//同步请求
                        contentType: "application/json",
                        dataType: "text",
                        cache: false,
                        data: "employeeId=" + strId,
                        success: function (thisResult, result) {

                            if (thisResult === "True") {
                                layer.open({
                                    content: '删除成功'
                                    , icon: 1
                                    ,offset: '40%'
                                    , btn: ['确定']
                                    , yes: function (index, layero) {

                                        layer.close(index);
                                        refushTreeAndData();

                                    }
                                });
                            } else {
                                layui.layer.alert("删除失败!", {
                                    icon: 2
                                    ,offset: '40%'
                                });

                            }
                        },
                        error: function (error) {
                            return false;
                        }
                    });
                    layer.close(index);
                });
            }

        }
    };

    $('.layui-btn-group .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //单击左侧数列表时
    function zTreeOnClick(event, treeId, treeNode) {
        $("#selectTypeId").val(treeNode.code);
        selectNode = treeNode;
        //执行重载
        table.reload('recordtable', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
          , where: {
              NowDateTime: new Date(),
              deptId: treeNode.code

          }
        });

    };
    var zTreeObj;
    var selectNode;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        check:
        {
            enable: false,
            chkStyle: "checkbox",
            chkboxType: { "Y": "ps", "N": "ps" }
        },
        callback: {
            onClick: zTreeOnClick

        }

    };
    var treeNodes;
    $(document).ready(function () {

        $.ajax({
            url: "/HrEmployeeBase/GetDepartmentJsonModel",
            type: "get",
            contentType: "application/json",
            dataType: "text",
            cache: false,
            async: false,
            data: { "parentId": 0 },
            success: function (result, status) {
                treeNodes = eval('(' + result + ')');
            }
        });
        zTreeObj = $.fn.zTree.init($("#ztree"), setting, treeNodes);

    });


    function refushTreeAndData() {
        //执行重载
        table.reload('recordtable', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
          , where: {
              NowDateTime: new Date(),
              deptId: $("#selectTypeId").val()

          }
        });
        $.ajax({
            url: "/HrEmployeeBase/GetDepartmentJsonModel",
            type: "get",
            contentType: "application/json",
            dataType: "text",
            cache: false,
            async: false,
            data: { "parentId": 0 },
            success: function (result, status) {
                treeNodes = eval('(' + result + ')');
            }
        });
        zTreeObj = $.fn.zTree.init($("#ztree"), setting, treeNodes);
        zTreeObj.selectNode(selectNode, true, true);
    }
});