{extend name="public:base" /}

{block name="body"}
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">帖子ID</label>
            <div class="layui-input-block">
              <input type="text" name="id" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">发帖人</label>
            <div class="layui-input-block">
              <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
              <input type="text" name="title" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
              <select name="sec">
			          <option value="">选择状态</option>
                <option value="1">正常</option>
                <option value="5">禁止</option>
                <option value="6">待审</option>
                <option value="2">置顶</option>
                <option value="3">加精</option>
                <option value="4">禁评</option>
              </select>
            </div>
          </div>
          <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-forum-list" lay-submit lay-filter="LAY-app-forumlist-search">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
          </div>
        </div>
      </div>
      
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-forum-list" data-type="add">添加</button>
        <button class="layui-btn layuiadmin-btn-forum-list" data-type="batchdel">删除</button>
        </div>
        <table id="LAY-app-forum-list" lay-filter="LAY-app-forum-list" ></table>
        <script type="text/html" id="avatarTpl">
          <div><img style="width: 25px; height: 25px;" src= "{{ d.avatar }}"></div>
        </script>
        <script type="text/html" id="buttonTpl">
          <input type="checkbox" name="is_top" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="isTop" {{ d.top == 1 ? 'checked' : '' }}>
        </script>
        <script type="text/html" id="buttonHot">
          <input type="checkbox" name="is_hot" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="isHot" {{ d.hot == 1 ? 'checked' : '' }}>
        </script>
        <script type="text/html" id="buttonReply">
          <input type="checkbox" name="is_reply" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="isReply" {{ d.reply == 0 ? 'checked' : '' }}>
        </script>
        <script type="text/html" id="buttonCheck">
          <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-filter="artStatus" lay-text="通过|{{ d.check == 0 ? '待审' : '禁止' }}" {{ d.check == 1 ? 'checked' : '' }}>
        </script>
        <script type="text/html" id="table-forum-list">
          <a class="layui-btn layui-btn-xs" lay-event="edit" ><i class="layui-icon layui-icon-edit"></i></a>
		      {if condition="checkRuleButton('forum/listdel')"}
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i></a>
		      {else /}<a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-disabled"><i class="layui-icon layui-icon-delete"></i></a>{/if}
        </script>
      </div>
    </div>
  </div>

{/block}

{block name="js"}

<script>
var forumList       = "{:url('Forum/list')}",
    forumListdel    = "{:url('Forum/listdel')}",
    forumListform   = "{:url('Forum/listform')}",
    forumReplys     = "{:url('Forum/replys')}",
    forumRedel      = "{:url('Forum/redel')}",
    forumReplysform = "{:url('Forum/replysform')}",
    forumTags       = "{:url('Forum/tags')}",
    forumTagsDelete = "{:url('Forum/tagsdelete')}",
    forumTagsForm   = "{:url('Forum/tagsform')}";
    forumEdit   = "{:url('Forum/edit')}";
layui.config({
  base: '/static/admin/' //静态资源所在路径
}).extend({
  index: 'lib/index' //主入口模块
}).use(['index', 'forum', 'table'], function(){
  var $ = layui.$
  ,form = layui.form
  ,table = layui.table;
  
  // 监听搜索
  form.on('submit(LAY-app-forumlist-search)', function(data){
    var field = data.field;
    $.post("{:url('Forum/list')}",field);
    //执行重载
    table.reload('LAY-app-forum-list', {
      where: field
    });
  });

   // 监听置顶
   form.on('switch(isTop)', function(obj){
    $.post("{:url('Forum/check')}",{id:obj.value, name:obj.elem.name,value:obj.elem.checked ? 1 : 0},function(res){
      layer.tips(obj.value + ' ' + obj.elem.name + '：'+ obj.elem.checked, obj.othis);
    });
  });

  // 监听加精
  form.on('switch(isHot)', function(obj){
    $.post("{:url('Forum/check')}",{id:obj.value, name:obj.elem.name,value: obj.elem.checked ? 1 : 0},function(res){
      layer.tips(obj.value + ' ' + obj.elem.name + '：'+ obj.elem.checked, obj.othis);
    });
  });

  // 监听回复
  form.on('switch(isReply)', function(obj){
    $.post("{:url('Forum/check')}",{id:obj.value, name:obj.elem.name,value: obj.elem.checked ? 0 : 1},function(res){
      layer.tips(obj.value + ' ' + obj.elem.name + '：'+ obj.elem.checked, obj.othis);
    });
  });

  // 监听审贴
  form.on('switch(artStatus)', function(obj){
    //layer.tips(obj.value + ' ' + obj.elem.name + '：'+ obj.elem.checked, obj.othis);
    $.post("{:url('Forum/check')}",{id:obj.value, name:obj.elem.name,value: obj.elem.checked ? 1 : -1},function(res){
      if(res.code == 0){
					layer.msg(res.msg,{icon:res.icon,time:2000})
			} else {
					layer.open({title:'审核失败',content:res.msg,icon:5,adim:6})
      }
    });
  }); 
    
  //事件
  var active = {
    batchdel: function(){
      var checkStatus = table.checkStatus('LAY-app-forum-list')
      ,checkData = checkStatus.data; //得到选中的数据

      if(checkData.length === 0){
        return layer.msg('请选择数据');
      }
    
      layer.confirm('确定删除吗？', function(index) {
        //获取所选id
        var idsArray = [];
        for (var i = 0; i < checkData.length; i++) {
            idsArray.push(checkData[i].id);
        }
        var ids = idsArray.toString();
            
        //执行 Ajax 后重载
        $.post("{:url('Forum/listdel')}",{"id": ids},function(res){
          if(res.code == 0){
            layer.msg('删除成功', {icon: 1,shade: 0});
          }
        });
        
        table.reload('LAY-app-forum-list');
        layer.msg('已删除');
      });
    }
	 ,add: function(){
        layer.open({
          type: 2
          ,title: '添加'
          ,content: 'add.html'
          ,maxmin: true
          ,area: ['100%', '100%']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
            ,submitID = 'article-add'
            ,submit = layero.find('iframe').contents().find('#'+ submitID);

            //监听提交
            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
              var field = data.field; //获取提交的字段
			  
              // tag
              var numArr = new Array();
              layero.find('iframe').contents().find(".layui-btn-container").children("button").each(function () {
                  numArr.push($(this).val()); //添加至数组
                });
              field.tags = numArr.lenth ? "" : numArr.join(",");

              //提交 Ajax 成功后，静态更新表格中的数据
              $.ajax({
                type:"post",
                url:"{:url('Forum/add')}",
                data: field,
                daType:"json",
                success:function (data){
                  if (data.code == 0) {
                    layer.msg(data.msg,{
                      icon:6,
                      time:2000
                    });
                  } else {
                    layer.open({
                      tiele:'添加失败',
                      content:data.msg,
                      icon:5,
                      anim:6
                    });
                  }
                }
              });
              table.reload('LAY-app-forum-list'); //数据刷新
              layer.close(index); //关闭弹层
            });  
            
            submit.trigger('click');
          }
        }); 
      }
  }

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

  //   $(document).on('focusin', function(e) {
  //   if ($(e.target).closest(".tox-tinymce, .tox-tinymce-aux, .moxman-window, .tam-assetmanager-root").length) {
  //     e.stopImmediatePropagation();
  //   }
  // });

});
</script>
{/block}
