<?php $this->display(tpl('head',true)); ?>
<style>
	.layui-table td, .layui-table th {
	    position: relative;
	    padding: 9px 5px;
	    min-height: 20px;
	    line-height: 20px;
	    font-size: 14px;
	    text-align: center;
	}
	.layui-table td:nth-child(3), .layui-table th:nth-child(3){
		text-align: left;
	}
	.layui-form-checkbox {
	    height: 20px;
	    line-height: 20px;
	    padding-right: 20px;
	}
	.layui-form-checkbox i {
	    width: 25px;
	    font-size: 18px;
	}
	.article_info{
		margin: 10px;
	}
</style>
<fieldset class="layui-elem-field layui-field-title">
  	<legend>专题"<?php echo $special[name] ?>"的信息</legend>
</fieldset>
<div class="layui-row">
	<button class="layui-btn layui-btn-sm layui-btn-danger" onclick="Popup()">添加信息到本专题</button>
</div>
<form class="layui-form" method="post" action="<?php echo admin_module_url ?>special/batch_article_action/">
	<table class="layui-table">
		<thead>
			<tr>
				<th width="20"></th>
				<th width="60">AID</th>
				<th align="left">标题</th>
				<th width="100">排序</th>
				<th width="75">操作</th>
			</tr>
		</thead>
		<tbody id="old_article_list" lay-filter="old_article_list">
	<?php foreach($data AS $row) { $url=U(['aid'=>$row[aid],'cid'=>$row[cid]]);?>
			<tr>
				<td><input type="checkbox" name="iddb[]" value="<?php echo $row[id] ?>" /></td>
				<td><?php echo $row[aid] ?></td>
				<td align="left" title="<?php echo $row[title] ?>">
					<a href="<?php echo $url ?>" target="_blank"><?php echo $row[title] ?></a>
				</td>
				
				<td><input name="reorder[<?php echo $row[id] ?>]" class="layui-input" value="<?php echo $row[reorder] ?>"></td>
				<td width="80">
					<div class="layui-table-cell ">
						<a class="layui-btn layui-btn-sm layui-btn-danger" href="/admin/special/articls_del/id_<?php echo $row[id] ?>">删除</a>
					</div>
				</td>
			</tr>
	<?php } ?>
		<?php if(!$data){ ?>
			<tr><td colspan="5">暂无数据</td></tr>
		<?php }?>
		</tbody>
	</table>
	<div class="button">
		<button class="layui-btn layui-btn-sm layui-btn-warm" onclick="return all_checkbox_check('1')">全选</button>
		<button class="layui-btn layui-btn-sm layui-btn-warm" onclick="return all_checkbox_check('-1')">反选</button>
		<button class="layui-btn layui-btn-sm layui-btn-warm" onclick="return all_checkbox_check('0')">取消选择</button>
		<button class="layui-btn layui-btn-sm layui-btn-danger" name="step" value="reorder">批量排序</button>
		<button class="layui-btn layui-btn-sm layui-btn-danger" name="step" value="del">批量删除</button>
	</div>
</form>
<div class="page">
	<?php echo $Get_Page ?>
</div>
<style>
	#article .layui-form span{
		float: left;
		line-height: 35px;
		margin-right:10px ;
	}
	#article .layui-form .layui-input,#article .layui-form  .layui-form-select{
		width: 200px;
		float: left;
	}
	#article .layui-form  .layui-form-select {
		margin-left: 10px;
		margin-right: 10px;
	}
	#article .layui-form .layui-btn-sm{
		height: 38px;
		line-height: 38px;
	}
	#article_list tr td:nth-child(2){
		text-align: left;
	}
	#article #page{
		text-align: center;
	}
</style>
<div id="article" style="display: none;">
	<fieldset class="layui-elem-field layui-field-title">
	  	<legend>信息搜索</legend>
	</fieldset>
	<div class="layui-row" style="margin-left: 20px;">
		<form class="layui-form">
			<span>关键字：</span><input name="keyword" class="layui-input" placeholder="请输入需要搜索的关键字" />
			<select name="cid">
				<option value="0">现有分类</option>
				<?php foreach($category as $row){  ?>
					<option value="<?php echo $row[cid] ?>"><?php echo $row[levels] ; echo $row[name] ?></option>
				<?php } ?>
			</select>
			<button class="layui-btn layui-btn-sm layui-btn-normal" lay-submit lay-filter="search">搜索</button>
		</form>
		
	</div>
	<div class="article_info">
		<table class="layui-table">
			<thead>
				<tr>
					<th width="60">AID</th>
					<th align="left">标题</th>
					<th width="75">操作</th>
				</tr>
			</thead>
			<tbody id="article_list"></tbody>
		</table>
		<div id="page"></div>
	</div>
	
