<?php
include_once("htui_header.php");

$scuser_id=$_SESSION["a_uid"];
if ($_GET['listid']!='' && $_GET['sp']==''){
   
	if (check_permission($scuser_id,'messagelist.php',3)){
        try {
           
            $db->startTrans();

			$listid = flc_request($_GET['listid']);
		    $sql = "delete from {$tablepre}message where msg_id in(".$listid.")";
		    $db->exec($sql);

			$idarr=explode(",",$listid);

			/*删除原来已上传的图片*/
			include_once("../classes/fileclass.php");
			$file = new fileoperate();  
			foreach ($idarr as $msg_id){
				$sql="select attach_path,thumbnail_path from {$tablepre}attachmng where msg_id=".$msg_id;
				$rm = $db->get_all($sql);
				if(is_array($rm) && !empty($rm)){
				   foreach ($rm as $r){
					  $file->del_attach("..",$r['attach_path']);
					  $file->del_attach("..",$r['thumbnail_path']);
				   }
				}

				$db->exec("delete from {$tablepre}categorymng where msg_id='".$msg_id."'");
				$db->exec("delete from {$tablepre}attachmng where msg_id='".$msg_id."'"); 
			}
			/**********************/
			$db->commit();
		    $response_arr = array("status" => 1,"message"  => '操作成功');

		} catch (Exception $e) {
			$db->rollback();
			$response_arr = array("status" => -1,"message"  => $e->getMessage());
		    echo json_encode($response_arr);
            exit;
        }
	}else{
        $response_arr = array("status" => -1,"message"  => '对不起,您无权限操作!');
	}

    echo json_encode($response_arr);
    exit;
}

if ($_GET['listid']!='' && $_GET['sp']==1){

	if (check_permission($scuser_id,'messagelist.php',4)){
		$listid = flc_request($_GET['listid']);
		$msg_status = intval($_GET['flag']);
		$sql = "update {$tablepre}message set msg_status='".$msg_status."' where msg_id in(".$listid.")";
		$db->exec($sql);
		$response_arr = array("status" => 1,"message"  => '操作成功');
	}else{
        $response_arr = array("status" => -1,"message"  => '对不起,您无权限操作!');
	}

    echo json_encode($response_arr);
    exit;
}

if ($_GET['listid']!='' && $_GET['sp']==2){

	if (check_permission($scuser_id,'messagelist.php',4)){
		$listid = flc_request($_GET['listid']);
		$msg_tuijian = intval($_GET['flag']);
		$sql = "update {$tablepre}message set msg_tuijian='".$msg_tuijian."' where msg_id in(".$listid.")";
		$db->exec($sql);
		$response_arr = array("status" => 1,"message"  => '操作成功');
	}else{
        $response_arr = array("status" => -1,"message"  => '对不起,您无权限操作!');
	}

    echo json_encode($response_arr);
    exit;
}

if ($_GET['listid']!='' && $_GET['sp']==3){

	if (check_permission($scuser_id,'messagelist.php',4)){
		$listid = flc_request($_GET['listid']);
		$msg_isad = intval($_GET['flag']);
		$sql = "update {$tablepre}message set msg_isad='".$msg_isad."' where msg_id in(".$listid.")";
		$db->exec($sql);
		$response_arr = array("status" => 1,"message"  => '操作成功');
	}else{
        $response_arr = array("status" => -1,"message"  => '对不起,您无权限操作!');
	}

    echo json_encode($response_arr);
    exit;
}

if ($_GET['listid']!='' && $_GET['sp']==4){

	if (check_permission($scuser_id,'messagelist.php',4)){
		$listid = flc_request($_GET['listid']);
		
        $idList=explode(",",$listid);
		$urls = array();
		for($i=0; $i<count($idList); $i++){
			$sql="select cm.category_id,m.msg_id from {$tablepre}message m inner join {$tablepre}categorymng cm on
				  m.msg_id=cm.msg_id inner join {$tablepre}category c on c.category_id=cm.category_id where cm.msg_id='".$idList[$i]."'";
			$row  = $db->get_row($sql);
			$cid=$row["category_id"];
			$msg_id=$row["msg_id"];
			if ($html_flag==1){
			   $submiturl=$site_url."/view-".$cid."-".$msg_id.".html";
			}else{
			   $submiturl=$site_url."/view.php?cid=".$cid."&msg_id=".$msg_id."";
			}
			$urls[] = $submiturl;
		}
		

		$domain=str_replace("http://","",$site_url);
		$api = $baidu_api.'?site='.$domain.'&token='.$baidu_token;

		$response_arr = array("status" => 1,"message"  => $api);
		$json = http_post_url($api,$urls);
		$array = json_decode($json,true);

		if ($array['success']>0){
		   $idstr = implode(",", $idList);
		   $str="update {$tablepre}message set msg_submit=1 where msg_id in(".$idstr.")";
		   $db -> exec($str);
		}

		$response_arr = array("status" => 1,"message"  => '提交成功');
	}else{
        $response_arr = array("status" => -1,"message"  => '对不起,您无权限操作!');
	}

    echo json_encode($response_arr);
    exit;
}

