<?php
/**
 * 收货地址管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author lss <lss@qq.com>
 */
namespace Mobile\Controller;
use Think\Controller;
class AddressController extends BaseController
{
    public function _initialize()
    {
        parent::_initialize();
        if (!MID) {
            $this->redirect('Mobile/Login/index');
        }
		if($this->m_info['is_tmp']==1&&!$this->m_info['mobile']){
			session('format_url',U('Mobile/Address/address_list'));
			$this->redirect('Mobile/Login/perfect');
		}elseif($this->m_info['is_tmp']==1&&$this->m_info['mobile']){
			$this->error('您已经领取过会员卡了！');exit;
		}
    }

    /**
     *收货地址列表
     */
    public function address_list()
    {
		
        $address_list = D('Common/Member')->get_address_list();
        $shop_id=get_wxsmall_shop_id();
        $jump=session('jump');//跳转方向（2订单）
        //echo $jump;exit;
        if($shop_id && $jump==1){
            foreach($address_list['list'] as $k=>$v){
                if($v['xpoint']>0 && $v['ypoint']>0){
                    $distribution_area=D('Wxsmall/Login')->judge_shop($v['xpoint'],$v['ypoint'],$shop_id);
                    if($distribution_area['status']==3){
                        $address_list['list'][$k]['usable']=0;
                    }else{
                        $address_list['list'][$k]['usable']=1;
                    }
                }
            }
        }
        $ids=I("ids");
        $order_parm=I("get.");
        if($ids){
            $this->assign('ids',$ids);
        }
        //echo "<pre>";print_r($order_parm);exit;
        if($order_parm){
            $this->assign('order_parm',json_encode($order_parm));
        }
        $this->assign($address_list);
        $this->assign('jump',$jump);
        $this->assign('tplseo_title', '我的收货地址' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '我的收货地址');
        $this->display();
    }

    /**
     *添加收货地址
     */
    public function add_address()
    {
        $id = I('get.id');
        $order_ids = I('order_ids');
        if($order_ids){
            $this->assign('order_ids',$order_ids);
        }
        if ($id > 0) {
            $address = D('Common/Member')->address_cont($id);
            $this->assign($address);
        }
        if ($id > 0) {
            $this->assign('tplseo_title', '编辑收货地址' . '_' . C('WEB_SITE_TITLE'));
            $this->assign('page_title', '编辑收货地址');
        } else {
            $this->assign('tplseo_title', '新增收货地址' . '_' . C('WEB_SITE_TITLE'));
            $this->assign('page_title', '新增收货地址');
        }
        $this->display();
    }

    /**
     *添加或修改地址
     */
    public function update_address()
    {
        $postdata = I('post.');
        $result = D('Common/Member')->modify_address($postdata);
        $jump = session('jump');
        $result['jump'] = $jump;
        switch ($jump)
        {
              case 1:
                $result['url'] = U('Mobile/Takeout/order',array('address_id'=>$result['address_id']));
                break;
              case 2:
                $result['url'] = U('Mobile/Order/index',array('address_id'=>$result['address_id']));
                break;
              default:
                $result['url'] = U('Mobile/Address/address_list');
        }
        $this->ajaxReturn($result);
    }

    /**
     *删除收货地址
     */
    public function del_address($id)
    {
        $data = D('Common/Member')->delete_address($id);
        $this->ajaxReturn($data);
    }
    /**
     *判断收货地址是否在配货区域
     */
    public function distance()
    {
        $id=I('post.address_id');
        $shop_id=get_wxsmall_shop_id();
        if($id){
            $map = array();
            $map['id']=$id;
            $memberaddress_info = M('MemberAddress')->where($map)->field(true)->find();
            if($memberaddress_info){
                $member_info['province'] = $memberaddress_info['province'];
                $member_info['city_id'] = $memberaddress_info['city_id'];
                $member_info['district'] = $memberaddress_info['district'];
                $member_info['temp_lng'] = $memberaddress_info['xpoint'];
                $member_info['temp_lat'] = $memberaddress_info['ypoint'];
                $member_info['temp_name'] = $memberaddress_info['address'];
                $member_info['city'] = idtoname($memberaddress_info['city'],'Area','name');
                session('siteinfo',$member_info);
                cookie('siteinfo',$member_info);
                session('loc',1);
                $upshop=true;
                $IS_LOCK_USER= C('IS_LOCK_USER');
                if($shop_id && empty($IS_LOCK_USER)){//检测当前店铺是否满足当前情况
                    $distribution_area=D('Wxsmall/Login')->judge_shop($member_info['temp_lng'],$member_info['temp_lat'],$shop_id);
                    if($distribution_area['status']!=3){$upshop=false;}
                }
                if($upshop && empty($IS_LOCK_USER)){
                    save_session();
                    $shop_id=get_wxsmall_shop_id();
                }
            }
        }
        
        if($id && $shop_id){
            $distribution_area=D('Wxsmall/Login')->judge_shop($memberaddress_info['xpoint'],$memberaddress_info['ypoint'],$shop_id);

            if($distribution_area['status']==3){
                $data['status']=0;
            }else{
                $data['status']=1;
            }
        }else{
            $data['status']=0;
        }
        $this->ajaxReturn($data);
    }
}