<?php
namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Shield\Entities\User;
use App\Libraries\GeneralLib;

class Geoapi extends BaseController
{
    use ResponseTrait;

    public function select_provinsi()
    {
        if (!auth()->loggedIn()) {
            return $this->fail(['message'=>'error'],400,null,'error');
        }

        $user = auth()->user();

        $jsonArray = json_decode(file_get_contents('php://input'), true);
        $search = "";
        $page = "1";
        if (isset($jsonArray['search'])) {
            $search = $jsonArray['search'];
        }
        if (isset($jsonArray['page'])) {
            $page = $jsonArray['page'];
        }

        $tmodel = model('ProvinsiModel');
        $temp = $tmodel->GetSelect($search, $page);

        return $this->respond($temp,200);
    }

    public function select_kabupaten()
    {
        if (!auth()->loggedIn()) {
            return $this->fail(['message'=>'error'],400,null,'error');
        }

        $user = auth()->user();

        $jsonArray = json_decode(file_get_contents('php://input'), true);
        $search = "";
        $page = "1";
        $extra_query = "";

        if (isset($jsonArray['search'])) {
            $search = $jsonArray['search'];
        }
        if (isset($jsonArray['page'])) {
            $page = $jsonArray['page'];
        }
        if (isset($jsonArray['extra_query'])) {
            $extra_query = $jsonArray['extra_query'];
        }

        $tmodel = model('KabupatenModel');
        $temp = $tmodel->GetSelect($search, $page, $extra_query);

        return $this->respond($temp,200);
    }

    public function select_kecamatan()
    {
        if (!auth()->loggedIn()) {
            return $this->fail(['message'=>'error'],400,null,'error');
        }

        $user = auth()->user();

        $jsonArray = json_decode(file_get_contents('php://input'), true);
        $search = "";
        $page = "1";
        $extra_query = "";

        if (isset($jsonArray['search'])) {
            $search = $jsonArray['search'];
        }
        if (isset($jsonArray['page'])) {
            $page = $jsonArray['page'];
        }
        if (isset($jsonArray['extra_query'])) {
            $extra_query = $jsonArray['extra_query'];
        }

        $tmodel = model('KecamatanModel');
        $temp = $tmodel->GetSelect($search, $page, $extra_query);

        return $this->respond($temp,200);
    }

    public function select_kelurahan()
    {
        if (!auth()->loggedIn()) {
            return $this->fail(['message'=>'error'],400,null,'error');
        }

        $user = auth()->user();

        $jsonArray = json_decode(file_get_contents('php://input'), true);
        $search = "";
        $page = "1";
        $extra_query = "";

        if (isset($jsonArray['search'])) {
            $search = $jsonArray['search'];
        }
        if (isset($jsonArray['page'])) {
            $page = $jsonArray['page'];
        }
        if (isset($jsonArray['extra_query'])) {
            $extra_query = $jsonArray['extra_query'];
        }

        $tmodel = model('KelurahanModel');
        $temp = $tmodel->GetSelect($search, $page,$extra_query);

        return $this->respond($temp,200);
    }
}
?>