<?php

namespace html2pdf\templates;

use html2pdf\helpers\html2pdf_template;

class wash_certificate extends html2pdf_template
{
    /**
     * @inheritDoc
     */
    public function getHtml($data = null): string
    {
        // self::addImage(self::pathSrc('truckwash-banner-white-compressed.png'), 'Certificate Image', 100, 100, 'center');
        $html = '<page backimg="' . self::pathSrc('wash_certificate_template.png') . '" backtop="0mm" backbottom="0mm" backleft="0mm" backright="0mm">
                    <page_header></page_header> 
                    <page_footer></page_footer>';
        /** Add the styles */
        $html .= self::getDefaultStyles();
        /** Add the header */
        //$html .= self::getHeader();
        /** Add the issuer company */
        //$html .= self::getIssuerCompany();
        /** Open the content div */
        $html .= '<div style="' . self::getStyle('content') . '">';
        /** Add the title */
        //$html .= self::getTitle('Wash Certificate');
        /** Add the seal number */
        //$html .= self::getSealNumber();
        /** Add the registration numbers, time, date and the carried out by */
        //$html .= self::getDetails();
        /** Add the provider details */
        //$html .= self::getProviderDetails();
        /** Add the regarding this document */
        //$html .= self::getRegardingThisDocument('DK');
        /** Add the regarding this document in English */
        //$html .= self::getRegardingThisDocument('EN');
        /** Add the signature */
        //$html .= self::getSignature();
        $html .= self::setDocumentFields(
            self::getKey('booking_number') ?: '-',
            self::getKey('time') . ' ' . self::getKey('date'),
            self::getKey('carried_out_by') ?: '-',
            self::getKey('seal_number') ?: '-',
        );
        $html .= self::setDocumentRegistrationNumberFields(
            self::getKey('reg_1') ?: '-',
            self::getKey('reg_2') ?: '-',
        );
        $html .= self::setDocumentCustomerSupplierFields(
            [
                self::getKey('customer_name') ?: '-',
                self::getKey('customer_address') ?: '-',
            ],
            [
                self::getKey('department_name') ?: '-',
                self::getKey('department_address') ?: '-',
            ],
        );
        $html .= self::setSignatureField(
            '123',
        );
        /** Close the content div */
        $html .= '</div>';
        /** Close the page */
        $html .= '</page>';
        return $html;
    }

    private function setDocumentFields(string $document_number_column, string $time_and_date_column, string $carried_out_by_column, string $safety_seal_or_plom_number_column): string
    {
        $column_width_px = 125;
        return '
            <table style="position: absolute; top: 240px; left: 0; width: 100%; border-collapse: collapse; margin-left: 60px; margin-right: 60px; align-content: space-between;">
                <tr>
                    <td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
                        <strong>' . htmlspecialchars($document_number_column, ENT_QUOTES, 'UTF-8') . '</strong>
                    </td>
                    <td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
                        <strong>' . htmlspecialchars($time_and_date_column, ENT_QUOTES, 'UTF-8') . '</strong>
                    </td>
                    <td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
                        <strong>' . htmlspecialchars($carried_out_by_column, ENT_QUOTES, 'UTF-8') . '</strong>
                    </td>
                    <td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
                        <strong>' . htmlspecialchars($safety_seal_or_plom_number_column, ENT_QUOTES, 'UTF-8') . '</strong>
                    </td>
                </tr>
            </table>
        ';
    }

    private function setDocumentRegistrationNumberFields(string $truck_registration_number_column, string $trailer_registration_number_column): string
    {
        $column_width_px = 132;
        $spacer_width_px = 66;
        return '
            <table style="position: absolute; top: 323px; left: 0; width: 100%; border-collapse: collapse; margin-left: 178px; margin-right: 178px; align-content: space-between;">
                <tr>
                    <td style="width: ' . $column_width_px . 'px; align-content: center; text-align: left; border: 1px solid transparent;">
                        <strong>' . htmlspecialchars($truck_registration_number_column, ENT_QUOTES, 'UTF-8') . '</strong>
                    </td>
                    <!-- Spacer -->
                    <td style="width: ' . $spacer_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
                        <strong></strong>
                    </td>
                    <td style="width: ' . $column_width_px . 'px; align-content: center; text-align: left; border: 1px solid transparent;">
                        <strong>' . htmlspecialchars($trailer_registration_number_column, ENT_QUOTES, 'UTF-8') . '</strong>
                    </td>
                </tr>
            </table>
        ';
    }

