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

namespace Mobile\Controller;
use Think\Controller;
class CartController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
		
        $siteinfo=session('siteinfo');
        $page_title=$siteinfo['temp_name']?$siteinfo['temp_name']:'请选择';
		
        $this->assign('tplseo_title','购物车_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title',$page_title);
    }
    public function index(){
        $type=I('param.type');
        if(!$type){
            $type=3;
        }
        $shop_id = get_wxsmall_shop_id();
        $data=D('Common/Cart')->cart_index($type);
        $shop_unable=array();
        if($data['data'] && $data['data']['list']){
            foreach($data['data']['list'] as $key=>$val){
                if($key!=$shop_id){
                    unset($data['data']['list'][$key]);
                    $shop_unable['data']['list'][$key]=$val;
                    //$data['data']['list'][$key]=$val;
                }
            }
        }//print_r($data);
        $this->assign('cart_list',$data);
        $this->assign('shop_unable',$shop_unable);
        $this->assign('shop_id',$shop_id);
//根据当前位置或配送地址 判断配送方式 并获取 包邮金额 开始
        $sexy=session('siteinfo');
        $bao_price =0;
        if($sexy){
            $send=D('Wxsmall/Login')->judge_shop($sexy['temp_lng'],$sexy['temp_lat'],$shop_id);
            if($send['status']!=3){
                $map=array();
                $map['shop_id']=$shop_id;

                if($send['send']==1){
                    $bao_price = M('ShopsDelivery')->where($map)->getField('start_price');
                }elseif($send['send']==2){
                    $order = 'id desc';
                    $shipping_info = M('ShippingFee')->where($map)->order($order)->field(true)->select();
                    $address[0]=$sexy['district'];
                    $address[1]=$sexy['city_id'];
                    $address[2]=$sexy['province'];
                    if(!empty($shipping_info)) {
                        foreach ($address as $val) {
                            foreach ($shipping_info as $k => $v) {
                                if (empty($v)) continue;
                                if (in_array($val, explode(',', $v['area_region']))) {
                                    $shipping_info['configure'] = unserialize($v['configure']);
                                }
                            }
                        }
                    }
                    $bao_price=$shipping_info['configure']['money'];
                }
            }
        }
        $this->assign('bao_price',$bao_price);
        $this->assign('eid',MID);
//        根据当前位置或配送地址 判断配送方式 并获取 包邮金额 结束
        $this->assign('tplseo_title','购物车_'.C('WEB_SITE_TITLE'));
        if($data['status']==0 || $data['data']['status']==0) $this->display('empty_cart');
        else $this->display();
    }
    public function change_act(){
        $cart_id=I('post.cart_id');
        $pro_id=I('post.act_id');
        $data=D('Common/Cart')->change_act($cart_id,$pro_id);
        $this->ajaxReturn($data);
    }
    public function del_cart(){
        $id=I('post.id');
        $data=D('Common/Cart')->del_cart($id);
        $this->ajaxReturn($data);
    }

    /**
     * 清空商品
     */
    public function clear_cart(){
        $data=I('post.');
        $member=checkuserauthtype();
        $uid=$member['id'];
        $data=D('Common/Cart')->clear_out_cart($uid);
        $this->ajaxReturn($data);
    }
    public function sub_cart(){
        session('format_url',U('Mobile/Cart/index'));
        if(empty($this->m_info['mobile'])){
            $data['status'] = 0;
            $data['msg'] = '请完善信息';
            $data['url'] = U('Mobile/Login/perfect');
            $this->ajaxReturn($data);
        }
        // if(!cookie('lshop_id') && getconfig('REGISTERBUY_OPEN')==0){
        //     $data['status'] = 0;
        //     $data['msg'] = '请先晋升等级在购买';
        //     $data['url'] = U('Mobile/pro_level/index');
        //     $this->ajaxReturn($data);
        // }
        $data=I('post.');
        $id=$data['id'];
        $number=array_combine($data['id'],$data['number']);
        $return=D('Common/Cart')->sub_cart($id,$number);
        $this->ajaxReturn($return);
    }
    public function change_pro_goods(){
        $return['status']=1;
        $data=I('post.');
        $res=D('Common/Cart')->change_pro_goods($data);
        if($res['status']==1){
            $this->assign('goods_info',$res['data']['goods_info']);
            $this->assign('gift_id',$res['data']['gift_id']);
            $this->assign('cart_ids',$data['cart_ids']);
            $this->assign('pro_id',$data['pro_id']);
            $return['hg_goods']=$this->fetch('hg_goods');
        }
        $this->ajaxReturn($return);
    }
    public function add_pro_goods(){
        $param=I('post.');
        $memberinfo = checkuserauthtype();
        if($memberinfo) {
            $param['user_id'] = $memberinfo['id'];
        }else{
            $param['user_id']=cookie('u_cookieid');
        }
        $res=D('Common/Cart')->add_pro_goods($param);
        $this->ajaxReturn($res);
    }

    /**
     * 购物车修改数量
     */
    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'];
        }
        $result=D("Common/Takeout")->add_cart_item($param);
        $result['eid']=MID;
        $this->ajaxReturn($result);
    }
    public function is_gift(){
        $param=I("post.");
        $map=array();
        $map['id']=$param['cart_ids'];
        $gift=M("Cart")->where($map)->getField('actgoodsid');
        $result=array('status'=>1);
        if($gift){
            $result['gift']=$gift;
        }else{
            $result['status']=0;
        }
        $this->ajaxReturn($result);
    }
}
?>