<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class WilayahController extends Controller
{
    public function province(){
        $data = DB::table('provinces')->where(function($q){
            if(request()->filled('id'))
            {
                $q->where('id',request('id'));
            }
        })->get();
        return response()->json([
            'content' => $data
            ]);
    }

    public function regency(){
        $data = DB::table('regencies')->where(function($q){
            if(request()->filled('province_id'))
            {
                $q->where('province_id',request('province_id'));
            }
            if(request()->filled('id'))
            {
                $q->where('id',request('id'));
            }
        })->get();
        return response()->json([
            'content' => $data
            ]);
    }

    public function district(){
        $data = DB::table('districts')->where(function($q){
            if(request()->filled('regency_id'))
            {
                $q->where('regency_id',request('regency_id'));
            }
            if(request()->filled('id'))
            {
                $q->where('id',request('id'));
            }
        })->get();
        return response()->json([
            'content' => $data
            ]);
    }

    public function village(){
        $data = DB::table('villages')
        ->where(function($q){
            if(request()->filled('district_id'))
            {
                $q->where('district_id',request('district_id'));
            }
            if(request()->filled('id'))
            {
                $q->where('id',request('id'));
            }
        })
        ->get();
        return response()->json([
            'content' => $data
            ]);
    }
}
