<?php

class Permits extends Base {

    // Variables.

    private $current_user_serial = 0;
    private $current_user_id = "";
    private $current_user_name = "";
    private $current_user_email = "";
    private $pagination_size = 20;
    private $pagination_page = 1;

    // =================
    // Class Constructor
    // =================

    public function __construct() {

        set_error_handler(array($this, "displayError"));

        date_default_timezone_set(self::TIMEZONE);

        $this->current_user_serial = $_SESSION[self::USER_SERIAL] ?? 0;
        $this->current_user_id = $_SESSION[self::USER_ID] ?? "";
        $this->current_user_name = $_SESSION[self::USER_NAME] ?? "";
        $this->current_user_email = $_SESSION[self::USER_EMAIL] ?? "";

        $this->pagination_page = $_SESSION[self::PAGINATION_PAGE] ?? 1;
    }

    // ==============
    // Manage Permits
    // ==============

    public function managePermits() {

        $permit = $this->getPermitsPage();
        $popovers = (new Popovers())->getPopovers();

        $XML = $this->mergeXML($permit, "<XML/>");
        $XML = $this->mergeXML($popovers, $XML);

        $content = $this->applyXSL($XML, $this->getXSL("managePermits"));

        $this->displayContent($content);
    }

    // ==========
    // Add Permit
    // ==========

