<?php

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

class Ht extends CI_Controller {
	
	private $data = array();

    public function __construct() {
        parent::__construct();
        //$this->load->model('Order_model');
        $this->load->model('BIZorder_model');
        $this->load->model('infoMetas_model');
        //$this->output->enable_profiler(TRUE);
		$this->data['nav'] = 'index';
        $this->data['nav_header'] = $this->load->view('index/order_header', '', true);
    }

    //订单列表页
    public function index($date = false) {
        $this->data['date'] = $date;
        if (empty($this->data['date'])) {
            $this->data['date'] = date('Y-m-d', time());
        }
		$coli_id = $this->input->post('coli_id');
		$startdate_bettwen = $this->input->post('startdate_bettwen') ? $this->input->post('startdate_bettwen') : date('Y-m-d',strtotime('1990-01-01'));
		$startdate_and = $this->input->post('startdate_and') ? $this->input->post('startdate_and') : date('Y-m-d',strtotime('2030-01-01'));
		$order_list = $this->BIZorder_model->get_order_list_ids($coli_id,$startdate_bettwen,$startdate_and);
		$this->data['coli_id'] = $coli_id;
		$this->data['startdate_bettwen'] = $this->input->post('startdate_bettwen');
		$this->data['startdate_and'] = $this->input->post('startdate_and');
        
        $this->data['order_list'] = $order_list;
        $this->load->view('header', $this->data);
        $this->load->view('index');
        $this->load->view('footer');
    }

    //获取信息，显示导游设置情况
    public function detail_modal($cold_sn) {
        $this->data['cold_info'] = $this->BIZorder_model->detail_by_cold($cold_sn);
        $this->data['meta_dest_guidename'] = $this->infoMetas_model->get($cold_sn, 'meta_dest_guidename');
        $this->data['meta_dest_guidephone'] = $this->infoMetas_model->get($cold_sn, 'meta_dest_guidephone');
        $this->data['meta_dest_guidetime'] = $this->infoMetas_model->get($cold_sn, 'meta_dest_guidetime');
        $this->data['meta_dest_guidehotel'] = $this->infoMetas_model->get($cold_sn, 'meta_dest_guidehotel');
		
        echo json_encode($this->load->view('guide_setting', $this->data, true));
    }

    //保存导游设置信息
    public function submit_guide_modal() {
        $meta_dest_guidename = $this->input->post('meta_dest_guidename');
        $meta_dest_guidephone = $this->input->post('meta_dest_guidephone');
        $meta_dest_guidetime = $this->input->post('meta_dest_guidetime');
        $meta_dest_guidehotel = $this->input->post('meta_dest_guidehotel');
        $cold_sn = $this->input->post('setguide_cold_sn');
        $cold_info = $this->BIZorder_model->detail_by_cold($cold_sn);

        if (!empty($meta_dest_guidename) && !empty($meta_dest_guidephone) && !empty($meta_dest_guidetime) && !empty($meta_dest_guidehotel)) {
            if ($this->infoMetas_model->get($cold_sn, 'meta_dest_guidename')) {
                $this->infoMetas_model->update($cold_sn, 'meta_dest_guidename', $meta_dest_guidename);
            } else {
                $this->infoMetas_model->add($cold_sn, 'meta_dest_guidename', $meta_dest_guidename);
            }

            if ($this->infoMetas_model->get($cold_sn, 'meta_dest_guidephone')) {
                $this->infoMetas_model->update($cold_sn, 'meta_dest_guidephone', $meta_dest_guidephone);
            } else {
                $this->infoMetas_model->add($cold_sn, 'meta_dest_guidephone', $meta_dest_guidephone);
            }

            if ($this->infoMetas_model->get($cold_sn, 'meta_dest_guidetime')) {
                $this->infoMetas_model->update($cold_sn, 'meta_dest_guidetime', $meta_dest_guidetime);
            } else {
                $this->infoMetas_model->add($cold_sn, 'meta_dest_guidetime', $meta_dest_guidetime);
            }

            if ($this->infoMetas_model->get($cold_sn, 'meta_dest_guidehotel')) {
                $this->infoMetas_model->update($cold_sn, 'meta_dest_guidehotel', $meta_dest_guidehotel);
            } else {
                $this->infoMetas_model->add($cold_sn, 'meta_dest_guidehotel', $meta_dest_guidehotel);
            }
			
			// 插入导游信息进入翰特系统 lzq
			// 拼接信息
			$current_date = date('Y-m-d',time());
			$update_info = "\r\n $cold_info->PAG_Code , 导游：$meta_dest_guidename , 电话：$meta_dest_guidephone , 接客时间：$meta_dest_guidetime , 酒店：$meta_dest_guidehotel ====== 确认人：谭湘亚 , 确认日期 ：$current_date";
			
			$taget = $this->Order_model->update_confirmInfo($update_info,$cold_info->COLI_ID);	
			
            $fromName = 'ChinaHighlights';
            $fromEmail = 'davidyang@chinahighlights.net';
            $toName = $cold_info->GUT_FirstName . ' ' . $cold_info->GUT_LastName;
            $toEmail = $cold_info->GUT_Email;
            $to_sex = $cold_info->GUT_SEX == 1 ? 'Mr.' : 'Ms.';
			$start_date = date('M.d.Y',strtotime($cold_info->COLD_StartDate));
			$coli_id = $cold_info->COLI_ID;
            $subject = "Tour guide information on $start_date , order No.$coli_id";
			$body = "
			Dear $to_sex $toName,<br/>
			Your tour guide on $start_date is $meta_dest_guidename (mobile $meta_dest_guidephone), he/she will pick you up at $meta_dest_guidetime from  $meta_dest_guidehotel.<br/>
			Have a nice trip!<br/>
			Best Regards<br/>
			Chinahighlights Beijing Office
			Phone:+86-10-56120208 (7*24 hours)
			";
            $this->BIZorder_model->save_mail($fromName, $fromEmail, $toName, $toEmail, $subject, $body, $frominfo = 'China Highlights', $M_Web = 'destination');
            echo json_encode('保存成功！');
        } else {
            echo json_encode('保存失败！请填写各项数据！');
        }
    }

}
