<?php

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

/** 
 * 
 * 机票
 * 
 */
class Iflight extends CI_Controller {

    public $site_code;

    public function __construct() {
        parent::__construct();
        $this->load->model('Site_model');
        $this->site_code = strtolower($this->config->item('Site_Code'));
        $this->load->library('Flightapi');
        set_time_limit (0);
        //$this->output->enable_profiler(TRUE);
    }

    public function index()
    {
        $data = array();
        $this->html_do->head($data, 'r');
        $this->load->view($this->site_code.'/flight/index', $data);
        $this->html_do->foot($data, 'r');
    }

    public function cn_flight()
    {
        $data=array();
        $this->load->view($this->site_code.'/flight/cn_flight', $data);
    }

    public function search() {
        $data = array();
        $this->html_do->head($data, 'r');
        //查询航班信息
        $requests='post';//航班排序时使用get方式提交数据
        if ($this->input->get('form_city_code')) $requests='get';
        $data['form_city']=$this->input->$requests('form_city');
        $data['to_city']=$this->input->$requests('to_city');
        $data['form_city_code']=$this->input->$requests('form_city_code');
        $data['to_city_code']=$this->input->$requests('to_city_code');
        $data['flight_date']=$this->input->$requests('flight_date');
        $data['return_date']=$this->input->$requests('return_date');
        $data['order_name']=$this->input->$requests('order_name');
        $data['order_type']=$this->input->$requests('order_type');
        if (empty($data['form_city_code'])) {
            $data['form_city'] = 'Beijing';
            $data['to_city'] = 'Hong kong';
            $data['form_city_code'] = 'BJS';
            $data['to_city_code'] = 'HKG';
            $data['return_date'] = false;
            $data['order_name'] = false;
            $data['order_type'] = false;
        }
        if (empty($data['flight_date'])) {
            $data['flight_date'] = date('m/d/Y', (time() + 10 * 24 * 60 * 60));
        }
        $result = $this->flightapi->international_flight($data['form_city_code'], $data['to_city_code'], $data['flight_date'], $data['return_date'], $data['order_name'], $data['order_type']);
        $data2=$this->get_flight_data($result);
        $data=array_merge($data,$data2);
        $data['result_data']=$result;

        $this->load->view($this->site_code.'/flight/search', $data);
        $this->html_do->foot($data, 'r');
    }

    public function ota_search() 
    {
        $data = array();
        $data['form_city'] = $this->input->post('form_city');
        $data['to_city'] = $this->input->post('to_city');
        $data['form_city_code'] = $this->input->post('form_city_code');
        $data['to_city_code'] = $this->input->post('to_city_code');
        $data['flight_date']  = $this->input->post('flight_date');
        $data['return_date']  = $this->input->post('return_date');
        if (empty($data['flight_date'])) {
            $data['flight_date'] = date('m/d/Y', (time() + 10 * 24 * 60 * 60));
        }
        $data['flight_date'] = date('Y-m-d',strtotime($data['flight_date']));

        //$result=file_get_contents('http://www.diychinatours.com/index.php/thirdparty/action/flight/flighttool/ota_flight/'.$data['form_city_code'].'/'.$data['to_city_code'].'/'.$data['flight_date'].'/2');
        $url='http://202.103.68.34:9091/index.php/thirdparty/action/flight/flighttool/ota_flight/'.$data['form_city_code'].'/'.$data['to_city_code'].'/'.$data['flight_date'].'/2';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        $result = curl_exec($ch);
        $data['result']=json_decode($result);
        $pricegroup=array();
        foreach ($data['result']->FDNO as $fdno) {
            $pricegroup[$fdno->flight][]=$fdno;
        }
        unset($data['result']->FDNO);
        $data['pricegroup']=$pricegroup;
        $this->load->view($this->site_code.'/flight/ota_search', $data);
    }
    
