<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ include file="/WEB-INF/include/taglib.jsp"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <script src="/static/weixin/common/layer_mobile/layer.js"></script>
    <link rel="stylesheet" href="/static/weixin/common/layer_mobile/need/right.css">
    <link rel="stylesheet" href="${ctxwxsta}/css/public.css">
    <link rel="stylesheet" href="${ctxwxsta}/css/basic.css">
    <link rel="stylesheet" href="${ctxwxsta}/css/SG_area_select.css?v=1.0.6">
    <script src="${ctxwxsta}/js/flexible.js"></script>
    <title>设置销售区域</title>
</head>
<body id="contain">
<div class="contain">
    <div class="bai-head">
        <span>设置销售区域</span>
        <a href="javascript:;" id="erty"  class="left">
            <img src="${ctxwxsta}/images/head-left.png"/>
        </a>
    </div>
    <form method="post" id="defaultForm">
        <input type="hidden" name="companyId"  id="companyId" value="${sysCompany.id}" >
        <input type="hidden" name="cid"  id="cid" value="${cid}" >

        <ul class="sales-area" id="sales-area">

        </ul>
    </form>
    <div class="add-sales" id="selectBtn">
        <span>
            <img src="${ctxwxsta}/images/jia.png" />
        </span>
        <i>添加可销售区域</i>
    </div>
</div>
<c:if test="${not empty newType}">
    <div class="pro-steps assigned-steps">
        <div class="steps">
            <button class="xia" type="button" onclick="tijiao()">确认提交</button>
            <button onclick="fanhui(1)" type="button">上一步</button>
        </div>
    </div>
</c:if>

<c:if test="${ empty newType}">
    <div class="pro-steps assigned-steps">
        <div class="steps">
            <button class="xia" type="button" onclick="tijiao()">确认提交</button>
        </div>
    </div>
</c:if>
<!--设置弹窗-->
<p id="erty_dan"></p>
<div class="tooltip-box">
    <p>限制销售区域的修改未保存，是否继续返回</p>
    <div class="tooltip-button">
        <button class="sure" type="button"  onclick="fanhui(0)">确定</button>
        <button class="no-qu">取消</button>
    </div>
</div>
<div class="tooltip-grm" >
    <p id="error_msg">操作成功</p>
    <div class="tooltip-button">
        <button class="sure" type="button"R>确定</button>
    </div>
</div>
<!--黑色蒙版弹窗-->
<div class="block-mask-more"></div>

<script src="${ctxwxsta}/js/jquery-1.8.3.min.js"></script>

<script src="${ctxwxsta}/js/iscroll.js"></script>
<script src="${ctxwxsta}/js/sales_area_select.js"></script>
<script src="${ctxwxsta}/js/basicTwo.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#selectBtn').on('click', function () {
            $.areaSelect();
        });
        area();
        initArea();
    });

    $(function () {
        var tool_height = parseInt($('.tooltip-box').height()/2);
        $('.tooltip-box').css({"margin-top":-tool_height+'px'});

        $('#erty').click(function () {
            $('.tooltip-box').css({"display":"block"});
            $('.block-mask-more').css({"display":"block"});
        });

        $('#erty_dan').click(function () {
            $('.tooltip-grm').show();
            $('.block-mask-more').css({"display":"block"});
        });
        $('.tooltip-grm .sure').click(function () {
            $('.tooltip-grm').hide();
            $('.block-mask-more').hide();
        });

        $('.tooltip-box button').click(function () {
            $('.tooltip-box').hide();
            $('.block-mask-more').hide();
        });



    })


    function  area(){
        var  companyId= $("#cid").val();
        $.ajax({
            type: 'POST',
            url: "/weixin/partner/getParentArea",
            data:{companyId:companyId},
            dataType:"json",
            async: false,
            success: function(data){
                console.log(JSON.stringify(data));
                if(data.rows.length>0){

                }else{
                          layer.open({
                              content: '配置下级区域限制权限不足，请联系客服400-662-9696。'
                              ,btn: ['确定']
                              ,yes: function(index){
                                   fanhui(0);
                              }
                          });
                }
            }
        });
    }


    /**
     * 初始化方法
     */
    function  initArea(){

          var li,el;
          el = document.getElementById('sales-area');
             var  companyId= $("#companyId").val();
              $.ajax({
                  type: 'POST',
                  url: "/weixin/partner/getAreaList",
                  data:{companyId:companyId},
                  dataType:"json",
                  async: false,
                  success: function(data){
                      console.log(JSON.stringify(data));
                      if(data.rows.length>0){
                          for (var i=0;i<data.rows.length;i++)
                          {
                              var html ="";
                              li = document.createElement('li');
                              citytype=data.rows[i].citytype.citytype==undefined?" 不限": " "+data.rows[i].citytype.citytype+" ";
                              city =data.rows[i].city.city==undefined?" 不限":" "+data.rows[i].city.city+" ";
                              html= '<p>'+data.rows[i].province.province+citytype +city+'</p>'
                              +  '<a onclick="removeSales(this)"><img src="${ctxwxsta}/images/trash-gray.png" />'
                              +  ' <input  type="hidden" value="'+data.rows[i].province.id+'" name="province">'
                              +  ' <input  type="hidden" value="'+data.rows[i].citytype.id+'" name="city">'
                              +  ' <input  type="hidden" value="'+data.rows[i].city.id+'" name="district">'
                              +  '  </a>';
                              li.innerHTML=html;
                              el.appendChild(li,el.childNodes[0]);
                          }

                      }else{

//                          layer.open({
//                              content: '配置下级区域限制权限不足，请联系客服400-662-9696。'
//                              ,btn: ['确定']
//                              ,yes: function(index){
//                                   fanhui(0);
//                              }
//                          });
                      }
                  }
              });

          }


        function  tijiao(){
            var  companyId= $("#companyId").val();
            var  newType='${newType}';

            layer.open({type: 2});

            //确认提交代码
                $.ajax({
                    type: 'POST',
                    url: "/weixin/partner/saveAreaList",
                    data:$('#defaultForm').serialize(),
                    dataType:"json",
                    async: false,
                    success: function(data){
                        console.log(JSON.stringify(data));
        //                alert(data.success);
                        if(data.success){

                            if(newType){
                                window.location.href="/weixin/partner/account/"+companyId+"?newType=1";
                            }else{
                                window.location.href="/weixin/partner/detatil/"+companyId;
                            }
                        }else{
                            layer.open({content:"操作失败！",btn: '我知道了'});
                        }
                    }
                });
        }

        function fanhui(type) {
            var  companyId =$("#companyId").val();
            if(type=='0'){
                //弹窗
                window.location.href="/weixin/partner/detatil/"+companyId;
            }
            if(type=='1'){
                //返回上一步
                window.location.href="/weixin/product/distributionList/"+companyId+"?newType=1";
            }

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

