<?php


namespace app\admin\controller;
use app\BaseController;
use think\facade\Db;use think\facade\Validate;


class Addr extends BaseController
{
    public function province(){
        $list=Db::name("province")->select();
        return app_show(0,"获取成功",$list);
    }
    public function area(){
        $post=$this->request->post();
        $city_code = isset($post['code'])&& $post['code']!="" ? $post['code']:"";
        if($city_code==""){
            return error_show(1002,"市级未选择");
        }
        $city =Db::name("city")->where(['city_code'=>$city_code])->find();
        if(empty($city)){
            return error_show(1003,"参数code非法数据");
        }
        $list=Db::name("area")->where(['city_code'=>$city_code])->select();
        return app_show(0,"获取成功",$list);
    }
    public function city(){
        $post=$this->request->post();
        $province_code = isset($post['code'])&& $post['code']!="" ? $post['code']:"";
        if($province_code==""){
            return error_show(1002,"省级未选择");
        }
        $provice = Db::name("province")->where(['province_code'=>$province_code])->find();
        if(empty($provice)){
            return error_show(1003,"参数code非法数据");
        }
        $list=Db::name("city")->where(['province_code'=>$province_code])->select();
        return app_show(0,"获取成功",$list);
    }

    public function all(){
        $post=$this->request->post();
        $city_code = isset($post['code'])&& $post['code']!="" ? $post['code']:"";
        if($city_code==""){
            return error_show(1002,"区级Code未选择");
        }
        $data=[];
        $area =Db::name("area")->where(['area_code'=>$city_code])->field("name,area_code as code,city_code")->find();
        if($area==false){
            return error_show(1003,"未找到区级数据");
        }
        $data["area"]=$area;
        $city=Db::name("city")->where(['city_code'=>$area['city_code']])->field("name,city_code as code,province_code")->find();
        if($city==false){
            $data["city"]=[];
        }
        $data['city']=$city??[];
        $province=Db::name("province")->where(['province_code'=>$city['province_code']])->field("name,province_code as code")->find();
        $data['province']=$province??[];
        return app_show(0,"获取成功",$data);
    }

    public function query(){
        $post=$this->request->only(["code"=>'',"name"=>"","level"=>"1"],"post","trim");
        $valid =Validate::rule(["code|查询编号"=>"max:255","name|名称"=>"max:255","level"=>"require|max:255|in:1,2,3"]);
        if($valid->check($post)==false)return error_show(1004,$valid->getError());
        $where=[];
        if($post['code']!='') $where[]=["parent_code","=",$post['code']];
        if($post['name']!='') $where[]=["search_name","like","%{$post['name']}%"];
        if($post['level']==1){
        	$model = Db::name("province")->field("name,province_code code,0 parent_code,name search_name")->buildSql();
        }
        if($post['level']==2){
        	$model = Db::name("city")->alias("a")
        	->leftJoin("province b","a.province_code=b.province_code")
        	->field("a.name,city_code code,a.province_code parent_code,CONCAT(b.name,a.name) search_name")->buildSql();
        }
		if($post['level']==3){
        	$model = Db::name("area")->alias("a")
        	->leftJoin("city b","a.city_code=b.city_code")
        	->leftJoin("province c","c.province_code=b.province_code")
        	->field("a.name,a.area_code code,a.city_code parent_code,CONCAT(c.name,b.name,a.name) search_name")
        	->buildSql();
        }
//		$count = Db::table($model . ' f')->where($where)->count();

		$data = Db::table($model . ' f')->field("f.*,'' as info,{$post['level']} level")->where($where)->withAttr
		("info",function ($val,$data){
			$trmp =[];
				if($data['level']==3){
					$trmp['area']=["code"=>$data['code'],"name"=>$data['name']];
					$city = Db::name("city")->where(['city_code'=>$data['parent_code']])->field("name,city_code,province_code")->findOrEmpty();
					$trmp['city']=["code"=>$city['city_code']??"","name"=>$city['name']??""];
					$province=Db::name("province")->where(['province_code'=>$city['province_code']])->field("name,province_code")->findOrEmpty();
					$trmp['province']=["code"=>$province['province_code']??"","name"=>$province['name']??""];
				}
			if($data['level']==2){
				$province=Db::name("province")->where(['province_code'=>$data['parent_code']])->field("name,province_code")->findOrEmpty();
				$trmp['province']=["code"=>$province['province_code']??"","name"=>$province['name']??""];
				$trmp['area']=[];
				$trmp['city']=["code"=>$data['code'],"name"=>$data['name']];
			}
			if($data['level']==1){
				$trmp['area']=[];
				$trmp['city']=[];
				$trmp['province']=["code"=>$data['code'],"name"=>$data['name']];
			}
			return $trmp;
		    })->select();
        return app_show(0,"获取成功",$data);
    }
}