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

class Index extends AdminApi_Controller {

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

	public function login(){
		$sid = $this->input->post('username');
		$password = $this->input->post('password');

		$this->load->model('shop_model');
		if($shopinfo = $this->shop_model->login($sid, $password))
		{
			$data['sauth'] = $this->shop->make_sauth($sid, $password);
			$data['name'] = $shopinfo['name'];
			$data['address'] = $shopinfo['address'];
			$data['loc'] = $shopinfo['loc'];
			list($gg_lon, $gg_lat) = explode(',', $shopinfo['loc']);
			$data['baidu_loc'] = implode(',',$this->bd_encrypt($gg_lon, $gg_lat));
			$data['category'] = (string)$shopinfo['category'];
			$data['create_time'] = (string)$shopinfo['create_time'];
			$data['level'] = $this->shop_model->getLevelByDate($shopinfo['sid'], date('Y-m-d'));
			foreach($shopinfo['tel'] as $tel){
				$data['tel'][] = (string)$tel['tel'];
			}

			$this->json(0, 'success', $data);
		}else{
			$this->json(2, '账号或密码错误');
		}
	}



	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 info(){
		$sauth = $this->input->post('sauth');
		$imageauth = trim($this->input->post('imageauth'));
		$this->load->library('User_service', array(), 'user');

		if(!($imageinfo = $this->user->decode($imageauth))){
			$this->json(550, 'imageinfo not exists');
		}

		if(!($userinfo = $this->user->getUserInfo())){
			$this->json(560, '该用户不存在');
		}

		$data['name'] = $this->shop->shopinfo['name'];
		$data['address'] = $this->shop->shopinfo['address'];
		$data['loc'] = $this->shop->shopinfo['loc'];
		$data['category'] = (string)$this->shop->shopinfo['category'];
		$data['create_time'] = (string)$this->shop->shopinfo['create_time'];
		$data['shop_level'] = $this->shop_model->getLevelByDate($this->shop->shopinfo['sid'], date('Y-m-d'));
		$data['user_level'] = $this->user->level;
		$data['sauth'] = $sauth;
		foreach($this->shop->shopinfo['tel'] as $tel){
			$data['tel'][] = (string)$tel['tel'];
		}
		$this->json(0, 'success', $data);
}


	public function record(){
		$level = trim($this->input->post('level'));
		$price = trim($this->input->post('price'));
		$discount_price = trim($this->input->post('discount_price'));
		$number = trim($this->input->post('number', true));
		$imageauth = trim($this->input->post('imageauth'));

		


		if($level != 1 && $level != 2 || !$level){
			$this->json('550', 'level error');
		}

		if(!is_numeric($price)){
			$this->json(550, 'price must number');
		}

		if(!is_numeric($discount_price)){
			$this->json(550, 'discount_price must number');
		}

		if(strlen($number) >= 40){
			$this->json(550, 'number too long');
		}
		$price = round($price, 2);
		$discount_price = round($discount_price, 2);
		$this->load->library('User_service', array(), 'user');

		if(!($imageinfo = $this->user->decode($imageauth))){
			$this->json(550, 'imageinfo not exists');
		}
		
		if(!($userinfo = $this->user->getUserInfo())){
			$this->json(560, '该用户不存在');
		}

		if($this->user->level < $level){
			$this->json('565', '用户没有高级权益');
		}

		if($this->shop->level < $level){
			$this->json('567', '今日无高级权益');
		}

		$this->load->model('shoporder_model', 'shoporder');
		
		if($this->shoporder->isExistsByCreatetime($this->shop->id, $this->user->uid)){
			$this->json('568', '今日该用户已经使用过');
		}



		
		$this->shoporder->insert(array(
			'level' => $level,
			'price' => $price,
			'discount_price' => $discount_price,
			'uid' => $this->user->uid,
			'username' => $this->user->username,
			'sid' => $this->shop->id,
			'number' => $number
		));

		$this->user_model->increase('buys', $this->user->uid);
		$this->shop_model->increase('buy_num', $this->shop->id);
		if($level == 1){
			$this->shop_model->increase('level1_buy_num', $this->shop->id);
		}else{
			$this->shop_model->increase('level2_buy_num', $this->shop->id);
		}
		$this->user_model->increase('save_money', $this->user->uid, $price - $discount_price);
		$this->json(0, 'success');


	}


	public function orderlist(){
		$date = $this->input->post('date');

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

		$start = ($page - 1) * $pagesize;
		$this->load->model('shoporder_model', 'shoporder');
		$data = $this->shoporder->getRecordBySid($this->shop->id, $start, $pagesize, $date);
		$this->json(0, 'success', $data);
	}


	public function password(){
		$old_password = $this->input->post('old_password');
		$new_password = $this->input->post('new_password');

		if(!$this->shop_model->login($this->shop->id, $old_password)){
			$this->json(563, '原密码错误');
		}

		$this->shop_model->updatePassword($new_password, $this->shop->id);
		$data['sauth'] = $this->shop->make_sauth($this->shop->id, $new_password);
		$this->json(0, '成功', $data);
	}
}
