<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\CountryStateCityModel;

class SelectCountry extends BaseController
{
    public $countrystatecityModel;
    public function __construct()
    {

    }

    public function index() {
  
        $this->countrystatecityModel = new CountryStateCityModel();
        $data['countries'] = $this->countrystatecityModel->getCountries();
        return view('dropdown-list', $data);
    }

    public function getCountry()
    {
        $this->data['countries']    = $this->countryModel->getAll();
        $this->data['states']       = $this->stateModel->getAll();
        $this->data['cities']       = $this->cityModel->getAll();
    }

    public function getStates() 
    {
        $this->countrystatecityModel = new CountryStateCityModel();
        $postData = [
            'country_id' => $this->request->getPost('country_id'),
        ];
 
        $data = $this->countrystatecityModel->getStates($postData);
        echo json_encode($data);
    }
 
    public function getCities() 
    {
        $this->countrystatecityModel = new CountryStateCityModel();
        $postData = [
            'state_id' => $this->request->getPost('state_id'),
        ];

        $data = $this->countrystatecityModel->getCities($postData);
        echo json_encode($data);
    } 

}