<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<div class="invoice-creative">
    <div class="row-my-firm">
        <?php 
        $kirilkirkovwipdf_logo_file = '';
        $kirilkirkovwipdf__logo_file = get_option( $Config::INPUTS_PREFIX.'logo_file' );
        if($kirilkirkovwipdf__logo_file) {
            $kirilkirkovwipdf__logo_file = json_decode($kirilkirkovwipdf__logo_file, true);
            if(json_last_error() === JSON_ERROR_NONE) {
                if($kirilkirkovwipdf_is_download) {
                    if(file_exists($kirilkirkovwipdf__logo_file['file'])) {
                        $kirilkirkovwipdf_logo_file = "data:image/png;base64," . base64_encode(file_get_contents($kirilkirkovwipdf__logo_file['file']));
                    }
                } else {
                    if(filter_var($kirilkirkovwipdf__logo_file['url'], FILTER_VALIDATE_URL)) {
                        $kirilkirkovwipdf_logo_file = $kirilkirkovwipdf__logo_file['url'];
                    }
                }
            }
        }
        if($kirilkirkovwipdf_logo_file !== '' && (int)get_option($Config::INPUTS_PREFIX.'hide_logo_from_invoices') < 1) {
            ?>
            <div class="firm-logo">
                <img src="<?php echo esc_attr($kirilkirkovwipdf_logo_file); ?>" alt="">
            </div>
        <?php } ?>

        <div class="my-firm-info">
            <p class="firm-name"><?php echo esc_html(sanitize_text_field(get_option( $Config::INPUTS_PREFIX.'company_name' ))); ?></p>
            <?php 
                $kirilkirkovwipdf_vat_registered = get_option($Config::INPUTS_PREFIX.'company_vat_registered');
                if ($kirilkirkovwipdf_vat_registered && (int)$kirilkirkovwipdf_vat_registered === 1) {
                    $kirilkirkovwipdf_company_vat_number__ = get_option( $Config::INPUTS_PREFIX.'company_vat_number' );
                    if(trim($kirilkirkovwipdf_company_vat_number__) !== '') {
                ?>
                <p><b><?php esc_html_e('Vat Number', 'kirilkirkov-pdf-invoice-manager' ) ?></b> <?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_company_vat_number__)); ?></p>
            <?php }
            } ?>
            <p><?php echo esc_html(sanitize_text_field(get_option( $Config::INPUTS_PREFIX.'company_address' ))); ?></p>
            <p><?php echo esc_html(sanitize_text_field(get_option( $Config::INPUTS_PREFIX.'company_city' ))); ?></p>
            <?php 
                $kirilkirkovwipdf_company_zip_code_ = get_option( $Config::INPUTS_PREFIX.'company_zip_code' );
                if(trim($kirilkirkovwipdf_company_zip_code_) !== '') {
            ?>
            <p><b><?php esc_html_e('Zip Code', 'kirilkirkov-pdf-invoice-manager' ) ?></b> <?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_company_zip_code_)); ?></p>
            <?php } ?>

            <?php 
                $kirilkirkovwipdf_company_accountable_person__ = get_option( $Config::INPUTS_PREFIX.'company_accountable_person' );
                if(trim($kirilkirkovwipdf_company_accountable_person__) !== '') {
            ?>
            <p><b><?php esc_html_e('Accountable Person', 'kirilkirkov-pdf-invoice-manager' ) ?></b> <?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_company_accountable_person__)); ?></p>
            <?php } ?>
        </div>
        <div class="clearfix"></div>

    </div> 
    <div class="row-invoice-info">
        <?php
        $kirilkirkovwipdf_to_inv = false;
        // comes translated
        $kirilkirkovwipdf_get_invoice_types = $Config::get_invoice_types();
        if ($invoice['invoice_type'] === 'invoice') {
            $kirilkirkovwipdf_inv_type = $kirilkirkovwipdf_get_invoice_types['invoice'];
        }
        if ($invoice['invoice_type'] === 'pro_forma') {
            $kirilkirkovwipdf_inv_type = $kirilkirkovwipdf_get_invoice_types['pro_forma'];
        }
        if ($invoice['invoice_type'] === 'debit_note') {
            $kirilkirkovwipdf_to_inv = true;
            $kirilkirkovwipdf_inv_type = $kirilkirkovwipdf_get_invoice_types['debit_note'];
        }
        if ($invoice['invoice_type'] === 'credit_note') {
            $kirilkirkovwipdf_to_inv = true;
            $kirilkirkovwipdf_inv_type = $kirilkirkovwipdf_get_invoice_types['credit_note'];
        }
        ?>
        <span class="invoice-type"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_inv_type)); ?></span>
        <div class="info">
            <table>

                <?php if ($kirilkirkovwipdf_to_inv === true) { ?>
                    <tr>
                        <td class="head-td">
                            <?php esc_html_e('To invoice', 'kirilkirkov-pdf-invoice-manager' ) ?>
                        </td>
                        <td>
                            <b><?php echo esc_html(sanitize_text_field($invoice['to_invoice_number'])); ?></b>
                        </td>
                    </tr> 
                    <tr>
                        <td class="head-td">
                            <?php esc_html_e('From Date', 'kirilkirkov-pdf-invoice-manager' ) ?>
                        </td>
                        <td>
                            <b><?php echo esc_html(sanitize_text_field($invoice['from_date'])); ?></b>
                        </td>
                    </tr>
                <?php } ?>

                <tr>
                    <td class="head-td">
                        <?php esc_html_e('Number', 'kirilkirkov-pdf-invoice-manager' ) ?>
                    </td>
                    <td>
                        <b><?php echo esc_html(sanitize_text_field($invoice['invoice_number'])); ?></b>
                    </td>
                </tr>
                <tr>
                    <td class="head-td">
                        <?php esc_html_e('Invoice Date', 'kirilkirkov-pdf-invoice-manager' ) ?>
                    </td>
                    <td>
                        <?php echo esc_html(sanitize_text_field($invoice['invoice_date'])); ?>
                    </td>
                </tr>
                <?php if(trim($invoice['due_date']) !== '') { ?>
                <tr>
                    <td class="head-td">
                        <?php esc_html_e('Due Date', 'kirilkirkov-pdf-invoice-manager' ) ?>
                    </td>
                    <td>
                        <?php echo esc_html(sanitize_text_field($invoice['due_date'])); ?>
                    </td>
                </tr>
                <?php }
                if(trim($invoice['date_tax_event']) !== '') {
                ?>
                <tr>
                    <td class="head-td">
                        <?php esc_html_e('Date Tax Event', 'kirilkirkov-pdf-invoice-manager' ) ?>
                    </td>
                    <td>
                        <?php echo esc_html(sanitize_text_field($invoice['date_tax_event'])); ?>
                    </td>
                </tr>
                <?php } ?>
                <tr>
                    <td class="head-td">
                        <?php esc_html_e('Amount', 'kirilkirkov-pdf-invoice-manager' ) ?>
                    </td>
                    <td>
                        <?php 
                            if($Config::is_string_float($invoice['final_total'])) {
                                echo esc_html($Config::get_amount_currency_placement(number_format($invoice['final_total'], $kirilkirkovwipdf_round_to_decimals, $kirilkirkovwipdf_numbers_format_system_[0], $kirilkirkovwipdf_numbers_format_system_[1]), $invoice['currency']));
                            } else {
                                echo esc_html($Config::get_amount_currency_placement($invoice['final_total'], $invoice['currency']));
                            }
                        ?>
                    </td>
                </tr>
            </table>

            <div class="client-info">
                <p><b><?php echo esc_html(sanitize_text_field($invoice['client']['client'])); ?></b></p>
                <?php if ((int)$invoice['client']['is_individual'] > 0) { ?>
                    <?php echo esc_html(sanitize_text_field($invoice['client']['identification_number'])); ?>
                <?php } if ((int)$invoice['client']['has_vat_registration'] === 1) { ?>
                    <p><?php echo esc_html(sanitize_text_field($invoice['client']['vat_number'])); ?></p>
                <?php } ?>
                <p><?php echo esc_html(sanitize_text_field($invoice['client']['address'])); ?></p>
                <p><?php echo esc_html(sanitize_text_field($invoice['client']['city'])); ?></p>
                <p><?php echo esc_html(sanitize_text_field($invoice['client']['country'])); ?></p>
                <p><?php echo esc_html(sanitize_text_field($invoice['client']['zip_code'])); ?></p>
                <?php if ((int)$invoice['client']['is_individual'] === 0 && trim(esc_html($invoice['client']['accountable_person'])) !== '') { ?>
                    <p><b><?php esc_html_e('Accountable person', 'kirilkirkov-pdf-invoice-manager' ) ?></b> <?php echo esc_html($invoice['client']['accountable_person']); ?></p> 
                <?php } ?>
            </div>
            <div class="origin">
                <?php echo esc_html(sanitize_text_field($inv_dwn_type)); ?>
            </div>
            <div class="clearfix"></div>
        </div>
    </div>

    <table class="table table-striped items-table">
        <thead>
            <tr>
                <th>#</th>
                <th><?php esc_html_e('Product Description', 'kirilkirkov-pdf-invoice-manager' ) ?></th>
                <th><?php esc_html_e('Qty', 'kirilkirkov-pdf-invoice-manager' ) ?></th>
                <th class="text-right"><?php esc_html_e('Unit Price', 'kirilkirkov-pdf-invoice-manager' ) ?></th>
                <th class="text-right"><?php esc_html_e('Amount', 'kirilkirkov-pdf-invoice-manager' ) ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            $kirilkirkovwipdf_loop = 0;
            while($kirilkirkovwipdf_loop <= (count($invoice['items']['item_from_list']) - 1)) { ?>
                <tr>
                    <td><?php echo esc_html($kirilkirkovwipdf_loop + 1); ?></td>
                    <td class="item-name"><?php echo esc_html(sanitize_text_field($invoice['items']['items_names'][$kirilkirkovwipdf_loop])); ?></td>
                    <td><?php echo esc_html(sanitize_text_field($invoice['items']['items_quantities'][$kirilkirkovwipdf_loop])); ?> <?php echo esc_html(sanitize_text_field($invoice['items']['items_quantity_types'][$kirilkirkovwipdf_loop])); ?></td>
                    <td class="text-right">
                        <?php 
                            if($Config::is_string_float($invoice['items']['items_prices'][$kirilkirkovwipdf_loop])) {
                                echo esc_html($Config::get_amount_currency_placement(number_format($invoice['items']['items_prices'][$kirilkirkovwipdf_loop], $kirilkirkovwipdf_round_to_decimals, $kirilkirkovwipdf_numbers_format_system_[0], $kirilkirkovwipdf_numbers_format_system_[1]), $invoice['currency']));
                            } else {
                                echo esc_html($Config::get_amount_currency_placement($invoice['items']['items_prices'][$kirilkirkovwipdf_loop], $invoice['currency']));
                            }
                        ?>
                    </td>
                    <td class="text-right">
                        <?php 
                            if($Config::is_string_float($invoice['items']['items_totals'][$kirilkirkovwipdf_loop])) {
                                echo esc_html($Config::get_amount_currency_placement(number_format($invoice['items']['items_totals'][$kirilkirkovwipdf_loop], $kirilkirkovwipdf_round_to_decimals, $kirilkirkovwipdf_numbers_format_system_[0], $kirilkirkovwipdf_numbers_format_system_[1]), $invoice['currency']));
                            } else {
                                echo esc_html($Config::get_amount_currency_placement($invoice['items']['items_totals'][$kirilkirkovwipdf_loop], $invoice['currency']));
                            }
                        ?>
                    </td>
                </tr> 
            <?php $kirilkirkovwipdf_loop++; } ?>
        </tbody>
    </table>
    
    <div class="invoice-payments">
        <div class="invoice-payment">
            <p><?php esc_html_e('Payment Type:', 'kirilkirkov-pdf-invoice-manager' ) ?></p>
            <p><?php echo esc_html(sanitize_text_field($invoice['payment_method'])); ?></p>
        </div>

        <div class="invoice-totals">
            <table>
                <tr>
                    <td class="info">
                        <?php esc_html_e('Subtotal:', 'kirilkirkov-pdf-invoice-manager' ) ?>
                    </td>
                    <td class="inv-amounts">
                        <?php 
                            if($Config::is_string_float($invoice['items_total'])) {
                                echo esc_html($Config::get_amount_currency_placement(number_format($invoice['items_total'], $kirilkirkovwipdf_round_to_decimals, $kirilkirkovwipdf_numbers_format_system_[0], $kirilkirkovwipdf_numbers_format_system_[1]), $invoice['currency']));
                            } else {
                                echo esc_html($Config::get_amount_currency_placement($invoice['items_total'], $invoice['currency']));
                            }
                        ?>
                    </td>
                </tr>

                <?php if((float)$invoice['discount'] > 0) { ?>
                <tr>
                    <td class="info">
                        <?php esc_html_e('Discount:', 'kirilkirkov-pdf-invoice-manager' ) ?>
                    </td>
                    <td class="inv-amounts">
                        <?php 
                            $kirilkirkovwipdf_discount__ = '';
                            if($invoice['discount_type'] === 'percent') {
                                $kirilkirkovwipdf_discount__ = $invoice['discount'];
                            } else {
                                if($Config::is_string_float($invoice['discount'])) {
                                    $kirilkirkovwipdf_discount__ = number_format($invoice['discount'], $kirilkirkovwipdf_round_to_decimals, $kirilkirkovwipdf_numbers_format_system_[0], $kirilkirkovwipdf_numbers_format_system_[1]);
                                } else {
                                    $kirilkirkovwipdf_discount__ = $invoice['discount'];
                                }
                            }
                        ?>
                        <?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_discount__)); ?> 
                        <?php echo $invoice['discount_type'] === 'percent' ? '%' : esc_html(sanitize_text_field($invoice['discount_type'])); ?>
                    </td>
                </tr>
                <?php } if(trim($invoice['tax_base']) != '') { ?>
                <tr>
                    <td class="info">
                        <?php esc_html_e('Tax base:', 'kirilkirkov-pdf-invoice-manager' ) ?>
                    </td>
                    <td class="inv-amounts">
                        <?php 
                            if($Config::is_string_float($invoice['tax_base'])) {
                                echo esc_html($Config::get_amount_currency_placement(number_format($invoice['tax_base'], $kirilkirkovwipdf_round_to_decimals, $kirilkirkovwipdf_numbers_format_system_[0], $kirilkirkovwipdf_numbers_format_system_[1]), $invoice['currency']));
                            } else {
                                echo esc_html($Config::get_amount_currency_placement($invoice['tax_base'], $invoice['currency']));
                            }
                        ?>
                    </td>
                </tr>
                <?php } ?>
                <?php if ((int)$invoice['no_vat'] === 1) { ?>
                    <tr>
                        <td class="info">
                            <?php esc_html_e('No vat:', 'kirilkirkov-pdf-invoice-manager' ) ?>
                        </td>
                        <td>
                            <?php echo esc_html(sanitize_text_field($invoice['no_vat_reason'])); ?>
                        </td>
                    </tr>
                <?php } else {
                    if(trim($invoice['vat_sum']) != '') {
                    ?>
                    <tr>
                        <td class="info">
                            <?php esc_html_e('Vat percent:', 'kirilkirkov-pdf-invoice-manager' ) ?>  -  <?php echo esc_html($invoice['vat_percent']); ?>%
                        </td>
                        <td class="inv-amounts">
                            <?php 
                                if($Config::is_string_float($invoice['vat_sum'])) {
                                    echo esc_html($Config::get_amount_currency_placement(number_format($invoice['vat_sum'], $kirilkirkovwipdf_round_to_decimals, $kirilkirkovwipdf_numbers_format_system_[0], $kirilkirkovwipdf_numbers_format_system_[1]), $invoice['currency']));
                                } else {
                                    echo esc_html($Config::get_amount_currency_placement($invoice['vat_sum'], $invoice['currency']));
                                }
                            ?>
                        </td>
                    </tr>
                <?php } 
                 }
                 ?>
                <tr class="final-total">
                    <td class="info">
                        <?php esc_html_e('Final Total:', 'kirilkirkov-pdf-invoice-manager' ) ?> 
                    </td>
                    <td class="total-price">
                        <?php 
                            if($Config::is_string_float($invoice['final_total'])) {
                                echo esc_html($Config::get_amount_currency_placement(number_format($invoice['final_total'], $kirilkirkovwipdf_round_to_decimals, $kirilkirkovwipdf_numbers_format_system_[0], $kirilkirkovwipdf_numbers_format_system_[1]), $invoice['currency']));
                            } else {
                                echo esc_html($Config::get_amount_currency_placement($invoice['final_total'], $invoice['currency']));
                            }
                        ?>
                    </td>
                </tr>
            </table>
        </div>
        <div class="clearfix"></div>
    </div>

    <div class="recipient">
        
        <?php if(((int)$invoice['client']['is_individual'] === 0 && trim($invoice['client']['beneficiary']) !== '') ||
        ((int)$invoice['client']['is_individual'] > 0 && trim($invoice['client']['client']) !== '')) { ?>
        <p class="rec">
            <b><?php esc_html_e('Beneficiary:', 'kirilkirkov-pdf-invoice-manager' ) ?></b>
            <?php if ((int)$invoice['client']['is_individual'] === 0) { ?>
                <?php echo esc_html(sanitize_text_field($invoice['client']['beneficiary'])); ?>
            <?php } else { ?>
                <?php echo esc_html(sanitize_text_field($invoice['client']['client'])); ?>
            <?php } ?>
        </p>
        <?php } ?>
    
        <?php if(trim($invoice['compiled']) !== '') { ?>
        <p class="comp"><?php esc_html_e('Compiled:', 'kirilkirkov-pdf-invoice-manager' ) ?> <?php echo esc_html(sanitize_text_field($invoice['compiled'])); ?></p>
        <?php } ?>
    </div>

    <div class="clearfix"></div>

    <div class="signature">
        <p class="sign"><?php esc_html_e('Signature:', 'kirilkirkov-pdf-invoice-manager' ) ?> ..................</p>
        <?php 
        $kirilkirkovwipdf_show_schiffer_field_in_invoices = (int)get_option($Config::INPUTS_PREFIX.'show_schiffer_field_in_invoices');

        if(trim($invoice['schiffer']) !== '' && $kirilkirkovwipdf_show_schiffer_field_in_invoices > 0) { ?>
        <p class="cipher"><?php esc_html_e('Schiffer:', 'kirilkirkov-pdf-invoice-manager' ) ?> <?php echo esc_html(sanitize_text_field($invoice['schiffer'])); ?></p>
        <?php } ?>
    </div>

    <div class="clearfix"></div>
    <?php if(trim($invoice['remarks']) !== '') { ?>
    <div class="remarks">
        <p><?php esc_html_e('Notes:', 'kirilkirkov-pdf-invoice-manager' ) ?></p>
        <p><?php echo nl2br(esc_html(sanitize_text_field($invoice['remarks']))); ?></p>
    </div>
    <?php } ?>
</div>