$page=intval($_GET["page"]);
if ($page==0) $page=1;
  
if ($_POST["searchcontent"]!=""){
    $searchcontent=flc_request(trim($_POST["searchcontent"]));
}elseif ($_GET["searchcontent"]!=""){
    $searchcontent=flc_request(trim($_GET["searchcontent"]));
}
  

if ($_POST["title_content"]!=""){
    $title_content=flc_request(trim($_POST["title_content"]));
}elseif ($_GET["title_content"]!=""){
    $title_content=flc_request(trim($_GET["title_content"]));
}
  
if ($title_content==''){
	 $where="";
}else{
	 $where="m.".$title_content." like '%".$searchcontent."%' and";
}

$sql = "select * from {$tablepre}message m inner join {$tablepre}categorymng cm on m.msg_id=cm.msg_id where ";
if($searchcontent!=""){
	$sql .= $where." 1=1 and m.msg_title like '%".$searchcontent."%' group by cm.msg_id order by m.msg_id desc";
}else{
	$sql .= $where." 1=1 group by cm.msg_id order by m.msg_id desc";
}

$p       = new htui_page;
$myrow   = $db->get_all($sql);
$numrows = count($myrow);

$p->setvar(array("title_content" => $title_content, "searchcontent" => $searchcontent)); 
$p->set($site_admin_num,$numrows,$psize); 
$ListArray = $db -> get_all($sql." limit ".$p->limit());


