<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

//[{"ticket_no":"E855807446107001B","passengername":"陈斯坤","passporttypeseid":"1","passportseno":"450521199406098313"}]

class Index extends CI_Controller{
    
    private $order_status_msg;//订单状态说明
    private $key;
    private $cx_api;//查询api
    private $dp_api;//订票api
    private $qxdd_api;//取消订单api
    private $pay_api;//请求出票，支付
    private $refund_api;//线上退票
    private $status_api;//状态查询
    private $code_zw;//座次信息
    private $piaotype;//票种信息
    private $passportty;//证件类型信息

    public function __construct() 
    {
        // header("Content-Type: text/html;charset=utf-8");       
        parent::__construct();
        $this->config->load('config');
        $this->order_status_msg=$this->config->item('train_order_status_msg');
        $this->key=JUHE_TRAIN_API_KEY;
        $this->cx_api=JUHE_TRAIN_CX_API;
        $this->dp_api=JUHE_TRAIN_DP_API;
        $this->qxdd_api=JUHE_TRAIN_CANCEL_API;
        $this->pay_api=JUHE_TRAIN_PAY_API;
        $this->refund_api=JUHE_TRAIN_REFUND_API;
        $this->status_api=JUHE_TRAIN_STATUS_API;
        $this->balance_api = "http://op.juhe.cn/trainTickets/balance.php";//余额
        $this->code_zw=$this->config->item('train_zw');
        $this->piaotype=$this->config->item('train_piaotype');
        $this->passportty=$this->config->item('train_passportty');

        $this->load->model("BIZ_train_model");//加载模型
    } 
    public function index()
    {
        $this->ht_train_order_info();
         
    	 // $this->load->view('bootstrap3/header');
    	 // $this->load->view('welcome');
    	 // $this->load->view('bootstrap3/footer');
    }
    public function welcome(){
        $this->load->view('bootstrap3/header');
        $this->load->view('welcome');
        $this->load->view('bootstrap3/footer');
    }
    public function export_bk(){
        $from_date=$this->input->post("from_date");
        $to_date=$this->input->post("to_date");
        if(!empty($from_date) && !empty($to_date)){
            $from_date=date("Y-m-d H:i",strtotime($from_date));
            $to_date=date("Y-m-d H:i",strtotime($to_date));
            $r="";//聚合返回的数据
            $string_r="";//写入excel表格数据
            $coli_id="";
            $wl_name="";
            $arr="";//整合完成的数组
            $url=JUHE_TRAIN_EXPORT_API;//请求的url
            $url.="?key=".$this->key;
            $url.="&since=".$from_date;
            $url.="&before=".$to_date;
            $r=$this->get_data($url);
            // $r=mb_convert_encoding($r, "utf-8", "gb2312");
            $r=explode("\n",$r);
            $excel_head='"记录编号","时间","火车票订单号","最新余额","信息","变化值","翰特订单号","外联"';
            $excel_head=mb_convert_encoding($excel_head,"gbk","utf-8");
            $string_r=$excel_head;
            for($i=1;$i<count($r)-1;$i++){
                $r_info=explode(",",$r[$i]);
                $juhe_order=substr($r_info[4], 1,14);
                $coli_id=$this->BIZ_train_model->jh_order_get_coli_id($juhe_order);
                if($coli_id){   
                   $r_info[6]=$coli_id[0]->COLI_ID; 
                   $wl_name = $this->BIZ_train_model->get_operatorinfo($r_info[6]);
                   $r_info[7]=mb_convert_encoding($wl_name[0]->OPI_Name, "gbk", "utf-8");  
                   // $r_info[7]=$wl_name[0]->OPI_Name; 
                }else{
                   $r_info[6]="";
                   $r_info[7]="";
                }
                $r_info[3]=mb_convert_encoding($r_info[3],"utf-8","gbk");

                if(is_numeric(mb_strpos($r_info[3],"扣款"))){
                    $r_info[3]="票款";
                } 
                if(is_numeric(mb_strpos($r_info[3],"扣手续费"))){
                    $r_info[3]="手续费";
                }
                if(is_numeric(mb_strpos($r_info[3],"线上退票成功"))){
                    $r_info[3]="退票费";
                }
                $r_info[3]=mb_convert_encoding($r_info[3],"gbk","utf-8");
                $arr[]=$r_info;
            }
            $ht_order_sort = array();
            $wl_name_sort = array();
            foreach ($arr as $a) {
              $wl_name_sort[] = $a[7];
            }
            foreach ($arr as $a) {
              $ht_order_sort[] = $a[6];
            }
            array_multisort($wl_name_sort, $ht_order_sort,SORT_ASC, SORT_STRING, $arr);
            foreach ($arr as $a) {
              $ht_order_sort[] = $a[6];
              $string_r.="\n"."$a[0],$a[2],$a[4],$a[5],$a[3],$a[1]".',"'.$a[6].'"'.',"'.$a[7].'"';
            }
            $string_r=mb_convert_encoding($string_r,"utf-8","gbk");
            header("Content-type:application/vnd.ms-excel");
            header("Content-Disposition:attachment;filename=juhe_train.csv");
            echo $string_r;die;
        }
        $this->load->view('bootstrap3/header');
        $this->load->view('export');
        $this->load->view('bootstrap3/footer'); 
    }   
    public function export(){
        $from_date=$this->input->post("from_date");
        $to_date=$this->input->post("to_date");
        $examine=$this->input->post("examine");
        $reback=array();//返回的数据
        $reback["from_date"]=$from_date;
        $reback["to_date"]=$to_date;
        $reback["examine"]=$examine;
        if(!empty($from_date) && !empty($to_date)){
            $from_date=date("Y-m-d H:i",strtotime($from_date));
            $to_date=date("Y-m-d H:i",strtotime($to_date));
            $r="";//聚合返回的数据
            $string_r="";//输出
            $coli_id="";
            $wl_name="";
            $arr="";//整合完成的数组,写进excel表的数据
            $url=JUHE_TRAIN_EXPORT_API;//请求的url
            $url.="?key=".$this->key;
            $url.="&since=".$from_date;
            $url.="&before=".$to_date;
            $r=$this->get_data($url);
            $r=explode("\n",$r);
            for($i=1;$i<count($r)-1;$i++){
                $r_info=explode(",",$r[$i]);
                $juhe_order=substr($r_info[4], 1,14);
                $coli_id=$this->BIZ_train_model->jh_order_get_coli_id($juhe_order);
                //去掉数据两边的双引号
                $r_info[2]=substr($r_info[2], stripos($r_info[2],'"')+1,strrpos($r_info[2],'"')-1);
                $r_info[1]=substr($r_info[1], stripos($r_info[1],'"')+1,strrpos($r_info[1],'"')-1);
                $r_info[5]=substr($r_info[5], stripos($r_info[5],'"')+1,strrpos($r_info[5],'"')-1);
                $r_info[6]="";//储存团名
                $r_info[7] = "";//储存外联名
                $r_info[8] = "";//储存coli_id
                if($coli_id){  
                   $r_info[8] = $coli_id[0]->COLI_ID;  
                   $gri_no=$this->BIZ_train_model->get_gri_no($r_info[8]);//团名
                   $wl_name = $this->BIZ_train_model->get_operatorinfo($r_info[8]);
                   
                   if($gri_no){
                     $r_info[6] = $gri_no[0]->GRI_No;
                   }
                   if($wl_name){
                     $r_info[7] = $wl_name[0]->OPI_Name;
                   }
                }
                $r_info[3]=mb_convert_encoding($r_info[3],"utf-8","gbk");

                if(is_numeric(mb_strpos($r_info[3],"充值"))){
                    if(is_numeric(mb_strpos($r_info[3],"扣款"))){
                    $r_info[3]="票款(有充值)";
                    } 
                    if(is_numeric(mb_strpos($r_info[3],"扣手续费"))){
                        $r_info[3]="手续费(有充值)";
                    }
                    if(is_numeric(mb_strpos($r_info[3],"线上退票成功"))){
                        $r_info[3]="退票费(有充值)";
                    }
                }    
                if(is_numeric(mb_strpos($r_info[3],"扣款"))){
                    $r_info[3]="票款";
                } 
                if(is_numeric(mb_strpos($r_info[3],"扣手续费"))){
                    $r_info[3]="手续费";
                }
                if(is_numeric(mb_strpos($r_info[3],"线上退票成功"))){
                    $r_info[3]="退票费";
                }
                // $r_info[3]=mb_convert_encoding($r_info[3],"gbk","utf-8");
                $arr[]=$r_info;
            }
            if(empty($examine)){
                header("Content-type:application/vnd.ms-excel");
                header("Content-Disposition:attachment;filename=juhe_train.xml");
                $string_r= $this->load->view("train_transaction_excel",array("arr"=>$arr),TRUE);
                echo $string_r;die;
            }else{
                krsort($arr);//数组倒序
                $reback["data"]=$arr;
            }
            
        }
        $this->load->view('bootstrap3/header');
        $this->load->view('export',$reback);
        $this->load->view('bootstrap3/footer'); 
    }
    //用于外联审核账单，查询我的支付信息
    public function get_ht_my_pay(){
        $coli_id=$this->input->get("id");
        $reback=array();
        $reback["status"]=0;
        if(is_numeric($coli_id)){
            $r=$this->BIZ_train_model->get_train_order_cost($coli_id);
            $reback["status"]=1;
            $reback["datas"]=$r;
        }
        echo json_encode($reback);
        return false;
    }
    public function search(){
        $from=$this->input->post("from");
        $to=$this->input->post("to");
        $date=$this->input->post("date");
        $url = "http://op.juhe.cn/trainTickets/cityCode";
        $post_from = array("stationName" => $from,"key"=>$this->key);
        $post_to = array("stationName" => $to,"key"=>$this->key);
        $output_from=$this->my_post($url,$post_from);
        $output_to=$this->my_post($url,$post_to);
        if($output_from["error_code"]===0 && $output_to["error_code"]===0){//error_code=0代表成功，其他有错，返回错误信息
            // var_dump($output_from["result"]["code"]);var_dump($output_to["result"]["code"]);
            $ticket_data=array("key"=>$this->key,"train_date"=>$date,"from_station"=>$output_from["result"]["code"],"to_station"=>$output_to["result"]["code"]);
            $ticket=$this->my_post("http://op.juhe.cn/trainTickets/ticketsAvailable",$ticket_data);
            
            if($ticket["error_code"]!=0){ //error_code=0代表成功，其他有错，返回错误信息
                die(json_encode(array("status"=>0,"mes"=>$ticket["reason"])));//
            }else{
                $backdata="";
                $backdata= $this->load->view("booking_table",array("result"=>$ticket["result"],"date"=>$date),true); 
                              
                die(json_encode(array("status"=>1,"mes"=>"成功","backdata"=>$backdata))); 
            }
        }else{
            die(json_encode(array("status"=>0,"mes"=>"站点名称错误")));
        }
    }
    public function ch_train_search(){
        $from=$this->input->get("from");
        $to=$this->input->get("to");
        $date=$this->input->get("date");
        $ticket_data=array("key"=>$this->key,"train_date"=>$date,"from_station"=>$from,"to_station"=>$to);
        $ticket=$this->post_data("http://op.juhe.cn/trainTickets/ticketsAvailable",$ticket_data);
        $ticket=json_decode($ticket);
    }
    public function ch_train_search_t(){

        // $seat_key = array(
        //         'A' => @$train_item->gr_num,//高级软卧
        //         'X' => @$train_item->qt_num,//其他
        //         '4' => @$train_item->rw_num,//软卧
        //         '2' => @$train_item->rz_num,//软座
        //         'P' => @$train_item->tz_num,//特等座
        //         '13' => @$train_item->wz_num,//无座
        //         '3' => @$train_item->yw_num,//硬卧
        //         '1' => @$train_item->yz_num,//硬座
        //         'O' => @$train_item->ze_num,//二等座
        //         'M' => @$train_item->zy_num,//一等座
        //         '9' => @$train_item->swz_num//商务座
        //     );        
        // $from=$this->input->post("from");
        // $to=$this->input->post("to");
        // $date=$this->input->post("date");
        $this->load->model("BIZ_train_model");
        //$code=$this->BIZ_train_model->get_train_station_code(array("TRS_StationCN"=>"北京南"));
        //var_dump($code[0]->TRS_Code);die;
        $from=$this->input->get("from_station");
        $to=$this->input->get("to_station");
        $date=$this->input->get("queryDate");

        // $url = "http://op.juhe.cn/trainTickets/cityCode";
        // $post_from = array("stationName" => $from,"key"=>$this->key);
        // $post_to = array("stationName" => $to,"key"=>$this->key);
        // $output_from=$this->my_post($url,$post_from);
        // $output_to=$this->my_post($url,$post_to);
        //if($output_from["error_code"]===0 && $output_to["error_code"]===0){//error_code=0代表成功，其他有错，返回错误信息
            // var_dump($output_from["result"]["code"]);var_dump($output_to["result"]["code"]);
            $ticket_data=array("key"=>$this->key,"train_date"=>$date,"from_station"=>$from,"to_station"=>$to);
            $ticket=$this->post_data("http://op.juhe.cn/trainTickets/ticketsAvailable",$ticket_data);
            $ticket=json_decode($ticket);
            // var_dump($ticket);
            if($ticket->error_code==0){
                $reback='{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{"datas":[';
                $reback_list="";
                foreach ($ticket->result->list as $v) {
                    $seat_key="";//座位代码串
                    $yp_info="";//价格
                    // $seat_key.=$v->gjrw_price!=0?"A":"";
                    // $seat_key.=$v->qtxb_price!=0?"X":"";
                    // $seat_key.=$v->rw_price!=0?"4":"";
                    // $seat_key.=$v->rz_price!=0?"2":"";
                    // $seat_key.=$v->tdz_price!=0?"P":"";
                    // $seat_key.=$v->wz_price!=0?"13":"";
                    // $seat_key.=$v->yw_price!=0?"3":"";
                    // $seat_key.=$v->yz_price!=0?"1":"";
                    // $seat_key.=$v->edz_price!=0?"O":"";
                    // $seat_key.=$v->ydz_price!=0?"M":"";
                    // $seat_key.=$v->swz_price!=0?"9":"";
                    
                    if($v->gjrw_price!=0){
                       $seat_key.= "A";
                       $yp_info.="A".str_pad($v->gjrw_price,5,"0",STR_PAD_LEFT).str_pad($v->gjrw_num,4,"0",STR_PAD_LEFT);
                    }
                    if($v->qtxb_price!=0){
                       $seat_key.= "X";
                       $yp_info.="X".str_pad($v->qtxb_price,5,"0",STR_PAD_LEFT).str_pad($v->qtxb_num,4,"0",STR_PAD_LEFT);
                    }
                    if($v->rw_price!=0){
                       $seat_key.= "4";
                       $yp_info.="4".str_pad($v->rw_price,5,"0",STR_PAD_LEFT).str_pad($v->rw_num,4,"0",STR_PAD_LEFT);
                    }
                    if($v->rz_price!=0){
                       $seat_key.= "2";
                       $yp_info.="2".str_pad($v->rz_price,5,"0",STR_PAD_LEFT).str_pad($v->rz_num,4,"0",STR_PAD_LEFT);
                    }
                    if($v->tdz_price!=0){
                       $seat_key.= "P";
                       $yp_info.="P".str_pad($v->tdz_price,5,"0",STR_PAD_LEFT).str_pad($v->tdz_num,4,"0",STR_PAD_LEFT);
                    }
                    
                    if($v->yw_price!=0){
                       $seat_key.= "3";
                       $yp_info.="3".str_pad($v->yw_price,5,"0",STR_PAD_LEFT).str_pad($v->yw_num,4,"0",STR_PAD_LEFT);
                    }
                    if($v->yz_price!=0){
                       $seat_key.= "1";
                       $yp_info.="1".str_pad($v->yz_price,5,"0",STR_PAD_LEFT).str_pad($v->yz_num,4,"0",STR_PAD_LEFT);
                    }
                    if($v->edz_price!=0){
                       $seat_key.= "O";
                       $yp_info.="O".str_pad($v->edz_price,5,"0",STR_PAD_LEFT).str_pad($v->edz_num,4,"0",STR_PAD_LEFT);
                    }
                    if($v->ydz_price!=0){
                       $seat_key.= "M";
                       $yp_info.="M".str_pad($v->ydz_price,5,"0",STR_PAD_LEFT).str_pad($v->ydz_num,4,"0",STR_PAD_LEFT);
                    }
                    if($v->swz_price!=0){
                       $seat_key.= "9";
                       $yp_info.="9".str_pad($v->swz_price,5,"0",STR_PAD_LEFT).str_pad($v->swz_num,4,"0",STR_PAD_LEFT);
                    }
                    if($v->wz_price!=0){
                       $seat_key.= "13";
                       $yp_info.="1".str_pad($v->wz_price,5,"0",STR_PAD_LEFT)."3".str_pad($v->wz_num,3,"0",STR_PAD_LEFT);
                    }//无座数量3开头
                    $start_station_telecode=$this->BIZ_train_model->get_train_station_code(array("TRS_StationCN"=>$v->start_station_name));//返回站点简码
                    $end_station_telecode=$this->BIZ_train_model->get_train_station_code(array("TRS_StationCN"=>$v->end_station_name));//返回站点简码
                    $reback_list.=',{"train_no":"'.$v->train_no.'","station_train_code":"'.$v->train_code.'","start_station_telecode":"'.$start_station_telecode[0]->TRS_Code.'","start_station_name":"'.$v->start_station_name.'","end_station_telecode":"'.$end_station_telecode[0]->TRS_Code.'","end_station_name":"'.$v->end_station_name.'","from_station_telecode":"'.$v->from_station_code.'","from_station_name":"'.$v->from_station_name.'","to_station_telecode":"'.$v->to_station_code.'","to_station_name":"'.$v->to_station_name.'","start_time":"'.$v->start_time.'","arrive_time":"'.$v->arrive_time.'","day_difference":"'.$v->arrive_days.'","train_class_name":"","lishi":"'.$v->run_time.'","canWebBuy":"Y","lishiValue":"'.$v->run_time_minute.'","yp_info":"'.$yp_info.'","control_train_day":"20301231","start_train_date":"'.$v->train_start_date.'","seat_feature":"O3M393","yp_ex":"O0M090","train_seat_feature":"3","seat_types":"'.$seat_key.'","location_code":"P2","from_station_no":"01","to_station_no":"11","control_day":29,"sale_time":"'.$v->sale_date_time.'","is_support_card":"1","note":"","controlled_train_flag":"0","controlled_train_message":"正常车次，不受控","gg_num":"--","gr_num":"'.$v->gjrw_num.'","qt_num":"'.$v->qtxb_num.'","rw_num":"'.$v->rw_num.'","rz_num":"'.$v->rz_num.'","tz_num":"'.$v->tdz_num.'","wz_num":"'.$v->wz_num.'","yb_num":"--","yw_num":"'.$v->yw_num.'","yz_num":"'.$v->yz_num.'","ze_num":"'.$v->edz_num.'","zy_num":"'.$v->ydz_num.'","swz_num":"'.$v->swz_num.'"}';
                }

                
            }
            $reback_list=substr($reback_list, 1);//去掉多于的,
            $reback.=$reback_list.'],"flag":true,"searchDate":""},"messages":[],"validateMessages":{}}';
            echo($reback);
            // if($ticket["error_code"]!=0){ //error_code=0代表成功，其他有错，返回错误信息
            //     die(json_encode(array("status"=>0,"mes"=>$ticket["reason"])));//
            // }else{
            //     $backdata="";
            //     //$backdata= $this->load->view("booking_table",array("result"=>$ticket["result"],"date"=>$date),true); 
                              
            //     //die(json_encode(array("status"=>1,"mes"=>"成功","backdata"=>$backdata))); 
            // }
        //}else{
            //die(json_encode(array("status"=>0,"mes"=>"站点名称错误")));
        //}
    }

