<extend name="Public/base" />

<block name="body">
  <link rel="stylesheet" href="__CSS__/select2.min.css" type="text/css" />
  <link rel="stylesheet" type="text/css" href="__CSS__/admin_table.css" media="all">
  <link href="__STATIC__/icons_alibaba/iconfont.css" rel="stylesheet">
  <script type="text/javascript" src="__STATIC__/uploadify/jquery.uploadify.min.js"></script>
  <script type="text/javascript" src="__STATIC__/provincecityarea/AreaData_min.js"></script>
  <script src="__STATIC__/layer/layer.js"></script>
  <script type="text/javascript" src="__JS__/select2.min.js"></script>

  <style>
    .tabcon1711 input.time {
      width: 150px;
    }
    #form .txt_area {
      width: 300px;
      height: 150px;
    }
    .tabcon1711 .form_unit {
      margin-left: 2px;
    }
    .tabcon1711 .mustmark {
      margin-left:-7px;
    }
    .list-ratio {
      display: table;
    }
    .list-ratio .li-ratio {
      display: flex;
      margin-bottom: 20px;
      align-items: center;
    }
    .list-ratio .li-ratio .turnover, .list-ratio .li-ratio .turnover-ratio {
      position: relative;
    }
    .list-ratio .li-ratio .turnover span, .list-ratio .li-ratio .turnover-ratio .error-message {
      color: red;
      position: absolute;
      left: 0;
      top: 30px;
      white-space: nowrap;
      display: none;
    }
    .iconfont-btn {
      cursor: pointer;
    }
    .iconfont-style {
      font-size: 18px;
      color: #fff;
      border-radius: 4px;
      border: 0;
      padding: 5px;
      margin-left: 10px;
    }
    .iconfont-selected {
      background-color: #0A9AF2;
    }
    .iconfont-selected:hover {
      background-color: #03a9f4;
    }
    .iconfont-unselected {
      background-color: #999;
    }
    .iconfont-unselected:hover {
      background-color: #ababab;
    }
    
    input[type=number]{
      padding: 4px 6px;
      font-size: 12px;
      line-height: 20px;
      color: #555;
      vertical-align: middle;
      -webkit-border-radius: 4px;
      -moz-border-radius: 4px;
      border-radius: 4px;
      background-color: #fff;
      border: 1px solid #ccc;
      -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,0.075);
      -moz-box-shadow: inset 0 1px 1px rgba(0,0,0,0.075);
      box-shadow: inset 0 1px 1px rgba(0,0,0,0.075);
      -webkit-transition: border linear .2s, box-shadow linear .2s;
      -moz-transition: border linear .2s, box-shadow linear .2s;
      -o-transition: border linear .2s, box-shadow linear .2s;
      transition: border linear .2s, box-shadow linear .2s;
    }
  </style>
  <div class="cf main-place top_nav_list navtab_list">
    <h3 class="page_title">新增公司绑定</h3>
