<?php
require '../conn/conn.php';
require '../conn/function.php';
require 'admin_check.php';
$page=$_GET["page"];
$action=$_GET["action"];
$A_id=intval($_GET["A_id"]);

if($page==""){
  $page=1;
}

if($A_id!=""){
  $aa="edit&A_id=".$A_id;
  $sql="select * from sl_ad where A_id=".$A_id;
  $result = mysqli_query($conn, $sql);
  $row = mysqli_fetch_assoc($result);
  if (mysqli_num_rows($result) > 0) {
    $A_pic=$row["A_pic"];
    $A_text=$row["A_text"];
    $A_order=$row["A_order"];
    $A_type=$row["A_type"];
    $A_url=$row["A_url"];
    $A_time=$row["A_time"];
    $A_long=$row["A_long"];
    $A_red=$row["A_red"];
    $A_bold=$row["A_bold"];

    if($A_pic==""){
      $A_pic="nopic.png";
    }
  }
  $title="编辑";
}else{
	$A_red=0;
	$A_bold=0;
  $A_order=0;
  $A_pic="nopic.png";
  $aa="add";
  $title="新增";
  $A_type=0;
  $A_time=date('Y-m-d H:i:s');
}

if($action=="add"){
  $A_pic=$_POST["A_pic"];
  $A_text=$_POST["A_text"];
  $A_url=$_POST["A_url"];
  $A_time=$_POST["A_time"];
  $A_long=intval($_POST["A_long"]);
  $A_order=intval($_POST["A_order"]);
  $A_type=intval($_POST["A_type"]);
  $A_red=intval($_POST["A_red"]);
  $A_bold=intval($_POST["A_bold"]);

  if($A_type==0 && $A_text==""){
  	die("{\"msg\":\"需要填写广告文字\"}");
  }

  if($A_url!=""){
    mysqli_query($conn,"insert into sl_ad(A_pic,A_text,A_order,A_url,A_type,A_time,A_long,A_red,A_bold) values('$A_pic','$A_text',$A_order,'$A_url',$A_type,'$A_time',$A_long,$A_red,$A_bold)");
    $A_id=getrs("select * from sl_ad where A_url='$A_url' and A_time='$A_time'","A_id");
    die("{\"msg\":\"success\",\"id\":\"".$A_id."\"}");
  }else{
    die("{\"msg\":\"请填全信息\"}");
  }
}

