<?php

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

class Ticket extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->config->load('config');
        $this->load->model("BIZ_train_model"); //加载模型
    }

    //输入翰特订单号cols_id,获取火车订票的相关信息，模拟翰特订单详情页面
    public function index() {
        $data = array();
        $coli_id = $this->input->post("ht_order"); //商务订单号
        //主订单信息
        $data['order_detail'] = $this->BIZ_train_model->confirm_line_info($coli_id);
        if (empty($data['order_detail'])) {
            echo '找不到订单';
            return false;
        }

        //外联信息
        $data['operator_detail'] = $this->BIZ_train_model->operator_detail($data['order_detail']->COLI_OPI_ID);
        if (empty($data['operator_detail'])) {
            echo '找不到外联信息';
            return false;
        }

        //子订单信息+产品信息,一对一
        $data['order_detail_list'] = $this->BIZ_train_model->confirm_line_detail($data['order_detail']->COLI_SN);
        if (empty($data['order_detail_list'])) {
            echo '找不到订单详情';
            return false;
        }
//子订单信息+乘客信息，一对多



        $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);
            $people = array();
            $i = 0;
            $list->info = "";
            foreach ($cold_sn as $v) {
                $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');
    }

    public function search() {
        $data = array();
        $data['from_station'] = $this->input->get_post("from_station");
        $data['to_station'] = $this->input->get_post("to_station");
        $data['query_date'] = $this->input->get_post("query_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" => "站点名称错误")));
        }
    }

}