<!--    <p class="description_text">说明:此功是创建推广员时所需填写信息</p>-->
  </div>

  <!-- 标签页导航 -->
  <div class="tab-wrap">
    <div class="tab-content tabcon1711" >
      <!-- 表单 -->
      <form id="form" action="{:U('addRelation')}" method="post" class="form-horizontal" >
        <div style="display: flex;">
          <!-- 基础文档模型 -->
          <div id="tab1" class="tab-pane in tab1">
            <table  border="0" cellspacing="0" cellpadding="0">
              <tbody>
                <tr>
                  <td class="l"><i class="mustmark">*</i>甲方公司类型:</td>
                  <td class="r">
                    <select name="first_company_type" id="first_company_type" class="select_gallery">
                    <option value="">请选择甲方公司类型</option>
                      <volist name="companyType" id="vo">
                        <option  value="{$key}" <?php if($defaultCompanyTypeA == $key):?>selected<?php endif;?>>{$vo}</option>
                      </volist>
                    </select>
                    <span class="notice-text"></span>
                  </td>
                </tr>


              <tr>
                <td class="l"><i class="mustmark">*</i>甲方公司:</td>
                <td class="r promote_id">
                  <select name="first_company_id" id="first_company_id" class="select_gallery">
                    <option value="">请先选择甲方公司类型</option>
                  </select>
                </td>
              </tr>
              <input type="hidden" name="first_company_name" id="first_company_name" value="" />

              <tbody id="first_company_info">
                
               </tbody>

              </tbody>
            </table>
          </div>

          <div id="tab2" class="tab-pane in tab2">
            <table  border="0" cellspacing="0" cellpadding="0">
              <tbody>
                <tr>
                  <td class="l"><i class="mustmark">*</i>乙方公司类型:</td>
                  <td class="r">
                    <select name="second_company_type" id="second_company_type" class="select_gallery">
                    <option value="">请选择乙方公司类型</option>
                      <volist name="companyType" id="vo">
                        <option  value="{$key}" <?php if($defaultCompanyTypeB == $key):?>selected<?php endif;?>>{$vo}</option>
                      </volist>
                    </select>
                    <span class="notice-text"></span>
                  </td>
                </tr>


                <tr>
                  <td class="l"><i class="mustmark">*</i>乙方公司:</td>
                  <td class="r promote_id">
                    <select name="second_company_id" id="second_company_id" class="select_gallery">
                      <option value="">请先选择乙方公司</option>
                    </select>
                  </td>
                </tr>
                <input type="hidden" name="second_company_name" id="second_company_name" value="" />
                <tbody id="second_company_info">
                
                </tbody>

              </tbody>
            </table>
          </div>
        </div>
        <div style="width: 80%;height: 1px;border-bottom: 1px solid #e6e6e6;margin: 10px;"></div>
        <div id="tab3" class="tab-pane in tab3">
          <table  border="0" cellspacing="0" cellpadding="0">
            <tbody>
              <tr>
                <td class="l"><i class="mustmark">*</i>结算周期:</td>
                <td class="r">
                  <select name="settlement_type" id="settlement_type" class="select_gallery" >
                      <option  value="0">无</option>
                      <option  value="1" selected>周结</option>
                      <option  value="2">月结</option>
                  </select>
                  <span class="notice-text"></span>
                </td>
              </tr>
              <tr>
                <td class="l"><i class="mustmark">*</i>开票类型:</td>
                <td class="r">
                  <select name="invoice_type" id="invoice_type" class="select_gallery">
                      <option  value="0" selected>无</option>
                      <option  value="1">专票</option>
                      <option  value="2">普票</option>
                  </select>
                  <span class="notice-text"></span>
                </td>
              </tr>

              <tr>
                <td class="l">开票内容:</td>
                <td class="r table_radio">
                  <input type="text" id="invoice_content" class="input" value="信息服务费" >
                  <input type="hidden" name="invoice_content" id="invoice_content_other" class="input invoice_content" value="信息服务费" >
                </td>
              </tr>

             <tr>
              <td class="l"><i class="mustmark">*</i>收款方:</td>
              <td class="r">
                <select name="collection" id="collection" class="select_gallery">
                    <option  value="1" >甲方</option>
                    <option  value="2" selected>乙方</option>
                </select>
                <span class="notice-text"></span>
              </td>
            </tr>

            
            <tr>
              <td class="l">备注:</td>
              <td class="r table_radio">
                <textarea type="text" name="remark" id="remark" class="txt_area"></textarea>
                <span class="notice-text"></span>
              </td>
            </tr>
            </tbody>
          </table>
        </div>
        <div class="form-item cf">
          <button class="submit_btn mlspacing" id="submit" type="submit" target-form="form-horizontal">
            保存
          </button>
          <a class="submit_btn " alt="返回上一页" title="返回上一页" href="javascript:window.history.back();" >
            返回
          </a>
        </div>
      </form>
    </div>
  </div>
</block>

<block name="script">
  <link href="__STATIC__/datetimepicker/css/datetimepicker.css" rel="stylesheet" type="text/css">
  <php>if(C('COLOR_STYLE')=='blue_color') echo '<link href="__STATIC__/datetimepicker/css/datetimepicker_blue.css" rel="stylesheet" type="text/css">';</php>
  <link href="__STATIC__/datetimepicker/css/dropdown.css" rel="stylesheet" type="text/css">
  <script type="text/javascript" src="__STATIC__/datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
  <script type="text/javascript" src="__STATIC__/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js" charset="UTF-8"></script>
  <script src="__STATIC__/czinputcheck.js?v=1.29" type="text/javascript"></script>