if($action=="edit"){
  $A_pic=$_POST["A_pic"];
  $A_text=$_POST["A_text"];
  $A_url=$_POST["A_url"];
  $A_time=$_POST["A_time"];
  $A_long=intval($_POST["A_long"]);
  $A_order=intval($_POST["A_order"]);
  $A_type=intval($_POST["A_type"]);
  $A_red=intval($_POST["A_red"]);
  $A_bold=intval($_POST["A_bold"]);

  if($A_type==0 && $A_text==""){
  	die("{\"msg\":\"需要填写广告文字\"}");
  }

  if($A_url!=""){
  mysqli_query($conn, "update sl_ad set
    A_pic='$A_pic',
    A_text='$A_text',
    A_url='$A_url',
    A_time='$A_time',
    A_long=$A_long,
    A_order=$A_order,
    A_red=$A_red,
    A_bold=$A_bold,
    A_type=$A_type
    where A_id=".$A_id);
    die("{\"msg\":\"success\",\"id\":\"".$A_id."\"}");
  }else{
    die("{\"msg\":\"请填全信息\"}");
  }
}

if($action=="delall"){
  $id=$_POST["id"];
  if(count($id)>0) {
    $shu=0 ;
    for ($i=0 ;$i<count($id);$i++ ) {
      mysqli_query($conn,"delete from sl_ad where A_id=".intval($id[$i]));
      $shu=$shu+1 ;
      $ids=$ids.$id[$i].",";
    }
    $ids= substr($ids,0,strlen($ids)-1);
    if($shu>0) {
      die("{\"msg\":\"success\",\"ids\":\"".$ids."\"}");
    } else {
      die("{\"msg\":\"删除失败\"}");
    }
  } else {
    die("{\"msg\":\"未选择要删除的内容\"}");
  }
}

$sql="select count(A_id) as A_count from sl_ad";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$A_counts=$row["A_count"];
?>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>广告管理 - 后台管理</title>
<link rel="icon" href="../media/<?php echo $C_ico?>" type="image/ico">
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/materialdesignicons.min.css" rel="stylesheet">
<link href="../css/style.min.css" rel="stylesheet">
<script type="text/javascript" src="../upload/upload.js"></script>
<link rel="stylesheet" href="../js/jconfirm/jquery-confirm.min.css">
<style type="text/css">

.showpic{height: 100px;border: solid 1px #DDDDDD;padding: 5px;}
.showpicx{width: 100%;max-width: 500px}
.list-group a{text-decoration:none}
.part{display:inline-block;width:40%;overflow:hidden;text-overflow:ellipsis;white-space: nowrap;}
.pagination{margin: 0px;}
</style>
</head>
  
<body>
<div class="lyear-layout-web">
  <div class="lyear-layout-container">
  <?php require 'nav.php';?>
    <main class="lyear-layout-content">
      
      <div class="container-fluid">
        
        <div class="row">
                
                <div class="col-lg-5">
                  <form id="list">
                  <div class="card card-primary">

                    <div class="card-header">
                      <h4>广告列表</h4>
                    </div>
                        <ul class="list-group">
                          <li class="list-group-item " style="background: #f7f7f7"><div class="part">广告文字</div><div class="part">广告类型</div></li>
                          <?php 
                            $sql="select * from sl_ad order by A_order,A_id desc limit ".(($page-1)*10).",10";
                                $result = mysqli_query($conn, $sql);
                                if (mysqli_num_rows($result) > 0) {
                                while($row = mysqli_fetch_assoc($result)) {
                                  if($row["A_id"]==$A_id){
                                    $active="active";
                                  }else{
                                    $active="";
                                  }
                                  switch($row["A_type"]){
                                    case 0:
                                    $type="文字广告";
                                    break;
                                    case 1:
                                    $type="图片广告";
                                    break;

                                  }
                                  if($row["A_pic"]==""){
                                    $A_picx="nopic.png";
                                  }else{
                                    $A_picx=$row["A_pic"];
                                  }
                                  if($row["A_text"]==""){
                                  	$A_textx="无文字";
                                  }else{
                                  	$A_textx=$row["A_text"];
                                  }

			                    if($row["A_red"]==1){
									$red="color:#ff0000;";
								}else{
									$red="";
								}
								if($row["A_bold"]==1){
									$bold="font-weight:bold;";
								}else{
									$bold="";
								}

                                  echo "<a id=\"".$row["A_id"]."\" href=\"?A_id=".$row["A_id"]."&page=$page\" class=\"list-group-item ".$active."\">
                                  <div class=\"part\" style=\"".$red.$bold."\"><input type=\"checkbox\" name=\"id[]\" value=\"".$row["A_id"]."\"> ".$row["A_order"].".".$A_textx."</div> 
                                  <div class=\"part\">".$type."</div>
                                  
                                  <img src=\"../media/".$A_picx."\" alt=\"<img src='../media/".$A_picx."' width='300'>\" style=\"height:25px;border-radius:10px;max-width:100px\" class=\"pull-right\"></a>";
                                }
                              }
                          ?>
                          
                        </ul>
                  </div>

                  <label><input type="checkbox" id="selectAll" name="selectAll"> 全选</label>
                  <button class="btn btn-sm btn-danger" type="button" onClick="delall()"><i class="fa fa-times-circle" ></i> 删除所选</button>
                  <a href="ad.php" class="btn btn-sm btn-primary"><i class="fa fa-plus-circle"></i> 新增广告</a>
                  <ul class="pagination" id="pagination" style="float: right;"></ul>
                  <input type="hidden" id="PageCount" runat="server" />
        <input type="hidden" id="PageSize" runat="server" value="10" />
        <input type="hidden" id="countindex" runat="server" value="10"/>
        <!--设置最多显示的页码数 可以手动设置 默认为7-->
        <input type="hidden" id="visiblePages" runat="server" value="7" />
                </form>
                </div>

                
                <div class="col-lg-7">
                  <form id="form">
                  <div class="card card-primary">
                    <div class="card-header ">
                      <h4><?php echo $title?>广告</h4>
                    </div>
                    <div class="card-body">

                      <div class="form-group row" >
                          <label class="col-md-3 col-form-label" >广告类型</label>
                          <div class="col-md-9 buy"">
                            <label aa="A_type" <?php if($A_type==0){echo "class='checked'";}?>><input type="radio" name="A_type" value="0" onclick="change(0)" <?php if($A_type==0){echo "checked='checked'";}?>> 文字广告</label>
                            <label aa="A_type" <?php if($A_type==1){echo "class='checked'";}?>><input type="radio" name="A_type" value="1" onclick="change(1)" <?php if($A_type==1){echo "checked='checked'";}?>> 图片广告</label>

                          </div>
                        </div>

                        <div class="form-group row" id="A_textx">
                          <label class="col-md-3 col-form-label" >广告文字</label>
                          <div class="col-md-9">
                            <input type="text"  name="A_text" class="form-control" value="<?php echo $A_text?>">
                            <label aa="A_red" <?php if($A_red==1){echo "class='checked'";}?>><input type="checkbox" name="A_red" value="1" <?php if($A_red==1){echo "checked='checked'";}?>> 加红</label>
                            <label aa="A_bold" <?php if($A_bold==1){echo "class='checked'";}?>><input type="checkbox" name="A_bold" value="1" <?php if($A_bold==1){echo "checked='checked'";}?>> 加粗</label> *建议不超过15字
                          </div>
                        </div>

                        <div class="form-group row" id="A_picx">
                          <label class="col-md-3 col-form-label" >广告图片</label>
                          <div class="col-md-9">
                            <p><img src="../media/<?php echo $A_pic?>" id="A_picx" class="showpic" onClick="showUpload('A_pic','A_pic','../media',1,null,'','');" alt="<img src='../media/<?php echo $A_pic?>' class='showpicx'>"> *推荐尺寸400*80</p>
                            <div class="input-group">
                                    <input type="text" id="A_pic" name="A_pic" class="form-control" value="<?php echo $A_pic?>">
                                    <span class="input-group-btn">
                                            <button class="btn btn-primary m-b-5 m-t-5" type="button" onClick="showUpload('A_pic','A_pic','../media',1,null,'','');">上传</button>
                                    </span>
                            </div>
                            
                          </div>
                        </div>

                        <!--<div class="form-group row">
                          <label class="col-md-3 col-form-label" >广告排序</label>
                          <div class="col-md-9">
                            <input type="text"  name="A_order" class="form-control" value="<?php echo $A_order?>" placeholder="数字越小，排序越靠前">
                          </div>
                        </div>-->

                          <div class="form-group row">
                            <label class="col-md-3 col-form-label" >发布时间</label>
                            <div class="col-md-9">
                              <input type="text"  name="A_time" class="form-control" value="<?php echo $A_time?>">
                            </div>
                          </div>

                          <div class="form-group row">
                            <label class="col-md-3 col-form-label" >投放时长</label>
                            <div class="col-md-9">
                              <div class="input-group">
                              <input type="text"  name="A_long" class="form-control" value="<?php echo $A_long?>">
                              <span class="input-group-addon">月</span>
                      </div>
                      <div style="font-size: 12px;margin-top: 10px;color: #33cabb">*广告到期后自动下架</div>
                            </div>

                          </div>

                          <div class="form-group row">
                            <label class="col-md-3 col-form-label" >广告链接</label>
                            <div class="col-md-9">
                              <input type="text"  name="A_url" class="form-control" value="<?php echo $A_url?>">
                            </div>
                          </div>

                        <div class="form-group row">
                          <label class="col-md-3 col-form-label" ></label>
                          <div class="col-md-9">
                            <button class="btn btn-info" type="button" onClick="save()"><?php echo $title?></button>
                          </div>
                        </div>
                    </div>

                  </div>
                  <p>说明：为达到最好的广告展示效果，请至少添加4个文字广告和1个图片广告，所有广告为随机展示</p>
                  </form>
                </div>
              </div>
      </div>
    </main>
    <!--End 页面主要内容-->
  </div>
</div>

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

<script src="../js/bootstrap-notify.min.js"></script>
<script type="text/javascript" src="../js/lightyear.js"></script>
<script src="../js/jqPaginator.min.js" type="text/javascript"></script>
<script src="../js/help.js" type="text/javascript"></script>
<script src="../js/jconfirm/jquery-confirm.min.js"></script>
<script type="text/javascript">
function change(id){
  if(id==0){
    $("#A_textx").show();
    $("#A_picx").hide();
  }
  if(id==1){
    $("#A_textx").hide();
    $("#A_picx").show();
  }
}
change(<?php echo $A_type?>);

  function loadData(num) {
            $("#PageCount").val("<?php echo $A_counts?>");
        }
function loadpage(id) {
    var myPageCount = parseInt($("#PageCount").val());
    var myPageSize = parseInt($("#PageSize").val());
    var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
    $("#countindex").val(countindex);

    $.jqPaginator('#pagination', {
        totalPages: parseInt($("#countindex").val()),
        visiblePages: parseInt($("#visiblePages").val()),
        currentPage: id,
        first: '<li class="first page-item"><a href="javascript:;" class="page-link">|<</a></li>',
        prev: '<li class="prev page-item"><a href="javascript:;" class="page-link"><i class="arrow arrow2"></i><</a></li>',
        next: '<li class="next page-item"><a href="javascript:;" class="page-link">><i class="arrow arrow3"></i></a></li>',
        last: '<li class="last page-item"><a href="javascript:;" class="page-link">>|</a></li>',
        page: '<li class="page page-item"><a href="javascript:;" class="page-link">{{page}}</a></li>',
        onPageChange: function (num, type) {
            if (type == "change") {
                window.location="?page="+num;
            }
        }
    });
}
$(function () {
    loadData(<?php echo $page?>);
    loadpage(<?php echo $page?>);

});

    function save(){
      lightyear.loading('show');
        $.ajax({
                url:'?action=<?php echo $aa?>',
                type:'post',
                data:$("#form").serialize(),
                success:function (data) {
	                data=JSON.parse(data);
	                lightyear.loading('hide');
	                if(data.msg=="success"){
	                	lightyear.notify('保存成功', 'success', 100);
	                	setTimeout("window.location.href='?A_id="+data.id+"'", 2000 )
	                }else{
	                  	lightyear.notify(data.msg, 'danger', 100);
	                }
                }
              });
      }

      function delall() {

    $.alert({
        title: '确认删除',
        content: '确认删除？',
    buttons: {
      confirm: {
        text: '确认',
        btnClass: 'btn-primary',
        action: function(){
          $.ajax({
            url: '?action=delall',
            type: 'post',
            data: $("#list").serialize(),
            success: function(data) {
                data = JSON.parse(data);
                if (data.msg == "success") {
                	lightyear.notify('删除成功', 'success', 100);
                    id = data.ids.split(",");
                    for (var i = 0; i < id.length; i++) {
                        $("#" + id[i]).hide();
                    };
                } else {
                	lightyear.notify(data.msg, 'danger', 100);

                }
            }
        });
        }
      },
      cancel: {
        text: '取消',
        action: function () {

        }
      }
    }
    });

}

$('input[name="selectAll"]').on("click",function(){
        if($(this).is(':checked')){
            $('input[name="id[]"]').each(function(){
                $(this).prop("checked",true);
            });
        }else{
            $('input[name="id[]"]').each(function(){
                $(this).prop("checked",false);
            });
        }
    });
  var path=window.location.pathname;
  $("a").each(function(){
    if(path.indexOf($(this).attr("href"))!=-1){
      console.log($(this).attr("href"));
      $(this).parent("li").addClass("active");
      $(this).parent("li").parent("ul").parent("li").addClass("open active");
    }
  });
</script>
</body>
</html>