<?php

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


class Order_model extends CI_Model {
        
    function __construct() 
    {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }

    //传统订单客人信息
    //$COLI_SN 订单ID
    public function get_customer_info($COLI_SN)
    { 
        $sql="SELECT    COLI_ID,
                        COLI_SN,
                        COLI_OPI_ID,
                        CUL_SN,
                        CUL_IsLinkMan,
                        MEI_SN,
                        MEI_FirstName,
                        MEI_LastName,
                        MEI_MailList,
                        OPI_FirstName,
                        OPI_ExtTel,
                        OPI_Telephone,
                        OPI_Email,
		            	COLI_CustomerType
                FROM    CUstomerList INNER JOIN MEmberInfo ON CUL_CUI_SN=MEI_SN
                        LEFT JOIN ConfirmLineInfo ON CUL_COLI_SN=COLI_SN
                        LEFT JOIN OperatorInfo ON COLI_OPI_ID=OPI_SN 
                        LEFT JOIN COuntryInfo2 ON COI2_COI_SN=MEI_Country AND COI2_LGC=2
                        LEFT JOIN V_System_Code ON MEI_ServiceLang=SYC_SN AND SYC_Type=102 AND LGC_LGC=2
                WHERE   COLI_SN=? 
                ORDER BY MEI_SN ASC ";
        $query = $this->HT->query($sql, array($COLI_SN));
        $result=$query->result();
        return $result;
    }

    //添加客人信息
    public function add_customer_info($MEI_Occupation,$MEI_Gender,$MEI_Nationality,$MEI_FirstName,$MEI_LastName,$MEI_PassportNo,$MEI_PassportValidDate,$MEI_Birthday,$MEI_Memo,$MEI_Street,$MEI_MailList='',$MEI_Phone='',$CUL_IsEmergency=0,$CUL_COLI_SN='',$visitor_link='',$MEI_ArrivalFlightNo,$MEI_ArrivalTime,$MEI_DepartureFlightNo,$MEI_DepartureTime)
    {
        $createdate=date('Y-m-d H:i:s');
        $sql="INSERT INTO MEmberInfoTmpCheck 
                (
                    MEI_Occupation,
                    MEI_Gender,
                    MEI_Nationality,
                    MEI_FirstName,
                    MEI_LastName,
                    MEI_PassportNo,
                    MEI_PassportValidDate,
                    MEI_Birthday,
                    MEI_Memo,
                    MEI_Street,
                    MEI_MailList, 
                    MEI_Phone,
                    MEI_ServiceLang,
                    MEI_IsVendor,
                    DeleteFlag,
                    MEI_NeedNews,
                    MEI_NeedBirthCard,
                    CUL_IsEmergency,
                    CUL_COLI_SN,
                    visitor_link,
                    CreateDate,
                    CUL_IsLinkMan,
                    copyState,
                    MEI_ArrivalFlightNo,
                    MEI_ArrivalTime,
                    MEI_DepartureFlightNo,
                    MEI_DepartureTime
                ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,102001,0,0,1,1,?,?,?,?,0,0,?,?,?,?) ";
        $this->HT->query($sql, array($MEI_Occupation,$MEI_Gender,$MEI_Nationality,$MEI_FirstName,$MEI_LastName,$MEI_PassportNo,$MEI_PassportValidDate,$MEI_Birthday,$MEI_Memo,$MEI_Street,$MEI_MailList,$MEI_Phone,$CUL_IsEmergency,$CUL_COLI_SN,$visitor_link,$createdate,$MEI_ArrivalFlightNo,$MEI_ArrivalTime,$MEI_DepartureFlightNo,$MEI_DepartureTime));
        $insertid = $this->HT->last_id('MEmberInfoTmpCheck');//$this->HT->insert_id();
        return $insertid;
    }

    //获取访问过的所有链接
    public function get_visitor_link($CUL_COLI_SN,$visitor_link=false){
        $map='';
        if($visitor_link){
            $map=" AND visitor_link='$visitor_link' ";
        }
        $sql="SELECT visitor_link from MEmberInfoTmpCheck where CUL_COLI_SN=? $map ";
        $query = $this->HT->query($sql, array($CUL_COLI_SN));
        $result=$query->result();
        return $result;
    }

    public function send_mail($fromName, $fromEmail, $toName, $toEmail, $subject, $body,$frominfo='ChinaHighlights Online Writers Platform',$M_Web='info') {
        $sql = "INSERT INTO Email_AutomaticSend \n"
                . "  ( \n"
                . " M_ReplyToName, M_ReplyToEmail, M_ToName, M_ToEmail, M_Title, M_Body, M_Web,  \n"
                . " M_FromName, M_State \n"
                . "  ) \n"
                . "VALUES \n"
                . "  (  \n"
                . " ?, ?, ?, ?, ?, ?, ?, ?, 0 \n"
                . "  ) ";
        $query = $this->HT->query($sql, array($fromName, $fromEmail, $toName, $toEmail, $subject, $body, $M_Web, $frominfo));
        return $query;
    }

}