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;
      helper.cookies.set('F_IDs', data.F_Id);
      if (F_UserID == F_CreateBy) {
        layer.open({
          type: 2,
          anim: 4,
          scrollbar: false,
          content: '/Tag/Edit',
          area: ['800px', '550px'],
          id: 'NoticeEdit',
          otherParams: {
            ids: data.F_ID
          },
          title: '修改标签'
          , end: function () {

            refushTreeAndData();

          }
        });
      }
      else {
        layui.layer.alert("无权限修改其他用户的公告!", {
          icon: 2
          ,offset: '40%'
        });
      }
    }

    else if (obj.event === 'delete') {
      //layer.msg('ID：' + data.ProjectKey + ' 的查看操作'); 
      var F_UserID1 = $("#F_UserID").val();
      var F_CreateBy1 = data.F_CreateBy;
      if (F_UserID1 == F_CreateBy1) {
        layer.confirm('确认要删除该记录吗？', function (index) {
          $.ajax({
            url: "/Tag/deletedata",
            type: "get",
            async: false,//同步请求
            contentType: "application/json",
            dataType: "text",
            cache: false,
            data: "id=" + data.F_Id,
            success: function (thisResult, result) {
              console.log(thisResult + result)

              if (eval('(' + thisResult + ')').msg === "删除成功") {
                layer.open({
                  content: '删除成功'
                  , icon: 1
                  , 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);
        });
      }
      else {
        layui.layer.alert("无权限删除其他用户的公告!", {
          icon: 2
          ,offset: '40%'
        });
      }
    }else if (obj.event === 'topBtn') {
      //layer.msg('ID：' + data.ProjectKey + ' 的查看操作'); 
      var F_UserID1 = $("#F_UserID").val();
      var F_CreateBy1 = data.F_CreateBy;
      if (F_UserID1 == F_CreateBy1) {
        layer.confirm('确认要置顶吗？', function (index) {
          $.ajax({
            url: "/Tag/setTopping",
            type: "get",
            async: false,//同步请求
            contentType: "application/json",
            dataType: "text",
            cache: false,
            data: "noticeid=" + data.F_Id,
            success: function (thisResult, result) {
              console.log(thisResult + result)

              if (result === "success") {
                layer.open({
                  content: '置顶成功'
                  , icon: 1
                  , 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);
        });
      }
      else {
        layui.layer.alert("无权限删除其他用户的公告!", {
          icon: 2
          ,offset: '40%'
        });
      }
    }

  });
  $ = layui.$, active = {

    //新增
    rowAdd: function () {
      debugger
      parentId = 0;
      layer.open({
        type: 2,
        anim: 4,
        scrollbar: false,
        content: '/Tag/Add',
        area: ['800px', '550px'],
        id: 'NoticeEdit',
        title: '新建标签'
        , end: function () {

          refushTreeAndData();

        }
      });
    },
    //查询按钮
    reload: function (obj) {
      //helper.cookies.set('keyWord', $("#keyWord").val());
      //alert(obj)
      //执行重载
      table.reload('recordtable', {
        page: {
          curr: 1 //重新从第 1 页开始
        }
        , where: {
          NowDateTime: new Date(),
          parentId: $("#selectTypeId").val(),
          key: $("#keyWord").val()
        }
        , done: function (res, curr, count) {
          var otext_ = $("#keyWord").val();

        }
      });
    }

  };

  $('.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(),
        key: $("#keyWord").val()

      }
    });
  }
});