<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
/**
 * 地址模型控制器
 */
class Address extends Home {

	 /* 取消订单 */
	public function index(){
		
		 if ( !is_login() ) {
					$this->error( "您还没有登陆",url("User/login") );
		   }		
		$uid=session_uid();	
		$map['uid']=$uid;//用户id
		
		 $list=db("address")->where($map)->order("id desc")->limit(10)->select();
		 
		$this->assign('list', $list);
		$list=db("address")->where($map)->order("id desc")->select();
		$num=10;
		if($list){
			$num=count($list);
			if($num<10){
			   $num=10-$num;	
			}else{
			  $num=10-$num;	
			}								
		}
		$this->assign('num', $num);
		$this->meta_title = '地址管理';
		$this->assign('meta_title',$this->meta_title);
	
		return $this->fetch();
	}


   public  function edit($id = null, $pid = 0){
           if ( !is_login() ) {
					$this->error( "您还没有登陆",url("User/login") );
		   }
		   
		   
           $uid=is_login();	
		   $map['uid']=$uid;//用户id
		   $id=safe_replace(input('id')); 	
           if(!($id && is_numeric($id))){
		       $this->error('ID错误！');
		   }else{
		      $map["id"]=$id;
		   }	
		   $address=db("address")->where($map)->find();
		   if(!$address){
			   $this->error( "无对应的地址" );
		   }
		   if($_POST){
			   $id=safe_replace(input('id'));
		     $data=array();
             foreach($_POST as $key=>$val){
		           $data[$key]=safe_replace($val);
		    }
			$data["uid"]=is_login();
			$Address = new \app\index\model\Address;
// 过滤post数组中的非数据表字段数据
            $Address->allowField(true)->save($data,['id' => $id]);	  		 			 
	        $address=db("address")->find($id);
               
				$result["id"]=$id;
				   $result["html"]='<div class="tit">
									<h3 class="username">'.$address['username'].'</h3>
									<span class="alias"></span>
								</div>
								<div class="mobile">
									<p>'.$address['mobile'].'</p>
								</div>
								<div class="address">'.$address['username'].'&nbsp;&nbsp;&nbsp;&nbsp'.$address['province'].'&nbsp;&nbsp;'.$address['city'].'&nbsp;&nbsp;'.$address['area'].'&nbsp;&nbsp;'.$address['address'].' </div>
								<i class="icon"></i>
								<a href="javascript:void(0);" onclick="updateAddress('.$address['id'].')"class="edit" >修改</a>
								<a href="javascript:void(0);" onclick="delAddress('.$address['id'].')"  class="delete" >删除</a>';	
							
		   $result["code"]=1;
	       return $result;
			   
		   }
		   
		   else{
				   $this->assign('address', $address);
				   unset($map["id"]);
				   $res=getLists("address",$map,5,"id desc","");
				   $this->assign('res', $res);
				   $this->meta_title = '编辑地址';
				   $this->assign('meta_title',$this->meta_title);
				   $area=new \app\index\model\Area;
				   $list=$area->getArea();
				   $this->assign( 'list',$list);
				   return $this->fetch("address/index");
		   
		   }
         
   }
   
