<?php
/** App登录控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <xiaoq@570035208.com>
 */

namespace Wxsmall\Controller;
class CartController extends BaseController {
	public function _initialize(){
        	parent::_initialize ();
    	}
	/**
	 * 获取附近门店列表页
	 */
	public function index(){
		$param = I('param.');
		$user_info=$this->checkuserauth();
		$param['uid']=$user_info['id'];
		$result = D('Cart')->index($param);
		$result['HCshop'] = $user_info['HCshop'];//当前会员匹配的门店存储到客户端
		$this->ajaxReturn($result);
	}
	/**
	 * 删除购物车中商品
	 */
	public function del_cart(){
		$id=I('post.id');
		if($id=='del_out_all'){
			$user_info=$this->checkuserauth();
			$data=D('Common/Cart')->clear_out_cart($user_info['id']);
		}else{
			$data=D('Common/Cart')->del_cart($id);
		}
        $this->ajaxReturn($data);
	}
	/**
	 * 满赠，加价购活动替换商品
	 */
	public function change_pro_goods(){
		$param = I('param.');
		$user_info=$this->checkuserauth();
		$param['uid']=$user_info['id'];
		$result = D('Cart')->change_pro_goods($param);
		$this->ajaxReturn($result);
	}
	/**
	 * 修改新增满赠，加价购活动替换商品
	 */
	public function add_pro_goods(){
		$param = I('param.');
		$user_info=$this->checkuserauth();
		$param['user_id']=$user_info['id'];
		$result = D('Common/Cart')->add_pro_goods($param);
		$this->ajaxReturn($result);
	}

    /**
     * 提交购物车
     */
    public function sub_cart(){
        $param=I('post.');
        $param_data=array();
        $param_data['cart_ids']=json_decode(htmlspecialchars_decode($param['cart_ids']),true);
        $user_info=$this->checkuserauth();
        $param_data['uid']=$user_info['id'];
        $result = D('Wxsmall/Cart')->sub_cart($param_data);
        $this->ajaxReturn($result);
    }

    /**
     * 数量加减
     */
    public function add_cart(){
        $param=I("post.");
        $map=array();
        $map['id']=$param['cart_id'];
        $spc=M("Cart")->where($map)->getField('goods_attr');
        if($spc){
            $goods_attr=unserialize($spc);
            $param['spec']=$goods_attr['key'];
            $param['name']=$goods_attr['name'];
        }
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $param['shop_id']=$user_info['HCshop']['bind_shop_id'];//追加当前会员匹配的门店
        $param['is_wxsmall']=1;
        $result=D("Common/Takeout")->add_cart_item($param);
        $result['msg']=$result['info'];
        $this->ajaxReturn($result);
    }
	
}