<?php

// =================================
// Export All Permits By Subdivision
// =================================

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Export_AllPermitsBySubdivision_Excel extends Base {

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

    public function __construct() {

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

        date_default_timezone_set(self::TIMEZONE);
    }

    // ===============
    // Export the Data
    // ===============

    public function export() {

        set_time_limit(0);

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

        $subscription = (new Subscriptions())->getSubscription($subscription_serial);

        $permit_area = $subscription->record->subscription_area;
        $projecttype = $subscription->record->subscription_projecttype;

        $permits = (new PermitsBySubdivision())->getAllPermitsBySubdivision($permit_area, $projecttype, $_POST);

        // Create the Spreadsheet.

        $spreadsheet = new Spreadsheet();

        $spreadsheet->getProperties()->setCreator("DEC International")->setTitle("PermitsBySubdivision")->setSubject("PermitsBySubdivision")
                ->setDescription("PermitsBySubdivision")->setKeywords("PermitsBySubdivision")->setCategory("PermitsBySubdivision");

        // Create the Base Sheet.

        $spreadsheet->setActiveSheetIndex(0);
        $spreadsheet->getActiveSheet()->setTitle("Permits");

        // Format the Cells and Create the Headings.

        $spreadsheet->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("E")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("F")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("G")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("H")->setAutoSize(true);

        $spreadsheet->getActiveSheet()->getStyle("A1:H1")->getFont()->setBold(true);
        $spreadsheet->getActiveSheet()->getStyle("A1:H1")->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB("D3D3D3");

        $spreadsheet->setActiveSheetIndex(0)->setCellValue("A1", "County")
                ->setCellValue("B1", "Subdivision")
                ->setCellValue("C1", "Address")
                ->setCellValue("D1", "City")
                ->setCellValue("E1", "Builder")
                ->setCellValue("F1", "Permit Date")
                ->setCellValue("G1", "Square Footage")
                ->setCellValue("H1", "Value");

        $spreadsheet->getActiveSheet()->getStyle("A:A")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("B:B")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("C:C")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("D:D")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("E:E")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("F:F")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("G:G")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER);
        $spreadsheet->getActiveSheet()->getStyle("H:H")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER);

        $spreadsheet->getActiveSheet()->freezePane("A2");

        // Create the Detail Rows.

        $row = 2;

        foreach ($permits as $permit) {

            $address = "{$permit->permit_address} {$permit->permit_city}, {$permit->permit_state} {$permit->permit_zip}";

            $spreadsheet->getActiveSheet()->setCellValue("A" . $row, (string) $permit->county_name_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("B" . $row, (string) $permit->permit_sub_div_name);
            $spreadsheet->getActiveSheet()->setCellValue("C" . $row, (string) $address);
            $spreadsheet->getActiveSheet()->setCellValue("D" . $row, (string) $permit->permit_city);
            $spreadsheet->getActiveSheet()->setCellValue("E" . $row, (string) $permit->permit_company);
            $spreadsheet->getActiveSheet()->setCellValue("F" . $row, (string) $permit->permit_permit_date_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("G" . $row, (string) $permit->permit_size_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("H" . $row, (string) $permit->permit_value_verbose);

            $row++;
        }

        $spreadsheet->setActiveSheetIndex(0);

        $filename = str_replace(' ', '_', $subscription->record->subscription_title) . "_" . date("Y-m-d_h:i:s");

        // Download the spreadsheet

        header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        header("Content-disposition: attachment; filename={$filename}.xlsx");
        header("Cache-Control: max-age=0");

        $excelWriter = new Xlsx($spreadsheet);

        @ob_clean();

        $excelWriter->save('php://output');

        exit();
    }

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

    public function debug($object = "") {

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

?>