   public  function save($id = null){
           if ( !is_login() ) {
					$this->error( "您还没有登陆",url("User/login") );
		   }
		   
		   
           $uid=is_login();	
		   $map['uid']=$uid;//用户id
		   $id=safe_replace(input('id')); 	
           if(!($id && is_numeric($id))){
		       $this->error('ID错误！');
		   }else{
		      $map["id"]=$id;
		   }	
		   $address=db("address")->where($map)->find();
		   if(!$address){
			   $this->error( "无对应的地址" );
		   }
		   if($_POST){
			   $id=safe_replace(input('id'));
		     $data=array();
             foreach($_POST as $key=>$val){
		           $data[$key]=safe_replace($val);
		    }
			$data["uid"]=is_login();
			$Address = new \app\index\model\Address;
// 过滤post数组中的非数据表字段数据
            $Address->allowField(true)->save($data,['id' => $id]);	  		 			 
	        $address=db("address")->find($id);
               
				$result["id"]=$id;
				  
					$result["html"]='<div class="info f14"><span style="color:#999999;">收&ensp;货&ensp;人 :</span><span> '.$address['username'].'</span></div>
						<div class="info f14"><span style="color:#999999;">联系方式 :</span><span>'.$address['mobile'].'</span></div>
						<div class="info f14"><span style="color:#999999;">收货地址 :</span><span> '.$_POST['province'].'&nbsp;&nbsp;'.$_POST['city'].'&nbsp;&nbsp;'.$_POST['area'].'&nbsp;&nbsp;'.$address['address'].'</span></div>
						';	
			$result["id"]=$address['id'];				
		   $result["code"]=1;
	       return $result;
			   
		   }
		   
		   else{
				   $this->assign('address', $address);
				   unset($map["id"]);
				   $res=getLists("address",$map,5,"id desc","");
				   $this->assign('res', $res);
				   $this->meta_title = '编辑地址';
				   $this->assign('meta_title',$this->meta_title);
				   $area=new \app\index\model\Area;
				   $list=$area->getArea();
				   $this->assign( 'list',$list);
				   return $this->fetch("address/index");
		   
		   }
         
   }
    public  function qiehuan($pid = 0){
           if ( !is_login() ) {
			  $this->error( "您还没有登陆",url("User/login") );
		   } 
		   $token=input("token");
		    if(!$token){
			   $this->error( "token不存在");
		   }
		   if($token!=session("token")){
			   $this->error( "token错误",url("User/login") );
		   }
		   $uid=is_login();	
	       $map['uid']=$uid;//用户id
		   
		   $list=db("address")->where($map)->order("id desc")->select();
		   $html="";
		   foreach($list as $k=>$v){
			 if($k==0){
			  $html.=' <li onclick="setSelect(this)" class="current" data-id="'.$v['id'].'">
						
						<div class="info f14"><span style="color:#999999;">收&ensp;货&ensp;人 :</span><span> '.$v['username'].'</span></div>
						<div class="info f14"><span style="color:#999999;">联系方式 :</span><span>'.$v['mobile'].'</span></div>
						<div class="info f14"><span style="color:#999999;">收货地址 :</span><span> '.$v['province'].'&nbsp;&nbsp;'.$v['city'].'&nbsp;&nbsp;'.$v['area'].'&nbsp;&nbsp;'.$v['address'].'</span></div>
						
					    <div class="tj" style="display:none;">默认地址</span></div>
			 </li>';
			 }else{
				 
				$html.=' <li onclick="setSelect(this)" class="" data-id="'.$v['id'].'">
						
						<div class="info f14"><span style="color:#999999;">收&ensp;货&ensp;人 :</span><span> '.$v['username'].'</span></div>
						<div class="info f14"><span style="color:#999999;">联系方式 :</span><span>'.$v['mobile'].'</span></div>
						<div class="info f14"><span style="color:#999999;">收货地址 :</span><span> '.$v['province'].'&nbsp;&nbsp;'.$v['city'].'&nbsp;&nbsp;'.$v['area'].'&nbsp;&nbsp;'.$v['address'].'</span></div>
						
					    <div class="tj" style="display:none;">默认地址</span></div>
			 </li>'; 
			 }
		   }
		    
		    $result["html"]=$html;
		    $result["code"]=1;
				 return $result;
         
   }
   
