<style>
.clear {clear: both;}
.train-summary{ font-size:12px; margin-bottom:10px;}
.train-summary span{ color:#9a0918;}
a:link.seat-a{background-image:url(/css/images/train/a-seat.jpg); background-repeat:no-repeat; width:131px; display:block; height:42px; float:left;}
a:hover.seat-a{background-image:url(/css/images/train/a-seath.jpg);}
.selected_seat-a{background-image:url(/css/images/train/a-seata.jpg)!important; background-repeat:no-repeat; width:55px; display:block; height:42px; float:left;}
a:link.seat-b{background-image:url(/css/images/train/b-seat.jpg); background-repeat:no-repeat; text-decoration:none; width:55px; display:block; height:42px; float:left;}
a:hover.seat-b{background-image:url(/css/images/train/b-seath.jpg);}
.selected_seat-b{background-image:url(/css/images/train/b-seata.jpg)!important; background-repeat:no-repeat; width:55px; display:block; height:42px; float:left;}
a:link.seat-c{background-image:url(/css/images/train/c-seat.jpg); background-repeat:no-repeat; width:131px; display:block; height:42px; float:left;}
a:hover.seat-c{background-image:url(/css/images/train/c-seath.jpg);}
.selected_seat-c{background-image:url(/css/images/train/c-seata.jpg)!important; background-repeat:no-repeat; width:55px; display:block; height:42px; float:left;}
a:link.seat-d{background-image:url(/css/images/train/d-seat.jpg); background-repeat:no-repeat; width:55px; display:block; height:42px; float:left;}
a:hover.seat-d{background-image:url(/css/images/train/d-seath.jpg);}
.selected_seat-d{background-image:url(/css/images/train/d-seata.jpg)!important; background-repeat:no-repeat; width:136px; display:block; height:42px; float:left;}
a:link.seat-f{background-image:url(/css/images/train/f-seat.jpg); background-repeat:no-repeat; width:136px; display:block; height:42px; float:left;}
a:hover.seat-f{background-image:url(/css/images/train/f-seath.jpg);}
.selected_seat-f{background-image:url(/css/images/train/f-seata.jpg)!important; background-repeat:no-repeat; width:136px; display:block; height:42px; float:left;}
a:link.sleep-a {background-image:url(/css/images/train/l-up.jpg); background-repeat:no-repeat; width:163px; display:block; height:38px; float:left; }
a:hover.sleep-a { background-image:url(/css/images/train/l-upa.jpg); } 
.selected_sleep-a { background-image:url(/css/images/train/l-upa.jpg)!important; width:163px; display:block; height:38px; float:left;  }
a:link.sleep-b {background-image:url(/css/images/train/r-up.jpg); background-repeat:no-repeat; width:104px; display:block; height:38px; float:left; }
a:hover.sleep-b { background-image:url(/css/images/train/r-upa.jpg); } 
.selected_sleep-b { background-image:url(/css/images/train/r-upa.jpg)!important; width:163px; display:block; height:38px; float:left;  }
a:link.sleep-c {background-image:url(/css/images/train/l-mid.jpg); background-repeat:no-repeat; width:163px; display:block; height:38px; float:left; }
a:hover.sleep-c { background-image:url(/css/images/train/l-mida.jpg); } 
.selected_sleep-c { background-image:url(/css/images/train/l-mida.jpg)!important; width:163px; display:block; height:38px; float:left;  }
a:link.sleep-d {background-image:url(/css/images/train/r-mid.jpg); background-repeat:no-repeat; width:104px; display:block; height:38px; float:left; }
a:hover.sleep-d { background-image:url(/css/images/train/r-mida.jpg); } 
.selected_sleep-d { background-image:url(/css/images/train/r-mida.jpg)!important; width:163px; display:block; height:38px; float:left;  }
a:link.sleep-e {background-image:url(/css/images/train/l-low.jpg); background-repeat:no-repeat; width:163px; display:block; height:38px; float:left; }
a:hover.sleep-e { background-image:url(/css/images/train/l-lowa.jpg); } 
.selected_sleep-e { background-image:url(/css/images/train/l-lowa.jpg)!important; width:163px; display:block; height:38px; float:left;  }
a:link.sleep-f {background-image:url(/css/images/train/r-low.jpg); background-repeat:no-repeat; width:104px; display:block; height:38px; float:left; }
a:hover.sleep-f { background-image:url(/css/images/train/r-lowa.jpg); } 
.selected_sleep-f { background-image:url(/css/images/train/r-lowa.jpg)!important; width:104px; display:block; height:38px; float:left;  }
</style>
<script>
$(function(){
	//var selected = $('.selectticket').find('.selected').length;
	//$('.selected_People').html(selected);
});

function selseat(seat){
	var type = $(seat).attr('type');
	var total = $(seat).parent().parent().find('.train-summary .seat_TotalPeople').html();
	if(total>=10){
		total = 10;
		$('.seat_TotalPeople').html(total);
	}
	var count = $(seat).parent().parent().find('.selected').length;
	console.log('执行之前的数量'+count);
	//处理选座事件
	
	if($(seat).hasClass('selected_'+type)){
		$(seat).removeClass('selected');
		$(seat).removeClass('selected_'+type);
		count = $(seat).parent().parent().find('.selected').length;
		$('.selected_People').html(count);
		console.log('减掉之后'+count);
	}else{
		if(count >= total){
			alert('You already chose seats for all the passengers.');
		}else{
			$(seat).addClass('selected_'+type);
			$(seat).addClass('selected');
			count = $(seat).parent().parent().find('.selected').length;
			$('.selected_People').html(count);
			console.log('增加之后'+count);
		}
		
	}	
	
}
</script>
<div style="width:90%;margin:30px auto;">
  <div class="panel panel-primary">
      <div class="panel-heading">
        <h3 class="panel-title">翰特订单号&nbsp;<a style="margin-left:50px;" target='_blank' href="<?php echo site_url('apps/trainsystem/pages/order_list');?>">订单列表>></a><a style="margin-left:50px;" target='_blank' href="<?php echo site_url('apps/trainsystem/pages/presale_list');?>">预售时间维护>></a> <a style="margin-left:50px;" target='_blank' href="<?php echo site_url('apps/trainsystem/pages/lanhaiExport');?>">澜海导出账单>></a><span style="margin-left:200px;">版本：V2.0</span><!--<span class="pull-right">聚合余额(RMB)：<?php echo $balance;?></span> --></h3>
        <!-- <a style="margin-left:50px;" target='_blank' href="<?php echo site_url('apps/trainsystem/pages/reschedule');?>">携程改签>></a> -->
      </div>
      <div class="panel-body">
       <form style="width: 300px;float: left;" action="/info.php/apps/trainsystem/pages/index/" method="post">
        <input type="text" name="ht_order" value="<?php echo isset($cols_id)?$cols_id:""; ?>">
        <button type="submit" id="sub" class="btn btn-warning btn-sm"><span class="glyphicon glyphicon-download-alt"></span> 获取信息</button>  
       </form>
       <p style="margin: 0 0 10px; width: 200px; float: left; line-height: 30px;">外联：<span><?php if(!empty($wl)){echo $wl[0]->OPI_Name;}?></span></p>
      </div>
    </div>
    <div class="panel panel-primary">
      <div class="panel-heading">
        <h3 class="panel-title">火车订单信息</h3>
      </div>
    <div class="panel-body">
      <?php if(!empty($info)):?>
      <?php $num=1; foreach($info as $v):?>
          <table class="table table-bordered table-hover"  style="text-align:center;">
        <thead>
          <tr>
            <th style="text-align:center;">序号</th>
            <th style="text-align:center;">车次</th>
            <th style="text-align:center;">座位</th>
            <th style="text-align:center;">出发城市</th>
            <th style="text-align:center;">抵达城市</th>
            <th style="text-align:center;">发车日期</th>
            <th style="text-align:center;">发车时间</th>
            <th style="text-align:center;">抵达时间</th>
            <th style="text-align:center;">票价</th>
            <th style="text-align:center;">是否提交过</th>
			<th style="text-align:center;">是否接受无座</th>
          </tr>
        </thead>
        <tbody>
        
          <tr>
            <td><?php echo $num++;?></td>
            <td><?php echo $v->train->FlightsNo;?></td>
            <td><?php echo $v->train->Cabin;?></td>
            <td><?php echo $v->train->DepartureCity;?></td>
            <td><?php echo $v->train->ArrivalCity;?></td>
            <td><?php echo $v->train->DepartureDate;?></td>
            <td><?php echo $v->train->DepartureTime;?></td>
            <td><?php echo $v->train->ArrivalTime;?></td>
            <td><?php echo $v->train->adultcost;?></td>
            <td><?php echo !empty($v->status)?"否":"<span style='color:green;'>是</span>";?></td>
            <td><input type="checkbox" name="istanding_<?php echo $v->train->FOI_COLD_SN;?>"/></td>
          </tr>
          <tr>
            <td colspan="11">
              <table class="table table-condensed table-bordered">
                <thead>
                  <tr>
                    <th style="text-align:center;"><input  class="check_people" type="checkbox" /></th>
                    <th style="text-align:center;">序号</th>
                    <th style="text-align:center;">姓名</th>
                    <th style="text-align:center;">护照</th>
                    <th style="text-align:center;">年龄类型</th>
                    <th style="text-align:center;">出生年月</th>
                    <th style="text-align:center;">证件有效期</th>
                    <th style="text-align:center;">国籍</th>
                  </tr>
                </thead>
                <tbody>
                  <?php foreach($v->people as $key=>$p): ?>
                  <tr>
					<?php if ($key < 10){?>
						<td><input name="" type="checkbox" checked="checked" value="<?php echo $p->BPE_SN;?>" num="<?php echo $key;?>"/></td>
					<?php }else{ ?>
						<td><input name="" type="checkbox" value="<?php echo $p->BPE_SN;?>" /></td>
					<?php } ?>		
                    <td><?php echo $key+1;?></td>
                    <td class="people_name"><?php echo $p->BPE_FirstName." ".$p->BPE_MiddleName." ".$p->BPE_LastName;?></td>
                    <td><?php echo $p->BPE_Passport;?></td>
                    <td><?php echo $p->BPE_GuestType==1?"成人":($p->BPE_GuestType==2?"儿童":"婴儿");?></td>
                    <td><?php echo $p->BPE_BirthDate;?></td>
                    <td><?php echo $p->BPE_PassExpdate;?></td>
                    <td><?php  echo $p->Nationality . "(".$p->BPE_Nationality .")"; ?></td>
                  </tr>
                  <?php endforeach;?>
				  <tr style="text-align:;">
                    <td colspan="11"  class="selectticket">
						<?php 
						$traintype = substr($v->train->FlightsNo,0,1);
						$arr = array('C','D','G');
						$sel_count = 0;
						if(in_array($traintype,$arr)){
							$selectseat = '';
							$train_select = $v->train->FOI_SelectedSeat;
							$a1=$b1=$c1=$d1=$f1=$a2=$b2=$c2=$d2=$f2=false;
							if($train_select){
								$obj = explode(',',$train_select);
								foreach($obj as $value){
									switch($value){
										case '1A':
											$a1 = true;
											$sel_count++;
											break;
										case '1B':
											$b1 = true;
											$sel_count++;
											break;
										case '1C':
											$c1 = true;
											$sel_count++;
											break;
										case '1D':
											$d1 = true;
											$sel_count++;
											break;
										case '1F':
											$f1 = true;
											$sel_count++;
											break;
										case '2A':
											$a2 = true;
											$sel_count++;
											break;
										case '2B':
											$b2 = true;
											$sel_count++;
											break;
										case '2C':
											$c2 = true;
											$sel_count++;
											break;
										case '2D':
											$d2 = true;
											$sel_count++;
											break;
										case '2F':
											$f2 = true;
											$sel_count++;
											break;		
									}
								}
							}
							$html = '';
							$html .= '<div class="train-summary">'.$v->train->Cabin.' for '.$v->train->FlightsNo.' <span>(<span class="selected_People">'.$sel_count.'</span> of <span class="seat_TotalPeople">'.count($v->people).'</span> Seats)</span></div>';
							$html .= '<div class="seatPick">';
							if($a1){
								$html .= '<a class="seat-a selected_seat-a selected" type="seat-a" href="javascript:void(0);" data="1A" onclick ="selseat(this)";></a>';
							}else{
								$html .= '<a class="seat-a" type="seat-a" href="javascript:void(0);" data="1A" onclick ="selseat(this)";></a>';
							}
							
							if($v->train->Aircraft == 'O' || $v->train->Aircraft == '8'){
								if($b1){
									$html .= '<a class="seat-b selected_seat-b selected" type="seat-b" href="javascript:void(0);" data="1B" onclick ="selseat(this);"></a>';
								}else{
									$html .= '<a class="seat-b" type="seat-b" href="javascript:void(0);" data="1B" onclick ="selseat(this);"></a>';
								}
								
							}
							if($c1){
								$html .= '<a class="seat-c selected_seat-c selected" type="seat-c" href="javascript:void(0);" data="1C" onclick ="selseat(this);"></a>';
							}else{
								$html .= '<a class="seat-c" type="seat-c" href="javascript:void(0);" data="1C" onclick ="selseat(this);"></a>';
							}
							
							if($v->train->Aircraft != '9'){
								if($d1){
									$html .= '<a class="seat-d selected_seat-d selected" type="seat-d" href="javascript:void(0);" data="1D" onclick ="selseat(this);"></a>';
								}else{
									$html .= '<a class="seat-d" type="seat-d" href="javascript:void(0);" data="1D" onclick ="selseat(this);"></a>';
								}
								
							}
							if($f1){
								$html .= '<a class="seat-f selected_seat-f selected" type="seat-f" href="javascript:void(0);" data="1F" onclick ="selseat(this);"></a>';
							}else{
								$html .= '<a class="seat-f" type="seat-f" href="javascript:void(0);" data="1F" onclick ="selseat(this);"></a>';
							}
							
							$html .= '<div class="clear"></div></div>';
							$html .= '<div class="seatPick">';
							if($a2){
								$html .= '<a class="seat-a selected_seat-a selected" type="seat-a" href="javascript:void(0);" data="2A" onclick ="selseat(this)";></a>';
							}else{
								$html .= '<a class="seat-a" type="seat-a" href="javascript:void(0);" data="2A" onclick ="selseat(this)";></a>';
							}
							
							if($v->train->Aircraft == 'O' || $v->train->Aircraft == '8'){
								if($b2){
									$html .= '<a class="seat-b selected_seat-b selected" type="seat-b" href="javascript:void(0);" data="2B" onclick ="selseat(this);"></a>';
								}else{
									$html .= '<a class="seat-b" type="seat-b" href="javascript:void(0);" data="2B" onclick ="selseat(this);"></a>';
								}
								
							}
							if($c2){
								$html .= '<a class="seat-c selected_seat-c selected" type="seat-c" href="javascript:void(0);" data="2C" onclick ="selseat(this);"></a>';
							}else{
								$html .= '<a class="seat-c" type="seat-c" href="javascript:void(0);" data="2C" onclick ="selseat(this);"></a>';
							}
							
							if($v->train->Aircraft != '9'){
								if($d2){
									$html .= '<a class="seat-d selected_seat-d selected" type="seat-d" href="javascript:void(0);" data="2D" onclick ="selseat(this);"></a>';
								}else{
									$html .= '<a class="seat-d" type="seat-d" href="javascript:void(0);" data="2D" onclick ="selseat(this);"></a>';
								}
								
							}
							if($f2){
								$html .= '<a class="seat-f selected_seat-f selected" type="seat-f" href="javascript:void(0);" data="2F" onclick ="selseat(this);"></a>';
							}else{
								$html .= '<a class="seat-f" type="seat-f" href="javascript:void(0);" data="2F" onclick ="selseat(this);"></a>';
							}
							
							$html .= '<div class="clear"></div></div>';
							
							if($v->train->Aircraft != 'F'){
								echo $html;
							}
						}
						?>
                    </td>
                  </tr>
                  <tr style="text-align:;">
                    <td>
                      <button type="button" class="btn btn-success checked_pay" data-order="<?php echo $v->train->FOI_COLD_SN;?>">澜海订票</button>
                    </td>
                    <td colspan="4"  class="biaoqian"><span class="back_mes" style="color:red;line-height: 30px;"></span>
                    </td>
                  </tr>
				  <tr style="text-align:;">
                    <td>
                      <button type="button" class="btn btn-success ctrip_pay" data-order="<?php echo $v->train->FOI_COLD_SN;?>">携程订票</button>
                    </td>
                    <td colspan="4"  class="biaoqian"><span class="ctrip_back_mes" style="color:red;line-height: 30px;"></span>
                    </td>
                  </tr>
				  <tr style="text-align:;">
					<td>
                      <button type="button" class="btn btn-success grab_ticket" data-order="<?php echo $v->train->FOI_COLD_SN;?>">抢票</button>
                    </td>
                    <td colspan="4">
					<span class="grab_config"><a style="text-decoration:none;cursor:pointer;">点击打开配置清单</a></span>
					<p class="grab_mes" style="color:red;line-height: 30px;"></p>
					<table class="table table-condensed table-bordered grab_config_table hidden">
						<tbody>
							<tr>
								<td colspan="2"><span style="float:left">截止时间 : <input type="text" name="deadline" class="date"/> 示例：2018-03-22 17:00:00(必填)</span></td>
								<td colspan="2"><span style="float:left">备选车次 : <input type="text" name="alternate_train"/> 示例：["k10","G10"]</span></td>
								<td colspan="2"><span style="float:left">备选座位 : <input type="text" name="alternate_seat"/> 示例：["1","2"]</span></td>
							</tr>
						</tbody>
					</table>
                    </td>
					
				  </tr>
                  <tr id="back_<?php echo $v->train->FOI_COLD_SN;?>" style="display:none;">
                    <td colspan="5">
                      快捷订票处理结果：<span style="color:red;"></span>
                    </td>
                  </tr>
				   
                </tbody>
              </table>
            </td>
          </tr>
        </tbody>
      </table>
    <?php endforeach;endif;?> 
    </div>
    </div>
</div>
 
<script>
  var url="<?php echo site_url('apps/train/index/submit_juhe_order?').'order=';?>";
  var order_ul="<?php echo site_url('apps/train/index/order?').'order=';?>";//订单详情页面
  
  $(".pay_api").click(function(){
    // alert(url+$(this).attr("data-order"));
    var THIS=$(this);
    var order=$(this).attr("data-order");
	var selectseat = '';
	$(this).parent().parent().next().find('.selected').each(function(){
		if($(this).hasClass('selected')){
			selectseat += $(this).attr('data');
		}
	});
    $.ajax({
        url:url+$(this).attr("data-order")+'&selectseat='+selectseat,
        beforeSend:function(data){
          THIS.html("处理中...");
          THIS.attr("disabled","disabled")
        },
        success:function(data){
          THIS.removeAttr("disabled");
          THIS.html("快捷订票");
            if(data.status==1){
              THIS.parent().html("<a href='"+order_ul+data.order+"' target='_blank'>订单详情</a>");
            }
            
            $("#back_"+order+" span").html(data.mes);
            $("#back_"+order).show();
              
          },
        dataType: "json", 
        
    });    
    return false;
  });

  $(".check_people").click(function(){
      if($(this).is(":checked")){
        $(this).parent().parent().parent().parent().find("input[type=checkbox]").attr("checked","checked");
      }else{
        $(this).parent().parent().parent().parent().find("input[type=checkbox]").removeAttr("checked");
      }
  });
  
  //聚合出票
  $(".checked_pay").click(function(){
    var url2="<?php echo site_url('apps/trainsystem/addorders/booktickets?').'order=';?>";
    var checkbox=$(this).parent().parent().parent().find(":checked");
    var people_sn="";
    checkbox.each(function(i){
      people_sn+=","+$(this).val();
    });
	var selectseat = '';
	$(this).parent().parent().prev().find('.selected').each(function(){
		if($(this).hasClass('selected')){
			selectseat += $(this).attr('data');
		}
	});
	
    people_sn=people_sn.substring(1);
	var cold_sn = $(this).attr('data-order');
	var istanding = $('input[name="istanding_'+cold_sn+'"]').is(':checked');
	
    url2+=$(this).attr("data-order")+"&people="+people_sn+"&selectseat="+selectseat+"&type=lanhai&istanding="+istanding;
	
    var THIS=$(this);
    THIS.parent().parent().find(".back_mes").html(" ");//清空提示
    $.ajax({
        url:url2,
        beforeSend:function(data){
          THIS.html("处理中...");
          THIS.attr("disabled","disabled");
        },
        success:function(data){
          THIS.removeAttr("disabled");
          THIS.html("澜海订票");
            var str = "<a href='http://www.mycht.cn/info.php/apps/trainsystem/pages/order?order="+data.order+"' target='_blank'>"+data.mes+"</a>";
            THIS.parent().parent().find(".back_mes").html(str);
              
          },
        dataType: "json", 
        
    });
	
    return false;
  });
  
  //携程出票
  $(".ctrip_pay").click(function(){
    var ctrip_url="<?php echo site_url('apps/trainsystem/addorders/booktickets?').'order=';?>";
    var checkbox=$(this).parent().parent().parent().find(":checked");
    var people_sn="";
    checkbox.each(function(i){
      people_sn+=","+$(this).val();
    });
	var selectseat = '';
	$(this).parent().parent().prev().prev().find('.selected').each(function(){
		if($(this).hasClass('selected')){
			selectseat += $(this).attr('data')+',';
		}
	});
	selectseat=selectseat.substr(0,selectseat.length-1);
    people_sn=people_sn.substring(1);
    ctrip_url+=$(this).attr("data-order")+"&people="+people_sn+"&selectseat="+selectseat+"&type=ctrip";

    var THIS=$(this);
    THIS.parent().parent().find(".ctrip_back_mes").html(" ");//清空提示
    $.ajax({
        url:ctrip_url,
        beforeSend:function(data){
          THIS.html("处理中...");
          THIS.attr("disabled","disabled")
        },
        success:function(data){
          THIS.removeAttr("disabled");
          THIS.html("携程订票");
            var str = "<a href='http://www.mycht.cn/info.php/apps/trainsystem/pages/order?order="+data.order+"' target='_blank'>"+data.mes+"</a>";
            THIS.parent().parent().find(".ctrip_back_mes").html(str);
              
          },
        dataType: "json", 
        
    });
	
    return false;
  });
  
  //抢票清单
  $('.grab_config').click(function(){
	  var table = $(this).next().next('.grab_config_table');
	  if(table.hasClass('hidden')){
		  table.removeClass('hidden');
	  }else{
		  table.addClass('hidden');
	  }
  });
  
  //开始抢票
  $('.grab_ticket').click(function(){
	var url2="<?php echo site_url('apps/train/tuniu_train/grabTicketBook?').'order=';?>";
    var checkbox=$(this).parent().parent().parent().find(":checked");
    var people_sn="";
    checkbox.each(function(i){
      people_sn+=","+$(this).val();
    });
    people_sn=people_sn.substring(1);
	// var coli_id = $('input[name="ht_order"]').val();
	var deadline = $(this).parent().next().find('.date').val();
	var alternate_train = $('input[name="alternate_train"]').val();
	var alternate_seat = $('input[name="alternate_seat"]').val();
    url2+=$(this).attr("data-order")+"&people="+people_sn+"&deadline="+deadline+"&alternate_train="+alternate_train+"&alternate_seat="+alternate_seat;
	
    var THIS=$(this);
    THIS.parent().parent().find(".back_mes").html(" ");//清空提示
    $.ajax({
        url:url2,
        beforeSend:function(data){
          THIS.html("处理中...");
          THIS.attr("disabled","disabled");
        },
        success:function(data){
          THIS.removeAttr("disabled");
          THIS.html("抢票");
          THIS.parent().next().find(".grab_mes").html(data.mes);
              
          },
        dataType: "json", 
        
    });
        
    return false;
  });
</script>