<?php 
  include './head.php';
?>
<div class="layui-row layui-col-space15">
        <div class="layui-col-md8 layui-col-md-offset2">
        <div class="layui-card" style="border-radius: 20px;">
            <div class="layui-card-header">
                <h3 style="padding-top: 3px;"><b>分类列表</b></h3>
            </div>
            <div class="layui-card-body">
                <table id="classList" lay-filter="classList"></table>
                <script type="text/html" id="viewAct">
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="pl">批量</a>
                    <a class="layui-btn layui-btn-success layui-btn-xs" lay-event="edit">编辑</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                </script>
            </div>
</div>
</div>

<script src="../public/static/layuiadmin/layui/layui.all.js"></script>
  <script>
  layui.config({
      base: '../public/static/layuiadmin/' //静态资源所在路径
    }).extend({
      index: 'lib/index' //主入口模块
    }).use(['index','form','table'], function(){
      var $ = layui.$
      ,table = layui.table
      ,layedit = layui.layedit
      ,laydate = layui.laydate
      ,form = layui.form
      ,admin = layui.admin
      ,element = layui.element
      ,layer = layui.layer;
      element.render();

    table.render({
      elem: '#classList',
      url: './ajax.php?act=classList',
      page: true,
      toolbar: '<div><i lay-event="viewAdd" class="layui-icon layui-icon-add-circle" style="font-size: 30px; color:#009688;"></i> </div>',
      cols: [[
      // {type: 'checkbox', fixed: 'left'},
        {field: 'id', title: '序号', width:80, sort: true},
        {field: 'path', title: '目录分类', width:100},
        {field: 'title', title: '目录名称', width:200},
        {field: 'word', title: '关键词', width:200},
        {field: 'viewNum', title: '页面数量', width:100,sort: true},
        {title: '操作', width: 200, toolbar: '#viewAct'}
      ]]
    })

    table.on('toolbar(classList)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id)
    ,data = checkStatus.data; //获取选中的数据
    switch(obj.event){
      case 'viewAdd':
       area = [$(window).width() > 600 ? '600px' : '90%', $(window).height() > 550 ? '550px' : '90%'];
        layer.open({
          type: 2,
          title: '添加分类',
          skin: 'layui-layer-molv', //加上边框
          area: area, //宽高
          content: './classAdd.php?act=add'
        });
      break;
    };
  });

    table.on('tool(classList)', function(obj){
      var data = obj.data;
      var layEvent = obj.event;
      var tr = obj.tr;  
      if(layEvent=='del'){
        if(data.viewNum>0){
          layer.confirm('<font color="red">删除分类同时也会删除分类下所有已生成的页面</font>', {
            btn: ['确定删除','再想想'] //按钮
          }, function(){
            var load = layer.load({time:false});
              $.ajax({
                  url: "./ajax.php?act=classDel",
                  data: {id:data.id,mod:'delAll'},
                  type: "GET",
                  dataType: "json",
                  success: function(data){
                      layer.close(load);
                      if(data.code==1){
                        layer.msg(data.msg,{icon:6});
                      }else{
                        layer.msg(data.msg,{icon:5});
                      }
                      if(data.code == 1){
                          obj.del(); 
                      }
                  },
                  error: function(data){
                      layer.close(load);
                      layer.msg("未知错误");
                  }
              });
          });
          return false;
        }
        layer.confirm('序号: '+data.id+'<br>分类：'+data.title+'<br>您确定要<font color="red"><b> 删除 </b></font>吗？', function(index){
              var load = layer.load({time:false});
              $.ajax({
                  url: "./ajax.php?act=classDel",
                  data: {id:data.id},
                  type: "GET",
                  dataType: "json",
                  success: function(data){
                      layer.close(load);
                      if(data.code==1){
                        layer.msg(data.msg,{icon:6});
                      }else{
                        layer.msg(data.msg,{icon:5});
                      }
                      if(data.code == 1){
                          obj.del(); 
                      }
                  },
                  error: function(data){
                      layer.close(load);
                      layer.msg("未知错误");
                  }
              });
          });
      }else if(layEvent=='edit'){
         area = [$(window).width() > 600 ? '600px' : '90%', $(window).height() > 550 ? '550px' : '90%'];
          layer.open({
            type: 2,
            title: '修改分类',
            skin: 'layui-layer-molv', //加上边框
            area: area, //宽高
            content: './classAdd.php?act=edit&id='+data.id
          });
      }else if(layEvent=='pl'){
        layer.confirm('当前分类：'+data.title+'<br>请选择您想要进行的<b><font color="red"> 批量 </font></b>操作', {
          btn: ['随机生成','百度推送'] //按钮
        }, function(){
            area = [$(window).width() > 600 ? '600px' : '90%', $(window).height() > 550 ? '550px' : '90%'];
            layer.open({
            type: 2,
            title: '批量生成配置',
            skin: 'layui-layer-molv', //加上边框
            area: area, //宽高
            content: './viewAdd.php?act=pl&class='+data.path
          });
        }, function(){
          var load = layer.load({time:false});
          $.ajax({
              url: "./ajax.php?act=viewTuisong",
              data: {mod:'class',id:data.id},
              type: "GET",
              dataType: "json",
              success: function(data){
                  layer.close(load);
                  if(data.code==1){
                      layer.msg(data.msg,{icon:6});
                    }else{
                      layer.msg(data.msg,{icon:5});
                    }
              },
              error: function(data){
                  layer.close(load);
                  layer.msg("未知错误");
              }
          });
        });
      }
  });
})
  </script>
<?php 
  include 'foot.php';
?>