<?php
ini_set('display_errors', 1);

require_once '../../resources/base_path_include.php';

//valdation file
require_once $base_path_to_learning_resources . "includes/connect.php"; // use require for mandatory files
require_once $base_path_to_learning_resources . 'lib/function.php';
require_once $base_path_to_learning_resources . 'lib/validate_api_signature.php';

require_once $base_path_to_learning.'user_management/authenticate.php';


$postdata = json_decode(file_get_contents("php://input"), true);

checkIfNoPhpContentsReceive();
if (!isset($postdata['api_signature']) or !checkIfIssetAndNotEmpty($postdata['api_signature']) or !isset($postdata['function_type']) or !checkIfIssetAndNotEmpty($postdata['function_type'])
) {
    errorreturn("All * fields are required.");
}
validateApiSignature($postdata['api_signature']);


function fetchState($conn_rjllearning, $search_string, $country_id, $limit, $offset)
{

    $sql = "Select count(*) as total_rows from state_info where state LIKE ? and country_id = ?";
    $array_params = ['%' . $search_string . '%', $country_id];
    $validate_region = $conn_rjllearning->prepare($sql);
    $validate_region->execute($array_params);

    $result_total = $validate_region->fetchAll();
    $totalrows = $result_total[0]['total_rows'];
    if ($limit == 'all') {

        $sql = "SELECT * FROM state_info WHERE state LIKE ? and country_id = ? ";
        $stmt = $conn_rjllearning->prepare($sql);
        $stmt->execute(array('%' . $search_string . '%', $country_id));
        $result = $stmt->fetchAll();
    } else {

        $sql = "SELECT * FROM state_info WHERE state LIKE ? and country_id = ?  LIMIT " . $offset . " ," . $limit . "  ";
        $stmt = $conn_rjllearning->prepare($sql);
        $stmt->execute(array('%' . $search_string . '%', $country_id));
        $data = array();
        $result = $stmt->fetchAll();

    }
    foreach ($result as $row) {


        $data[] = array('state_id' => $row['id'],  'country_id' => $row['country_id'], 'state' => $row['state'], 'state_code' => $row['state_code']);
    }
  
        if (count($result) < 50) {

            return array('total_rows' => $totalrows, 'state_info' => $data, 'maxreach' => 1);
        } else {

            return array('total_rows' => $totalrows, 'state_info' => $data, 'maxreach' => 0);
        }
   
}

if ($postdata['function_type']  == "fetch_state") {
    
    if(!isset($postdata['country_id']) or empty($postdata['country_id'])) {
        errorreturn("Access denied.");
    } else $country_id = trim($postdata['country_id']);

    if (isset($postdata['search_string']) and !empty($postdata['search_string'])) {
        $search_string = trim($postdata['search_string']);
    } else {
        $search_string = null;
    }

    $limit = $postdata['limit'];
    $page = (isset($postdata['page']) and isNumeric($postdata['page'])) ? (int)$postdata['page'] : 1;

    if ($limit != 'all') {
        $offset = ($page - 1) * $limit;
        $success['data'] = fetchState($conn_rjllearning, $search_string, $country_id, $limit, $offset);
        successreturn($success);
    } else {
        $success['data'] = fetchState($conn_rjllearning, $search_string, $country_id, $limit, null);
        successreturn($success);
    }
}
