<?php
require '../conn/conn.php';
require '../conn/function.php';
require 'member_check.php';

$page=$_GET["page"];
if($page==""){
  $page=1;
}


$action=$_GET["action"];
if($action=="save"){
  $P_from=intval($_POST["P_from"]);
  $P_price=round($_POST["P_price"],2);
  $P_sort=intval($_POST["P_sort"]);

  $P=getrs("select * from sl_product where P_del=0 and P_id=".$P_from);
  if($P==""){
    die("{\"msg\":\"商品ID有误，请重新检查！\"}");
  }else{
    if($P["P_mid"]==$M_id){
      die("{\"msg\":\"不支持对接自己店铺的商品！\"}");
    }else{
      if($P["P_from"]>0){
        die("{\"msg\":\"该商品非自建商品，无法对接！\"}");
      }else{
        if($P["P_sh"]==0){
          die("{\"msg\":\"该商品尚未通过审核，无法对接！\"}");
        }else{
          if(getrs("select * from sl_product where P_mid=$M_id and P_from=$P_from")==""){
            if($P_price>$P["P_price2"]){
              sql("insert into sl_product(P_from,P_price,P_sort,P_mid,P_sh,P_time,P_title,P_pic,P_content,P_on,P_use,P_piccontent) values($P_from,$P_price,$P_sort,$M_id,1,'".date('Y-m-d H:i:s')."','".$P["P_title"]."','".$P["P_pic"]."','".$P["P_content"]."',1,'".$P["P_use"]."','".$P["P_piccontent"]."')");
              die("{\"msg\":\"success\"}");
            }else{
              die("{\"msg\":\"出售价需大于成本价！\"}");
            }
          }else{
            die("{\"msg\":\"已对接过该商品！\"}");
          }
        }
      }
    }
  }
}
$P_all=getrs("select count(P_id) as P_count from sl_product where  P_del=0 and P_sh=1 and P_on=1 and not P_mid=$M_id and P_from=0","P_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="shortcut icon" href="../media/<?php echo $C_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>
<style>
  .box{border:solid 1px #DDD;margin-bottom: 20px}
  .P_pic{width: 100%;height:200px}
  .P_title{display:-webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 2;overflow: hidden;height: 45px;font-weight: bold;}
  .P_price{color: #f90;font-weight: bold}
  .shop{margin-top:5px;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;}
  .shop img{height: 15px;margin-right: 5px;}
  #P_pic{width: 200px;border:solid 1px #DDD;padding:10px}
</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-md-12">
            <div class="card">
              <div class="card-header"><h4>货源市场</h4></div>
              <div class="card-body">
                <div class="row">
                
                <?php
$list=getlist("select * from sl_product where  P_del=0 and P_sh=1 and P_on=1 and not P_mid=$M_id and P_from=0 order by P_id desc limit ".(($page-1)*24).",24");
                            foreach($list as $row){
                              if($row["P_mid"]==0){
                                $shop="官方自营店";
                                $logo=$C_ico;
                              }else{
                                $M=getrs("select * from sl_member where M_id=".$row["P_mid"]);
                                $shop=$M["M_webtitle"];
                                $logo=$M["M_ico"];
                              }
                              
                              echo "<div class=\"col-lg-2 col-md-3 col-xs-6\"><div class=\"box\">
                              <a href=\"../shop/?id=".$row["P_id"]."\" target=\"_blank\"><img src=\"../media/".$row["P_pic"]."\" class=\"P_pic\"></a>
                              <div style=\"padding:10px\">
                              <div class=\"P_title\">".$row["P_title"]."</div>
                              <div><span style=\"font-size:12px;\">成本价</span><span style=\"font-weight:bold;color:#f60;font-size:18px\">￥".$row["P_price2"]."</span> </div>
                              <div><span style=\"font-size:12px;\">商品ID</span>：<span style=\"font-weight:bold;color:#06f;font-size:18px\">".$row["P_id"]."</span><button class=\"btn btn-xs btn-info pull-right\" type=\"button\" onclick=\"abutment(".$row["P_id"].",'".$row["P_title"]."','".$row["P_pic"]."',".$row["P_price2"].")\">对接</button></div>
                              <div class=\"shop\"><img src=\"../media/$logo\" >".$shop."</div>
                              </div>
                              </div>
                              </div>";
                            }


                ?>
              </div>

<div style="text-align: center;">
              <ul class="pagination" id="pagination"></ul>
                <input type="hidden" id="PageCount" runat="server" />
                <input type="hidden" id="PageSize" runat="server" value="24" />
                <input type="hidden" id="countindex" runat="server" value="24"/>
                <input type="hidden" id="visiblePages" runat="server" value="7" />
</div>
            </div>
              
            </div>
          </div>

        </div>
      </div>
    </main>
  </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">对接商品</h4>
      </div>
      <div class="modal-body">
        <form id="form">
        <input type="hidden" name="P_from" id="P_from">
        <p style="text-align: center;"><img id="P_pic"></p>
        <p><div class="input-group">
            <span class="input-group-addon">对接商品</span>
            <input type="text" name="P_price" class="form-control" id="P_title" disabled>
        </div></p>

        <p><div class="input-group">
            <span class="input-group-addon">成本价格</span>
            <input type="text" name="P_price2" class="form-control" id="P_price2" disabled>
            <span class="input-group-addon">元</span>
        </div></p>

        <p><div class="input-group">
            <span class="input-group-addon">设置售价</span>
            <input type="text" name="P_price" class="form-control">
            <span class="input-group-addon">元</span>
        </div></p>


        <p><div class="input-group"><span class="input-group-addon">设置分类</span><select name="P_sort" class="form-control">
                              <?php
                                $list=getlist("select * from sl_psort where S_del=0 and S_mid=$M_id order by S_order,S_id desc");
                                foreach($list as $row){
                                    if($P_sort==$row["S_id"]){
                                      $selected="selected";
                                    }else{
                                      $selected="";
                                    }
                                    echo "<option value=\"".$row["S_id"]."\" ".$selected.">".$row["S_title"]."</option>";
                                }
                              ?>

                            </select></div></p>
                            <p style="color:#f60">说明：发货内容及售后均由对接的商品店铺提供。</p>
                          </form>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="button" class="btn btn-primary" onclick="save()">对接</button>
      </div>
    </div>
  </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 src="../js/member.js"></script>
<script>
  function abutment(id,title,pic,price2){
    $("#P_from").val(id);
    $("#P_title").val(title);
    $("#P_price2").val(price2);
    $("#P_pic").attr("src","../media/"+pic);
    $('#myModal').modal('show');
  }
  function save(){
    $.ajax({
      url:'?action=save',
      type:'post',
      data:$("#form").serialize(),
      success:function (data) {
        data=JSON.parse(data);
        lightyear.loading('hide');
        if(data.msg=="success"){
          lightyear.notify('保存成功', 'success', 100);
          $('#myModal').modal('hide');
        }else{
          lightyear.notify(data.msg, 'danger', 100);
        }
      }
    });
  }
  function loadData(num) {
            $("#PageCount").val("<?php echo $P_all?>");
        }
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?>);

});
</script>
</body>
</html>