<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Cities extends SiteController {
  function __construct () {
    parent::__construct();
    $this->data['page'] = 'cities';
    $this->data['metatags'] = $this->getMetatags($this->data['page']);

    $this->load->model('internBannerModel');
    $this->data['banner'] = $this->internBannerModel->getRowWhere(['slug' => 'cities']);
  }

  public function index () {
    $this->load->view('cities', $this->data);
  }

  public function search () {
    $city = antiInjection(normalize($this->input->get('city')));

    $this->load->model('UnityModel');

    $unity = $this->UnityModel->getByCityName($city);
    if ($unity) {
      $unity->full_address = $this->setupAddress($unity);
      $this->data['unity'] = $unity;
    }

    $this->data['isSearching'] = true;
    $this->load->view('cities', $this->data);
  }

  private function setupAddress ($unity) {
    if (!$unity) return null;

    $street = $unity->address_street ? "$unity->address_street, " : '';
    $neighborhood = $unity->address_neighborhood ? "$unity->address_neighborhood. " : '';
    $zip_code = $unity->address_zip_code ? "$unity->address_zip_code, " : '';
    $city = $unity->address_city ? "$unity->address_city - " : '';
    $uf = $unity->address_uf ? $unity->address_uf : '';
    
    return $street . $neighborhood . $zip_code . $city . $uf;
  }
}