<?php
/**
 * 购物车
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */

namespace Uapp\Controller;
use Think\Controller;
class CartController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
        $this->checkuserauth();
    }
    public function index(){
        $param = I('param.');
        $user_info=$this->checkuserauth();
        // $param['uid']=$user_info['id'];
        // $param['HCshopid']=$user_info['HCshop']['bind_shop_id'];
        $result = D('Wxsmall/Cart')->index($param);
        $result['HCshop'] = $user_info['HCshop'];//当前会员匹配的门店存储到客户端
        $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']=$param['uid'];
        $param['HCshopid']=$user_info['HCshop']['bind_shop_id'];//追加当前会员匹配的门店
        $param['is_wxsmall']=1;
        $result=D("Common/Takeout")->add_cart_item($param);
        $result['msg']=$result['info'];
        if($result['status']==0)$result['status']=3;
        $this->ajaxReturn($result);
    }
    /**
     * 删除购物车中商品
     */
    public function del_cart(){
        $id=I('get.id');
        if($id=='del_out_all'){
            // $user_info=$this->checkuserauth();
            $uid=I('uid');
            $data=D('Common/Cart')->clear_out_cart($uid);
        }else{
            $data=D('Common/Cart')->del_cart($id);
        }
        $this->ajaxReturn($data);
    }
    /**
     * 提交购物车
     */
    public function sub_cart(){
        $param=I('post.');
        // $param_data=array();
        $param['cart_ids']=json_decode(htmlspecialchars_decode($param['cart_ids']),true);
        $user_info=$this->checkuserauth();
        // $param_data['uid']=$user_info['id'];
        if(!param['HCshopid']){
            $param['HCshopid']=$user_info['HCshop']['bind_shop_id'];
        }
        $result = D('Wxsmall/Cart')->sub_cart($param);
        $this->ajaxReturn($result);
    }
    /**
     * 修改新增满赠，加价购活动替换商品
     */
    public function add_pro_goods(){
        $param=I('param.');
        $param['user_id'] = $param['uid'];
        $res=D('Common/Cart')->add_pro_goods($param);
        $this->ajaxReturn($res);
    }
    /**
     * 满赠，加价购活动替换商品
     */
    public function change_pro_goods(){
        $param = I('param.');
        $result = D('Wxsmall/Cart')->change_pro_goods($param);
        $this->ajaxReturn($result);
    }
}
?>