    public  function change($pid = 0){

		   $pid=input('pid'); 	
           if(!($pid && is_numeric($pid))){
		       $this->error('数据类型错误！');
		   }
		   $map["pid"]=$pid;
		   $list=db("area")->where($map)->select();
		   return $list;
         
   }
  public  function del() {
		
		if ( !is_login() ) {
			$this->error( "您还没有登陆",url("User/login") );
		} 
		$id=intval(input("id"));
		if(!($id && is_numeric($id))){
		       $this->error('用户ID错误！');
		 }
		$uid=is_login(); 
		if($id){
		   $map["uid"]=$uid;	
		   $map["id"]=$id;	
		   $info= db("address")->where($map)->find(); 
		   if(!$info){
			   $this->error( "地址不存在" );
		   }
		   $res=db("address")->where($map)->delete();
		   if($res){
				
				$url=url("address/index");
				$this->success("删除成功",$url);
			}
			else{ 
				
				$this->error("删除失败");
			}
        }
   }
   public  function set() {
		
		if ( !is_login() ) {
			$this->error( "您还没有登陆",url("User/login") );
		} 
		$id=input("id");
		if(!($id && is_numeric($id))){
		       $this->error('用户ID错误！');
		 }
		$uid=is_login(); 
		if($id){
		   $map["uid"]=$uid;	
		   $map["id"]=$id;	
		   $info= db("address")->where($map)->find(); 
		   if(!$info){
			   $this->error( "地址不存在" );
		   }
		   db("address")->where(array("uid"=>$uid))->setField("status",0);
		   $res=db("address")->where($map)->setField("status",1);
		   if($res){
				$url=url("address/index");
				$this->success("设置成功",$url);
			}
			else{ 
				
				$this->error("设置失败");
			}
        }
   }
    public  function add() {
		
		if ( !is_login() ) {
			$this->error( "您还没有登陆",url("User/login") );
		} 
	
		$status=input("status");
		$uid=is_login();	
	    $map['uid']=$uid;//用户id
        if($_POST){ //提交表单			
		 $list=db("address")->where($map)->order("id desc")->select();	 
		 if($list){
			$num=count($list);
			 if($num>=10){
				$this->error("最多可保存10条地址");
			 }
		  }
		     $Address = model("Address");
			 $data=array();
             foreach($_POST as $key=>$val){
		           $data[$key]=safe_replace($val);
		    }
			$data["uid"]=is_login();
		     $id=Db::name("address")->insertGetId($data);
             if($id){
				
			    $address=db("address")->find($id);
               
				$result["id"]=$id;
				   $result["html1"]='<h4 class="f16">收货人信息<a onclick="updateAddress()" class="xg f12">[修改]</a></h4>
						<p class="info f12">'.$address['username'].'&nbsp;&nbsp;&nbsp;&nbsp'.$address['province'].'&nbsp;&nbsp;'.$address['city'].'&nbsp;&nbsp;'.$address['area'].'&nbsp;&nbsp;'.$address['address'].'<span class="icon"></span>
						<span class="num">'.$address['mobile'].'</span></p>';
					$result["html"]='<div class="info f14"><span style="color:#999999;">收&ensp;货&ensp;人 :</span><span> '.$address['username'].'</span></div>
						<div class="info f14"><span style="color:#999999;">联系方式 :</span><span>'.$address['mobile'].'</span></div>
						<div class="info f14"><span style="color:#999999;">收货地址 :</span><span> '.$address['province'].'&nbsp;&nbsp;'.$address['city'].'&nbsp;&nbsp;'.$address['area'].'&nbsp;&nbsp;'.$address['address'].'</span></div>
						<a onclick="updateAddress('.$address['id'].')" class="xg f14">修改地址</a>
						<div class="line"></div>
						<div class="qiehuan" onclick="qiehuan()"><span class="fa fa-map-marker"></span><span>切换地址</span></div>
					    <div class="tj" onclick="showmodal()"><span class="fa fa-plus-circle"></span><span >添加地址</span></div>';	
						
				 $result["code"]=1; $result["msg"]="新增成功";
				 $result["url"]=url('address/index');
				 return $result;
				
            } else {
                $error = $Address->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } 
	}
  
	
   public  function choose() {
		
		if ( !is_login() ) {
			$this->error( "您还没有登陆",url("User/login") );
		} 
	
		$id=safe_replace(input('id'));
		 if ( !$id ) {
			$this->error( "id错误" );
		}
	    $address=db("address")->find($id);
               
				$result["id"]=$id;
				   $result["html"]='<h4 class="f16">收货人信息<a onclick="updateAddress()" class="xg f12">[修改]</a></h4>
						<p class="info f12">'.$address['username'].'&nbsp;&nbsp;&nbsp;&nbsp'.$address['province'].'&nbsp;&nbsp;'.$address['city'].'&nbsp;&nbsp;'.$address['area'].'&nbsp;&nbsp;'.$address['address'].'<span class="icon"></span>
						<span class="num">'.$address['mobile'].'</span></p>';
					$result["html"]='<div class="info f14"><span style="color:#999999;">收&ensp;货&ensp;人 :</span><span> '.$address['username'].'</span></div>
						<div class="info f14"><span style="color:#999999;">联系方式 :</span><span>'.$address['mobile'].'</span></div>
						<div class="info f14"><span style="color:#999999;">收货地址 :</span><span> '.$address['province'].'&nbsp;&nbsp;'.$address['city'].'&nbsp;&nbsp;'.$address['area'].'&nbsp;&nbsp;'.$address['address'].'</span></div>
						<a onclick="updateAddress('.$address['id'].')" class="xg f14">修改地址</a>
						<div class="line"></div>
						<div class="qiehuan"  onclick="qiehuan()"><span class="fa fa-map-marker"></span><span>切换地址</span></div>
					    <div class="tj" onclick="showmodal()"><span class="fa fa-plus-circle"></span><span>添加地址</span></div>';	
						
		$result["code"]=1;
	    return $result;
		
	}
	  public  function insert() {
		
		if ( !is_login() ) {
			$this->error( "您还没有登陆",url("User/login") );
		} 
	
		$status=input("status");
		$uid=is_login();	
	    $map['uid']=$uid;//用户id
        if($_POST){ //提交表单			
		 $list=db("address")->where($map)->order("id desc")->select();	 
		 if($list){
			$num=count($list);
			 if($num>=10){
				$this->error("最多可保存10条地址");
			 }
		  }
		     $Address = model("Address");
			 $data=array();
             foreach($_POST as $key=>$val){
		           $data[$key]=safe_replace($val);
		    }
			$data["uid"]=is_login();
		     $id=Db::name("address")->insertGetId($data);
             if($id){
				
			    $address=db("address")->find($id);
               
				$result["id"]=$id;
				   
						
							$result["html"]='<div class="item"  onclick="choose(this)" onmouseover="hover(this)"  onmouseout="moveout(this)" id="item'.$address['id'].'" data-id="'.$address['id'].'">
								<div class="tit">
									<h3 class="username">'.$address['username'].'</h3>
									<span class="alias"></span>
								</div>
								<div class="mobile">
									<p>'.$address['mobile'].'</p>
								</div>
								<div class="address">'.$address['username'].'&nbsp;&nbsp;&nbsp;&nbsp'.$address['province'].'&nbsp;&nbsp;'.$address['city'].'&nbsp;&nbsp;'.$address['area'].'&nbsp;&nbsp;'.$address['address'].' </div>
								<i class="icon"></i>
								<a href="javascript:void(0);" onclick="updateAddress('.$address['id'].')"class="edit" >修改</a>
								<a href="javascript:void(0);" onclick="delAddress('.$address['id'].')"  class="delete" >删除</a>
           
                             </div>';
						
						
				 $result["code"]=1; 
				 $result["msg"]="新增成功";
				 $result["url"]=url('address/index');
				 return $result;
				
            } else {
                $error = $Address->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } 
	}
	
	 public  function modify() {
		
		if ( !is_login() ) {
			$this->error( "您还没有登陆",U("User/login") );
		} 
	
		$id=safe_replace(input('id'));
		 if ( !$id ) {
			$this->error( "id错误" );
		}
	    $address=db("address")->find($id);
        $url=url('address/edit');$img=INSTALL_PATH.'public/index/images';    
				   $result["id"]=$id;
				   $result["html"]='<div class="modal_content"><h1>
<span class="header">修改收货人地址</span> <a class="img" onclick="closemodal()" >✕</a></h1>
							<form class="search-form" id="adr_form" 
								action="'.$url.'">
						
							<div class="ipts f14">
								<div class="div_01">
									<div class="div_l fl"><span>*</span>收货人姓名：</div>
									
									<div class="div_r fl"><input type="text" name="username" class="ipt_r fl" value="'.$address['username'].'"/></div>
								</div>
								<div class="div_02">
									<div class="div_l fl"><span>*</span>所&nbsp;在&nbsp;地&nbsp;区：</div>
									<div class="select"id="add" >
						<select id="province" name="province" > 
<option value="-1">选择省份</option>
 </select> 
   <select id="city" name="city"> <option value="-1">选择城市</option> </select>
 <select id="area" name="area"> <option value="-1">选择地区</option> </select>


	
	<script>
	$("#add").distpicker({
  province: "'.$address['province'].'",
  city: "'.$address['city'].'",
  district: "'.$address['area'].'"
});
				</script>
							</div>	</div>
								<div class="div_03">
									<div class="div_l fl"><span>*</span>详&nbsp;细&nbsp;地&nbsp;址：</div>
									<div class="div_r fl">
										<input type="text" name="address" class="ipt_r" value="'.$address['address'].'"/>
									
									</div>
								</div>
								<div class="div_04">
									<div class="div_l fl"><span>*</span>手&nbsp;机&nbsp;号&nbsp;码：</div>
									<div class="div_r fl">
										<input type="text" id="ipt_01" name="mobile" class="fl" value="'.$address['mobile'].'"/>
										
									</div>
									<input type="hidden"  name="id" value="'.$address['id'].'" id="ipt_02" class="fl"/>
								</div>
								<div class="btn-group">
								<a onclick="save()" class="save">保存</a>
								<a onclick="closemodal()" class="cancel">取消</a></div>
							</div>
						</form></div>';	
				
		$result["code"]=1;
	    return $result;
		
	}
	
    public  function update() {
		
		if ( !is_login() ) {
			$this->error( "您还没有登陆",U("User/login") );
		} 
	
		$status=input("status");
		$uid=is_login();	
	    $map['uid']=$uid;//用户id
		$id=input('id'); 	
        if(!($id && is_numeric($id))){
		       $this->error('分类ID错误！');
		}
	    $map["id"]=$id;
			
		$address=db("address")->where($map)->find();
		if(!$address){
			   $this->error( "无对应的地址" );
		}
        if($_POST){ //提交表单
		     $Address = model("Address");
			  $data=array();
            foreach($_POST as $key=>$val){
		         $data[$key]=safe_replace($val);
		    }
		     $res=$Address->validate(true)->allowField(true)->save($data,$map);
             if(false!==$res){
				$id=$id?$id:$res;
			    $address=db("address")->find($id);
				if($status){
					db("address")->where(array("uid"=>$uid))->setField("status",0);
					db("address")->where($map)->setField("status",1);
				}
				$result["code"]=1;
				$result["id"]=$id;$result["msg"]="操作成功";
				$result["html"]='<h4 class="f16">收货人信息<a onclick="updateAddress()" class="xg f12">[修改]</a></h4>
						<p class="info f12">'.$address['username'].'&nbsp;&nbsp;&nbsp;&nbsp'.$address['province'].'&nbsp;&nbsp;'.$address['city'].'&nbsp;&nbsp;'.$address['area'].'&nbsp;&nbsp;'.$address['address'].'<span class="icon"></span>
						<span class="num">'.$address['mobile'].'</span></p>';
				return $result;
				
            } else {
                $error = $Address->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } 
	}

   
}