    public function booking(){
        $data['from_station_code']=$this->input->get("from_station_code");
        $data['from_station_name']=$this->input->get("from_station_name");
        $data['to_station_code']=$this->input->get("to_station_code");
        $data['to_station_name']=$this->input->get("to_station_name");
        $data['train_code']=$this->input->get("train_code");
        $data['date']=$this->input->get("date");
        $data['start_time']=$this->input->get("start_time");
        $data['arrive_time']=$this->input->get("arrive_time");

        $data['swz_num']=$this->input->get("swz_num");
        $data['swz_price']=$this->input->get("swz_price");

        $data['tdz_num']=$this->input->get("tdz_num");
        $data['tdz_price']=$this->input->get("tdz_price");

        $data['ydz_num']=$this->input->get("ydz_num");
        $data['ydz_price']=$this->input->get("ydz_price");

        $data['edz_num']=$this->input->get("edz_num");
        $data['edz_price']=$this->input->get("edz_price");

        $data['gjrw_num']=$this->input->get("gjrw_num");
        $data['gjrw_price']=$this->input->get("gjrw_price");

        $data['rw_num']=$this->input->get("rw_num");
        $data['rw_price']=$this->input->get("rw_price");

        $data['yw_num']=$this->input->get("yw_num");
        $data['yw_price']=$this->input->get("yw_price");

        $data['rz_num']=$this->input->get("rz_num");
        $data['rz_price']=$this->input->get("rz_price");

        $data['yz_num']=$this->input->get("yz_num");
        $data['yz_price']=$this->input->get("yz_price");

        $data['wz_num']=$this->input->get("wz_num");
        $data['wz_price']=$this->input->get("wz_price");

        $this->load->view('bootstrap3/header');
        $this->load->view('booking',$data);
        $this->load->view('bootstrap3/footer');        
    }
    //接收订单COLD_SN和客户BPE_SN 获取车次，乘客信息，拼接成聚合提交订单的url
    public function get_sn_submit_juhe() {
        $cold_sn=$this->input->get("order");
        $bpe_sn=$this->input->get("people");
        $data = array();
        $rebakc=array();//返回数据
        $rebakc["status"]=0;
        $rebakc["mes"]="";
        if(!is_numeric($cold_sn)){
            $rebakc["mes"]="订单号是数字";
            echo json_encode($rebakc);
            return false; 
        }
        if(empty($bpe_sn)){
            $rebakc["mes"]="请选择乘客";
            echo json_encode($rebakc);
            return false; 
        }

        $data['train'] = $this->BIZ_train_model->biz_order_detail($cold_sn);
        $data['people_list']=$this->BIZ_train_model->in_bpesn_people_info($bpe_sn);
        if (empty($data['train'])) {
            //显示错误,找不到车次
            $rebakc["mes"]="找不到车次";
            echo json_encode($rebakc);
            return false;

        }
        if (empty($data['people_list'])) {
            //显示错误，找不到用户信息
            $rebakc["mes"]="找不到乘客信息";
            echo json_encode($rebakc);
            return false;
        }

        if (count($data['people_list']) > 5) {
            //显示错误，用户超过五个
            $rebakc["mes"]="乘客不能超过五个";
            echo json_encode($rebakc);
            return false;
        }
        $db_train_zw = $this->config->item('db_train_zw');
        $train_zw = $this->config->item('train_zw');
        $zwcode = $db_train_zw[$data['train']->Aircraft]; //座位简码
        $zwname = $train_zw[$db_train_zw[$data['train']->Aircraft]]; //座位名称
        $passengers="";
        foreach ($data['people_list'] as $key => $item) {
            
            //乘客姓名
            $passengersename = trim($item->BPE_FirstName) . trim($item->BPE_MiddleName) . trim($item->BPE_LastName);
            //乘客类型
            switch ($item->BPE_GuestType) {
                case 1:
                    $piaotype = 1;
                    $piaotypename = "成人票";
                    break;
                case 2:
                    $piaotype = 2;
                    $piaotypename = "儿童票";
                    break;
                default://外国人应该就两种票吧
                    $piaotype = 1;
                    $piaotypename = "成人票";
                    break;
            }
            $passporttypeseid = "B"; //护照
            $passporttypeseidname = "护照";
            $passportseno = $item->BPE_Passport;
            $passengers.=',{"passengerid":' . ( ++$key) . ',"passengersename":"' . $passengersename . '","piaotype":"' . $piaotype . '","piaotypename":"' . $piaotypename . '","passporttypeseid":"' . $passporttypeseid . '","passporttypeseidname":"' . $passporttypeseidname . '","passportseno":"' . $passportseno . '","price":"1","zwcode":"' . $zwcode . '","zwname":"' . $zwname . '"}';
        }
        $passengers.="]";
        $passengers = substr($passengers, 1);
        $passengers = "[" . $passengers;
        $url=$this->dp_api;
        $post_data=array(
                "key"=>$this->key,
                "user_orderid"=>$cold_sn,//自定义订单号
                "train_date"=>substr($data["train"]->DepartureDate, 0, 10),
                "from_station_name"=>$data["train"]->DepartAirport_cn,
                "from_station_code"=>$data["train"]->DepartAirport,
                "to_station_code"=>$data["train"]->ArrivalAirport,
                "to_station_name"=>$data["train"]->ArrivalAirport_cn,
                "passengers"=>$passengers,
                "checi"=>$data["train"]->FlightsNo
            );
        $bakc_json=$this->post_data($url,$post_data);
        $bakc=json_decode($bakc_json);//json=>obj

        $add_data=new StdClass();
        $add_data->JOL_COLD_SN=(int)$cold_sn;
        
        $add_data->JOL_Status="0";
        $add_data->JOL_RebackMsg=$bakc->reason;
        $add_data->JOL_FromStation=$data["train"]->DepartAirport_cn;
        $add_data->JOL_ToStation=$data["train"]->ArrivalAirport_cn;
        $add_data->JOL_FromStationCode=$data["train"]->DepartAirport;
        $add_data->JOL_ToStationCode=$data["train"]->ArrivalAirport;
        $add_data->JOL_TrainCode=$data["train"]->FlightsNo;
        $add_data->JOL_BackTxt=$bakc_json;
        
        if(!$bakc->error_code){
            $add_data->JOL_JuheOrder=$bakc->result->orderid;
            $rebakc["status"]=1;
            $rebakc["order"]=$bakc->result->orderid;
            $rebakc["mes"]="订单提交成功，等待回调";                             
        }else{ 
            $add_data->JOL_JuheOrder=null;
            $rebakc["mes"]= $bakc_json;
            $add_data->JOL_Status="e";
        }
        $add_back_data=$this->BIZ_train_model->add_biz_jol($add_data);
        
        echo json_encode($rebakc);
        return false;

    }
    //根据汉特订单明细表SN来获取车次，乘客信息，拼接成聚合提交订单的url
    public function submit_juhe_order() {
        $cold_sn=$this->input->get("order");
        $data = array();
        $rebakc=array();//返回数据
        $rebakc["status"]=0;
        $rebakc["mes"]="";
        
        if(!is_numeric($cold_sn)){
            $rebakc["mes"]="订单号是数字";
            echo json_encode($rebakc);
            return false; 
        }
        $data['train'] = $this->BIZ_train_model->biz_order_detail($cold_sn);
        if (empty($data['train'])) {
            //显示错误,找不到车次
            $rebakc["mes"]="找不到车次";
            echo json_encode($rebakc);
            return false;

        }

        $data['people_list'] = $this->BIZ_train_model->biz_people($cold_sn);
        if (empty($data['people_list'])) {
            //显示错误，找不到用户信息
            $rebakc["mes"]="找不到乘客信息";
            echo json_encode($rebakc);
            return false;
        }
        if (count($data['people_list']) > 5) {
            //显示错误，用户超过五个
            $rebakc["mes"]="乘客不能超过五个";
            echo json_encode($rebakc);
            return false;
        }

        $db_train_zw = $this->config->item('db_train_zw');
        $train_zw = $this->config->item('train_zw');

        $passengers='';
        foreach ($data['people_list'] as $key => $item) {
            $zwcode = $db_train_zw[$data['train']->Aircraft]; //座位简码
            $zwname = $train_zw[$db_train_zw[$data['train']->Aircraft]]; //座位名称
            //乘客姓名
            $passengersename = trim($item->BPE_FirstName) . trim($item->BPE_MiddleName) . trim($item->BPE_LastName);
            //乘客类型
            switch ($item->BPE_GuestType) {
                case 1:
                    $piaotype = 1;
                    $piaotypename = "成人票";
                    break;
                case 2:
                    $piaotype = 2;
                    $piaotypename = "儿童票";
                    break;
                default://外国人应该就两种票吧
                    $piaotype = 1;
                    $piaotypename = "成人票";
                    break;
            }
            $passporttypeseid = "B"; //护照
            $passporttypeseidname = "护照";
            $passportseno = $item->BPE_Passport;
            $passengers.=',{"passengerid":' . ( ++$key) . ',"passengersename":"' . $passengersename . '","piaotype":"' . $piaotype . '","piaotypename":"' . $piaotypename . '","passporttypeseid":"' . $passporttypeseid . '","passporttypeseidname":"' . $passporttypeseidname . '","passportseno":"' . $passportseno . '","price":"1","zwcode":"' . $zwcode . '","zwname":"' . $zwname . '"}';
        }
        $passengers.="]";
        $passengers = substr($passengers, 1);
        $passengers = "[" . $passengers;
        $url=$this->dp_api;
        $post_data=array(
                "key"=>$this->key,
                "user_orderid"=>$cold_sn,//自定义订单号
                "train_date"=>substr($data["train"]->DepartureDate, 0, 10),
                "from_station_name"=>$data["train"]->DepartAirport_cn,
                "from_station_code"=>$data["train"]->DepartAirport,
                "to_station_code"=>$data["train"]->ArrivalAirport,
                "to_station_name"=>$data["train"]->ArrivalAirport_cn,
                "passengers"=>$passengers,
                "checi"=>$data["train"]->FlightsNo
            );
        $bakc_json=$this->post_data($url,$post_data);
        $bakc=json_decode($bakc_json);//json=>obj

        $add_data=new StdClass();
        $add_data->JOL_COLD_SN=(int)$cold_sn;
        
        $add_data->JOL_Status="0";
        $add_data->JOL_RebackMsg=$bakc->reason;
        $add_data->JOL_FromStation=$data["train"]->DepartAirport_cn;
        $add_data->JOL_ToStation=$data["train"]->ArrivalAirport_cn;
        $add_data->JOL_FromStationCode=$data["train"]->DepartAirport;
        $add_data->JOL_ToStationCode=$data["train"]->ArrivalAirport;
        $add_data->JOL_TrainCode=$data["train"]->FlightsNo;
        $add_data->JOL_BackTxt=$bakc_json;
        
        if(!$bakc->error_code){
            $add_data->JOL_JuheOrder=$bakc->result->orderid;
            $rebakc["status"]=1;
            $rebakc["order"]=$bakc->result->orderid;
            $rebakc["mes"]="订单提交成功，等待回调";                             
        }else{ 
            $add_data->JOL_JuheOrder=null;
            $rebakc["mes"]= $bakc_json;
            $add_data->JOL_Status="e";
        }
        $add_back_data=$this->BIZ_train_model->add_biz_jol($add_data);
        
        echo json_encode($rebakc);
        return false;
    }    
    //汉特接口
    public function ht_api(){
        $order=$this->input->get("order");
        $rebakc=array();//返回数据
        $rebakc["status"]=0;
        $rebakc["mes"]="";
        
        if(is_numeric($order)){
            $this->load->model("order_people_model","op");
            $data=$this->op->biz_people($order);
            if(!empty($data->cold)){
                $people_num=count($data->people);
                if($people_num<=5){
                    $db_train_zw=$this->config->item('db_train_zw');
                    $train_zw=$this->config->item('train_zw');

                    if(!empty($db_train_zw[$data->cold[0]->Aircraft])){
                        $zwcode=$db_train_zw[$data->cold[0]->Aircraft];//座位简码
                        $zwname=$train_zw[$db_train_zw[$data->cold[0]->Aircraft]];//座位名称

                        $passengers="";//聚合订单数据
                        for($i=0;$i<$people_num;$i++){
                            $passengersename="";//乘客名字
                            if(!empty($data->people[$i]->BPE_FirstName)){
                                $passengersename.=$data->people[$i]->BPE_FirstName." ";
                            }
                            if(!empty($data->people[$i]->BPE_MiddleName)){
                                $passengersename.=$data->people[$i]->BPE_MiddleName." ";
                            }
                            if(!empty($data->people[$i]->BPE_LastName)){
                                $passengersename.=$data->people[$i]->BPE_LastName;
                            }                        
                            $passengersename=trim($passengersename);

                            //乘客类型
                            switch ($data->people[$i]->BPE_GuestType) {
                                case 1:
                                    $piaotype=1;
                                    $piaotypename="成人票";
                                    break;
                                case 2:
                                    $piaotype=2;
                                    $piaotypename="儿童票";
                                    break;
                                default://外国人应该就两种票吧
                                    $piaotype=1;
                                    $piaotypename="成人票";
                                    break;
                            }
                            $passporttypeseid="B";//护照
                            $passporttypeseidname="护照";
                            $passportseno=$data->people[$i]->BPE_Passport;
                            $passengers.=',{"passengerid":'.($i+1).',"passengersename":"'.$passengersename.'","piaotype":"'.$piaotype.'","piaotypename":"'.$piaotypename.'","passporttypeseid":"'.$passporttypeseid.'","passporttypeseidname":"'.$passporttypeseidname.'","passportseno":"'.$passportseno.'","price":"1","zwcode":"'.$zwcode.'","zwname":"'.$zwname.'"}';
                        }

                        $passengers.="]";
                        $passengers=substr($passengers, 1);
                        $passengers="[".$passengers;
                        $url=$this->dp_api;
                        $post_data=array(
                                "key"=>$this->key,
                                "user_orderid"=>$order,//自定义订单号
                                "train_date"=>substr($data->cold[0]->COLD_StartDate, 0, 10),
                                "from_station_name"=>$data->cold[0]->LeaveStation,
                                "from_station_code"=>$data->cold[0]->DepartAirport,
                                "to_station_code"=>$data->cold[0]->ArrivalAirport,
                                "to_station_name"=>$data->cold[0]->ServerStation,
                                "passengers"=>$passengers,
                                "checi"=>$data->cold[0]->FlightsNo
                            );
                        var_dump($post_data);
                        die;
                        $bakc_json=$this->post_data($url,$post_data);
                        $bakc=json_decode($bakc_json);//json=>obj

                        $add_data=new StdClass();
                        $add_data->JOL_COLD_SN=(int)$order;
                        
                        $add_data->JOL_Status="0";
                        $add_data->JOL_RebackMsg=$bakc->reason;
                        $add_data->JOL_FromStation=$data->cold[0]->LeaveStation;
                        $add_data->JOL_ToStation=$data->cold[0]->ServerStation;
                        $add_data->JOL_FromStationCode=$data->cold[0]->DepartAirport;
                        $add_data->JOL_ToStationCode=$data->cold[0]->ArrivalAirport;
                        $add_data->JOL_TrainCode=$data->cold[0]->FlightsNo;
                        $add_data->JOL_BackTxt=$bakc_json;
                        
                        if(!$bakc->error_code){
                            $add_data->JOL_JuheOrder=$bakc->result->orderid;
                            $rebakc["status"]=1;
                            $rebakc["order"]=$bakc->result->orderid;
                            $rebakc["mes"]="订单提交成功，等待回调";                             
                        }else{ 
                            $add_data->JOL_JuheOrder=null;
                            $rebakc["mes"]= $bakc_json;
                            $add_data->JOL_Status="e";
                        }
                        $add_back_data=$this->op->add_jh_order($add_data);
                        

                    }else{
                       $rebakc["mes"]="座次信息有误";  
                    }
                }else{
                   $rebakc["mes"]="乘客人数不得超过五个"; 
                } 
            }else{
                $rebakc["mes"]="此订单号不存在";
            }    
        }else{
           $rebakc["mes"]="此订单号不是数字";
        } 
        // return $rebakc;
        var_dump($rebakc);
        die(json_encode($rebakc));
    }
    
