<?php


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


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);
    }
}