{include file="common/top"/}
<div class="body_top">
	<ul class="title">
    	<li class="active"><a href="javascript:">{$tdata['catename']}-管理</a></li>
    	<li><a href="javascript:self_close();">关闭列表</a></li>
    </ul>
</div>

<div class="body_content_list">

<script type="text/html" id="barDemo">
<span class="tq_table_bar">
  <a class="layui-btn layui-btn-xs" href="{:url('field_edit',array('pid'=>$tdata['pid'],'catename'=>$tdata['catename']))}&id={{d.id}}');">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</span>
</script>

<script type="text/html" id="is_effect">
<span class="tq_table_bar">
	<input type="checkbox" name="is_effect" value="{{d.id}}" lay-skin="switch" lay-text="停用|启用" lay-filter="is_effect" {{ d.is_effect ==2 ? 'checked' : '' }}>
</span>
</script>

<script type="text/html" id="sort">
<span class="tq_table_bar">
	<a class="layui-btn layui-btn-xs tq_sort"><input type="text" name="{{d.id}}" autocomplete="off" value="{{d.sort}}"></a>
  	<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="sort_t">置顶</a>
  	<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="sort_p">上移</a>
  	<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="sort_n">下移</a>
</span>
</script>

<script type="text/html" id="tq_pic">
<span class="tq_table_bar">
	{{#  if(d.pic != '' && d.pic !=null ){ }}
		<a class="layui-btn layui-btn-normal layui-btn-xs tq_img_look" data-file="{{d.pic}}">预览</a>
	{{#  } }}
</span>
</script>

<script type="text/html" id="tq_btn">
  	<a class="layui-btn layui-btn-normal layui-btn-sm" href="{:url('/Admin/Article/field_add',array('pid'=>$tdata['pid'],'catename'=>$tdata['catename']))}">添加</a>
  	<a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="sort_all">排序</a>
  	<a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="check_all">全选</a>
  	<a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="list_delete">删除</a>
</script>

<table class="layui-hide" id="list" lay-filter="list"></table>

</div>
<input name="" id="add_type" type="hidden" value="0" />
{include file="common/bottom"/}
<script>
iname=parent.frameElement.name;
layui.use('table', function(){
  var table = layui.table,form = layui.form;
  var table_height=$('.body_content_list').height();
  var true_height=table_height-52-38-51-13;//自适应每页条数
  var auto_page='{$auto_page}';
  if(auto_page==1){
	  var catepage=Math.floor(true_height/39);
  }else{
	  var catepage='{$auto_page_num}';
  }
  //var catepage=10;
 
  table.render({
    elem: '#list'
	,title:'栏目列表信息'
    ,url:'{:url("Article/field_list_data",array("pid"=>$tdata["pid"]))}'
    ,cellMinWidth:80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
	,even: true
	,toolbar: '#tq_btn'
	,defaultToolbar: ['filter']
	,height: 'full-71'
	,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
      layout: ['limit','count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
	  ,limit:catepage //每页显示的数据条数
	  ,limits:[catepage, 20, 30]
      ,groups:8 //只显示 1 个连续页码
	  ,theme: 'leng'
    }
	,where: {}
	,cols: [[
      {type: 'checkbox', width:40}
      ,{field:'id', width:222, title: '排序',templet:'#sort'}
      ,{field:'title', title: '标题'}
      ,{field:'pic', width:80, title: '图片', align:'center', templet:'#tq_pic'}
      ,{field:'is_effect', width:100, align:'center', title:'状态',templet:'#is_effect', sort: true}
      ,{field:'create_time', width:110, align:'center', title: '发布时间', sort: true,templet:'<div>{{ layui.util.toDateString(d.create_time, "yyyy-MM-dd") }}</div>'}
      ,{field:'id', width:166,align:'center',toolbar:'#barDemo', title: '操作'}
    ]]
	
	,done: function(res, curr, count){
		//如果是异步请求数据方式，res即为你接口返回的信息。
		//如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
		//console.log(res);
		//得到当前页码
		//console.log(curr); 
		//得到数据总量
		//console.log(count);
  }	
  });

//监听工具条
table.on('tool(list)', function(obj){
	var data = obj.data;
	
	if(obj.event === 'del'){
		window.top.delete_c_field(obj,iname);
	}
	
	if(obj.event==='add'){
		add(data['id'],1);
	}
	if(obj.event==='edit'){
		add(data['id']);
	}
	if(obj.event==='sort_t'){
		is_sort('t',data['id'],'field',data['sort'],data['pid']);
	}
	if(obj.event==='sort_p'){
		is_sort('p',data['id'],'field',data['sort'],data['pid']);
	}
	if(obj.event==='sort_n'){
		is_sort('n',data['id'],'field',data['sort'],data['pid']);
	}	

});

//表头按钮操作
table.on('toolbar(list)', function(obj){
	
	//input排序
	if(obj.event==='sort_all'){
		var idArray = new Array();
		var sortArray = new Array();
		$('.tq_sort input').each(function(){
            idArray.push($(this).attr('name'));
            sortArray.push($(this).val());
		});
		$.post('/admin/Common/sort_all',{id:idArray,sort:sortArray,dbname:'field'},function(data){
			table.reload('list', { });
		});
	}

	//全选
	if(obj.event==='check_all'){
		$('input[name="layTableCheckbox"]+').click();
	}
	
	//批量属性
	var checkStatus = table.checkStatus(obj.config.id);
	var data = checkStatus.data;
	var idArray = new Array();
	for(j=0,len=data.length; j<len; j++) {
		idArray.push(data[j].id);
	}
	
	//批量删除
	if(obj.event==='list_delete'){
		if(data.length==0){
			window.top.tq_alert('请至少选择一条信息','',2);
			return false;
		}
		window.top.delete_ids_field(idArray,iname);
	}

});

//监听账号状态操作
form.on('switch(is_effect)', function(obj){
	var is_effect=obj.elem.checked==true?2:1;
	$.post('/admin/Common/is_edit',{id:this.value,is_effect:is_effect,dbname:'field'},function(data){ });
});

  
});//layui end;



//article页面添加修改操作	
function add(title,url){
	window.top.article_open(title,url);
	//window.parent.iframe_f(title,url);
}


function delete_id(info){
	$.post('/admin/Common/delete_id',{id:info.data.id,table:'field'},function(data){  info.del(); });
}

function delete_ids(info){
	//$.post('/admin/Common/list_delete',{id:info,table:'field'},function(data){
	$.post('/admin/Common/list_delete_true',{id:info,table:'field'},function(data){
		layui.use('table', function(){
			var table = layui.table;
			table.reload('list', { });
		});
	});
}

//关闭自己
function self_close(){
	window.parent.cate_frame_close();
}

//排序
function is_sort(type,id,dbname,sort,pid){
	$.post('/admin/Common/is_sort',{type:type,id:id,dbname:dbname,sort:sort,pid:pid},function(data){
		if(data['type']==1){
			layui.use('table', function(){
				var table = layui.table;
				table.reload('list', { where: {} });
			});
		}
	});
}
</script>