    private function setDocumentCustomerSupplierFields(array $customer_details_column, array $supplier_details_column): string
    {
        $column_width_px = 257;
        $spacer_width_px = 32;
        $column_2_additional_width_px = 12;
        // Parse the customer details column
        $customer_details_html = '';
        foreach ( $customer_details_column as $key => $value ) {
            // If the item is the first item, add a strong tag
            if ($key === 0) {
                $customer_details_html .= '<strong>' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '</strong><br>';
            } else {
                $customer_details_html .= htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '<br>';
            }
        }
        // Parse the supplier details column
        $supplier_details_html = '';
        foreach ( $supplier_details_column as $key => $value ) {
            // If the item is the first item, add a strong tag
            if ($key === 0) {
                $supplier_details_html .= '<strong>' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '</strong><br>';
            } else {
                $supplier_details_html .= htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '<br>';
            }
        }
        return '
            <table style="position: absolute; top: 712px; left: 0; width: 100%; border-collapse: collapse; margin-left: 72px; margin-right: 72px; align-content: space-between;">
                <tr>
                    <td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
                        ' . $customer_details_html . '
                    </td>
                    <!-- Spacer -->
                    <td style="max-width: ' . $spacer_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent; min-width: ' . $spacer_width_px . 'px; padding: 0; margin: 0;">
                        <div style="width: ' . $spacer_width_px . 'px; height: 1px; background-color: transparent;"></div>
                    </td>
                    <td style="width: ' . ($column_width_px + $column_2_additional_width_px) . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
                        ' . $supplier_details_html . '
                    </td>
                </tr>
            </table>
        ';
    }

    private function setSignatureField(string $column_1): string
    {
        $column_width_px = 219;
        $column_height_px = 30;
        $margin_left_px = 263;
        $margin_right_px = $margin_left_px;
        return '
            <table style="position: absolute; top: 878px; left: 0; width: 100%; border-collapse: collapse; margin-left: ' . $margin_left_px . 'px; ' . $margin_right_px . 'px; align-content: space-between; max-height: ' . $column_height_px . 'px; min-height: ' . $column_height_px . 'px;">
                <tr>
                    <td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent; min-height: ' . $column_height_px . 'px; max-height: ' . $column_height_px . 'px;">
                       <img src="' . self::pathSrc('signature_truckwash_transparent.png') . '" alt="Signature" style="max-height: ' . $column_height_px . 'px; max-width: ' . $column_width_px . 'px; width: auto; height: auto;" />
                    </td>
                </tr>
            </table>
        ';
    }

    /**
     * Get the seal number for the wash certificate
     * @notation This method is used to generate the seal number for the wash certificate.
     * @return string HTML table
     * @see addData() - This method is used to add data to the template.
     */
    public function getSealNumber(): string
    {
        return '
            <table style="width: 80%; border-collapse: collapse; margin-top: 20px; margin-left: 10%; margin-right: 10%; align-content: space-between;">
                <tr>
                    <!-- Seal number -->
                    <td style="width: 100%; padding: 10px; align-content: center; text-align: center">
                        <strong>SEAL/PLOM number</strong> <br>' . htmlspecialchars($this->getKey('seal_number'), ENT_QUOTES, 'UTF-8') . '
                    </td>
                </tr>
            </table>
        ';
    }

    /**
     * Get the HTML details for the wash certificate
     * @notation This method is used to generate the details of the wash certificate. (E.g. registration numbers, time, date and the carried out by)
     * @return string HTML table
     * @see addData() - This method is used to add data to the template.
     */
    public function getDetails(): string
    {
        return '
            <table style="width: 80%; border-collapse: collapse; margin-left: 10%; margin-right: 10%; align-content: space-between;">
                <tr>
                    <!-- Customer name -->
                    <td style="width: 33%; padding: 10px; align-content: center; text-align: center">
                        <strong>Company name</strong> <br>' . htmlspecialchars($this->getKey('customer_name'), ENT_QUOTES, 'UTF-8') . '
                    </td>
                    <!-- Reg number 1 -->
                    <td style="width: 33%; padding: 10px; align-content: center; text-align: center">
                        <strong>Reg.no.</strong> <br>' . htmlspecialchars($this->getKey('reg_1'), ENT_QUOTES, 'UTF-8') . '
                    </td>
                    <!-- Date -->
                    <td style="width: 33%; padding: 10px; align-content: center; text-align: center">
                        <strong>Date</strong> <br>' . htmlspecialchars($this->getKey('time'), ENT_QUOTES, 'UTF-8') . ' ' . htmlspecialchars($this->getKey('date'), ENT_QUOTES, 'UTF-8') . '
                    </td>
                </tr>
                <tr>
                    <!-- Type -->
                    <td style="width: 33%; padding: 10px; align-content: center; text-align: center">
                        <strong>Type</strong> <br>' . htmlspecialchars($this->getKey('type'), ENT_QUOTES, 'UTF-8') . '
                    </td>
                    <!-- Reg number 2 -->
                    <td style="width: 33%; padding: 10px; align-content: center; text-align: center">
                        <strong>Trailerno.</strong> <br>' . htmlspecialchars($this->getKey('reg_2'), ENT_QUOTES, 'UTF-8') . '
                    </td>
                    <!-- Time -->
                    <td style="width: 33%; padding: 10px; align-content: center; text-align: center">
                        <strong>Time</strong> <br>' . htmlspecialchars($this->getKey('time'), ENT_QUOTES, 'UTF-8') . '
                    </td>
                </tr>
            </table>
        ';
    }