</div>
<script>
	function all_checkbox_check(type) {
		if(document.getElementsByClassName('layui-form-checkbox') != undefined && document.getElementsByClassName('layui-form-checkbox').length > 0) {
			var class_box = document.getElementsByClassName('layui-form-checkbox');
			var ids = document.getElementsByName('iddb[]');
			if(type == "1") { //全选
				for(var i = 0; i < class_box.length; i++) {
					$(ids[i]).prop("checked", true);
					$(class_box[i]).addClass("layui-form-checked");
				}
			} else if(type == "0") { //全不选
				for(var i = 0; i < class_box.length; i++) {
					$(ids[i]).prop("checked", false);
					$(class_box[i]).removeClass("layui-form-checked");
				}
			} else if(type == "-1") { //反选
				for(var i = 0; i < class_box.length; i++) {
					if(ids[i].checked == true) {
						$(ids[i]).prop("checked", false);
					} else {
						$(ids[i]).prop("checked", true);
					}
					$(class_box[i]).toggleClass("layui-form-checked");
				}
			}
		}
		return false;
	}
	var form,laypage,keyword,cid='0',is_show_page=false;
	layui.use(['table','form', 'layer','laypage'], function() {
		var table = layui.table,layer = layui.layer;
		form=layui.form;
		laypage = layui.laypage;
		form.on('submit(search)', function(data){
			console.log(data.field.keyword)
		  	is_show_page=false;
		  	if(!data.field.keyword){
		  		layer.tips('请输入关键字', '吸附元素选择器，如#id');
		  	}
		  	keyword=data.field.keyword;
		  	cid=data.field.cid;
		  	ajax_article({'id':<?php echo $_GET[id] ?>,'keyword':keyword,'cid':cid});
  			return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
		});
	});
	
	function Popup(){
		layer.open({
			type: 1,
			area:["80%","450px"],
			title: "搜索信息",
			content: $('#article'),
			anim:2,
			success: function(layero, index){
			    ajax_article({'id':<?php echo $_GET[id] ?>});
			}
		});
	}
	function ajax_article(data){
		$.ajax({
			url: "/admin/special/ajax_article/",
			type: 'POST',
			data:data,
			dataType: 'json',
			success: function(data) {
				$("#article_list").html('');
				if(is_show_page==false){
					is_show_page=true;
					laypage.render({
					    elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
					    ,count: data.count //数据总数，从服务端得到
					    ,limit:data.rows
					    ,jump: function(obj, first){
						    console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
						    //首次不执行
						    if(!first){
						      	var data={'id':<?php echo $_GET[id] ?>,'page':obj.curr,'cid':cid,'keyword':keyword};
						      	console.log(data);
						      	ajax_article(data);
						    }
						}
					});
				}
				if(data.data==null){
					$("#article_list").html('<tr><td colspan="3">暂无数据</td></tr>');
					return false;
				}
				for(var i=0;i<data.data.length;i++){
					var htm='<tr><td>'+data.data[i].aid+'</td><td align="left" title="'+data.data[i].title+'"><a href="'+data.data[i].url+'" target="_blank">'+data.data[i].title+'</a></td><td width="80"><div class="layui-table-cell "><a class="layui-btn layui-btn-sm layui-btn-danger" onclick="add_article(this,'+data.data[i].aid+',\''+data.data[i].title+'\',\''+data.data[i].url+'\')">添加</a></div></td></tr>';
					$("#article_list").append(htm);
				}
				
			}
		})
	}
	function add_article(a,b,c,d){
		$.ajax({
			url: "/admin/special/ajax_add_article/",
			type: 'POST',
			data:{'aid':b,'id':<?php echo $_GET[id] ?>},
			dataType: 'json',
			success: function(data) {
				if(data.code==1){
					var htm='<tr><td><input type="checkbox" name="iddb[]" value="'+data.id+'" /></td><td>'+b+'</td><td align="left" title="'+c+'"><a href="'+d+'" target="_blank">'+c+'</a></td><td><input name="reorder[]" class="layui-input" value="0"></td><td width="80"><div class="layui-table-cell "><a class="layui-btn layui-btn-sm layui-btn-danger" href="<?php echo admin_module_url ?>special/articls_del/id_'+data.id+'">删除</a></div></td></tr>';
					$("#old_article_list").prepend(htm);
					a.remove();
					form.render("checkbox");
				}
			}
		})
	}
</script>
<?php $this->display(tpl('foot',true)); ?>
