<?php

/**
 * 前台 城市地标 模块 Model
 * ============================================================================
 * ZhunaCMS 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhaojianjun $  <zwttmac@qq.com>
 * $Id: lable.class.php 87 2011-12-28 08:04:56Z zhaojianjun $
 * http://un.zhuna.cn；
 */
class lable extends Model
{

    private $db;
    private $hotel_table;
    private $city_table;
    private $point_class_table;
    private $hotel_left_model;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->hotel_table = DB_PREFIX . 'hotel';
        $this->city_table = DB_PREFIX . 'city';
        $this->point_class_table = DB_PREFIX . 'point_class';
        $this->hotel_left_model = Tmac::model('hotelLeft');
    }

    /**
     * 取省列表 或根据省id取城市列表
     * @param <type> $type  默认c取city列表 p取province列表
     * @param <int>  $pid   省级id
     * @return array
     */
    public function getProvince()
    {
        $rs = $this->db->GetAll("SELECT pid, pname,cityid FROM $this->city_table WHERE province = 1");
        return $rs;
    }

    /**
     * 取ajax联动城市菜单选项
     * @param <type> $pid
     */
    public function getSelectProvince($pid)
    {
        //取省级名字
        $province_ary = $this->getCity('p');
        //把缩写和名字组合在一起
        $province_array = array();
        $province_array[0] = '直辖市';
        $zhixia = array('3', '25', '27', '32');
        foreach ($province_ary AS $k => $v) {
            if (in_array($v['Pid'], $zhixia))
                continue;
            $province_array[$v{'Pid'}] = $v['Pname'];
        }
        $province_option = Utility::Option($province_array, $pid);
        return $province_option;
    }

    public function getLableArray($cityid, $classid)
    {
        $nofind_lable = '';
        $pages = $this->P('Pages');
        $page = $pages->getNowPage();
        $cfg_apiurl = Tmac::config('configcache.config.cfg_apiurl', APP_NAME, '.inc.php');

        $url = PHP_SELF . "?m=lable&cityid={$cityid}&classid={$classid}&page=";
        $xml_url = $cfg_apiurl . "lable_json.php?cityid=$cityid&classid={$classid}&pg={$page}";
        $xml_root = VAR_ROOT . "json/citylable/" . $cityid . '/';
        $xml_name = "lable_{$cityid}_{$classid}_{$page}.json";
        $this->hotel_left_model->createXML($xml_url, $xml_root, $xml_name);
        $xmlstring = $xml_root . $xml_name;

        $lable_result = json_decode(file_get_contents($xmlstring), true);
        $lable_ary = array();
        if ($lable_result['retmsg'] == 'ok') {
            $totalput = $lable_result['retHeader']['totalput'];

            $pages->setTotal($totalput);
            $pages->setUrl($url);
            $pages->setPrepage(200);
            $pages->getSqlLimit();

            $lable_ary = array();
            if (count($lable_result['reqdata']) > 0) {
                foreach ($lable_result['reqdata'] as $k => $basexml) {
                    $lable_ary[$k]['key'] = $basexml['name'];
                    $lable_ary[$k]['key_urlencode'] = urlencode($basexml['name']);
                    $lable_ary[$k]['cityid'] = $basexml['cityid'];
                    $lable_ary[$k]['classid'] = $basexml['classid'];
                    $lable_ary[$k]['classname'] = $basexml['classname'];
                }
            } else {
                $nofind_lable = "暂无此地标列表!";
            }
        } else {
            $nofind_lable = "暂无此地标列表!";
        }

        $result['rs'] = $lable_ary;
        $result['nofind'] = $nofind_lable;
        $result['page'] = $pages->show();
        return $result;
    }

    public function getClassname($classid)
    {
        $sql = "SELECT classname FROM $this->point_class_table WHERE class_id = $classid LIMIT 0, 1";
        $rs = $this->db->getOne($sql);
        return $rs;
    }

    /**
     * 根据pid取省会城市的cityinfo(cityid,cityname)
     * @param <type> $pid
     */
    public function getCityinfoByPid($pid)
    {
        $cityinfo = $this->M('common')->getCityinfo($pid, 'pid');
        $city['cityname'] = $cityinfo['CityName'];
        $city['cityid'] = $cityinfo['CityID'];
        return $city;
    }

    /**
     * 取省列表 或根据省id取城市列表
     * @param <type> $type  默认c取city列表 p取province列表
     * @param <int>  $pid   省级id
     * @return array
     */
    public function getCity($type = 'c', $pid = null)
    {
        if ($type == 'p') {
            $rs = $this->db->getAll("SELECT Pid, Pname, suoxie FROM $this->city_table WHERE province = 1");
        } else {
            $rs = $this->db->getAll("SELECT cityid, cityname, suoxie FROM $this->city_table WHERE pid = $pid ORDER BY province DESC, abcd ASC");
        }
        return $rs;
    }

}