    public function sub_callback(){
        $data_post=$this->input->post();
        $data=json_decode($data_post["data"]);
        
        $this->load->model("order_people_model","op");
        $update_data=new StdClass();
        $update_data->JOL_BackTxt=$data_post["data"];
        $update_data->JOL_RebackMsg=$data->msg;
        $update_data->JOL_Status=$data->status;
        $update_data->JOL_JuheOrder=$data->orderid;
        $update_data->JOL_Price=$data->passengers[0]->price;
        if($data->status=="2"){
            $post_data=array(
                        "key"=>$this->key,
                        "orderid"=>$data->orderid
                            );
            $back_json=$this->my_post($this->pay_api,$post_data);
            $back=json_decode($back_json);
            $update_data->JOL_BackTxt=$back_json;
            $update_data->JOL_RebackMsg=$back->reason;
        }elseif($data->status=="4"){
            //付款成功 写入TOC表
            $add_train_order_data->TOC_Memo=$data->orderid;
            $add_train_order_data->TOC_COLD_SN=$data->user_orderid;
            $add_train_order_data->TOC_TrainNumber=$data->checi;
            $add_train_order_data->TOC_DepartureDate=$data->train_date;
            $add_train_order_data->TOC_TicketCost=$data->orderamount;
            $add_train_order_data->poundage=(count($data->passengers)*2)."";//手续费，每人两块，转换成字符串
            $add_train_order_data->FOI_TrainNetOrderNo=$data->ordernumber;
            $this->op->add_train_order($add_train_order_data);                         
        }elseif($data->status=="7"){
            //退票成功 写入TOC表
            $newtime="";//记录最新操作时间
            $refund_passportseno="";//退票人护照号
            $refund_money="";//退票金额
            foreach ($data->passengers as $p) {
                //找出退票人，规则：操作时间最新的
                if($p->refundTimeline){//是否有退票操作
                    //$p->refundTimeline[count($p->refundTimeline)-1] 最新操作
                    if($p->refundTimeline[count($p->refundTimeline)-1]->time > $newtime){
                        $newtime=$p->refundTimeline[count($p->refundTimeline)-1]->time;
                        $refund_passportseno=$p->refundTimeline[count($p->refundTimeline)-1]->detail->passportseno;
                        $refund_money=$p->refundTimeline[count($p->refundTimeline)-1]->detail->returnmoney;
                    }
                }
            }
            $add_train_order_data->TOC_Memo=$data->orderid." ".$refund_passportseno;
            $add_train_order_data->TOC_COLD_SN=$data->user_orderid;
            $add_train_order_data->TOC_TrainNumber=$data->checi;
            $add_train_order_data->TOC_DepartureDate=$data->train_date;
            $add_train_order_data->TOC_TicketCost=-$refund_money;
            $add_train_order_data->FOI_TrainNetOrderNo=null;//退票不用更新取票号，以此在模型里面判断是否为退票消息
            $this->op->add_train_order($add_train_order_data);
        }
                
        
        $this->op->update_jh_order($update_data);
        echo "success";
    }
    //汉特&聚合 订单列表
    public function ht_order_list(){
        $this->load->model("order_people_model","op");
        $page_size=10;
        $page=$this->input->get("page");
        $order=$this->input->get("order");
        $where="1=1";//搜索条件
        $page_parameter="";//返回的分页条件参数
        if(empty($page) or !is_numeric($page)){
            $page=0;
        }
        if(!empty($order)){
            $where="BIZ_ConfirmLineInfo.COLI_ID='{$order}' OR JOL_JuheOrder='{$order}'";
            $list["order"]=$order;
            $page_parameter="order=".$order."&";
        }
        $data=$this->op->get_order($page_size,$page,$where);
        $list["data"]=$data->list;
        
        $this->load->library('pagination');

        $config['base_url'] = site_url("/apps/train/index/ht_order_list?{$page_parameter}");
        $config['total_rows'] = $data->count;
        $config['per_page'] = $page_size;
        $config['page_query_string']=TRUE;
        $config['query_string_segment']="page";
        $config['cur_tag_open'] = '<li class="active"><a href="#">';        
        $config['cur_tag_close'] = '</a></li>';
        $config['first_tag_open']=$config['last_tag_open']=$config['next_tag_open']=$config['prev_tag_open']=$config['num_tag_open']="<li>";
        $config['first_tag_close']=$config['last_tag_close']=$config['next_tag_close']=$config['prev_tag_close']=$config['num_tag_close']="</li>";

        $this->pagination->initialize($config);

        $list["page_link"]=$this->pagination->create_links();

        foreach ($list["data"] as $key => $value) {
            $value->info=$this->order_status_msg[$value->JOL_Status];//自定义说明信息;
        }  
        
                     
        
        $this->load->view('bootstrap3/header');
        $this->load->view('ht_order_list.html',$list);
        $this->load->view('bootstrap3/footer');
    }
    //输入翰特订单号cols_id,获取火车订票的相关信息，模拟翰特订单详情页面
    public function ht_train_order(){
        $cols_id=$this->input->post("ht_order");
        $list=new StdClass;
        if(!empty($cols_id)){
            $cold_sn=$this->BIZ_train_model->get_biz_cold($cols_id);
            $list->wl=$this->BIZ_train_model->get_operatorinfo($cols_id);
            $i=0;
            $list->info=array();
            foreach ($cold_sn as $v) {
                $list->info[$i]=new StdClass;
                $list->info[$i]->people=$this->BIZ_train_model->biz_people($v->COLD_SN);
                $list->info[$i]->train=$this->BIZ_train_model->get_biz_foi($v->COLD_SN);
                $list->info[$i]->status=$this->BIZ_train_model->get_biz_jol($v->COLD_SN);
                $i++;
            }
            $list->cols_id=$cols_id;
        }
        $this->load->view('bootstrap3/header');
        $this->load->view('ht_train_order.html',$list);
        $this->load->view('bootstrap3/footer'); 
    }
    //输入翰特订单号cols_id,获取火车订票的相关信息，模拟翰特订单详情页面
    public function ht_train_order_info(){
        $cols_id=$this->input->post("ht_order");
        $list=new StdClass;
        if(!empty($cols_id)){
            $cold_sn=$this->BIZ_train_model->get_biz_cold($cols_id);
            $list->wl=$this->BIZ_train_model->get_operatorinfo($cols_id);
            $i=0;
            $list->info=array();
            foreach ($cold_sn as $v) {
                $list->info[$i]=new StdClass;
                $list->info[$i]->people=$this->BIZ_train_model->biz_people($v->COLD_SN);
                $list->info[$i]->train=$this->BIZ_train_model->get_biz_foi($v->COLD_SN);
                $list->info[$i]->status=$this->BIZ_train_model->get_biz_jol($v->COLD_SN);
                $i++;
            }
            $list->cols_id=$cols_id;
        }
        $post_data=array(
                        "key"=>$this->key
                            ); 
        $back_data=$this->post_data($this->balance_api,$post_data);
        $back_data = json_decode($back_data);
        if(!empty($back_data->result)){
            $list->balance = $back_data->result;
        }else{
            $list->balance = "NULL";
        }
        $this->load->view('bootstrap3/header');
        $this->load->view('ht_train_order_info',$list);
        $this->load->view('bootstrap3/footer'); 
    }
    //订单信息填写
    public function booking_write(){
        $this->load->model("order_people_model","op");
        $order=$this->input->post("order");
        $data["order"]="";
        if(!empty($order) && is_numeric($order)){
            $data["order"]=$this->op->biz_people($order);
            $data["order_id"]=$order;
        }
        
        $data["code_zw"]=$this->code_zw;
        $data["piaotype"]=$this->piaotype;
        $data["passportty"]=$this->passportty;

        $this->load->view('bootstrap3/header');
        $this->load->view('booking_write',$data);
        $this->load->view('bootstrap3/footer');
    }

        
    public function booking_do(){
        //以下是为测试用，未做完善的判断！

        $this->load->helper('form');
        $this->load->library('my_form_validation');
        $zw=$this->input->post("train_booking_zw");
        $piaotype_post=$this->input->post("train_booking_piaotype");
        $passengersename=$this->input->post("train_booking_passengersename");
        $passportty=$this->input->post("train_booking_passportty");
        $passportseno=$this->input->post("train_booking_passportseno");
        $mobile=$this->input->post("train_booking_mobile");
        $train_code=$this->input->post("train_booking_train_code");
        $from_station_code=$this->input->post("train_booking_from_station_code");
        $from_station_name=$this->input->post("train_booking_from_station_name");
        $to_station_code=$this->input->post("train_booking_to_station_code");
        $to_station_name=$this->input->post("train_booking_to_station_name");
        $date=$this->input->post("train_booking_date");

        $rebakc="";//返回数据
        $flag=TRUE;//标示变量
        $passengers="";//乘客信息集合
        $num=count($zw);
        if($num>5){
            // die("乘客太多");
            $rebakc="乘客太多";
        }else{
            $config_zw=$this->code_zw;//检查座位信息是否和配置文件相符合
            $config_piaotype=$this->piaotype;//检查票种信息是否和配置文件相符合
            $config_passportty=$this->passportty;//检查证件信息是否和配置文件相符合
            
            for($i=0;$i<$num;++$i){
               
               if(empty($config_zw[$zw[$i]])){
                   $zwname[]=$config_zw["1"];
                   $zwcode[]="1"; 
               }else{
                   $zwname[]=$config_zw[$zw[$i]];
                   $zwcode[]=$zw[$i];
               }

               if(empty($config_piaotype[$piaotype_post[$i]])){
                   $piaotypename[]=$config_piaotype["1"];
                   $piaotype[]="1"; 
               }else{
                   $piaotypename[]=$config_piaotype[$piaotype_post[$i]];
                   $piaotype[]=$piaotype_post[$i];
               }

               if(empty($config_passportty[$passportty[$i]])){
                   $zwname[]=$config_passportty["1"];
                   $zwcode[]="1"; 
               }else{
                   $zwname[]=$config_passportty[$passportty[$i]];
                   $zwcode[]=$passportty[$i];
               }
               
            }
            //表单验证
            $this->form_validation->set_rules('train_booking_train_code', '车次不能为空', 'required');
            $this->form_validation->set_rules('train_booking_from_station_name', '出发站点不能为空', 'required');
            $this->form_validation->set_rules('train_booking_to_station_name', '目的站点不能空', 'required');
            $this->form_validation->set_rules('train_booking_date', '日期不能为空', 'required');
            $this->form_validation->set_rules('train_booking_passportseno[]', '证件号不能为空且为数字', 'required|numeric');
                     
            $this->form_validation->set_rules('train_booking_passengersename[]', '姓名不能为空', 'required');

            if($this->form_validation->run()){
                for($i=0;$i<$num;$i++){
                    $passengers.=',{"passengerid":'.($i+1).',"passengersename":"'.$passengersename[$i].'","piaotype":"'.$piaotype[$i].'","piaotypename":"'.$piaotypename[$i].'","passporttypeseid":"'.$passporttypeseid[$i].'","passporttypeseidname":"'.$passporttypeseidname[$i].'","passportseno":"'.$passportseno[$i].'","price":"1","zwcode":"'.$zwcode[$i].'","zwname":"'.$zwname[$i].'"}';
                }
                $passengers.="]";
                $passengers=substr($passengers, 1);
                $passengers="[".$passengers;
                // die($passengers);
                $url=$this->dp_api;
                $post_data=array(
                        "key"=>$this->key,
                        "user_orderid"=>time(),//自定义订单号
                        "train_date"=>$date,
                        "from_station_name"=>$from_station_name,
                        "from_station_code"=>$from_station_code,
                        "to_station_code"=>$to_station_code,
                        "to_station_name"=>$to_station_name,
                        "passengers"=>$passengers,
                        "checi"=>$train_code
                    );

                $bakc=$this->my_post($url,$post_data);
                if($bakc['error_code']){
                    $rebakc=$bakc;
                }else{
                    //提交订单成功，但并不意味着信息没错
                    $url_status=$this->cx_api;//订单状态查询
                    $post_status=array(
                        "key"=>$this->key,
                        "orderid"=>$bakc["result"]["orderid"]
                        );
                    $bakc_status=$this->my_post($url_status,$post_status);
                    
                    
                    if($bakc_status["error_code"]){
                        $rebakc=$bakc_status;
                    }else{
                        $this->load->model("api_model","api");
                        $this->api->_insert("order",array(
                            "order_num"=>$bakc['result']['orderid'],
                            "sub_time"=>time(),
                            "status"=>$bakc_status["result"]["status"],
                            "msg"=>$bakc_status["result"]["msg"],
                            "from_station"=>$from_station_name,
                            "to_station"=>$to_station_name,
                            "train_code"=>$train_code,
                            "price"=>$bakc_status["result"]["orderamount"]
                            ));
                        // echo "<script>window.location.href='".site_url("apps/train/index/order_list")."'</script>";
                        redirect("apps/train/index/order_list");//windows服务器会报错？
                    }
                    
                }
                var_dump($rebakc);                 
            }else{
                // var_dump($this->form_validation->_error_array);
                var_dump(validation_errors());                
            }
            
        }
        
    }


