<?php

namespace Shop\Src;

use Dever;
use Main\Lib\Core;

class Buy extends Core
{
	public function __construct()
	{
		parent::__construct();
		$this->checkLogin();
		$this->shop_id = Dever::input('shop_id');

		if (!$this->shop_id) {
			Dever::alert('请选择门店');
		}

		$lng = Dever::input('lng');
		$lat = Dever::input('lat');
		if ($lng && $lat) {
			$this->shop = Dever::load('shop/lib/info')->fetch($this->shop_id, false, $lng, $lat, false);
		} else {
			$this->shop = Dever::db('shop/info')->getOne($this->shop_id);
		}

		if (!$this->shop) {
			Dever::alert('门店不存在');
		}
	}

	# 分享
	public function share()
	{
		$act_id = Dever::input('act_id', 1);
		$path = Dever::input('path');
		$this->data = Dever::load('shop/lib/share')->up($this->uid, $this->shop_id, $act_id, $path);

		return $this->data;
	}

	# 获取店铺的优惠券
	public function getCoupon()
	{
		$act_id = Dever::input('act_id', 1);

		$this->data['coupon'] = Dever::load('shop/lib/coupon')->getAll($this->shop_id, $act_id);

		$this->data['shop'] = $this->shop;

		$this->data['act'] = Dever::db('act/info')->find($act_id);

		return $this->data;
	}

	# 领取优惠券 也得判断一下用户是否有权限领取
	public function takeCoupon_commit()
	{
		$act_id = Dever::input('act_id', 1);
		# 验证这个券是否可以领取
		$shop_coupon_id = Dever::input('shop_coupon_id');
		$coupon = explode(',', $shop_coupon_id);

		$this->data['take'] = array();

		$state = Dever::load('shop/lib/share')->getOne($this->uid, $this->shop['id'], 1);
        if (!$state) {
            Dever::alert('您还未完成分享');
            return false;
        }
		foreach ($coupon as $k => $v) {
			$this->data['take']['shop_coupon_id'] = $v;
			$this->data['take']['status'] = Dever::load('shop/lib/coupon')->getOne($this->uid, $this->shop, $v, $act_id);
		}

		return $this->data;
	}

	# 获取商品详细信息
	public function getGoodsInfo()
	{
		$id = Dever::input('goods_id');
		if (!$id) {
			Dever::alert('错误的商品');
		}

		$this->data['goods'] = Dever::load('shop/lib/info')->getGoodsInfo($this->shop_id, $id, false, true, -1);

		//$this->data['shop'] = $this->shop;

		return $this->data;
	}

	# 获取购物车的数据
	public function getCart()
	{
		$where['uid'] = $this->uid;
		$where['shop_id'] = $this->shop_id;

		$this->data['cart'] = Dever::db('shop/cart')->select($where);

		if ($this->data['cart']) {
			foreach ($this->data['cart'] as $k => $v) {
				$this->data['cart'][$k]['goods'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);

				$w = array();
	            $w['goods_id'] = $v['goods_id'];
	            $w['sku_id'] = $v['sku_id'];
	            $w['shop_id'] = $this->shop_id;
	            $goods_sku = Dever::db('shop/goods_sku')->find($w);
	            if ($goods_sku) {
	                if (isset($goods_sku['price_id']) && $goods_sku['price_id'] > 0) {
	                    $price_template = Dever::load('price/lib/data')->get($v['goods_id'], $v['sku_id'], $goods_sku['price_id']);
	                    if ($price_template) {
	                        if ($price_template['price_sell'] && $price_template['price_sell'] > 0) {
	                            $this->data['cart'][$k]['goods']['price'] = $price_template['price_sell'];
	                        }
	                    }
	                }
	            }

				if (isset($this->data['cart'][$k]['goods']['sku_id'])) {
					$this->data['cart'][$k]['price_id'] = $this->data['cart'][$k]['goods']['sku_id'];
				}
			}
		}

		return $this->data['cart'];
	}

	# 加入到购物车
	public function addCart()
	{
		$where['uid'] = $this->uid;
		$where['shop_id'] = $this->shop_id;
		$where['goods_id'] = Dever::input('goods_id');
		$where['sku_id'] = Dever::input('price_id');
		if (!$where['goods_id']) {
			Dever::alert('错误的商品');
		}

		$info = Dever::db('shop/cart')->find($where);

		$where['num'] = Dever::input('num');
		if (!$info) {
			# 验证库存
			Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
			$state = Dever::db('shop/cart')->insert($where);
		} else {
			$where['num'] += $info['num'];
			# 验证库存
			Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
			$where['where_id'] = $info['id'];
			$state = Dever::db('shop/cart')->update($where);
		}

		return $this->getCart();
	}

