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') {
            var F_UserID = $("#F_UserID").val();
            var F_CreateBy = data.F_CreateBy;
            layer.open({
                type: 2,
                anim: 4,
                scrollbar: false,
                content: '/SystemManage/NoticeEdit/?noticeId=' + data.F_NoticeId + "&editType=2",
                area: ['800px', '550px'],
                id: 'NoticeEdit',
                title: '编辑公告'
                , end: function () {
                    refushTreeAndData();
                }
            });
            // if (F_UserID == F_CreateBy) {
                
            // }
            // else {
            //     layui.layer.alert("无权限修改其他用户的公告!", {
            //         icon: 2
            //     });
            // }
        }
        else if (obj.event === 'delete') {
            //layer.msg('ID：' + data.ProjectKey + ' 的查看操作'); 
            var F_UserID1 = $("#F_UserID").val();
            var F_CreateBy1 = data.F_CreateBy;
            
            layer.confirm('确认要删除该行公告么？', function (index) {
                $.ajax({
                    url: "/SystemManage/DeleteNoticeData",
                    type: "get",
                    async: false,//同步请求
                    contentType: "application/json",
                    dataType: "text",
                    cache: false,
                    data: "noticeId=" + data.F_NoticeId,
                    success: function (thisResult, result) {

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

                                    layer.close(index);
                                    refushTreeAndData();
                                }
                            });
                        } else {
                            layui.layer.alert("无权限删除其他用户的公告!", {
                                icon: 2
                            });

                        }
                    },
                    error: function (error) {
                        return false;
                    }
                });
                layer.close(index);
            });
            // if (F_UserID1 == F_CreateBy1) {  }
            // else {
            //     layui.layer.alert("无权限删除其他用户的公告!", {
            //         icon: 2
            //     });
            // }
        }
        else if (obj.event === 'start') {
            var t2 = $("#txt2").val();
            var t3 = $("#txt3").val();
            var F_UserID2 = $("#F_UserID").val();
            var F_CreateBy2 = data.F_CreateBy;
            layer.confirm(t3, { icon: 3, title: "提示：" }, function (index) {

                var timeno = new Date().getTime();
                $.ajax({
                    type: 'get',
                    url: '/SystemManage/SetNoticeState/?id=' + data.F_NoticeId + '&timeno=' + timeno,
                    dataType: 'html',
                    success: function (res) {
                        if (res === "success") {
                            //执行重载
                            table.reload('recordtable', {
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                }
                                , where: {
                                    NowDateTime: new Date()
                                }
                            });
                        }
                        else {
                            var t1 = $("#txt1").val();
                            layer.msg(t1);
                        }
                    }
                });

                layer.close(index);
            });
            // if (F_UserID2 == F_CreateBy2) {
                
            // }
            // else {
            //     layui.layer.alert("无权限启用其他用户的公告!", {
            //         icon: 2
            //     });
            // }
        }
        else if (obj.event === 'view') {
            layer.open({
                type: 2,
                anim: 4,
                scrollbar: false,
                content: '/SystemManage/NoticeView/?noticeId=' + data.F_NoticeId,
                area: ['800px', '550px'],
                id: 'NoticeEdit',
                title: '查看公告'
                , end: function () {
                    refushTreeAndData();
                }
            });
        }
    });
    $ = layui.$, active = {

        //新增
        rowAdd: function () {
            parentId = 0;
            layer.open({
                type: 2,
                anim: 4,
                scrollbar: false,
                content: '/SystemManage/NoticeEdit/?noticeId=' + parentId + "&editType=1",
                area: ['800px', '550px'],
                id: 'NoticeEdit',
                title: '新建公告'
                , end: function () {

                    refushTreeAndData();

                }
            });
        }

    };

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


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

            }
        }); 
    }
});