    //国际机票入库表单
    public function order()
    {
        $data = array();
        $this->html_do->head($data, 'r');
        //查询选择的航班信息
        $data['from_city']=$this->input->post('from_city');
        $data['to_city']=$this->input->post('to_city');
        $data['form_city_code']=$this->input->post('form_city_code');
        $data['to_city_code']=$this->input->post('to_city_code');
        $data['flight_date']=$this->input->post('flight_date');
        $data['return_date']=$this->input->post('return_date');
        $data['route_rearch_token']=$this->input->post('route_rearch_token');
        $result = $this->flightapi->international_flight($data['form_city_code'], $data['to_city_code'], $data['flight_date'], $data['return_date'], false, false,$data['route_rearch_token']);
        $data2=$this->get_flight_data($result);
        $data=array_merge($data,$data2);
        $this->load->view($this->site_code.'/flight/order', $data);
        $this->html_do->foot($data, 'r');
    }

    //国际机票入库
    public function flight_thankyou()
    {
        $data = array();
        $this->html_do->head($data, 'r');
        $data['from_city']=$this->input->post('from_city');
        $data['to_city']=$this->input->post('to_city');
        $data['form_city_code']=$this->input->post('form_city_code');
        $data['to_city_code']=$this->input->post('to_city_code');
        $data['flight_date']=$this->input->post('flight_date');
        $data['return_date']=$this->input->post('return_date');
        $data['route_rearch_token']=$this->input->post('route_rearch_token');
        $result = $this->flightapi->international_flight($data['form_city_code'], $data['to_city_code'], $data['flight_date'], $data['return_date'], false, false,$data['route_rearch_token']);
        $flight_data = $this->get_flight_data($result);
        $flight         = $flight_data['flight_group'][0];
        $flight_list    = $flight_data['flight_list'];
        $flight_price   = $flight_data['flight_price'];

        $round_trip_flag=false;//是否往返航班
        $stopover=1;//中转类型 1：直飞 2：1次中转 3:2次中转
        $OrderDetailText='';
        foreach ($flight->FlightSegmentList->FlightSegment as $key_air => $air) 
        {
            //判断是否往返航班
            if(!$round_trip_flag && $air->OrigDestSeqID==2) {
                $round_trip_flag=TRUE;
                $OrderDetailText.='预订类型：往返航班；';
            }
            //航班中转类型
            if($air->SegmentSeqID>$stopover) {
               $stopover=$air->SegmentSeqID;
               $OrderDetailText.='中转情况：'.($stopover-1).'次中转；';
            }
        }
        //价格处理
        $org_price  = $this->input->post('org_price',TRUE);    //成人票价
        $org_taxes  = $this->input->post('org_taxes',TRUE);    //成人机建加燃油
        $priceY     = $this->input->post('org_price_org',TRUE);//Y仓价格，即全价
        $adultCost  = $org_price+$org_taxes;   //成人成本
        $childCost  = ($priceY+$org_taxes)*0.5;//儿童成本
        $babyCost   = $priceY*0.1;             //婴儿成本
        //信用卡手续费 0.03，服务费 成人和小孩70元 婴儿50块
        $adultPrice = $this->currency->GetSiteMoney(($adultCost+70)*1.03);//成人报价
        $childPrice = $this->currency->GetSiteMoney(($childCost+70)*1.03);//儿童报价
        $babyPrice  = $this->currency->GetSiteMoney(($babyCost+50)*1.03); //婴儿报价
        $totalPrice_rmb = (($adultCost+70)*$adult_num+($childCost+70)*$child_num+($babyCost+50)*$baby_num)*1.03;
        $totalPrice = $this->currency->GetSiteMoney($totalPrice_rmb);
        if($this->input->post('total_price_usd')>$totalPrice) $totalPrice = $this->input->post('total_price_usd');

        $this->load->model('Orders_model');
        //存储联系人
        $this->Orders_model->GUT_FirstName = $this->input->post('fullname',TRUE);
        $this->Orders_model->GUT_Title = '';//sex;
        $this->Orders_model->GUT_Email = $this->input->post('email',TRUE);
        $this->Orders_model->GUT_NationalityID = $this->input->post('Nationality',TRUE);
        $this->Orders_model->GUT_Passport = '';
        $this->Orders_model->GUT_TEL = '';//mobilephone;
        $this->Orders_model->GUT_MoveTel = $this->input->post('phone',TRUE);
        $gut_sn = $this->Orders_model->biz_guest_save();
        
        //存储参团人
        $bpe_sn_array = array();    //存放新增的参团人SN
        $bpe_mail_txt = '';
        $flight_mail_txt = '';
        $adult_num = $this->input->post('passenerno',TRUE);
        $child_num = $this->input->post('passenerno_chd',TRUE);
        $baby_num = $this->input->post('passenerno_baby',TRUE);

        $sex = $this->input->post('sex',TRUE);
        $guestName = $this->input->post('guestName',TRUE);
        $guestpassport = $this->input->post('guestpassport',TRUE);
        $birthday_m = $this->input->post('birthday_m',TRUE);
        $birthday_d = $this->input->post('birthday_d',TRUE);
        $birthday_y = $this->input->post('birthday_y',TRUE);

        $sexCHD = $this->input->post('sexCHD',TRUE);
        $guestNameCHD = $this->input->post('guestNameCHD',TRUE);
        $guestpassportCHD = $this->input->post('guestpassportCHD',TRUE);
        $birthday_m_CHD = $this->input->post('birthday_m_CHD',TRUE);
        $birthday_d_CHD = $this->input->post('birthday_d_CHD',TRUE);
        $birthday_y_CHD = $this->input->post('birthday_y_CHD',TRUE);

        $sexBaby = $this->input->post('sexBaby',TRUE);
        $guestNameBaby = $this->input->post('guestNameBaby',TRUE);
        $guestpassportBaby = $this->input->post('guestpassportBaby',TRUE);
        $birthday_m_Baby = $this->input->post('birthday_m_Baby',TRUE);
        $birthday_d_Baby = $this->input->post('birthday_d_Baby',TRUE);
        $birthday_y_Baby = $this->input->post('birthday_y_Baby',TRUE);
        //参团的成人
        for ($i=0; $i<$adult_num; $i++)
        {
            $bpe_mail_txt .= "参团人".($i+1)." 姓名：".$guestName[$i]." , 护照：".$guestpassport[$i]." , 生日：".$birthday_m[$i]."/".$birthday_d[$i]."/".$birthday_y[$i]."\n\r";
            $this->Orders_model->BPE_FirstName = $guestName[$i];
            $this->Orders_model->BPE_MiddleName = '';
            $this->Orders_model->BPE_LastName = '';
            $this->Orders_model->BPE_GuestType = 1;
            $this->Orders_model->BPE_Nationality = $this->input->post('Nationality',TRUE);
            $this->Orders_model->BPE_SEX = 100000+trim($sex[$i]);
            $this->Orders_model->BPE_Passport = $guestpassport[$i];
            $this->Orders_model->BPE_BirthDate = $birthday_m[$i].'/'.$birthday_d[$i].'/'.$birthday_y[$i];
            $bpe_sn_array[] = $this->Orders_model->biz_book_people_save();
        }
        //参团的小孩
        for ($i=0; $i<$child_num; $i++)
        {
            $bpe_mail_txt .= "参团人".($adult_num+$i+1)." 姓名：".$guestNameCHD[$i]." , 护照：".$guestpassportCHD[$i]." ,生日：".$birthday_m_CHD[$i]."/".$birthday_d_CHD[$i]."/".$birthday_y_CHD[$i]."\n\r";
            $this->Orders_model->BPE_FirstName = $guestNameCHD[$i];
            $this->Orders_model->BPE_MiddleName = '';
            $this->Orders_model->BPE_LastName = '';
            $this->Orders_model->BPE_GuestType = 2;
            $this->Orders_model->BPE_Nationality = $this->input->post('Nationality',TRUE);
            $this->Orders_model->BPE_SEX = 100000+trim($sexCHD[$i]);
            $this->Orders_model->BPE_Passport = $guestpassportCHD[$i];
            $this->Orders_model->BPE_BirthDate = $birthday_m_CHD[$i].'/'.$birthday_d_CHD[$i].'/'.$birthday_y_CHD[$i];
            $bpe_sn_array[] = $this->Orders_model->biz_book_people_save();
        }
        //参团的婴儿
        for ($i=0; $i<$baby_num; $i++)
        {
            $bpe_mail_txt .= "参团人".($adult_num+$child_num+$i+1)." 姓名：".$guestNameBaby[$i]." , 护照：".$guestpassportBaby[$i]." , 生日：".$birthday_m_Baby[$i]."/".$birthday_d_Baby[$i]."/".$birthday_y_Baby[$i]."\n\r";
            $this->Orders_model->BPE_FirstName = $guestNameBaby[$i];
            $this->Orders_model->BPE_MiddleName = '';
            $this->Orders_model->BPE_LastName = '';
            $this->Orders_model->BPE_GuestType = 3;
            $this->Orders_model->BPE_Nationality = $this->input->post('Nationality',TRUE);
            $this->Orders_model->BPE_SEX = 100000+trim($sexBaby[$i]);
            $this->Orders_model->BPE_Passport = $guestpassportBaby[$i];
            $this->Orders_model->BPE_BirthDate = $birthday_m_Baby[$i].'/'.$birthday_d_Baby[$i].'/'.$birthday_y_Baby[$i];
            $bpe_sn_array[] = $this->Orders_model->biz_book_people_save();
        }
        
        //商务订单主表
        $this->Orders_model->BIZ_COLI_ID = $this->Orders_model->biz_make_order_number();
        $trace_code = $this->Orders_model->BIZ_COLI_ID;
        $this->Orders_model->BIZ_COLI_GUT_SN = $gut_sn;
        $this->Orders_model->BIZ_COLI_AgencyID = 0;
        $this->Orders_model->BIZ_COLI_servicetype = '3';
        $this->Orders_model->BIZ_COLI_sourcetype = 32013;        
        $this->Orders_model->BIZ_COLI_ConfirmType = 52002;  //确认方式:邮件
        $this->Orders_model->BIZ_COLI_TrueCardRate = 0.03; //信用卡手续费
        $this->Orders_model->BIZ_COLI_SenderIP = $this->input->ip_address();
        $this->Orders_model->BIZ_COLI_OrderDetailText = $OrderDetailText.$this->input->post('SpecialRequest',TRUE);
        $this->Orders_model->BIZ_COLI_Price = $totalPrice;
        $this->Orders_model->BIZ_COLI_Cost = 0;
        $biz_coli_id = $this->Orders_model->biz_confirm_save();

        foreach ($flight->FlightSegmentList->FlightSegment as $key_air => $plane) 
        {
            //起落机场
            $flight_port_nameEN=ucfirst(strtolower($flight_list['FlightID'.$plane->FlightID]->DepartureAirport->PortNameEN));
            if(strtolower($flight_port_nameEN)!=strtolower($flight_list['FlightID'.$plane->FlightID]->DepartureCity->CityNameEN)) {
                $flight_port_nameEN=$flight_list['FlightID'.$plane->FlightID]->DepartureCity->CityNameEN.' '.$flight_port_nameEN;
            }
            $ArrivalPortNameEN = ucfirst(strtolower($flight_list['FlightID'.$plane->FlightID]->ArrivalAirport->PortNameEN));
            if(strtolower($ArrivalPortNameEN)!=strtolower($flight_list['FlightID'.$plane->FlightID]->ArrivalCity->CityNameEN)) {
                $ArrivalPortNameEN=$flight_list['FlightID'.$plane->FlightID]->ArrivalCity->CityNameEN.' '.$ArrivalPortNameEN;
            }
            
            //商务订单子表
            $this->Orders_model->COLD_COLI_SN = $biz_coli_id;
            $this->Orders_model->COLD_ServiceType = '3';
            $this->Orders_model->COLD_StartDate = date('Y-m-d H:i',$flight_list['FlightID'.$plane->FlightID]->TakeoffDateTime);
            $this->Orders_model->COLD_EndDate = date('Y-m-d H:i',$flight_list['FlightID'.$plane->FlightID]->ArrivalDateTime);
            $this->Orders_model->COLD_TotalCost = 0;
            $this->Orders_model->COLD_TotalPrice = $totalPrice;
            $this->Orders_model->COLD_Count = (int)$adult_num + (int)$baby_num + (int)$child_num;
            $this->Orders_model->COLD_PersonNum = (int)$adult_num;
            $this->Orders_model->COLD_ChildNum = (int)$child_num;
            $this->Orders_model->COLD_BabyNum = (int)$baby_num;
            $this->Orders_model->cold_state = 1;
            $this->Orders_model->DeleteFlag = 0;
            $cold_sn1 = $this->Orders_model->biz_confirm_detail_save();

            //机票详细子表
            $this->Orders_model->FOI_COLD_SN = $cold_sn1;
            $this->Orders_model->Aircompany = $flight_list['FlightID'.$plane->FlightID]->Carrier->AirlineCode;
            $this->Orders_model->FlightsNo = $flight_list['FlightID'.$plane->FlightID]->FlightNumber;
            $this->Orders_model->Cabin = $flight_price['PriceUnitID'.$flight->PriceList->Price[0]->PriceUnitID]->BookingClassInfoListList->BookingClassInfoList[0]->BookingClassInfo[$key_air]->CabinClass.'('.$flight_price['PriceUnitID'.$flight->PriceList->Price[0]->PriceUnitID]->BookingClassInfoListList->BookingClassInfoList[0]->BookingClassInfo[$key_air]->BookingClass.')';
            $this->Orders_model->DepartAirport = $flight_port_nameEN.' International Airport '.$flight_list['FlightID'.$plane->FlightID]->DepartureAirport->Terminal;
            $this->Orders_model->ArrivalAirport = $ArrivalPortNameEN.' International Airport '.$flight_list['FlightID'.$plane->FlightID]->ArrivalAirport->Terminal;
            $this->Orders_model->DepartureCity = $flight_list['FlightID'.$plane->FlightID]->DepartureCity->CityNameEN;
            $this->Orders_model->ArrivalCity = $flight_list['FlightID'.$plane->FlightID]->ArrivalCity->CityNameEN;
            $this->Orders_model->DepartureTime = date('H:i',$flight_list['FlightID'.$plane->FlightID]->TakeoffDateTime);
            $this->Orders_model->Arrivaltime = date('H:i',$flight_list['FlightID'.$plane->FlightID]->ArrivalDateTime);
            $this->Orders_model->DepartureDate = $this->Orders_model->COLD_StartDate;
            $this->Orders_model->adultCost = $adultCost;
            $this->Orders_model->childCost = $childCost;
            $this->Orders_model->babyCost = $babyCost;
            $this->Orders_model->adultPrice = $adultPrice;
            $this->Orders_model->childPrice = $childPrice;
            $this->Orders_model->babyPrice = $babyPrice;
            $this->Orders_model->Stopover = '';
            $this->Orders_model->PriceY = $priceY;
            $this->Orders_model->price_low = $adultCost;
            $this->Orders_model->FOI_Mile = '';
            $this->Orders_model->biz_flight_order_save();

            //联系人关联表
            foreach ($bpe_sn_array as $bsa_item)
            {
                $this->Orders_model->biz_bookpeople_List_save($cold_sn1,$bsa_item);
            }
            
            //航班信息
            $flight_key=$key_air+1;
            $flight_mail_txt .= "起飞城市".$flight_key." - 机场".$flight_key."：".$this->Orders_model->DepartureCity." - ".$this->Orders_model->DepartAirport."\n\r";
            $flight_mail_txt .= "抵达城市".$flight_key." - 机场".$flight_key."：".$this->Orders_model->ArrivalCity." - ".$this->Orders_model->ArrivalAirport."\n\r";
            $flight_mail_txt .= "起飞时间".$flight_key." - 抵达时间".$flight_key."：".$this->Orders_model->COLD_StartDate." - ".$this->Orders_model->COLD_EndDate."\n\r";
            $flight_mail_txt .= "航班".$flight_key." - 仓位：".$this->Orders_model->FlightsNo." - ".$this->Orders_model->Cabin."\n\r\n\r";
        }                                
        //给外联发送邮件
        $fromName = $this->input->post('fullname',TRUE);
        $fromEmail= $this->input->post('email',TRUE);
        $toName = $this->config->item('Site_ContactName');
        $toEmail = $this->config->item('Site_ContactEmail');
        $mailtitle = '机票订单('.$trace_code.')';
        $mailtxt = " ------- 机票订单(订单号) : ".$trace_code." ------\n\r\n\r";
        
        $temp_country = $this->Site_model->get_country_by_id($this->Orders_model->GUT_NationalityID);
        if (empty($temp_country))
        {
            $temp_country = $this->Site_model->get_country_by_id($this->Orders_model->GUT_NationalityID,1);
        }
        
        $mailtxt .= "联系人名字：".$this->Orders_model->GUT_FirstName."\n\r";
        $mailtxt .= "联系人Email：".$this->Orders_model->GUT_Email."\n\r";
        $mailtxt .= "联系人国籍：".$temp_country[0]->COI2_Country."\n\r";
        $mailtxt .= "联系人电话：".$this->Orders_model->GUT_TEL."\n\r";
        $mailtxt .= "联系人手机：".$this->Orders_model->GUT_MoveTel."\n\r\n\r";
        $mailtxt .= " ------------------机票信息----------------- "."\n\r\n\r";
        if ($round_trip_flag) {
            $mailtxt .= "预定类型：往返"."\n\r\n\r";
        }else{
            $mailtxt .= "预定类型：单程"."\n\r\n\r";
        }
        if ($stopover==1) {
            $mailtxt .= "中转情况：直飞"."\n\r\n\r";
        }else{
            $mailtxt .= "中转情况：".($stopover-1)."次中转"."\n\r\n\r";
        }
        $mailtxt .= $flight_mail_txt;
        $mailtxt .= "总价：".$this->currency->get_money_char($totalPrice)."\n\r\n\r";
        $mailtxt .= " -----------------参团人信息---------------- "."\n\r\n\r";
        $mailtxt .= "成人数 - 小孩数 - 婴儿数：".$adult_num." - ".$child_num." - ".$baby_num."\n\r";
        //header("Content-type: text/html; charset=utf-8");
        //echo $mailtxt .= $bpe_mail_txt;die();
        $mailtxt .= $bpe_mail_txt;
        $this->Orders_model->SendMail($fromName, $fromEmail, $toName, $toEmail, $mailtitle, $mailtxt);
        //只下订单(传递支付方式给thankyou页面)
        //$data['paynow'] = TRUE;
        //$pay_center_url = '/orders/flight_pay/'.$trace_code;
        $this->load->view('orders/r_thankyou/'.$this->site_code, $data);
        $this->html_do->foot($data, 'r');
    }

