<?php


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

class Apish extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('apish_model');
        $this->load->library('Currency');
        
    }
    
    var $imageCdn = "//images.shanghaihighlights.com";//有些HT绑定图片需要加cdn

    public function index()
    {
        echo("<1>apish</h1>");    
        
    }

    /**
     * @description: 返回一日游价格
     * @param {*} $returntype
     * @return {*}
     * @Date Changed: 
     */
    public function getDaytripsPrice(){
        if (isset($_GET["param"])){
            $param = $_GET["param"];
            if (strpos($param,",")!== false){
                $pagecode = explode(",",$param)[0];
                $personnum=explode(",",$param)[1];
            }else{
                $pagecode = $param;
                $personnum=2;
            }
            if (isset($_GET["personnum"])){
                $personnum=$_GET["personnum"];
            }else{
                $personnum=2;
            }
            $result["status"]="ok";
            $price = $this->apish_model->getDaytripsPrice($pagecode,$personnum); 
            $PKP_AdultSpecialPrice = $price->PKP_AdultSpecialPrice;
            $PKP_AdultPrice = $price->PKP_AdultPrice;
            if ($PKP_AdultPrice!=$PKP_AdultSpecialPrice && $PKP_AdultSpecialPrice>0){
                $result["price"] =   $this->currency->convert_moneny_by_char($PKP_AdultSpecialPrice,"USD");
            }else{
                $result["price"] =   $this->currency->convert_moneny_by_char($PKP_AdultPrice,"USD");
            }
             
            echo json_encode($result);
        }else {
            $result["status"]="no param";
            echo json_encode($result);
        }

    }


    /**
     * @description: 获取精华线路价格（2人等) 
     * @param {*}
     * @return {*}
     * @Date Changed: 
     */
    public function getTourPrice(){
        if (isset($_GET["param"])){
            $param = $_GET["param"];
            $result["status"]="ok";
            $price = $this->apish_model->getTourPrice($param);
            $result["pricecn"]=$price;
            if (is_numeric($price)){
                $price = $this->currency->calc_show_price($price);
            }else{
                $price = "0";
            }
            
            $result["price"] = $price;
            echo json_encode($result);
        }else{
            $result["status"]="no param";
            echo json_encode($result);
        }
    }


    /**
     * @description: 获取HT及信息平台的一日游列表数据
     * @param {*}
     * @return {*}
     * @Date Changed: 
     */
    public function getDaytripList(){
        //先处理传递过来的参数（逗号隔开两个参数的格式：城市名称，父ID）
        $whereHT="";
        $whereInfo="";
        if (isset($_GET["param"])){
            $param = str_replace("'","''",$_GET["param"]);
            if (strpos($param,",")!== false){
                $city = explode(",",$param)[0];
                $whereHT = " and (CII2_Name ='".$city."') ";
                $parentid = explode(",",$param)[1];
                $whereInfo = " and is_parent_id = '".$parentid."'";
            }else{
                $whereHT = " and (CII2_Name ='".$param."') ";
                $whereInfo = " and is_parent_id = -1 ";
            }
        }
        

        $list = $this->apish_model->getDaytripList($whereHT,$whereInfo);

        foreach ($list as $row) {
            //获取价格
            $price = $this->apish_model->getDaytripsPrice($row->code,2);
            if ($price!=null){
                $PKP_AdultSpecialPrice = $price->PKP_AdultSpecialPrice;
                $PKP_AdultPrice = $price->PKP_AdultPrice;
                if ($PKP_AdultPrice!=$PKP_AdultSpecialPrice && $PKP_AdultSpecialPrice>0){
                    $addPrice =   $this->currency->convert_moneny_by_char($PKP_AdultSpecialPrice,"USD");
                }else{
                    $addPrice=   $this->currency->convert_moneny_by_char($PKP_AdultPrice,"USD");
                }
            }else{
                $addPrice=0;
            }

            //URL
            if (empty($row->url)){
                $str_Url = "/shanghai-day-trips/".strtolower($row->code).".htm";
                $row->url = $str_Url; //更新url
            }
		
				
			//图片

            if (!empty($row->pic1)){
                $picurl = $row->pic1;                
            }else if(!empty($row->pic2)){
                $picurl = $row->pic2;
            }else{
                $picurl="//data.shanghaihighlights.com/image/loading.gif";
            }

            $row->price = $addPrice; //新增价格属性
            $picurl = $this->apish_model->getPicCdn($picurl).$picurl;
            $row->pic =$picurl; //图片地址
            
        }

        echo json_encode($list);
    }


   /**
    * @description: 获取新旧网站的子类列表
    * @param {*} $old_pid
    * @param {*} $new_pid
    * @return {*}
    * @Date Changed: 
    */ 
   public function getGuideList($old_pid = null,$new_pid=null)
   {
       //处理参数
       if (isset($_GET["param"])){
        $param = str_replace("'","''",$_GET["param"]);
        if (strpos($param,",")!== false){
            $new_pid = explode(",",$param)[0];
            $old_pid = explode(",",$param)[1];
        }else{
            $old_pid = -1;
            $new_pid = $param;
        }     

       }

       if (empty($old_pid) || !is_numeric($old_pid)){
           $old_pid = -1 ;
       }

       if (empty($new_pid)|| !is_numeric($new_pid)){
           $new_pid = -1;
       }

       $list = $this->apish_model->getGuideList($old_pid,$new_pid);
       foreach ($list as $row) {
           //图片
           if (!empty($row->ic_photo)){
               $picurl = $row->ic_photo;
           }else if (!empty($row->pic1)){
               $picurl = $row->pic1;
           }else{
               $picurl="//data.chinatravel.com/images/no-photos.jpg";
           }
        //    if (strpos($picurl,"chinatravel.com")!== false){
        //    }else{
        //         $picurl = $this->imageCdn.$picurl;
        //     }
           $picurl = $this->apish_model->getPicCdn($picurl).$picurl;
           $row->pic = $picurl; //显示图片地址
       }

       echo json_encode($list);
   }

   /**
    * @description: 获取线路sn
    * @param {*} $clino
    * @return {*}
    * @Date Changed: 
    */
   public function getCliSn(){
        //处理参数
       if (isset($_GET["param"])){
           $param = $_GET["param"];
           $result["status"]="ok";
           $tour = $this->apish_model->getCliSn($param);
           $CLI_SN = $tour->CLI_SN;
           if (!empty($CLI_SN)){
            $result["clisn"]=$CLI_SN;
           }else {
               $result["clisn"]=0;
           }
       }else{
            $result["status"]="no param";
            $result["clisn"]=0;
            
       }
       echo json_encode($result);
   }


   /**
    * @description: 一日游价格表
    * @param {*}
    * @return {*}
    * @Date Changed: 
    */
   public function getDaytripPriceList(){
        if (isset($_GET["param"])){
            $param = str_replace("'","''",$_GET["param"]);
            $list = $this->apish_model->getDaytripPriceList($param);
            if (!empty($list)){
                if (count($list)==1){
                    //echo "1";
                    foreach ($list as $row) {
                        $PKP_AdultPrice = $row->PKP_AdultPrice;
                        $PKP_AdultSpecialPrice = $row->PKP_AdultSpecialPrice;
                        if ($PKP_AdultPrice!=$PKP_AdultSpecialPrice && $PKP_AdultSpecialPrice>0){
                            $PKP_AdultPrice=$PKP_AdultSpecialPrice;
                        }
                        $PKP_AdultPrice =  $this->currency->convert_moneny_by_char($PKP_AdultPrice,"USD");
                        echo '<table border="0" align="center" cellpadding="10" cellspacing="1" class="pricetable"><tr><th>>1 Person</th></tr><tr><td><strong class="orange"> $'.$PKP_AdultPrice.'</strong></td></tr></table>';
                    }
                }else{
                    //echo "2";
                    $str_th="";
                    $str_td="";
                    foreach ($list as $row) {
                        $PKP_AdultPrice = $row->PKP_AdultPrice;
                        $PKP_AdultSpecialPrice = $row->PKP_AdultSpecialPrice;
                        if ($PKP_AdultPrice!=$PKP_AdultSpecialPrice && $PKP_AdultSpecialPrice>0){
                            $PKP_AdultPrice=$PKP_AdultSpecialPrice;
                        }
                        $PKP_AdultPrice =  $this->currency->convert_moneny_by_char($PKP_AdultPrice,"USD");
                        $PKP_PersonStart = $row->PKP_PersonStart;
                        $PKP_PersonStop = $row->PKP_PersonStop;
                        //'根据数据生成显示内容
                        if ($PKP_PersonStart == $PKP_PersonStop){
                            $str_th .= "<th> ".$PKP_PersonStart." Person</th>";
                        }
                        else if ($PKP_PersonStop=="1000") {
                            $str_th .= "<th> >".$PKP_PersonStart." Person</th>";
                        }
                        else{
                            $str_th .= "<th> ".$PKP_PersonStart." - ".$PKP_PersonStop." Person</th>";
                        }
                    
                        $str_td .="<td><strong class='orange'> $ ".$PKP_AdultPrice."</strong></td>";
                    }
    
                    echo '<table border="0" align="center" cellpadding="10" cellspacing="1" class="pricetable"><tr>'.$str_th.'</tr><tr>'.$str_td.'</tr></table>';
                }
            }
            
        }else{
            echo "";
        }
   }


}

/* End of file Api.php */
