<?php

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

class Confirm extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Order_model');
    }

    // site_code 默认是空，CT、AH 网站需要传值
    public function index($COLI_SN = false, $visitor_link = false, $token = false,$site_code='') {
        $data = array();
        if (empty($COLI_SN) || strtoupper(md5($visitor_link)) != strtoupper($token)) {
            echo '<div class="hidden">校验不对</div>';
            $this->load->view($site_code.'/link_tips', $data);
            return false;
        }

        $data['visitor_link'] = $visitor_link;

        $data['userinfo'] = $this->Order_model->get_customer_info($COLI_SN);
        $data['username'] = '';
        if (!empty($data['userinfo'])) {
            foreach ($data['userinfo'] as $v) {
                if ($v->CUL_IsLinkMan == 1) {
                    $data['username'] = $v->MEI_FirstName;
                    $data['usermail'] = $v->MEI_MailList;
                }
            }
        }else {
            $this->load->view($site_code.'/link_tips', $data);
            return false;
        }

        $visitor_link_arr = $this->Order_model->get_visitor_link($COLI_SN);
        foreach ($visitor_link_arr as $l) {
            if ($visitor_link == $l->visitor_link) {

                $this->load->view($site_code.'/link_tips', $data);
                return false;
            }
        }

        $this->load->view($site_code.'/confirm_order', $data);
    }

    // https://www.chinahighlights.com/secureinfoconfirm/confirm/test/991733/20230314153025/82899713caf652fc5e4cbe76655bdd3f/ah
    public function test($COLI_SN = false, $visitor_link = false, $token = false,$site_code='') {
      $data = array();
      if (empty($COLI_SN)) {
          echo '<div class="hidden">链接不对</div>';
          return false;
      }

      $data['visitor_link'] = $visitor_link;

      $data['userinfo'] = $this->Order_model->get_customer_info($COLI_SN);
      $data['username'] = '';
      if (!empty($data['userinfo'])) {
          foreach ($data['userinfo'] as $v) {
              if ($v->CUL_IsLinkMan == 1) {
                  $data['username'] = $v->MEI_FirstName;
                  $data['usermail'] = $v->MEI_MailList;
              }
          }
      }

      $this->load->view($site_code.'/confirm_order', $data);
      }

    public function save_confirm_info() {

        //客人信息
        $gender = $this->input->post('gender');
        $given_name = $this->input->post('given_name');
        $birthday = $this->input->post('birthday');
        $passport = $this->input->post('passport');
        $expiration_date = $this->input->post('passport_expiration_date');
        $nationality = $this->input->post('nationality');

        $occupation=$this->input->post('occupation');

        $country_code = $this->input->post('country_code');
        $contact_phone = $country_code . ' ' . $this->input->post('mobile');
        $COLI_SN = $this->input->post('COLI_SN');
        $visitor_link = $this->input->post('visitor_link');
        $COLI_ID = $this->input->post('COLI_ID');
        //航班信息
        $MEI_ArrivalFlightNo = $this->input->post('MEI_ArrivalFlightNo');
        $MEI_DepartureFlightNo = $this->input->post('MEI_DepartureFlightNo');
        // HTML datetime-local 获取的值：2023-05-29T15:45，需要替换 T 为空格才能符合数据类型
        $MEI_ArrivalTime = str_replace("T", " ", $this->input->post('MEI_ArrivalDate').' '.$this->input->post('MEI_ArrivalTime'));
        $MEI_DepartureTime = str_replace("T", " ", $this->input->post('MEI_DepartureDate').' '.$this->input->post('MEI_DepartureTime'));


        //已经确认过
        $had_save = $this->Order_model->get_visitor_link($COLI_SN, $visitor_link);

        if (!empty($had_save)) {
            echo json_encode(array('status' => 'ok'));
            return;
        }

        $MEI_SN = false;
        foreach ($this->input->post('sur_name') as $key => $name) {
            if (!empty($name)) {
                if ($this->input->post('home_address') != '') {
                    $MEI_Street = $this->input->post('home_address');
                } elseif ($this->input->post('address_street')) {
                    $MEI_Street = $this->input->post('address_street') . ',' . $this->input->post('address_city') . ',' . $this->input->post('address_state') . ',' . $this->input->post('address_country');
                } else {
                    $MEI_Street = $this->input->post('street').','.$this->input->post('city').','.$this->input->post('country');
                }
                $data['MEI_Street'] = $MEI_Street;
                if (isset($given_name[$key]) && $given_name[$key] != 'None') {
                    $MEI_FirstName = $given_name[$key];
                } else {
                    $MEI_FirstName = '';
                }


                $MEI_LastName = $name;

                $CUL_Memo = $this->input->post('insurance_information');

                $MEI_Memo = $this->input->post('special_request') . ' ' . $CUL_Memo;

                $birthday_tmp = date('Y-m-d H:i:s', strtotime($birthday[$key]));
                $expiration_date_tmp = date('Y-m-d H:i:s', strtotime($expiration_date[$key]));

                // 现在表单只用填写第一个客人的手机，所以其他客人
                if ($key > 0) {
                  $contact_phone = '';
                }
                $MEI_SN = $this->Order_model->add_customer_info($occupation[$key],$gender[$key], $nationality[$key], $MEI_FirstName, $MEI_LastName, $passport[$key], $expiration_date_tmp, $birthday_tmp, $MEI_Memo, $MEI_Street, '', $contact_phone, 0, $COLI_SN, $visitor_link,$MEI_ArrivalFlightNo,$MEI_ArrivalTime,$MEI_DepartureFlightNo,$MEI_DepartureTime);
            }
        }

        if ($MEI_SN && $this->input->post('emergency_person_name') != '') {
            $add_emergency_res = $this->Order_model->add_customer_info(
                   '', '', '', $this->input->post('emergency_person_name'), '', '', '', '', $this->input->post('emergency_relationship'), '', $this->input->post('emergency_email'), $this->input->post('emergency_telephone'), 1, $COLI_SN, $visitor_link
                ,$MEI_ArrivalFlightNo,$MEI_ArrivalTime,$MEI_DepartureFlightNo,$MEI_DepartureTime
            );
        }

        if ($MEI_SN) {
            $customer_usermail = $this->input->post('customer_usermail');
            $customer_username = $this->input->post('customer_username');
            $toname = $this->input->post('toname');
            $tomail = $this->input->post('tomail');

            $data['postdata'] = $this->input->post();
            $mail_text = $this->load->view('mailtext', $data, true);
            $this->Order_model->send_mail('chinahighlights', 'service@chinahighlights.com', $toname, $tomail, $COLI_ID . ' 团个人信息导入HT', $COLI_ID . ' 团个人信息导入HT<br>' . $mail_text, 'China Highlights', $M_Web = 'CHT_USERINFO');
            //发送信息给客人
            //$this->Order_model->send_mail($toname, $tomail, $customer_username, $customer_usermail, 'Passenger Information', $mail_text, 'China Highlights', $M_Web = 'CHT_USERINFO');

            echo json_encode(array('status' => 'ok'));
        } else {
            echo json_encode(array('status' => 'no'));
        }
    }

    function review() {
      header('Content-type: application/json;charset=utf-8');
      $data = array();
      $site_code = $this->input->get('site_code');
      $viewName = $site_code.'/mailtext';
      $data['postdata'] = $this->input->post();
      $mail_text = $this->load->view($viewName, $data, true);
      echo json_encode($mail_text);
    }

}