    public function get_flight_data($result)
    {
        $SearchResult=$result->IntlFlightSearchResponse->SearchResult;
        //飞机列表
        $flight_list = array();
        if (!is_array($SearchResult->FlightList->Flight)) {
            $SearchResult->FlightList->Flight=array(0=>$SearchResult->FlightList->Flight);
        }
        foreach ($SearchResult->FlightList->Flight as $flight) {
            $flight->TakeoffDateTime = strtotime(str_replace('T', ' ', $flight->TakeoffDateTime));
            $flight->ArrivalDateTime = strtotime(str_replace('T', ' ', $flight->ArrivalDateTime));
            $flight_list['FlightID' . $flight->FlightID] = $flight;
        }

        //价格列表
        $price_list = array();
        if (!is_array($SearchResult->PriceUnitList->PriceUnit)) {
            $SearchResult->PriceUnitList->PriceUnit=array(0=>$SearchResult->PriceUnitList->PriceUnit);
        }
        foreach ($SearchResult->PriceUnitList->PriceUnit as $f) {
            //表单页单独查询一趟航班时会返回两个价格，暂取第一个
            if (is_array($f->PriceInfoList->PriceInfo)) {
                $f->PriceInfoList->PriceInfo=array_shift($f->PriceInfoList->PriceInfo);
            }
            if (!is_array($f->BookingClassInfoListList->BookingClassInfoList)){
                $f->BookingClassInfoListList->BookingClassInfoList=array(0=>$f->BookingClassInfoListList->BookingClassInfoList);
            }
            foreach ($f->BookingClassInfoListList->BookingClassInfoList as $i => $value) {
                if (!is_array($value->BookingClassInfo)) {
                    $value->BookingClassInfo=array(0=>$value->BookingClassInfo);
                }
                //计算余票用的数据
                $f->BookingClass=end($value->BookingClassInfo)->BookingClass;
                $f->flight_key = count($value->BookingClassInfo)-1;
                //获取每个价格对应的展示舱等
                foreach ($value->BookingClassInfo as $j => $class_list_info) {
                    if ($i == 0 && $j == 0) {
                        $f->web_CabinClass = $class_list_info->CabinClass;
                    } else if ($f->web_CabinClass != $class_list_info->CabinClass) {
                        $f->web_CabinClass = '多舱等';
                    }
                }
            }
            $price_list['PriceUnitID'.$f->PriceUnitID] = $f;
        }

        //每架飞机每种舱位的余票列表
        $seat_count = array();
        foreach ($SearchResult->FlightList->Flight as $s) {
            foreach ($s->SeatCountList->SeatCount as $seat) {
                $seatID = 'FlightID' . $s->FlightID;
                $seat_count["$seatID"][$seat->Class] = $seat->Count;
            }
        }

        //航班组合列表
        $AirlineNameArr = array();//本次查询出现的航空公司
        $group_list=$SearchResult->FlightProductGroupList->FlightProductGroup;
        if (!is_array($group_list)) {
            $group_list=array(0=>$group_list);
        }
        foreach ($group_list as $group) {
            if (!is_array($group->FlightSegmentList->FlightSegment)) {
                $group->FlightSegmentList->FlightSegment=array(0=>$group->FlightSegmentList->FlightSegment);
            }
            $OrigDestSeqID_flag = false;//往返标识
            $endflight_key=0;//去程的抵达航班序号
            foreach ($group->FlightSegmentList->FlightSegment as $key => $segment) {
                if ($key == 0) {
                    $AirlineCode = $flight_list['FlightID'.$segment->FlightID]->Carrier->AirlineCode;
                    $group->AirlineName = $flight_list['FlightID'.$segment->FlightID]->Carrier->AirlineName;
                    $group->AirlineIcon = $AirlineCode;
                    $group->TakeoffDateTime = $flight_list['FlightID'.$segment->FlightID]->TakeoffDateTime;
                    $group->DeparturePortCode = $flight_list['FlightID'.$segment->FlightID]->DepartureAirport->PortCode;
                }
                else
                {
                    //多个航空公司提供服务 
                    if ($AirlineCode != $flight_list['FlightID' . $segment->FlightID]->Carrier->AirlineCode) {
                        $group->AirlineName = '多个航司';
                        $group->AirlineIcon = 'multi';
                    }
                    //当查询往返航班时设置往返标识
                    if (!$OrigDestSeqID_flag && $segment->OrigDestSeqID == '2'){
                        $OrigDestSeqID_flag = TRUE;
                    }
                    //计算抵达航班
                    elseif($segment->OrigDestSeqID!=2){
                        $endflight_key++;
                    }
                }
            }
            //去程航班到达时间及机场简码
            $endflight = $group->FlightSegmentList->FlightSegment[$endflight_key];
            $end_flightID = 'FlightID' . $endflight->FlightID;
            $group->ArrivalDateTime = $flight_list[$end_flightID]->ArrivalDateTime;
            $group->ArrivalPortCode = $flight_list[$end_flightID]->ArrivalAirport->PortCode;

            //中转情况
            if ($endflight_key == 0) {
                $group->flight_type = 'Nonstop';
            } else {
                $group->flight_type = $endflight_key. ' stop';
            }
            //收集本次查询出现过的航空公司
            if (!in_array($group->AirlineName, $AirlineNameArr)){
                $AirlineNameArr[] = $group->AirlineName;
            }
            //每个价格对应的余票
            if (!is_array($group->PriceList->Price)) {
                $group->PriceList->Price = array(0=>$group->PriceList->Price);
            }
            foreach ($group->PriceList->Price as $price) {
                //取票数最少的那一个
                foreach ($group->FlightSegmentList->FlightSegment as $k=>$f_list) {
                    $temp_seat_count = $seat_count['FlightID'.$f_list->FlightID][$price_list['PriceUnitID'.$price->PriceUnitID]->BookingClass];
                    if(!is_numeric($temp_seat_count)) $temp_seat_count=11;
                    if ($k==0) {
                        $price->seat_count = $temp_seat_count;
                    }else if($price->seat_count>$temp_seat_count){
                        $price->seat_count=$temp_seat_count;
                    }
                    //如果都是11说明返回的余票数是字母
                    if ($price->seat_count==11) {
                        $seat_FlightID=$group->FlightSegmentList->FlightSegment[$price_list->flight_key]->FlightID;
                        $price->seat_count=$seat_count['FlightID'.$seat_FlightID][$price_list['PriceUnitID'.$price->PriceUnitID]->BookingClass];
                    }
                }
            }
        }

        //每种价格对应的行李可携带重量
        $remark_list = array();
        foreach ($SearchResult->TextRemarkList->TextRemark as $remark) {
            $RemarkID = 'RemarkID' . $remark->RemarkID;
            $remark_list["$RemarkID"] = $remark;
        }

        $data['flight_list']  = $flight_list;
        $data['flight_price'] = $price_list;
        $data['airline_arr']  = $AirlineNameArr;
        $data['remark_list']  = $remark_list;
        $data['flight_group'] = $group_list;

        return $data;
    }

    //国际机票城市自动提示
    public function get_flight_city_list() {
        $q = $this->input->get('q');
        $q = strtolower($q);
        if (!$q)
            return;
        $this->lang->load('flight_cityinfo', 'english');
        $tmp = $this->lang->line('flight_city_info');
        $showcount = 1;
        foreach ($tmp as $key => $city) {
            if ($showcount > 5)
                break;

            if (stripos(strtolower($city['CityName_En']), $q) === 0) {
                echo $city['CityName_En'] . ',' . $key . "\n";
                $showcount++;
            }
        }
    } 

    public function redirect_page()
    {
        $this->load->view($this->site_code.'/public/redirect', $data);
    }  
}
