<?php


header('Access-Control-Allow-Origin: *');
require "conexion.php";
require "crud.php";

//========= OBTENER DATOS =========
if (isset($_GET['option'])) {
  $option = $_GET["option"];
  if($option == 'searchCP'){
    $cp = $_GET['cp'];
  }
  else if($option == 'searchCPOne'){
    $cp = $_GET['cp'];
  }
}

else if (isset($_POST['option'])) {
  $option = $_POST["option"];
}


switch ($option) {
  case 'searchCP':
    searchCP();
  break;
  case 'searchCPOne':
    searchCPOne();
  break;
}


function searchCP(){
  global $cp;
  $arrayF = array();
  $arrayDataE = array();
  $arrayDataM = array();
  $arrayDataC = array();
  $array = array(
    'status' => 'error',
    'message' => 'No hay datos',
    'data' => $arrayData
  );
  $model = new Crud;
  $model->select = "D_mnpio,d_estado,d_ciudad,d_asenta";
  $model->from = "postal_code";
  $model->condition = "d_codigo = '$cp'";
  $model->Read();
  $filas = $model->rows;
  $status = 'error';
  $message = 'No hay datos';
  if (is_array($filas) || is_object($filas)) {
    foreach ($filas as $fila) {
      $arrayDataE[] = array('d_estado' => $fila["d_estado"]);
      $arrayDataM[] = array('D_mnpio' => $fila["D_mnpio"]);
      $arrayDataC[] = array('d_asenta' => $fila["d_asenta"]);
      $status = 'success';
      $message = 'Datos obtenidos correctamente';
    }
  }
  $arrayDataE = array_unique($arrayDataE, SORT_REGULAR);
  $arrayDataM = array_unique($arrayDataM, SORT_REGULAR);
  $arrayDataC = array_unique($arrayDataC, SORT_REGULAR);
  array_push($arrayF, $arrayDataE, $arrayDataM, $arrayDataC);
  $array = array(
  'status' => $status,
  'message' => $message,
  'data' => $arrayF
  );

  $array = utf8_converter($array);
  echo json_encode($array);
}

function searchCPOne(){
  global $cp;
  // $arrayDataE = array();
  // $arrayDataM = array();
  // $arrayDataC = array();
  // $array = array(
  //   'status' => 'error',
  //   'message' => 'No hay datos',
  //   'data' => $arrayData
  // );
  $model = new Crud;
  $model->select = "D_mnpio,d_estado,d_ciudad,d_asenta";
  $model->from = "postal_code";
  $model->condition = "d_codigo = '$cp'";
  $model->Read();
  $filas = $model->rows;
  $status = 'error';
  $message = 'No hay datos';
  $arrayF = array();
  if (!empty($filas)) {
    foreach ($filas as $fila) {
      $arrayF[] = array('d_estado' => $fila["d_estado"], 'D_mnpio' => $fila["D_mnpio"], 'd_asenta' => $fila["d_asenta"]);
      $status = 'success';
      $message = 'Datos obtenidos correctamente';
      break;
    }
  }
  // $arrayDataE = array_unique($arrayDataE, SORT_REGULAR);
  // $arrayDataM = array_unique($arrayDataM, SORT_REGULAR);
  // $arrayDataC = array_unique($arrayDataC, SORT_REGULAR);
  // array_push($arrayF, $arrayDataE, $arrayDataM, $arrayDataC);

  $array = array(
  'status' => $status,
  'message' => $message,
  'data' => $arrayF
  );

  $array = utf8_converter($array);
  echo json_encode($array);
}


function utf8_converter($array)

{

  array_walk_recursive(

  $array,

  function (&$item, $key) {

    if (!mb_detect_encoding($item, 'utf-8', true)) {

      $item = utf8_encode($item);

    }

  }

  );

  return $array;

}

?>