	# 清空购物车
	public function dropCart()
	{
		$where['uid'] = $this->uid;
		$where['shop_id'] = $this->shop_id;

		$state = Dever::db('shop/cart')->delete($where);

		return $this->alert($state);
	}

	# 更新购物车数量
	public function upCart()
	{
		$where['uid'] = $this->uid;
		$where['shop_id'] = $this->shop_id;
		$where['id'] = Dever::input('cart_id');

		$status = Dever::input('status', 1);
		$type = Dever::input('type', 1);
		$num = Dever::input('num');

		$info = Dever::db('shop/cart')->find($where);

		$state = false;

		if ($info) {
			if ($type == 1) {
				$num = $info['num'] + $num;
			} else {
				$num = $info['num'] - $num;
			}
			$where = array();
			$where['where_id'] = $info['id'];
			if ($num <= 0) {
				$state = Dever::db('shop/cart')->delete($info['id']);
			} else {
				$where['status'] = $status;
				$where['num'] = $num;
				
				# 验证库存
				$total = Dever::load('shop/lib/info')->checkTotal($num, $info['goods_id'], $this->shop_id, $info['sku_id']);

				if ($where['num'] > $total) {
					# 库存不足
					$where['num'] = $total;
					$state = Dever::db('shop/cart')->update($where); 
					return array('cart' => 2, 'data' => $total);
				}
				$state = Dever::db('shop/cart')->update($where); 
			}
		}

		return array('cart' => 1, 'data' => $this->getCart());
	}

	# 确认订单页面
	public function confirm()
	{
		$this->data['uid'] = $this->uid;
		$this->data['user'] = $this->user;
		$this->data['shop'] = $this->shop;
		Dever::load('shop/lib/sell')->goods($this->data);
		Dever::load('shop/lib/sell')->coupon($this->data);

		return $this->data;
	}

	# 开始下单
	public function pay_commit()
	{
		$refer = Dever::input('refer');
		$cart = Dever::input('cart', 1);
		$address_id = Dever::input('address_id');
		$invoice_id = Dever::input('invoice_id');
		
		$info = Dever::input('info');

		$this->data['uid'] = $this->uid;
		$this->data['user'] = $this->user;
		$this->data['shop'] = $this->shop;

		Dever::load('shop/lib/sell')->goods($this->data);
		Dever::load('shop/lib/sell')->coupon($this->data, 2);


		if ($this->data['pay_method'] > 1) {
			$cart = false;
		}

		if (!isset($this->data['card'])) {
			$this->data['card'] = false;
		}

		if (!isset($this->data['wallet_cash'])) {
			$this->data['wallet_cash'] = 0;
		}

		if (!$this->data['coupon_id'] && !$this->data['card'] && !$this->data['wallet_cash'] && $this->data['price'] <= 0) {
			Dever::alert('已售空');
		}

		if ($this->data['method'] == 2 && !$address_id) {
			Dever::alert('收货地址不能为空');
		}

		$pay = Dever::load('shop/lib/sell')->pay($this->data['method'], $this->data['pay_method'], 1, $this->user, $this->shop, $this->data['name'], $this->data['num'], $this->data['list'], $this->data['oprice'], $this->data['price'], 0, $address_id, $invoice_id, $info, $this->data['card'], $this->data['wallet_cash'], $this->data['coupon_id'], $this->data['user_coupon_id'], $this->data['coupon_cash'], $this->data['discount_id'], $this->data['discount_cash'], $cart, 5, $refer);
		return $pay;
	}

	# 再次付款
	public function r_pay()
	{
		$refer = Dever::input('refer');
		$order_id = Dever::input('order_id');

		$pay = Dever::load('shop/lib/sell')->rpay($order_id, 5, $refer);

		return $pay;
	}

	# 查询是否支付
	public function c_pay()
	{
		$refer = Dever::input('refer');
		$order_id = Dever::input('order_id');

		$pay = Dever::load('shop/lib/sell')->cpay($order_id, 5, $refer);

		return $pay;
	}
}