<script type="text/javascript">
//导航高亮
highlight_subnav("{:U('index')}");
$(".select_gallery").select2();
var first_company={};
var second_company={};
var first_company_type=0;
var second_company_type=0;

$(function(){
    //
    $('#submit').click(function (e) {
        //查看是否报错
        var flag = true;
        var obj = {
            "first_company_type":["required","甲方公司类型不能为空","select"],
            "first_company_id":["required","甲方公司不能为空","select"],
            "second_company_type":["required","乙方公司类型不能为空","select"],
            "second_company_id":["required","乙方公司不能为空","select"]
        }
        var cres = CIC.checkAddInput(obj);
        if(!cres){
            return false;
        }

        if(cres.first_company_type == cres.second_company_type){
          layer.msg("合作甲乙双方不能是同类型公司", {icon: 2});
          return false;
        }
        if(cres.first_company_type != 0 && cres.second_company_type !=0){
          layer.msg("合作甲乙双方必须有个是己方公司", {icon: 2});
          return false;
        }
        $("select[disabled]").each(function() {
          if (parseInt($(this).val()) != -1) {
            $(this).attr("disabled", false);
          }
        });
        
        var target = $('form').get(0).action;
        var query = $('form').serialize();
        var that = this;
        $(that).addClass('disabled').attr('autocomplete','off').prop('disabled',true);
          $.post(target,query).success(function(data){
              if(layer) {layer.closeAll('loading');}
              if (data.code==1) {
                  if (data.url) {
                      updateAlert(data.msg + ' 页面即将自动跳转~');
                  }else{
                      updateAlert(data.msg);
                  }
                  setTimeout(function(){
                      $(that).removeClass('disabled').prop('disabled',false);
                      if (data.url) {
                          location.href=data.url;
                      }else if( $(that).hasClass('no-refresh')){
                          $('#tip').find('.tipclose').click();
                      }else{
                          location.reload();
                      }
                  },1500);
              }else{
                $(that).removeClass('disabled').prop('disabled',false);
                layer.msg(data.msg,{icon: 2});
              }
          });
      });
    
    $("#first_company_type").on("change",function(){
        //置空
        $("#first_company_info").html('');
        $("#first_company_name").val('');
        //逻辑
        var type =  $(this).find("option:selected").val();
        first_company_type = type;
        var typename=setTypeName(type);
        var url = "{:U('getCompanyList')}"+"&type="+type;
        $.get(url,function(data){
            first_company = data.info;
            var companystr = '<option value="">'+typename+'</option>';
            for (var i in first_company) {
              var selected = ''
              if (first_company[i].company_name == '福建金菠菜网络科技有限公司') {
                selected = 'selected'
              }
              companystr += "<option value='" + first_company[i].id + "' " + selected + ">" + first_company[i].company_name + "</option>"
            }
            $("#first_company_id").html(companystr);
            $("#first_company_id").select2();
            $("#first_company_id").change();
        })
    })
    $("#first_company_id").on("change",function(){
        var id =  $(this).find("option:selected").val();
        //获取基础信息
        if(id == ''){return ;}
        var companyinfo = first_company.find(function(x){
            return x.id == id;
        })
      console.log(companyinfo)
      if (companyinfo.type) {
        setAggregateInfo(companyinfo.invoice_item,companyinfo.withdraw_type,companyinfo.invoice_type,1);
      }

        $("#first_company_name").val(companyinfo.company_name);
        $("#first_company_info").html(setCompanyInfo(companyinfo));
    });
    $("#first_company_type").change();

    $("#second_company_type").on("change",function(){
        //置空
        $("#second_company_info").html('');
        $("#second_company_name").val('');
        //逻辑
        var type =  $(this).find("option:selected").val();
        second_company_type = type;
        var typename=setTypeName(type,"second");
        var url = "{:U('getCompanyList')}"+"&type="+type;
        $.get(url,function(data){
            second_company = data.info;
            var companystr = '<option value="">'+typename+'</option>';
            for (var i in second_company) {
                companystr += "<option value='" + second_company[i].id + "'>" + second_company[i].company_name + "</option>"
            }
            $("#second_company_id").html(companystr);
            $("#second_company_id").select2();
        })
    })
    $("#second_company_type").change();
    $("#second_company_id").on("change",function(){
        var id =  $(this).find("option:selected").val();
        //获取基础信息
        if(id == ''){return ;}
        var companyinfo = second_company.find(function(x){
            return x.id == id;
        })

      if (companyinfo.type) {
        setAggregateInfo(companyinfo.invoice_item,companyinfo.withdraw_type,companyinfo.invoice_type,2);
      }

        $("#second_company_name").val(companyinfo.company_name);
        $("#second_company_info").html(setCompanyInfo(companyinfo));
    });

  $("#collection").on("change",function() {
    var get_collection = $(this).find("option:selected").val();
    var companyinfo;
    var id;

    if (get_collection == 1) {
      id =  $("#first_company_id").find("option:selected").val();
      companyinfo = first_company.find(function(x){
        return x.id == id;
      });
    } else if (get_collection == 2) {
      id =  $("#second_company_id").find("option:selected").val();
      companyinfo = second_company.find(function(x){
        return x.id == id;
      });
    }
    setAggregateInfo(companyinfo.invoice_item,companyinfo.withdraw_type,companyinfo.invoice_type,get_collection);
  });

  $('#invoice_content').on("change",function() {
    $('#invoice_content').val($(this).val());
  });

    function setAggregateInfo(invoice_content,settlement_type,invoice_type,collection) {
      var get_collection = $("#collection").find("option:selected").val();
      if (collection == get_collection) {


        $('.invoice_content').val(invoice_content);
        $('#invoice_content').val(invoice_content);

        $('#settlement_type').find("option").each(function() {
          $(this).removeAttr("selected");
        });
        // if (!settlement_type) {
        //   $('#settlement_type').removeAttr("disabled");
        // } else {
        //   $('#settlement_type').prop("disabled",true);
        // }
        $('#invoice_type').find("option").each(function() {
          $(this).removeAttr("selected");
        });

        // if (!invoice_type) {
        //   $('#invoice_type').removeAttr("disabled");
        // } else {
        //   $('#invoice_type').prop("disabled",true);
        // }
        $("#settlement_type").find("option[value="+settlement_type+"]").prop("selected",true);

        $("#invoice_type").find("option[value="+invoice_type+"]").prop("selected",true);
        $("#settlement_type").change();
        $("#invoice_type").change();

      }

    }

    function setCompanyInfo(companyinfo){
        var companyinfostr = '';
        if("company_belong" in companyinfo){
            companyinfostr += '<tr><td class="l">内外团:</td><td class="r table_radio"><span class="form_radio table_btn">'+companyinfo.company_belong+'</span></td></tr>';
        }
        if("company_type" in companyinfo){
            companyinfostr += '<tr><td class="l">结算性质:</td><td class="r table_radio"><span class="form_radio table_btn">'+companyinfo.company_type+'</span></td></tr>';
        }
        if("company_relation" in companyinfo){
            companyinfostr += '<tr><td class="l">开发类型:</td><td class="r table_radio"><span class="form_radio table_btn">'+companyinfo.company_relation+'</span></td></tr>';
        }
        if("link_man" in companyinfo){
            companyinfostr += '<tr><td class="l">联系人:</td><td class="r table_radio"><span class="form_radio table_btn">'+companyinfo.link_man+'</span></td></tr>';
        }
        if("link_phone" in companyinfo){
            companyinfostr += '<tr><td class="l">联系电话:</td><td class="r table_radio"><span class="form_radio table_btn">'+companyinfo.link_phone+'</span></td></tr>';
        }
        return companyinfostr;
    }


    function setTypeName(type,company="first") {
        var typename = '';
        switch (type) {
            case '':
                typename = (company == "first" ? "请先选择甲方公司类型" : "请先选择乙方公司类型");
                break;
            case '0':
                typename = "请选择已方公司";
                break;
            case "1":
                typename = "请选择上游CP公司";
                break;
            case "2":
                typename ="请选择联运下游渠道公司";
                break;
            case "3":
                typename ="请选择聚合下游渠道公司";
                break;
            default:
                break;
        }
        return typename;
    }

    
});
</script>
</block>