    /**
     * Get the HTML provider details for the wash certificate
     * @notation This method is used to generate the provider details of the wash certificate. (E.g. department, company name, address, zip, city, phone prefix, phone, email and website)
     * @return string HTML table
     * @see addData() - This method is used to add data to the template.
     */
    public function getProviderDetails(): string
    {
        return '
            <table style="width: 80%; border-collapse: collapse; margin-left: 10%; margin-right: 10%; align-content: space-evenly;">
                <tr>
                    <!-- Service provider -->
                    <td style="width: 33%; padding: 10px; align-content: center; text-align: center">
                        <strong>Service Provider</strong>
                        <br>' . self::getCompanyName() . '
                        <br>' . self::getCompanyAddress() . '
                        <br>' . self::getCompanyZip() . ' ' . self::getCompanyCity() . '
                       
                    </td>
                    <!-- Contact details -->
                    <td style="width: 33%; padding: 10px; align-content: center; text-align: center">
                        <strong>Contact Details</strong>
                        <br>+' . self::getCompanyPhoneCountryCode() . ' ' . self::getCompanyPhone() . '
                        <br>' . self::getCompanyEmail() . '
                    </td>
                    <!-- Operator -->
                    <td style="width: 33%; padding: 10px; align-content: center; text-align: center">
                        <strong>Carried Out By</strong> 
                        <br>' . htmlspecialchars($this->getKey('carried_out_by'), ENT_QUOTES, 'UTF-8') . '
                    </td>
                </tr>
            </table>
        ';
    }

    /**
     * Get the regarding this document
     * @notation This method is used to generate the regarding this document.
     * @param string $language Language code (e.g. 'DK', 'EN', etc.)
     * @return string HTML table
     */
    public function getRegardingThisDocument(string $language = 'DK'): string
    {
        $translations = [
            'DK' => [
                'Angående dette dokument',
                'Der kvitteres herved for udførelse af indvendig vask af denne trailer. Vasken er udført med en godkendt sæbe til formålet – samt afskyllet med vand bagefter.',
                'Der er udført egenkontrol af vasken, Herved bekræftes det at vasken er udført optimalt.',
                'Datablade kan rekvireres hos Copenhagen Truck Wash.',
                'Bakterielle prøver bør tages umiddelbart efter vask, dog max 3 timer efter. Prøven skal tages på et tørt sted og ikke på gulvet',
            ],
            'EN' => [
                'Regarding this document',
                'This is hereby confirmed for the performance of internal washing of this trailer. The wash has been performed with an approved soap for the purpose - and rinsed with water afterwards.',
                'Self-control of the wash has been performed, hereby confirming that the wash has been performed optimally.',
                'Data sheets can be requested from Copenhagen Truck Wash.',
                'Bacterial samples should be taken immediately after washing, but no later than 3 hours after. The sample should be taken in a dry place and not on the floor.',
            ],
        ];
        return '
            <table style="width: 80%; border-collapse: collapse; margin-top: 20px; margin-left: 10%; margin-right: 10%; align-content: space-between;">
                <tr>
                    <!-- Regarding this document -->
                    <td style="width: 100%; padding: 10px; align-content: center; text-align: left">
                        <strong>' . htmlspecialchars($translations[$language][0], ENT_QUOTES, 'UTF-8') . '</strong>
                        <br>' . htmlspecialchars($translations[$language][1], ENT_QUOTES, 'UTF-8') . '
                        <br>' . htmlspecialchars($translations[$language][2], ENT_QUOTES, 'UTF-8') . '
                        <br>' . htmlspecialchars($translations[$language][3], ENT_QUOTES, 'UTF-8') . '
                        <br>' . htmlspecialchars($translations[$language][4], ENT_QUOTES, 'UTF-8') . '
                    </td>
                </tr>
            </table>
        ';
    }

    private function getBackground(): string
    {
        // add the template pdf as a background
        $template = self::pathSrc('wash_certificate_template.png');
        $html = '<div style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: -1;">';
        //$html .= self::addImage($template, 'Certificate Image', -1, -1, 'center', 'background');
        $html .= '</div>';
        return $html;
    }
}