<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Validator;
use App\Responses;
use App\Helper;
use App\Models\Zone;
use App\Models\Region;

class RegionController extends Controller
{
	public function api_list_city(Request $req)
	{
		try {
			$now = time();

			$input = [
				'kodeProv' => $req->kodeProv,
			];
			$rulesInput = [
				'kodeProv' => 'required|numeric',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			$apiResp = Responses::success('success get list district');
			$apiResp['data'] = Region::listCity($req->kodeProv);
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}

	public function api_list_district(Request $req)
	{
		try {
			$now = time();

			$input = [
				'kodeKab' => $req->kodeKab,
			];
			$rulesInput = [
				'kodeKab' => 'required|numeric',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			$apiResp = Responses::success('success get list village');
			$apiResp['data'] = Region::listDistrict($req->kodeKab);
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}

	public function api_list_village(Request $req)
	{
		try {
			$now = time();

			$input = [
				'kodeKec' => $req->kodeKec,
			];
			$rulesInput = [
				'kodeKec' => 'required|numeric',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			$apiResp = Responses::success('success get list village');
			$apiResp['data'] = Region::listVillage($req->kodeKec);
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}
}