    public function addPermit() {

        $step = filter_input(INPUT_POST, "step") ?: "prompt";

        switch ($step) {

            case "prompt":

                $constants = $this->getConstants();
                $counties = (new Counties())->getCounties();

                $XML = $this->mergeXML($constants, "<XML/>");
                $XML = $this->mergeXML($counties, $XML);

                $content = $this->applyXSL($XML, $this->getXSL("addPermit"));

                $this->displayContent($content);

                break;

            case "add":

                $permit_permit_facts_id = filter_input(INPUT_POST, "permit_permit_facts_id", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_permit_number = filter_input(INPUT_POST, "permit_permit_number") ?: '';
                $permit_entry_date = filter_input(INPUT_POST, "permit_entry_date") ?: '';
                $permit_permit_date = filter_input(INPUT_POST, "permit_permit_date") ?: '';
                $permit_sub_div_name = filter_input(INPUT_POST, "permit_sub_div_name") ?: '';
                $permit_project_type = filter_input(INPUT_POST, "permit_project_type") ?: '';
                $permit_county = filter_input(INPUT_POST, "permit_county") ?: '';
                $permit_company = filter_input(INPUT_POST, "permit_company") ?: '';
                $permit_phone = filter_input(INPUT_POST, "permit_phone") ?: '';
                $permit_project_addr = filter_input(INPUT_POST, "permit_project_addr") ?: '';
                $permit_city = filter_input(INPUT_POST, "permit_city") ?: '';
                $permit_state = filter_input(INPUT_POST, "permit_state") ?: '';
                $permit_zip = filter_input(INPUT_POST, "permit_zip", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_project_city = filter_input(INPUT_POST, "permit_project_city") ?: '';
                $permit_project_state = filter_input(INPUT_POST, "permit_project_state") ?: '';
                $permit_value = filter_input(INPUT_POST, "permit_value", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_lot = filter_input(INPUT_POST, "permit_lot", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_dist_ll = filter_input(INPUT_POST, "permit_dist_ll", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_owner_name = filter_input(INPUT_POST, "permit_owner_name") ?: '';
                $permit_owner_phone = filter_input(INPUT_POST, "permit_owner_phone", FILTER_SANITIZE_NUMBER_INT) ?: '';
                $permit_owner_address = filter_input(INPUT_POST, "permit_owner_address") ?: '';
                $permit_owner_city = filter_input(INPUT_POST, "permit_owner_city") ?: '';
                $permit_owner_state = filter_input(INPUT_POST, "permit_owner_state") ?: '';
                $permit_owner_zip = filter_input(INPUT_POST, "permit_owner_zip", FILTER_SANITIZE_NUMBER_INT) ?: '';
                $permit_size = filter_input(INPUT_POST, "permit_size", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_units = filter_input(INPUT_POST, "permit_units", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_building_type = filter_input(INPUT_POST, "permit_building_type", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_work_type = filter_input(INPUT_POST, "permit_work_type", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_ct1 = filter_input(INPUT_POST, "permit_ct1", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_ytd_permits = filter_input(INPUT_POST, "permit_ytd_permits", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_ytd_value = filter_input(INPUT_POST, "permit_ytd_value") ?: 0;
                $permit_12mth_prev_permits = filter_input(INPUT_POST, "permit_12mth_prev_permits", FILTER_SANITIZE_NUMBER_INT) ?: 0;

                $permit_entry_date = date("Y-m-d", strtotime($permit_entry_date));
                $permit_permit_date = date("Y-m-d", strtotime($permit_permit_date));
                $permit_value = str_replace(",", "", $permit_value);

                $SQL = "INSERT INTO permits (
                                    permit_permit_facts_id,
                                    permit_permit_number,
                                    permit_entry_date,
                                    permit_permit_date,
                                    permit_sub_div_name,
                                    permit_project_type,
                                    permit_county,
                                    permit_company,
                                    permit_phone,
                                    permit_project_addr,
                                    permit_city,
                                    permit_state,
                                    permit_zip,
                                    permit_project_city,
                                    permit_project_state,
                                    permit_value,
                                    permit_lot,
                                    permit_dist_ll,
                                    permit_owner_name,
                                    permit_owner_phone,
                                    permit_owner_address,
                                    permit_owner_city,
                                    permit_owner_state,
                                    permit_owner_zip,
                                    permit_size,
                                    permit_units,
                                    permit_building_type,
                                    permit_work_type,
                                    permit_ct1,
                                    permit_ytd_permits,
                                    permit_ytd_value,
                                    permit_12mth_prev_permits
                        ) VALUES (
                                    :permit_permit_facts_id,
                                    :permit_permit_number,
                                    :permit_entry_date,
                                    :permit_permit_date,
                                    :permit_sub_div_name,
                                    :permit_project_type,
                                    :permit_county,
                                    :permit_company,
                                    :permit_phone,
                                    :permit_project_addr,
                                    :permit_city,
                                    :permit_state,
                                    :permit_zip,
                                    :permit_project_city,
                                    :permit_project_state,
                                    :permit_value,
                                    :permit_lot,
                                    :permit_dist_ll,
                                    :permit_owner_name,
                                    :permit_owner_phone,
                                    :permit_owner_address,
                                    :permit_owner_city,
                                    :permit_owner_state,
                                    :permit_owner_zip,
                                    :permit_size,
                                    :permit_units,
                                    :permit_building_type,
                                    :permit_work_type,
                                    :permit_ct1,
                                    :permit_ytd_permits,
                                    :permit_ytd_value,
                                    :permit_12mth_prev_permits
                                )";

                $statement = $this->connect()->prepare($SQL);

                $statement->bindValue(":permit_permit_facts_id", $permit_permit_facts_id);
                $statement->bindValue(":permit_permit_number", $permit_permit_number);
                $statement->bindValue(":permit_entry_date", $permit_entry_date);
                $statement->bindValue(":permit_permit_date", $permit_permit_date);
                $statement->bindValue(":permit_sub_div_name", $permit_sub_div_name);
                $statement->bindValue(":permit_project_type", $permit_project_type);
                $statement->bindValue(":permit_county", $permit_county);
                $statement->bindValue(":permit_company", $permit_company);
                $statement->bindValue(":permit_phone", $permit_phone);
                $statement->bindValue(":permit_project_addr", $permit_project_addr);
                $statement->bindValue(":permit_city", $permit_city);
                $statement->bindValue(":permit_state", $permit_state);
                $statement->bindValue(":permit_zip", $permit_zip);
                $statement->bindValue(":permit_project_city", $permit_project_city);
                $statement->bindValue(":permit_project_state", $permit_project_state);
                $statement->bindValue(":permit_value", $permit_value);
                $statement->bindValue(":permit_lot", $permit_lot);
                $statement->bindValue(":permit_dist_ll", $permit_dist_ll);
                $statement->bindValue(":permit_owner_name", $permit_owner_name);
                $statement->bindValue(":permit_owner_phone", $permit_owner_phone);
                $statement->bindValue(":permit_owner_address", $permit_owner_address);
                $statement->bindValue(":permit_owner_city", $permit_owner_city);
                $statement->bindValue(":permit_owner_state", $permit_owner_state);
                $statement->bindValue(":permit_owner_zip", $permit_owner_zip);
                $statement->bindValue(":permit_size", $permit_size);
                $statement->bindValue(":permit_units", $permit_units);
                $statement->bindValue(":permit_building_type", $permit_building_type);
                $statement->bindValue(":permit_work_type", $permit_work_type);
                $statement->bindValue(":permit_ct1", $permit_ct1);
                $statement->bindValue(":permit_ytd_permits", $permit_ytd_permits);
                $statement->bindValue(":permit_ytd_value", $permit_ytd_value);
                $statement->bindValue(":permit_12mth_prev_permits", $permit_12mth_prev_permits);

                $statement->execute();

                break;

            default:

                $this->displayHome();
        }
    }

    // ==============
    // Delete Permits
    // ==============

    public function deletePermits() {

        $permit_serial = filter_input(INPUT_POST, "permit_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;

        $permit = $this->getPermits($permit_serial);

        if ($permit->count() == 0) {
            $this->logError("Invalid Permits ({$permit_serial}). " . __METHOD__);
        }

        $SQL = "delete from permits
                  where permit_serial = {$permit_serial}";

        $this->executeSQL($SQL);
    }

    // ==============
    // Edit a Permits
    // ==============

    public function editPermit() {

        $step = filter_input(INPUT_POST, "step") ?: "prompt";

        switch ($step) {

            case "prompt":

                $permit_serial = filter_input(INPUT_POST, "permit_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;

                $permit = $this->getPermits($permit_serial);

                if ($permit->count() == 0) {
                    $this->logError("Invalid Permits ({$permit_serial}). " . __METHOD__);
                }

                $constants = $this->getConstants();
                $counties = (new Counties())->getCounties();

                $XML = $this->mergeXML($permit, "<XML/>");
                $XML = $this->mergeXML($constants, $XML);
                $XML = $this->mergeXML($counties, $XML);

                $content = $this->applyXSL($XML, $this->getXSL("editPermit"));
                $this->displayContent($content);

                break;

            case "update":

                $permit_serial = filter_input(INPUT_POST, "permit_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_permit_facts_id = filter_input(INPUT_POST, "permit_permit_facts_id", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_permit_number = filter_input(INPUT_POST, "permit_permit_number") ?: '';
                $permit_entry_date = filter_input(INPUT_POST, "permit_entry_date") ?: '';
                $permit_permit_date = filter_input(INPUT_POST, "permit_permit_date") ?: '';
                $permit_sub_div_name = filter_input(INPUT_POST, "permit_sub_div_name") ?: '';
                $permit_project_type = filter_input(INPUT_POST, "permit_project_type") ?: '';
                $permit_county = filter_input(INPUT_POST, "permit_county") ?: '';
                $permit_company = filter_input(INPUT_POST, "permit_company") ?: '';
                $permit_phone = filter_input(INPUT_POST, "permit_phone") ?: '';
                $permit_project_addr = filter_input(INPUT_POST, "permit_project_addr") ?: '';
                $permit_city = filter_input(INPUT_POST, "permit_city") ?: '';
                $permit_state = filter_input(INPUT_POST, "permit_state") ?: '';
                $permit_zip = filter_input(INPUT_POST, "permit_zip", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_project_city = filter_input(INPUT_POST, "permit_project_city") ?: '';
                $permit_project_state = filter_input(INPUT_POST, "permit_project_state") ?: '';
                $permit_value = filter_input(INPUT_POST, "permit_value", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_lot = filter_input(INPUT_POST, "permit_lot", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_dist_ll = filter_input(INPUT_POST, "permit_dist_ll", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_owner_name = filter_input(INPUT_POST, "permit_owner_name") ?: '';
                $permit_owner_phone = filter_input(INPUT_POST, "permit_owner_phone", FILTER_SANITIZE_NUMBER_INT) ?: '';
                $permit_owner_address = filter_input(INPUT_POST, "permit_owner_address") ?: '';
                $permit_owner_city = filter_input(INPUT_POST, "permit_owner_city") ?: '';
                $permit_owner_state = filter_input(INPUT_POST, "permit_owner_state") ?: '';
                $permit_owner_zip = filter_input(INPUT_POST, "permit_owner_zip", FILTER_SANITIZE_NUMBER_INT) ?: '';
                $permit_size = filter_input(INPUT_POST, "permit_size", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_units = filter_input(INPUT_POST, "permit_units", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_building_type = filter_input(INPUT_POST, "permit_building_type", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_work_type = filter_input(INPUT_POST, "permit_work_type", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_ct1 = filter_input(INPUT_POST, "permit_ct1", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_ytd_permits = filter_input(INPUT_POST, "permit_ytd_permits", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $permit_ytd_value = filter_input(INPUT_POST, "permit_ytd_value") ?: 0;
                $permit_12mth_prev_permits = filter_input(INPUT_POST, "permit_12mth_prev_permits", FILTER_SANITIZE_NUMBER_INT) ?: 0;

                $permit_entry_date = date("Y-m-d", strtotime($permit_entry_date));
                $permit_permit_date = date("Y-m-d", strtotime($permit_permit_date));
                $permit_value = str_replace(",", "", $permit_value);

                // Verify the Permits
                $permit = $this->getPermits($permit_serial);

                if ($permit->count() == 0) {
                    $this->logError("Invalid Permits ({$permit_serial}). " . __METHOD__);
                }

                $SQL = "UPDATE permits 
                           SET permit_permit_facts_id        = :permit_permit_facts_id,
                               permit_permit_number          = :permit_permit_number,
                               permit_entry_date             = :permit_entry_date,
                               permit_permit_date            = :permit_permit_date,
                               permit_sub_div_name           = :permit_sub_div_name,
                               permit_project_type           = :permit_project_type,
                               permit_county                 = :permit_county,
                               permit_company                = :permit_company,
                               permit_phone                  = :permit_phone,
                               permit_project_addr           = :permit_project_addr,
                               permit_city                   = :permit_city,
                               permit_state                  = :permit_state,
                               permit_zip                    = :permit_zip,
                               permit_project_city           = :permit_project_city,
                               permit_project_state          = :permit_project_state,
                               permit_value                  = :permit_value,
                               permit_lot                    = :permit_lot,
                               permit_dist_ll                = :permit_dist_ll,
                               permit_owner_name             = :permit_owner_name,
                               permit_owner_phone            = :permit_owner_phone,
                               permit_owner_address          = :permit_owner_address,
                               permit_owner_city             = :permit_owner_city,
                               permit_owner_state            = :permit_owner_state,
                               permit_owner_zip              = :permit_owner_zip,
                               permit_size                   = :permit_size,
                               permit_units                  = :permit_units,
                               permit_building_type          = :permit_building_type,
                               permit_work_type              = :permit_work_type,
                               permit_ct1                    = :permit_ct1,
                               permit_ytd_permits            = :permit_ytd_permits,
                               permit_ytd_value              = :permit_ytd_value,
                               permit_12mth_prev_permits     = :permit_12mth_prev_permits
                         WHERE permit_serial                 = :permit_serial";

                $statement = $this->connect()->prepare($SQL);

                $statement->bindValue(":permit_serial", $permit_serial);
                $statement->bindValue(":permit_permit_facts_id", $permit_permit_facts_id);
                $statement->bindValue(":permit_permit_number", $permit_permit_number);
                $statement->bindValue(":permit_entry_date", $permit_entry_date);
                $statement->bindValue(":permit_permit_date", $permit_permit_date);
                $statement->bindValue(":permit_sub_div_name", $permit_sub_div_name);
                $statement->bindValue(":permit_project_type", $permit_project_type);
                $statement->bindValue(":permit_county", $permit_county);
                $statement->bindValue(":permit_company", $permit_company);
                $statement->bindValue(":permit_phone", $permit_phone);
                $statement->bindValue(":permit_project_addr", $permit_project_addr);
                $statement->bindValue(":permit_city", $permit_city);
                $statement->bindValue(":permit_state", $permit_state);
                $statement->bindValue(":permit_zip", $permit_zip);
                $statement->bindValue(":permit_project_city", $permit_project_city);
                $statement->bindValue(":permit_project_state", $permit_project_state);
                $statement->bindValue(":permit_value", $permit_value);
                $statement->bindValue(":permit_lot", $permit_lot);
                $statement->bindValue(":permit_dist_ll", $permit_dist_ll);
                $statement->bindValue(":permit_owner_name", $permit_owner_name);
                $statement->bindValue(":permit_owner_phone", $permit_owner_phone);
                $statement->bindValue(":permit_owner_address", $permit_owner_address);
                $statement->bindValue(":permit_owner_city", $permit_owner_city);
                $statement->bindValue(":permit_owner_state", $permit_owner_state);
                $statement->bindValue(":permit_owner_zip", $permit_owner_zip);
                $statement->bindValue(":permit_size", $permit_size);
                $statement->bindValue(":permit_units", $permit_units);
                $statement->bindValue(":permit_building_type", $permit_building_type);
                $statement->bindValue(":permit_work_type", $permit_work_type);
                $statement->bindValue(":permit_ct1", $permit_ct1);
                $statement->bindValue(":permit_ytd_permits", $permit_ytd_permits);
                $statement->bindValue(":permit_ytd_value", $permit_ytd_value);
                $statement->bindValue(":permit_12mth_prev_permits", $permit_12mth_prev_permits);

                $statement->execute();

                break;

            default:

                $this->displayHome();
        }
    }

    // ================================
    // Return an Active Permits Object
    // ================================

    public function getActivePermits() {

        $SQL = "select view_permits.*
              from view_permits
             where permit_status is true";

        return $this->getTable("permits", $SQL);
    }

    // ========================
    // Return an Permits Object
    // ========================

    public function getPermit($permit_serial = 0) {

        $SQL = "select view_permits.*
              from view_permits
             where permit_serial = {$permit_serial}";

        return $this->getTable("permits", $SQL);
    }

    // ========================
    // Return a Permits Object
    // ========================

    public function getPermits() {

        $SQL = "select view_permits.*
              from view_permits";

        return $this->getTable("permits", $SQL);
    }

    // ========================
    // Return a Permits Page Object
    // ========================

    public function getPermitsPage() {

        // Find Criteria

        $find_permit = filter_input(INPUT_POST, "find_permit") ?: "";
        $find_permit = $this->sanitizeString($find_permit);

        $this->pagination_size = filter_input(INPUT_POST, "permits_per_page", FILTER_SANITIZE_NUMBER_INT) ?: $this->pagination_size;

        $permits_order_by = filter_input(INPUT_POST, "permits_order_by") ?: "";
        $permits_order_direction = filter_input(INPUT_POST, "permits_order_direction") ?: "";

        if ($permits_order_by === "") {
            $orderby = "permit_permit_number desc";
        } else {
            $orderby = $permits_order_by . " " . $permits_order_direction;
        }

        // Record Count

        $connection = $this->connect();

        $SQL = "select count(*) as count
                  from view_permits
                 where permit_permit_number        regexp :permit_permit_number 
                    or permit_entry_date_verbose   regexp :permit_entry_date_verbose 
                    or permit_permit_date_verbose  regexp :permit_permit_date_verbose
                    or permit_sub_div_name         regexp :permit_sub_div_name 
                    or permit_project_type         regexp :permit_project_type";

        $statement = $connection->prepare($SQL);

        $statement->bindValue(":permit_permit_number", $find_permit);
        $statement->bindValue(":permit_entry_date_verbose", $find_permit);
        $statement->bindValue(":permit_permit_date_verbose", $find_permit);
        $statement->bindValue(":permit_sub_div_name", $find_permit);
        $statement->bindValue(":permit_project_type", $find_permit);

        $statement->execute();

        $recordset = $statement->fetch(PDO::FETCH_ASSOC);

        $count = (count($recordset) > 0) ? (integer) $recordset["count"] : (integer) 0;

        // Pagination

        $pagination = filter_input(INPUT_POST, "pagination") ?: "";

        switch ($pagination) {

            case "first" : $this->pagination_page = 1;
                break;
            case "previous" : $this->pagination_page = max(($this->pagination_page - 1), 1);
                break;
            case "next" : $this->pagination_page = ($this->pagination_page + 1);
                break;
            case "last" : $this->pagination_page = ($count / $this->pagination_size);
                break;
        }

        $offset = (($this->pagination_page * $this->pagination_size) - $this->pagination_size);
        $start = ($offset + 1);
        $end = min($count, (($start + $this->pagination_size) - 1));

        // Data

        $SQL = "select view_permits.*
                  from view_permits
                 where permit_permit_number        regexp :permit_permit_number 
                    or permit_entry_date_verbose   regexp :permit_entry_date_verbose 
                    or permit_permit_date_verbose  regexp :permit_permit_date_verbose
                    or permit_sub_div_name         regexp :permit_sub_div_name 
                    or permit_project_type         regexp :permit_project_type
              order by {$orderby}
                 limit {$this->pagination_size}
                offset {$offset}";

        $statement = $connection->prepare($SQL);

        $statement->bindValue(":permit_permit_number", $find_permit);
        $statement->bindValue(":permit_entry_date_verbose", $find_permit);
        $statement->bindValue(":permit_permit_date_verbose", $find_permit);
        $statement->bindValue(":permit_sub_div_name", $find_permit);
        $statement->bindValue(":permit_project_type", $find_permit);

        $statement->execute();

        $permits = $this->getTableXML("permits", $statement);

        // Insert Pagination Attributes

        $permits->addAttribute("count", $count);
        $permits->addAttribute("start", $start);
        $permits->addAttribute("end", $end);
        $permits->addAttribute("page", $this->pagination_page);

        $_SESSION[self::PAGINATION_PAGE] = $this->pagination_page;

        return $permits;
    }
}

?>