if(!empty($ListArray) && is_array($ListArray)){
     foreach($ListArray as $k => $v){
		  $ListArray[$k]['msg_createtime'] = dateFormat($v['msg_createtime'],'Y-m-d');
		  $str1="select c.category_name,cm.hits_num from {$tablepre}categorymng cm inner join {$tablepre}category c 
				on cm.category_id=c.category_id where cm.msg_id='".$v['msg_id']."'"; 
		  $categoryArray=$db->get_all($str1);
		  if(is_array($categoryArray) && !empty($categoryArray)){
		     foreach ($categoryArray as $ca){
			    $caname.=$ca['category_name'].",";
		     }
		     $catename = substr($caname,0,strlen($caname)-1);
		     $ListArray[$k]['category_name']=$catename;
		     unset($caname);
	      }
     }
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta id="scale" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>信息管理列表 - 飞天素材网</title>
<meta name="author" content="ftsucai.com">
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/fonts.css" rel="stylesheet">
<link href="../css/htstyle.css" rel="stylesheet">
<link href="../css/jquery-confirm.min.css" rel="stylesheet">
</head>
  
<body>
    <!--页面主要内容-->
    <main class="ftdms-layout-content">
      
      <div class="container-fluid" style="margin-bottom:90px;">
        
        <div class="row" style="margin-top:15px;">
          <div class="col-lg-12">
            <div class="card">
              <div class="card-toolbar clearfix">
                <form class="pull-right search-bar" method="post" action="" role="form" id="search">
                  <div class="input-group">
                    <div class="input-group-btn">
                      <input type="hidden" name="title_content" id="title_content" value="msg_title">
                      <button class="btn btn-default dropdown-toggle" id="search-btn" data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                      标题 <span class="caret"></span>
                      </button>
                      <ul class="dropdown-menu">
                        <li> <a tabindex="-1" href="javascript:void(0)" data-field="msg_title">标题</a> </li>
                        <li> <a tabindex="-1" href="javascript:void(0)" data-field="msg_content">内容</a> </li>
                      </ul>
                    </div>
                    <input type="text" class="form-control" value="" name="searchcontent" placeholder="<?php echo $searchcontent;?>">
                  </div>
                </form>

                <div class="toolbar-btn-action">
				  <a id="post" class="btn btn-default" href="add_message.php"> 发布信息</a>
				  <a id="sh" class="btn btn-default submenuitem"> 批量审核</a>
				  <a id="del" class="btn btn-default submenuitem"> 批量删除</a>
				  <a id="baidu" class="btn btn-default submenuitem"> 提交到百度</a>
                </div>

              </div>
              <div class="card-body">
                
                <div class="table-responsive">
                  <table class="table table-bordered">
                    <thead>
                      <tr>
                        <th>
                          <label class="ftdms-checkbox checkbox-success">
                            <input type="checkbox" id="check-all"><span></span>
                          </label>
                        </th>
                        <th>信息标题</th>
                        <th>所属栏目</th>
                        <th>下载地址</th>
                        <th>演示地址</th>
						<th>发布时间</th>
						<th>提交百度</th>
						<th>广告</th>
						<th>推荐</th>
                        <th>审核</th>
                        <th>操作</th>
                      </tr>
                    </thead>
                    <tbody>
					  <?php
					  foreach ($ListArray as $k=>$v){
						  $chkbox='否';$baidu='否';$checked="";$flag=1;$status=1;$checked_status="";$checked_ad="";$isad=1;
						  if ($v['msg_tuijian']==1){
                             //$chkbox="checked";
							 $chkbox="是";
							 $checked ="checked"; //选中状态
							 $flag = 0;
						  }

						  if ($v['msg_status']==1){
							 $checked_status ="checked"; //选中状态
							 $status = 2;
						  }

						  if ($v['msg_submit'] == 1){
                             $baidu="是";
						  }

						  if ($v['msg_isad'] == 1){
							 $checked_ad="checked";
                             $isad=0;
						  }
					  ?>
                      <tr>
                        <td>
                          <label class="ftdms-checkbox checkbox-success">
                            <input type="checkbox" name="ids" value="<?php echo $v['msg_id']?>" data_id="<?php echo $v['msg_id']?>"><span></span>
                          </label>
                        </td>
                        <td><font color="<?php echo $v['msg_titlecolor']?>"><?php echo $v['msg_title']?></font></td>
                        <td><?php echo $v['category_name']?></td>
                        <td><a href="<?php echo $v['msg_downurl']?>">下载资源</a></td>
                        <td><a href="<?php echo $v['msg_demourl']?>">查看演示</a></td>
						<td><?php echo $v['msg_createtime']?></td>
						<td><?php echo $baidu;?></td>
						<td>
						  <label class="ftdms-switch switch-solid switch-success">
							 <input type="checkbox" value="<?php echo $isad;?>" name="msg_isad" <?php echo $checked_ad;?> onclick="chkbox(<?php echo $v['msg_id']?>,<?php echo $isad;?>,3);">
							 <span></span>
						  </label>
						</td>
						<td>
						  <label class="ftdms-switch switch-solid switch-success">
							 <input type="checkbox" value="<?php echo $flag;?>" name="msg_tuijian" <?php echo $checked;?> onclick="chkbox(<?php echo $v['msg_id']?>,<?php echo $flag?>,2);">
							 <span></span>
						  </label>
						</td>

                        <td>
                          <label class="ftdms-switch switch-solid switch-success">
                             <input type="checkbox" value="<?php echo $status;?>" name="msg_status" <?php echo $checked_status;?> onclick="chkbox(<?php echo $v['msg_id']?>,<?php echo $status;?>,1);">
                             <span></span>
                          </label>
						</td>
                        <td>
                          <div class="btn-group">
                            <a class="btn btn-xs btn-default submenuitem" href="edit_message.php?id=<?php echo $v['msg_id']?>" target= "_self" title="修改信息" data-toggle="tooltip"><i class="ftsucai-edit-2"></i></a>
                            <a class="btn btn-xs btn-default submenuitem del"  title="删除用户" data-toggle="tooltip" onclick="del_data(<?php echo $v['msg_id']?>)"><i class="ftsucai-del"></i></a>
                          </div>
                        </td>
                      </tr>
					  <?php
					  }
					  ?>
                    </tbody>
                  </table>
				  <nav>
                    <ul class="pagination no-border">
                     <?php echo $pager=$p->output(1);?>
                    </ul>
                  </nav>
                </div>

				
       
              </div>
            </div>
          </div>
          
        </div>
        
      </div>
      
    </main>
    <!--End 页面主要内容-->

<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/bootstrap.js"></script>
<script type="text/javascript" src="../js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="../js/main.min.js"></script>
<script type="text/javascript" src="../js/jquery-confirm.min.js"></script>
<script type="text/javascript" src="../js/bootstrap-notify.min.js"></script>
<script type="text/javascript" src="../js/tips.js"></script>

<script type="text/javascript">
$(function(){
    $('.search-bar .dropdown-menu a').click(function() {
        var field = $(this).data('field') || '';
        $('#title_content').val(field);
        $('#search-btn').html($(this).text() + ' <span class="caret"></span>');
    });
});

$(document).keyup(function(event){
  if(event.keyCode ==13){
    $("#search").trigger("click");
  }
});

$("#add").click(function(){//输出选中的值 

	var chk_value =[]; 
    $('input[name="ids"]:checked').each(function(){ 
        chk_value.push($(this).val()); 
		//str += this.value+',';
    }); 

	//alert(chk_value);
    alert(chk_value.length==0 ?'你还没有选择任何内容！':chk_value);
}); 


$('#del').on('click', function(){
	//post字段
	var params =[]; var id='';
    $('input[name="ids"]:checked').each(function(){ 
    	//params.push($(this).val());
    	id += ','+this.value;
    });

    //alert(JSON.stringify(params));
    var listid=id.substr(1);
    //if (params.length==0){
    if (listid==''){
        tips.notify('你还没有选择任何内容！', 'danger', 1000);
		return false;
	}else{
		$.alert({
	        title: '你确定要删除吗？',
	        content: '',
			buttons: {
				confirm: {
					text: '确认',
					btnClass: 'btn-primary',
					action: function(){
			        	tips.loading('show');
			            //php端可用$_GET,$_post,file_get_contents("php://input")方式获取提交的数据
			    		$.post('messagelist.php?listid='+listid, function(data) {
			                try {
			    				var ret = eval('('+data+')');
			    				if (ret.status == 1) {
			    					//alert(ret.msg);
			    					//tips.loading('hide');
			    					//tips.notify('操作成功，页面即将自动跳转~', 'success', 3000);
			    					window.location.reload();  //刷新当前页面
			    					//parent.location.reload();  //刷新父亲对象
			    				}else {
			    					tips.notify('操作失败！', 'danger', 1000);
			    				}
			    			} catch (e) {
			    				tips.notify('网络异常,操作失败！', 'danger', 1000);
			    			}
			    		});
					}
				},
				cancel: {
					text: '取消',
					action: function () {
	                    //$.alert('你点击了取消!');
					}
				}
			}
	    });
	}
	
});

function del_data(id) {

	$('input[data_id^=' + id + ']').attr("checked","true");

	$.alert({
		title: '你确定要删除吗？',
		content: '',
		buttons: {
			confirm: {
				text: '确认',
				btnClass: 'btn-primary',
				action: function(){
					tips.loading('show');
					$.post('messagelist.php?listid='+id, function(data) {
						try {
							var ret = eval('('+data+')');
							if (ret.status == 1) {
								tips.loading('hide');
								//tips.notify('操作成功，页面即将自动跳转~', 'success', 3000);
								window.location.reload();  //刷新当前页面
								//parent.location.reload();  //刷新父亲对象
							}else {
								tips.loading('hide');
								tips.notify(ret.message, 'danger', 600);
							}
						} catch (e) {
							tips.loading('hide');
							tips.notify('网络异常,操作失败！', 'danger', 1000);
						}
					});
				}
			},
			cancel: {
				text: '取消',
				action: function () {
					//$.alert('你点击了取消!');
				}
			}
		}
	});
}

$('#sh').on('click', function(){

	var id='';
    $('input[name="ids"]:checked').each(function(){ 
    	id += ','+this.value;
    });

    var listid=id.substr(1);
    if (listid==''){
        tips.notify('你还没有选择任何内容！', 'danger', 1000);
		return false;
	}else{

		$.post('messagelist.php?sp=1&flag=1&listid='+listid, function(data) {
			try {
				var ret = eval('('+data+')');
				if (ret.status == 1) {
					//alert(ret.msg);
					tips.loading('hide');
					tips.notify(ret.message, 'success', 3000);
					window.location.reload();  //刷新当前页面
					//parent.location.reload();  //刷新父亲对象
				}else {
					tips.notify(ret.message, 'danger', 1000);
				}
			} catch (e) {
				tips.notify('网络异常,操作失败！', 'danger', 1000);
			}
		});

	}
	
});

$('#baidu').on('click', function(){

	var id='';
    $('input[name="ids"]:checked').each(function(){ 
    	id += ','+this.value;
    });

    var listid=id.substr(1);
    if (listid==''){
        tips.notify('你还没有选择任何内容！', 'danger', 1000);
		return false;
	}else{
        tips.loading('show');
		$.post('messagelist.php?sp=4&listid='+listid, function(data) {
			try {
				var ret = eval('('+data+')');
				if (ret.status == 1) {
					//alert(ret.msg);
					tips.loading('hide');
					tips.notify(ret.message, 'success', 1000);
					window.location.reload();  //刷新当前页面
					//parent.location.reload();  //刷新父亲对象
				}else {
					tips.notify(ret.message, 'danger', 1000);
				}
			} catch (e) {
				tips.loading('hide');
				tips.notify('网络异常,操作失败！', 'danger', 1000);
			}
		});

	}
	
});

function chkbox(id,flag,sp) {
	var id;
	var flag;
	var sp;
	$.post('messagelist.php?sp='+sp+'&listid='+id+'&flag='+flag, function(data) {
		try {
			var ret = eval('('+data+')');
			if (ret.status == 1) {
				//tips.notify(ret.message, 'success', 1000);
				window.location.reload();
			}else {
				tips.notify(ret.message, 'danger', 1000);
			}
		} catch (e) {
			tips.notify('网络异常,操作失败！', 'danger', 1000);
		}
	});
}

</script>
</body>
</html>