<?php

require_once ("tcpdf/tcpdf.php");

// ======================
// Print Selected Permits
// ======================

class Print_Selected_Permits extends Base {

    // Variables.

    private $output_type = "normal";

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

    public function __construct($output_type = "normal") {

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

        date_default_timezone_set(self::TIMEZONE);

        $this->output_type = $output_type;
    }

    // ================
    // Print the Report
    // ================

    public function print() {

        $report = new Permit_Print();

        $report->selected_permits = explode(",", $_SESSION[self::REPORTS_POST]["selected_permits"]);
        $report->output_type = $this->output_type;

        ob_clean();

        if ($this->output_type == "string") {
            return $report->render();
        } else {
            $report->render();
        }
    }
}

// ==========================
// Class to Generate the .pdf
// ==========================

class Permit_Print extends TCPDF {

    // Variables

    public $selected_permits = array();
    public $permit_serial = 0;
    public $first_page = true;
    public $output_type = "normal";

    // ================
    // Debug an Object.
    // ================

    public function debug($object = "") {

        echo "<pre>";
        print_r($object);
        exit();
    }

    // =================
    // Render the Report
    // =================

    public function render() {

        set_time_limit(0);

        // Process each Selected Work Order

        foreach ($this->selected_permits as $this->permit_serial) {

            // Get the data for the report

            $Permit = new Permits();

            $permit = $Permit->getPermit($this->permit_serial);

            // Set report defaults

            $this->title = "Print Permit Record";

            $this->SetFont("Calibri");
            $this->SetMargins(10, 26, 10);
            $this->SetHeaderMargin(10);
            $this->SetFooterMargin(10);
            $this->setCellPaddings(0, 0, 0, 0);
            $this->setCellMargins(2, 0, 0, 0);

            $this->addPage("P", "LETTER");

            // -------------------
            // Permit Record  Details
            // -------------------

            $permit = $permit->record ?? "";

            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Permit Facts ID:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_permit_facts_id, 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Entry Date:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_entry_date_verbose, 0, 1, "L", false, "", 3);

            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Permit Number:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_permit_number, 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Permit Date:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_permit_date_verbose, 0, 1, "L", false, "", 3);

            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Project Address:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_permit_project_addr, 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Subdivision Name:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_sub_div_name, 0, 1, "L", false, "", 3);

            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Project Type:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_project_type, 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Company", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_company, 0, 1, "L", false, "", 3);

            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Address:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, "{$permit->permit_address} {$permit->permit_city}, {$permit->permit_state} {$permit->permit_zip}", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Phone:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->$permit->permit_phone_verbose, 0, 1, "L", false, "", 3);

            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Project City:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_phone_verbose, 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Project State:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_project_state, 0, 1, "L", false, "", 3);

            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Size:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_intersection_namepermit_size, 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Units:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_size, 0, 1, "L", false, "", 3);

            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Building Type:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_building_type, 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Value:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_building_type, 0, 1, "L", false, "", 3);

            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "County:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_county, 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Work Type", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, "$permit->permit_work_type", 0, 1, "L", false, "", 3);

            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Lot:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_lot, 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Dist/LL", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, "$permit->permit_dist_ll", 0, 1, "L", false, "", 3);

            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "CTL:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_ct1, 0, 0, "L", false, "", 3);

            $this->Ln(6);

            $this->Line($this->GetX(), $this->GetY(), $this->GetX() + 195, $this->GetY());

            $this->Ln(2);

            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Owner Name:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_owner_name, 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Address", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, "{$permit->permit_owner_address} {$permit->permit_owner_city}, {$permit->permit_owner_state} {$permit->permit_owner_zip}", 0, 1, "L", false, "", 3);

            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Owner Phone:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_owner_phone, 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "Address", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_owner_phone, 0, 1, "L", false, "", 3);

            $this->Ln(2);

            $this->Line($this->GetX(), $this->GetY(), $this->GetX() + 195, $this->GetY());

            $this->Ln(2);

            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "YTD Permits:", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_ytd_permits, 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "B", 10);
            $this->Cell(25, 0, "YTD Value", 0, 0, "L", false, "", 3);
            $this->SetFont("Calibri", "", 10);
            $this->Cell(65, 0, $permit->permit_ytd_value, 0, 1, "L", false, "", 3);
            // ----------------------------
            // Rectangle around the Details
            // ----------------------------

            $this->RoundedRect(10, 24, 195, $this->GetY() - 22, 1.50, "1111");
        }

        // --------------
        // Return the pdf
        // --------------

        if ($this->output_type == "string") {
            return $this->Output("PermitRecord.pdf", "S");
        } else {
            $this->Output("PermitRecord.pdf");
        }
    }

    // ======
    // Header
    // ======

    public function Header() {

        $this->setCellPaddings(0, 0, 0, 0);
        $this->setCellMargins(2, 0, 0, 0);

        $this->Image("images/dec-international-logo.png", 10, 10, 20, 8);

        $this->SetFont("Calibri", "B", 16);

        $this->Cell(0, 6, "DEC International", 0, 0, "C");
        $this->Ln(6);
        $this->Cell(0, 6, "Permit Record", 0, 1, "C");
    }

    // ======
    // Footer
    // ======

    public function Footer() {

        $this->SetFont("Calibri", "I", 8);

        $X = $this->GetX();

        $this->Cell(0, 10, "DEC International", 0, false, "C");

        $this->SetX($X);

        $this->Cell(40, 10, "Page " . $this->getAliasNumPage() . "/" . $this->getAliasNbPages(), 0, false, "L");
        $this->Cell(0, 10, date("n/j/Y g:i A"), 0, false, "R");
    }

    // =======================
    // Stripe the printed line
    // =======================

    private function stripeLine($pRowCount = 0, $pRowWidth = 0) {

        $this->SetFillColor(($pRowCount % 2) ? 220 : 255);
        $X = $this->GetX();
        $this->Cell($pRowWidth, 0, "", 0, 0, "L", true);
        $this->SetX($X);
    }
}

?>