    public function order_list(){
        $this->load->model("api_model","api");
        $list["data"]=$this->api->getAll("order");
        $count=count($list["data"]);
        for ($i=0; $i < $count; $i++) { 
            $list["data"][$i]["info"]=$this->order_status_msg[$list["data"][$i]["status"]];//自定义说明信息
        }
        $this->load->view('bootstrap3/header');
        $this->load->view('order_list.html',$list);
        $this->load->view('bootstrap3/footer');
    }
    public function order(){
        
        if($order=$this->input->get("order")){
           $post_data=array(
                        "key"=>"79f03107b921ef31310bd40a1415c1cb",
                        "orderid"=>$order
                            ); 
           $back_data=$this->my_post("http://op.juhe.cn/trainTickets/orderStatus",$post_data);
           $data=array(
                "JOL_Status"=>$back_data["result"]["status"],
                "JOL_RebackMsg"=>$back_data["result"]["msg"]
            );
           $this->load->model("BIZ_train_model");
           //查询到订单最新情况，更新本地数据库
           $this->BIZ_train_model->update_biz_jol(array("JOL_JuheOrder"=>$order),$data);
           // var_dump($back_data);
           $this->load->view('bootstrap3/header');
           $this->load->view('order',$back_data);
           $this->load->view('bootstrap3/footer'); 
        }
    }
    //取消订单
    public function cancel_order(){
       if($order=$this->input->get("order")){
           $post_data=array(
                        "key"=>$this->key,
                        "orderid"=>$order
                            );
           $back=$this->my_post($this->qxdd_api,$post_data);
           var_dump($back);

       } 
    }

