<?php

// ============
// Export Class
// ============

class Export extends Base {

    // Variables

    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_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;
    }

    // =============
    // Choose Export
    // =============

    public function chooseExport() {

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

        switch ($step) {

            case "prompt":

                $dropdowns = (new Dropdowns())->getDropdowns();

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

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

                $this->displayContent($content);

                break;

            case "run":

                $process_date = filter_input(INPUT_POST, "export_processdate") ?: "";
                $accounting_period = filter_input(INPUT_POST, "export_accounting_period") ?: "";
                $export_type = filter_input(INPUT_POST, "export_type", FILTER_SANITIZE_NUMBER_INT) ?: 0;

                // Format Dates

                $process_date = ($process_date == "") ? null : date("Y-m-d", strtotime($process_date));

                switch ($export_type) {

                    case "1": // ALL Export Types

                        break;

                    case "2": // Only Hunting & Fishing

                        (new HuntingAndFishing())->createHuntingAndFishingExport($process_date, $accounting_period);

                        break;

                    case "3": // Only PTAX

                        break;

                    case "4": // Only FRVIS

                        break;
                }


                // Send Completion Notifications
//                $this->sendNotifications($workorder_serial);

                break;

            default:

                $this->displayHome();
        }
    }

    // ==================================
    // Send Work Order Notification Email
    // ==================================

    public function sendNotifications($workorder_serial = 0) {

        if (empty($workorder_serial)) {
            return;
        }

        $users = (new Users())->getUsers();
        $to = array();

        foreach ($users as $user) {

            $user_notifications = (integer) $user->user_notifications;
            $user_email = (string) $user->user_email;

            if (($user_notifications == true) and (!(empty($user_email)))) {
                $to[] = $user_email;
            }
        }

        $to = array_unique($to);

        $pdf = (new PrintWorkOrder())->print($workorder_serial, "string");

        if (count($to) > 0) {

            $subject = "New ATLHousingReport Work Order";
            $body = "Attached is a new ATLHousingReport Work Order.";

            (new Email())->sendEmail($to, $subject, $body, $pdf, "WorkOrder.pdf");
        }
    }
}

?>
