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

class Shop extends Api_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->library('shop_service', array(), 'shop');
		$this->load->model('shop_model');
		$this->load->model('card_model');
	}



	public function get_list(){
		$page = intval($this->input->post('page'));
        $pagesize = intval($this->input->post('pagesize'));
        $page = $page < 1 ? 1 : $page;
        $pagesize = $pagesize < 1 ? 10 : $pagesize;

		$order = !$this->input->post('order') ? 'create_time' : trim($this->input->post('order'));
		$category = !$this->input->post('category') ? 0 : trim($this->input->post('category'));
		$level = !$this->input->post('level') ? 0 : intval($this->input->post('level'));
		$aid = !$this->input->post('area_id') ? 0 : intval($this->input->post('area_id'));

	
		$this->load->model('Area_model', 'area');
		$area = $this->area->area_list();
		array_unshift($area, array('id' => 0, 'area_name' =>  '区域/商圈'));

		
		
		if(!in_array($order, array('buy_num', 'praise_num', 'create_time'))){
			$this->json('551', 'order error');
		}

		if(!in_array($category, array(0,1,2,3,4))){
			$this->json('552', 'category error');
		}

		if($level == 2){
			$result = $this->shop->getLevel2List($page , $pagesize, '', $category, $aid, $order);
		}else{
			$result = $this->shop->getList($page, $pagesize, $category, $aid, $order);
		}

		
	
		$output['list'] = $result;
        $output['area'] = $area;
        $this->json('0', 'succcess', $output);


	}


	public function get_level2_list($show_aid = false){


		$page = intval($this->input->post('page'));
        $pagesize = intval($this->input->post('pagesize'));
        $page = $page < 1 ? 1 : $page;
        $pagesize = $pagesize < 1 ? 10 : $pagesize;

		$date = !$this->input->post('date') ? '' : trim($this->input->post('date'));
		$order = !$this->input->post('order') ? 'create_time' : trim($this->input->post('order'));
		$category = !$this->input->post('category') ? 0 : trim($this->input->post('category'));


		if(!empty($date)){
			list($y,$m,$d) = explode('-', $date);
			if(!checkdate($m, $d, $y)){
				$this->json('555', 'date error');
			}
		}


		
		$result = $this->shop->getLevel2List($page , $pagesize, $date, $category, 0,$order);
		$this->json('0', '成功', $result);

	}

	public function detail(){
		
		$this->load->model('shop_model');
		$id = intval($this->input->post('sid'));
		$date = !$this->input->post('date') ? date('Y-m-d') : $this->input->post('date');


		if(empty($id)){
			$this->json('556', 'sid error');
		}

		$result = $this->shop_model->getShopInfo($id);

		if(empty($result)){
			$this->json('404', 'not exists');
		}

		$return['sid'] = (string)$result['sid'];
		$return['name'] = $result['name'];
		$return['title'] = $result['title'];
		$return['level2_des'] = $result['level2_des'];
		$return['level'] = $this->shop_model->getLevelByDate($result['sid'], $date);
		$return['praise_num'] = (string)$result['praise_num'];
		$return['buy_num'] = (string)$result['buy_num'];
		$return['shop_des'] = $result['shop_des'];
		$return['pic'] = isset($result['pic'][0]['pic']) ? $result['pic'][0]['pic'] : '';

		foreach($result['top_pic'] as $pic){
			$return_pic['url'] = $pic['pic'];
			$return_pic['height'] = $pic['height'];
			$return_pic['width'] = $pic['width'];
			$return['top_pic'][] = $return_pic;
		}

		foreach($result['message_pic'] as $pic){
			$return_pic['url'] = $pic['pic'];
			$return_pic['height'] = $pic['height'];
			$return_pic['width'] = $pic['width'];
			$return['message_pic'][] = $return_pic;
		}


		$return['address'] = $result['address'];
		$return['message'] = $result['message'];
		$return['tel'] = array();

		$this->load->model('card_model');
		$card = $this->card_model->getCardInfo();

		$this->load->model('mocuz/setting_model', 'setting');
		$data = $this->setting->get_keys(array('card_share_link', 'card_share_text'));
		$return['share_text'] = "我在用{$card['name']}，来{$result['name']}{$data['card_share_text']}。";

		$this->load->helper('url');
		$return['share_url'] = site_url('api/shop/share/'.$result['sid']);


		foreach($result['tel'] as $tel){
			$return['tel'][] = (string)$tel['tel'];
		}
		$return['loc'] = (string)$result['loc'];
		list($gg_lon, $gg_lat) = explode(',', $return['loc']);
		$return['baidu_loc'] = implode(',',$this->bd_encrypt($gg_lon, $gg_lat));

		$this->load->model('praise_model');


		if($this->user->checkLogin() && $this->praise_model->isPraise($this->user->uid, $id)){
			$return['praised'] = '1';
		}else{
			$return['praised'] = '0';
		}
		$this->json(0, '成功', $return);
	}


	function bd_encrypt($gg_lon,$gg_lat)
    {
        $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        $x = $gg_lon;
        $y = $gg_lat;
        $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
        $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
        $data['bd_lon'] = $z * cos($theta) + 0.0065;
        $data['bd_lat'] = $z * sin($theta) + 0.006;
        return $data;
    }

	public function praise(){
		$sid = $this->input->post('sid');

		if(!$this->user->checkLogin()){
			$this->json(10, '用户未登录');
		}

		if(!$this->shop_model->isExists($sid)){
			$this->json(561, 'shop not exists');
		}
		$this->load->model('praise_model');
		if($this->praise_model->isPraise($this->user->uid, $sid)){
			$this->json(562, '已经点赞');
		}

		
		$this->praise_model->praise($this->user->uid, $sid);
		$this->shop_model->increase('praise_num', $sid);

		$this->json(0, '成功');
	}
	

	public function share($sid){


		$this->load->model('mocuz/setting_model', 'setting');
	
 

		$shop_info = $this->shop_model->getShopInfo(intval($sid));
		$shop_info['download_url'] = $this->config->item('download_url');
		$this->load->helper('url');
		$this->load->model('card_model');
		$card = $this->card_model->getCardInfo();
		$shop_info['share_text'] = "我在用{$card['name']}，来{$shop_info['name']}立享五折。";
		$shop_info['card_name'] = $card['name'];

		
		$shop_info['client_name'] = $card['client_name'];
		$this->load->view('share', $shop_info);
	}
}