    //请求出票，支付
    public function pay(){
       if($order=$this->input->get("order")){
           $post_data=array(
                        "key"=>$this->key,
                        "orderid"=>$order
                            );
           $back=$this->my_post($this->pay_api,$post_data);
           var_dump($back);
       }
    }

    //线上退票
    public function refund(){
       if($order=$this->input->get("order")){
           $post_data=array(
                        "key"=>$this->key,
                        "orderid"=>$order
                            ); 
           $back_data=$this->my_post($this->status_api,$post_data);
           $data=array(
                "JOL_Status"=>$back_data["result"]["status"],
                "JOL_RebackMsg"=>$back_data["result"]["msg"]
            );
           $this->load->model("BIZ_train_model");
           //查询到订单最新情况，更新本地数据库
           $this->BIZ_train_model->update_biz_jol(array("JOL_JuheOrder"=>$order),$data);
           
           $this->load->view('bootstrap3/header');
           $this->load->view('refund',$back_data);
           $this->load->view('bootstrap3/footer'); 
        }
    }
    public function refund_do(){
        $order=$this->input->get("order");
        $passportseno=$this->input->get("passportseno");
        $passporttypeseid=$this->input->get("passporttypeseid");
        $ticket_no=$this->input->get("ticket_no");
        $passengername=$this->input->get("name");

        if(!empty($order) && !empty($passportseno) && !empty($passporttypeseid) && !empty($ticket_no) && !empty($passengername)){
            $post_data=array(
                        "key"=>$this->key,
                        "orderid"=>$order,
                        "tickets"=>'[{"ticket_no":"'.$ticket_no.'","passengername":"'.$passengername.'","passporttypeseid":"'.$passporttypeseid.'","passportseno":"'.$passportseno.'"}]',
                            ); 
           $back_data=$this->my_post($this->refund_api,$post_data);
           
           if($back_data["error_code"]==0){
                echo "<script>alert('请求成功，正在处理退票...');location.href='".site_url("/apps/train/index/refund?order=$order")."';</script>";
           }
        }
        return;
    }
    function my_post($url,$post_data){
        // $url = "http://op.juhe.cn/trainTickets/cityCode";
        // $post_from = array("stationName" => $from,"key"=>"79f03107b921ef31310bd40a1415c1cb");
        // $post_to = array("stationName" => $to,"key"=>"79f03107b921ef31310bd40a1415c1cb");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        
        curl_close($ch);
        
        $output=json_decode($output,TRUE);//json => array
        return  $output;     
    }
    function get_data($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    function post_data($url,$post_data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        
        curl_close($ch);
        
        // $output=json_decode($output,TRUE);//json => array
        return  $output;     
    }

}