<?php

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

class Call extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->permission->is_admin(false);
        $this->load->model('form_model');
    }

    //表单列表
    public function index() {
        echo 'hi world!';
    }

    //设置页头，保证其他域名可以调用
    public function allow_domain() {
        //  $this->output->set_header('Access-Control-Allow-Origin: http://localhost,http://www.asiahighlights.com,https://www.asiahighlights.com,http://www.chinahighlights.com,http://www.arachina.com');
        $this->output->set_header('Access-Control-Allow-Origin: *');
    }

    //调用表单
    public function call_form() {
        $this->allow_domain();
        $data = array();
        $form_sn = $this->input->get_post('form_sn');
        $data['form_data'] = $this->form_model->get_froms($form_sn);
        $this->load->view('ajax_form', $data);
    }

    //网前填写表单入HT库
    public function save_order($type = 'tailormade') {
        $this->allow_domain();
        $order_type_array = array('contactus' => '32007', 'tailormade' => '32003', 'question' => '32002');
        $order_type = mb_strtolower($type);
        if (isset($order_type_array[$order_type])) {
            $order_type = $order_type_array[$order_type];
        } else {
            $order_type = '32003';
        }
        //订单表字段值
        $this->form_model->COLI_ID = $this->form_model->MakeOrderNumber();
        $this->form_model->COLI_sourcetype = $order_type;
        $this->form_model->COLI_PersonNum = 0; //$this->input->post('AdultsNumber');
        $this->form_model->COLI_ChildNum = 0; //$this->input->post('ChildrenNumber');
        $this->form_model->COLI_BabyNum = 0; //$this->input->post('BabiesNumber');
        $this->form_model->COLI_OrderStartDate = $this->input->post('DepartureDate') ? $this->input->post('DepartureDate') : '';
        $this->form_model->COLI_Days = $this->input->post('TripLength') ? $this->input->post('TripLength') : 0;
        $this->form_model->COLI_SenderIP = $this->input->ip_address();
        $this->form_model->COLI_Sended = 1;
        $this->form_model->COLI_WebCode = 'CHT';
        $this->form_model->COLI_Servicetype = 'T';
        $this->form_model->COLI_Area = 2;
        $this->form_model->COLI_Currency = "USD";
        $this->form_model->COLI_InterestRate = 0.2;
        $this->form_model->COLI_TrueCardRate = 0.025;
        $this->form_model->COLI_CustomerClass = 3001;
        $this->form_model->COLI_OrderType = '19001';
        $this->form_model->COLI_GroupType = '19001';
        $this->form_model->COLI_PayManner = 123001;
        $this->form_model->COLI_CustomerType = 101;
        switch ($this->check_device()) {
            case 'mobile':
                $this->form_model->COLI_OrderSource = '62003';
                break;
            case 'tablet':
                $this->form_model->COLI_OrderSource = '62002';
                break;
            default:
                $this->form_model->COLI_OrderSource = '62001';
        }

        $this->form_model->COLI_OrderDetailText = '';
        foreach ($this->input->post() as $key => $value) {
            if (is_array($value)) {
                $this->form_model->COLI_OrderDetailText.=$key . '：';
                foreach ($value as $k => $v) {
                    $this->form_model->COLI_OrderDetailText.=$value . " 、";
                }
                $this->form_model->COLI_OrderDetailText.="；\n\r";
            } else {
                $this->form_model->COLI_OrderDetailText.=$key . '：' . $value . "；\n\r";
            }
        }
        $this->form_model->COLI_OrderDetailText.=$_SERVER['HTTP_REFERER'];

        //客人信息表字段值
        $pos = strstr($this->input->post('Name'), ' ');
        if ($pos === false) {
            $this->form_model->MEI_FirstName = $this->input->post('Name') ? $this->input->post('Name') : '';
        } else {
            $MEI_FirstName = str_replace($pos, '', $this->input->post('Name'));
            $this->form_model->MEI_FirstName = empty($MEI_FirstName) ? '' : $MEI_FirstName;
            $this->form_model->MEI_LastName = $pos;
        }
        $this->form_model->MEI_MailList = $this->input->post('Email') ? $this->input->post('Email') : '';
        $this->form_model->MEI_Nationality = $this->input->post('Nationality');
        $this->form_model->MEI_Phone = $this->input->post('Phone') ? $this->input->post('Phone') : '';
        $this->form_model->MEI_Gender = 100001;
        if ($this->input->post('Gender') == 'Female' || $this->input->post('Gender') == 'Ms.') {
            $this->form_model->MEI_Gender = 100003;
        }
        $this->form_model->MEI_ServiceLang = 102001;
        $this->form_model->MEI_Birthday = ''; //$this->input->post('Birthday');


        $haha = $this->form_model->TourOrderSave();

        //发送邮件给客人
        $mail['gender'] = $this->input->post('Gender');
        $mail['realname'] = $this->input->post('Name');
        $mail['Nationality'] = $this->input->post('Nationality');
        $mail['email'] = $this->input->post('Email');
        $mail['PhoneNo'] = $this->input->post('Phone');
        $mail['traveltype'] = $this->input->post('GroupType');
        $mail['days'] = $this->input->post('TripLength');
        $mail['DatesFlexible'] = $this->input->post('flexible');
        $mail['Starting_Date'] = $this->input->post('DepartureDate');
        $mail['hotelstyle'] = $this->input->post('Hotel');
        $mail['EveryDayGuided'] = $this->input->post('NeedFreeTime');
        $mail['FlightsPrefered'] = $this->input->post('traffic');
        $DetailText = '';
        foreach ($this->input->post() as $key => $value) {
            if (in_array($key, array('Gender', 'Name', 'Nationality', 'Email', 'Phone', 'GroupType', 'TripLength', 'flexible', 'DepartureDate', 'Hotel', 'NeedFreeTime', 'traffic'))) {
                continue;
            }
            if (is_array($value)) {
                $DetailText.=$key . '：';
                foreach ($value as $k => $v) {
                    $DetailText.=$value . " 、";
                }
                $DetailText.="；\n\r";
            } else {
                $DetailText.=$key . '：' . $value . "；\n\r";
            }
        }

        $mail['Other_Request'] = $DetailText;
        $maildata['mail'] = $mail;

        $mail_body = $this->load->view('mail_tpl', $maildata, true);

        $this->load->model('InfoSMS_model');
        $this->InfoSMS_model->send_mail('ChinaHighlights', 'service@chinahighlights.com', $mail['realname'], $mail['email'], 'You have successfully sent your enquiry', $mail_body, 'CH_FORM', $M_Web = 'CHT');
        $this->InfoSMS_model->send_mail($mail['realname'], $mail['email'], 'ChinaHighlights', 'service@chinahighlights.com', $type, $this->form_model->COLI_OrderDetailText, 'CH_FORM', $M_Web = 'CHT');

        //处理支付链接
        if ($this->input->post('formbuilder_paynowactive') == 'true') {
            $paynowamount = $this->input->post('formbuilder_paynowamount');
            $paynownumber = $this->input->post('formbuilder_paynownumber');
            $paypal_url = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=pays@chinahighlights.com&amount=$paynowamount&currency_code=USD&lc=en_us&item_number=$paynownumber&item_name=$paynownumber&no_shipping=1&pbtype=general&bn=paywiz4mso-001.000&mrb=R-4VT64848FL959270J";
            $data = array("name" => "go", "msg" => $paypal_url);
        } else {
            switch ($type) {
                case 'tailormade':
                    $data = array("name" => "go", "msg" => "http://www.chinahighlights.com/forms/thankyou-tailormadetour.asp");
                    break;
                case 'contactus':
                    $data = array("name" => "go", "msg" => "http://www.chinahighlights.com/forms/thankyou-contact.asp");
                    break;
                case 'question':
                    $data = array("name" => "go", "msg" => "http://www.chinahighlights.com/forms/thankyou.asp");
                    break;
                default :
                    $data = array("name" => "ok", "msg" => "Submit Success！Thank you for your inquiry.");
            }
        }
        echo json_encode($data);
    }

    public function check_device() {
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            $ua = $_SERVER['HTTP_USER_AGENT'];
        } else {
            $ua = '';
        }
        ## This credit must stay intact (Unless you have a deal with @lukasmig or frimerlukas@gmail.com
        ## Made by Lukas Frimer Tholander from Made In Osted Webdesign.
        ## Price will be $2
        $iphone = strstr(strtolower($ua), 'mobile'); //Search for 'mobile' in user-agent (iPhone have that)
        $android = strstr(strtolower($ua), 'android'); //Search for 'android' in user-agent
        $windowsPhone = strstr(strtolower($ua), 'phone'); //Search for 'phone' in user-agent (Windows Phone uses that)

        if (!function_exists('androidTablet')) {

            function androidTablet($ua) { //Find out if it is a tablet
                if (strstr(strtolower($ua), 'android')) { //Search for android in user-agent
                    if (!strstr(strtolower($ua), 'mobile')) { //If there is no ''mobile' in user-agent (Android have that on their phones, but not tablets)
                        return true;
                    }
                }
            }

        }
        $androidTablet = androidTablet($ua); //Do androidTablet function
        $ipad = strstr(strtolower($ua), 'ipad'); //Search for iPad in user-agent

        if ($androidTablet || $ipad) { //If it's a tablet (iPad / Android)
            return 'tablet';
        } elseif ($iphone && !$ipad || $android && !$androidTablet || $windowsPhone) { //If it's a phone and NOT a tablet
            return 'mobile';
        } else { //If it's not a mobile device
            return 'desktop';
        }
    }

}
