<?php
	
	namespace app\common\controller;
	
	use app\common\model\geo\City;
	use app\common\model\geo\County;
	use app\common\model\geo\Group;
	use app\common\model\geo\Province;
	use app\common\model\geo\Town;
	use app\common\model\geo\Village;
	use think\facade\Db;
	use think\response\Json;
	
	class GeoController extends BaseLikeAdminController
	{
        public array $notNeedLogin=['province','city', 'county','towns','villages','groups'];
		// 获取省份
		public function province(): Json
		{
			$data = Province::field('province_code,province_name')->select();
			return $this->success('请求成功',$data->toArray());
		}
		
		// 获取城市
		public function city(): Json
		{
			$province_code = $this->request->get('province_code');
			if(empty($province_code)){
				return $this->fail('请选择省份');
			}
			$data = City::field('city_code,city_name')->where('province_code',$province_code)->select();
			return $this->success('请求成功',$data->toArray());
		}
		
		// 获取区县
		public function county(): Json
		{
			$city_code = $this->request->get('city_code');
			if(empty($city_code)){
				return $this->fail('请选择城市');
			}
			$data = County::field('county_code,county_name')->where('city_code',$city_code)->select();
			return $this->success('请求成功',$data->toArray());
		}
		
		// 获取镇街
		public function towns(): Json
		{
			$county_code = $this->request->get('county_code');
			if(empty($county_code)){
				return $this->fail('请选择区县');
			}
			$data = Town::field('town_code,town_name,lng,lat')->where('county_code',$county_code)->select();
			return $this->success('请求成功',$data->toArray());
		}
		
		// 获取村社
		public function villages(): Json
		{
			$town_code = $this->request->get('town_code');
			if(empty($town_code)){
				return $this->fail('请选择镇街');
			}
			$data = Village::field('village_code,village_name')->where('town_code',$town_code)->select();
			return $this->success('请求成功',$data->toArray());
		}
		
		// 获取小组
		public function groups(): Json
		{
			$data = Group::field('id as group_code,group_name')->select();
			return $this->success('请求成功',$data->toArray());
		}
	}