<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<div id="kirilkirkov" class="wrap">
    <div class="create-invoice-page mx-auto">
        <div class="p-0 md:p-20">

            <form method="post" action="">
                <?php wp_nonce_field('kkwi_create_invoice_action', 'kkwi_create_invoice_nonce'); ?>

                <input type="hidden" name="id" value="<?php 
                // No nonce verification needed here – just reading the id for editing.
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended
                echo isset($_GET['edit']) ? esc_attr((int)$_GET['edit']) : 0; ?>
                ">

                <?php 
                    require 'Create/SubmitMessages.php';
                    // No nonce verification needed here – just reading if is edit for showing preview link.
                    // phpcs:ignore WordPress.Security.NonceVerification.Recommended 
                    if(isset($_GET['edit'])) { ?>
                    <div class="bg-white p-2 border-wp-color border-1 mb-4">
                        <a href="<?php 
                            // No nonce verification needed here – just reading if is edit for showing preview link.
                            // phpcs:ignore WordPress.Security.NonceVerification.Recommended 
                            echo esc_url(admin_url( 'admin.php?page=' . $Config::SUB_MENU_PREVIEW_INVOICE_PAGE_SLUG . '&view=' . sanitize_text_field(wp_unslash($_GET['edit'])) )) ?>" class="button media-button">
                            <?php esc_html_e('Preview', 'kirilkirkov-pdf-invoice-manager' ) ?>
                        </a>
                        <a href="<?php echo esc_url(admin_url('options-general.php?page=' . $Config::SETTINGS_GET_PARAM)); ?>" class="button media-button px-2"><?php esc_html_e( 'Settings', 'kirilkirkov-pdf-invoice-manager' ); ?></a>
                    </div>
                <?php } 
                    // has class HIDDEN if edit is set
                    require 'Create/InvoiceTypes.php';
                ?>
                
                <div class="invoice">
                    <h1 class="mb-4">
                        <?php
                            // current invoice type header
                            $kirilkirkovwipdf_inv_types = $Config::get_invoice_types();
                            if(isset($invoice[$Config::INPUTS_PREFIX.'invoice_type']) && isset($kirilkirkovwipdf_inv_types[$invoice[$Config::INPUTS_PREFIX.'invoice_type']])) {
                                echo esc_html($kirilkirkovwipdf_inv_types[$invoice[$Config::INPUTS_PREFIX.'invoice_type']]);
                            } else {
                                esc_html_e( 'Invoice', 'kirilkirkov-pdf-invoice-manager' );
                            }
                        ?>
                    </h1>

                    <div class="flex flex-wrap">
                        <div class="w-full md:w-1/2">

                            <?php
                                // show next two inputs only for two types of invoice
                                $kirilkirkovwipdf_in_types_fields = array(
                                    'debit_note',
                                    'credit_note',
                                );
                            ?>
                            <!-- Only debit and credit types -->
                            <div class="mb-4 <?php echo isset($invoice[$Config::INPUTS_PREFIX.'invoice_type']) && in_array($invoice[$Config::INPUTS_PREFIX.'invoice_type'], $kirilkirkovwipdf_in_types_fields) ? '' : 'hidden'; ?> debit-or-credit-type">
                                <div><label><b><?php esc_html_e( 'To invoice number:', 'kirilkirkov-pdf-invoice-manager' ); ?></b></label></div>
                                <input type="text" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'to_invoice_number'; ?>" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'to_invoice_number']) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'to_invoice_number'])) : ''; ?>" />
                            </div>
                            <div class="mb-4 <?php echo isset($invoice[$Config::INPUTS_PREFIX.'invoice_type']) && in_array($invoice[$Config::INPUTS_PREFIX.'invoice_type'], $kirilkirkovwipdf_in_types_fields) ? '' : 'hidden'; ?> debit-or-credit-type">
                                <div><label><b><?php esc_html_e( 'From date:', 'kirilkirkov-pdf-invoice-manager' ); ?></b></label></div>
                                <input class="datepicker" type="text" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'from_date'; ?>" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'from_date']) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'from_date'])) : ''; ?>" />
                                <p class="text-error m-0"><?php echo isset($errors[$Config::INPUTS_PREFIX.'from_date']) ? esc_html(sanitize_text_field($errors[$Config::INPUTS_PREFIX.'from_date'])) : ''; ?></p>
                            </div>
                            
                            <?php
                                // client data fields 
                                require 'Create/Client.php'; 
                            ?>
                            
                        </div>
                        <div class="w-full md:w-1/2 flex justify-start md:justify-right">
                            <div class="inv">
                                <div class="mb-4">
                                    <div><label><b class="inv-type-number"><?php esc_html_e( 'Invoice', 'kirilkirkov-pdf-invoice-manager' ); ?></b> <b><?php esc_html_e( '№:', 'kirilkirkov-pdf-invoice-manager' ); ?></b></label></div>
                                    <input type="text" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'invoice_number'; ?>" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'invoice_number']) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'invoice_number'])) : esc_attr(sanitize_text_field($next_inv_number)); ?>" />
                                    <p class="text-error m-0"><?php echo isset($errors[$Config::INPUTS_PREFIX.'invoice_number']) ? esc_html(sanitize_text_field($errors[$Config::INPUTS_PREFIX.'invoice_number'])) : ''; ?></p>
                                </div>
                                <div class="mb-4">
                                    <div><label><b><?php esc_html_e( 'Invoice date:', 'kirilkirkov-pdf-invoice-manager' ); ?></b></label></div>
                                    <input type="text" class="datepicker" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'invoice_date'; ?>" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'invoice_date']) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'invoice_date'])) : esc_attr(sanitize_text_field($today_date)); ?>" />
                                    <p class="text-error m-0"><?php echo isset($errors[$Config::INPUTS_PREFIX.'invoice_date']) ? esc_html(sanitize_text_field($errors[$Config::INPUTS_PREFIX.'invoice_date'])) : ''; ?></p>
                                </div>
                                <div class="mb-4">
                                    <div><label><b><?php esc_html_e( 'A date of a tax event:', 'kirilkirkov-pdf-invoice-manager' ); ?></b></label></div>
                                    <input type="text" class="datepicker" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'date_tax_event'; ?>" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'date_tax_event']) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'date_tax_event'])) : esc_attr(sanitize_text_field($today_date)); ?>" />
                                    <p class="text-error m-0"><?php echo isset($errors[$Config::INPUTS_PREFIX.'date_tax_event']) ? esc_html(sanitize_text_field($errors[$Config::INPUTS_PREFIX.'date_tax_event'])) : ''; ?></p>
                                </div>
                                <div class="mb-4">
                                    <div><label><b><?php esc_html_e( 'Due Date:', 'kirilkirkov-pdf-invoice-manager' ); ?></b></label></div>
                                    <input type="text" class="datepicker" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'due_date'; ?>" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'due_date']) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'due_date'])) : ''; ?>" />
                                    <p class="text-error m-0"><?php echo isset($errors[$Config::INPUTS_PREFIX.'due_date']) ? esc_html(sanitize_text_field($errors[$Config::INPUTS_PREFIX.'due_date'])) : ''; ?></p>
                                </div>
                                <div class="mb-4">
                                    <label>
                                        <input type="hidden" value="0" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'has_maturity'; ?>">
                                        <input type="checkbox" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'has_maturity'; ?>" <?php echo isset($invoice[$Config::INPUTS_PREFIX.'has_maturity']) && (int)$invoice[$Config::INPUTS_PREFIX.'has_maturity'] > 0 ? 'checked' : ''; ?> class="maturity-date" value="1">
                                        <?php esc_html_e( 'Indicate a maturity date', 'kirilkirkov-pdf-invoice-manager' ); ?>
                                    </label>
                                </div>
                                <div class="mb-4 <?php echo isset($invoice[$Config::INPUTS_PREFIX.'has_maturity']) && (int)$invoice[$Config::INPUTS_PREFIX.'has_maturity'] > 0 ? '' : 'hidden'; ?> has-maturity-date">
                                    <div><label><b><?php esc_html_e( 'Maturity date:', 'kirilkirkov-pdf-invoice-manager' ); ?></b></label></div>
                                    <input type="text" class="datepicker" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'maturity_date'; ?>" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'maturity_date']) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'maturity_date'])) : ''; ?>" />
                                </div>
                                <p class="text-error m-0 mb-4"><?php echo isset($errors[$Config::INPUTS_PREFIX.'maturity_date']) ? esc_html(sanitize_text_field($errors[$Config::INPUTS_PREFIX.'maturity_date'])) : ''; ?></p>
                                <div class="mb-4">
                                    <label>
                                        <input type="hidden" value="0" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'cash_accounting'; ?>">
                                        <input type="checkbox" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'cash_accounting'; ?>" value="1" <?php echo isset($invoice[$Config::INPUTS_PREFIX.'cash_accounting']) && (int)$invoice[$Config::INPUTS_PREFIX.'cash_accounting'] > 0 ? 'checked' : ''; ?>>
                                        <?php esc_html_e( 'Cash accounting', 'kirilkirkov-pdf-invoice-manager' ); ?>
                                    </label>
                                </div>
                                <div class="mb-4">
                                    <?php 
                                        $kirilkirkovwipdf_current_user_name = '';

                                        $kirilkirkovwipdf_disable_auto_fill_compiled_field = (int)get_option($Config::INPUTS_PREFIX.'disable_auto_fill_compiled_field');
                                        if(!$kirilkirkovwipdf_disable_auto_fill_compiled_field || $kirilkirkovwipdf_disable_auto_fill_compiled_field < 1) {
                                            global $current_user;
                                            $kirilkirkovwipdf_current_user_name = $current_user->display_name;
                                        }
                                    ?>
                                    <div><label><b><?php esc_html_e( 'Compiled from:', 'kirilkirkov-pdf-invoice-manager' ); ?></b></label></div>
                                    <input type="text" name="<?php echo esc_attr(sanitize_text_field($Config::INPUTS_PREFIX).'compiled'); ?>" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'compiled']) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'compiled'])) : esc_attr(sanitize_text_field($kirilkirkovwipdf_current_user_name)); ?>" />
                                </div>

                                <?php 
                                    $kirilkirkovwipdf_show_schiffer_field_in_invoices = (int)get_option($Config::INPUTS_PREFIX.'show_schiffer_field_in_invoices');
                                ?>
                                <div class="mb-4 <?php echo $kirilkirkovwipdf_show_schiffer_field_in_invoices > 0 ? '' : 'hidden'; ?>">
                                    <div><label><b><?php esc_html_e( 'Schiffer (replaces signature):', 'kirilkirkov-pdf-invoice-manager' ); ?></b></label></div>
                                    <input type="text" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'schiffer'; ?>" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'schiffer']) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'schiffer'])) : ''; ?>" />
                                </div>

                                <div class="attach-inv-to-user-from-list">
                                    <?php 
                                    $kirilkirkovwipdf_customer_name_value = '';
                                    if(isset($invoice[$Config::INPUTS_PREFIX.'customer_user_id']) && (int)$invoice[$Config::INPUTS_PREFIX.'customer_user_id'] > 0 ) {
                                        $kirilkirkovwipdf_customer_obj = get_user_by('id', (int)$invoice[$Config::INPUTS_PREFIX.'customer_user_id']);
                                        if($kirilkirkovwipdf_customer_obj !== false) {
                                            $kirilkirkovwipdf_customer_name_value = $kirilkirkovwipdf_customer_obj->user_login . "({$kirilkirkovwipdf_customer_obj->user_email})";
                                        }
                                    }
                                    ?>
                                    <input type="hidden" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'customer_user_id'; ?>" class="hidden-customer_user_id" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'customer_user_id']) && (int)$invoice[$Config::INPUTS_PREFIX.'customer_user_id'] > 0 ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'customer_user_id'])) : ''; ?>">
                                    <div><label><b><?php esc_html_e( 'Assign to user:', 'kirilkirkov-pdf-invoice-manager' ); ?></b></label></div>
                                    <input type="text" readonly name="<?php echo esc_attr($Config::INPUTS_PREFIX).'raw_customer_names'; ?>" value="<?php echo esc_attr(sanitize_text_field($kirilkirkovwipdf_customer_name_value)); ?>" class="visible-customer_user_id" />
                                    <p>
                                        <?php esc_html_e( 'The invoice will appear in the user\'s invoice list if you use a shortcode.', 'kirilkirkov-pdf-invoice-manager' ); ?>
                                    </p>
                                </div>

                            </div>
                        </div>
                    </div>

                    <div class="currency-select flex items-center mb-4">
                        <label class="mr-2">
                            <?php esc_html_e( 'Currency:', 'kirilkirkov-pdf-invoice-manager' ); ?>
                        </label>
                        <select class="select-main-currency" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'currency'; ?>">
                            <?php 
                                $kirilkirkovwipdf_currency = isset($invoice[$Config::INPUTS_PREFIX.'currency']) ? $invoice[$Config::INPUTS_PREFIX.'currency'] : '';

                                if($currencies && is_array($currencies) && count($currencies)) {
                                    $kirilkirkovwipdf_has_selection = false;
                                    foreach($currencies as $kirilkirkovwipdf_c) {
                                        $kirilkirkovwipdf_selected = '';
                                        if($kirilkirkovwipdf_c === $kirilkirkovwipdf_currency) {
                                            $kirilkirkovwipdf_selected = 'selected="selected"';
                                            $kirilkirkovwipdf_has_selection = true;
                                        }
                            ?>
                                    <option <?php selected($kirilkirkovwipdf_currency, $kirilkirkovwipdf_c); ?> value="<?php echo esc_attr(sanitize_text_field($kirilkirkovwipdf_c)); ?>"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_c)); ?></option>
                            <?php   }
                                }
                                // if this currency is not in the list
                                if($kirilkirkovwipdf_has_selection === false && $kirilkirkovwipdf_currency !== '') {
                            ?>
                                    <option selected="selected" value="<?php echo esc_attr(sanitize_text_field($kirilkirkovwipdf_currency)); ?>"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_currency)); ?></option>
                            <?php } ?>
                        </select>
                    </div>

                    <!-- Items -->
                    <div class="table-invoice-items-wapper">
                        <table class="table table-invoice-items">
                            <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><?php esc_html_e( 'Unit Price', 'kirilkirkov-pdf-invoice-manager' ); ?></th>
                                    <th class="text-right--important"><?php esc_html_e( 'Amount:', 'kirilkirkov-pdf-invoice-manager' ); ?></th>
                                </tr>
                            </thead>
                            <tbody class="body-items">

                                <?php
                                    // items foreach
                                    require 'Create/Items.php'; 
                                ?>

                            </tbody>
                        </table>
                    </div>

                    <p class="text-error m-0 mb-4"><?php echo isset($errors['items_quantity']) ? esc_html(sanitize_text_field($errors['items_quantity'])) : ''; ?></p>
                    <p class="text-error m-0 mb-4"><?php echo isset($errors['items_price']) ? esc_html(sanitize_text_field($errors['items_price'])) : ''; ?></p>
                    <p class="text-error m-0 mb-4"><?php echo isset($errors[$Config::INPUTS_PREFIX . 'items']) ? esc_html(sanitize_text_field($errors[$Config::INPUTS_PREFIX . 'items'])) : ''; ?></p>

                    <div class="mb-4">
                        <a href="javascript:void(0);" class="add-new-item">
                            <span class="dashicons dashicons-plus"></span>
                            <?php esc_html_e( 'Add Item', 'kirilkirkov-pdf-invoice-manager' ); ?>
                        </a>
                    </div>

                    <div class="flex flex-wrap mb-4">
                        <div class="md:w-1/2"></div>
                        <div class="w-full md:w-1/2">
                            <div class="flex flex-wrap mb-4">
                                <div class="w-1/2 flex items-center">
                                    <?php esc_html_e( 'Invoice amount', 'kirilkirkov-pdf-invoice-manager' ); ?>
                                </div>
                                <div class="w-1/2 flex justify-right items-center inv-items-total">
                                    <span class="items-txt-total <?php echo $disabled_js_calc && (int)$disabled_js_calc === 1 ? 'hidden' : ''; ?>"><?php echo isset($invoice[$Config::INPUTS_PREFIX.'items_total']) ? esc_html(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'items_total'])) : '0.00'; ?></span> 
                                    <span class="currency-text"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_selected_currency)); ?></span>
                                    <input type="<?php echo $disabled_js_calc && (int)$disabled_js_calc === 1 ? 'text' : 'hidden'; ?>" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'items_total'; ?>" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'items_total']) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'items_total'])) : '0.00'; ?>" class="items-input-total">
                                </div>
                            </div>

                            <div class="flex flex-wrap mb-4 items-center">
                                <div class="w-1/2">
                                    <?php esc_html_e( 'Discount', 'kirilkirkov-pdf-invoice-manager' ); ?>
                                </div>
                                <div class="w-1/2 flex justify-right">
                                    <?php 
                                        $kirilkirkovwipdf_default_discount = get_option( $Config::INPUTS_PREFIX.'default_discount' );
                                        if(isset($invoice[$Config::INPUTS_PREFIX.'discount'])) {
                                            $kirilkirkovwipdf_default_discount = $invoice[$Config::INPUTS_PREFIX.'discount'];
                                        }
                                        $kirilkirkovwipdf_default_discount_type = get_option($Config::INPUTS_PREFIX.'default_discount_type');
                                        if(isset($invoice[$Config::INPUTS_PREFIX.'discount_type'])) {
                                            $kirilkirkovwipdf_default_discount_type = $invoice[$Config::INPUTS_PREFIX.'discount_type'];
                                        }
                                    ?>
                                    <input class="w-80p discount-input-field calc-field is-nan-validation" value="<?php echo esc_attr(sanitize_text_field($kirilkirkovwipdf_default_discount)); ?>" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'discount'; ?>" type="text">
                                    <select name="<?php echo esc_attr($Config::INPUTS_PREFIX).'discount_type'; ?>" class="w-100p discount-select-type calc-field">
                                        <option <?php echo (!$kirilkirkovwipdf_default_discount_type || $kirilkirkovwipdf_default_discount_type !== 'percent') ? 'selected="selected"' : '' ; ?> value="<?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_selected_currency)); ?>"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_selected_currency)); ?></option>
                                        <option <?php echo ($kirilkirkovwipdf_default_discount_type && $kirilkirkovwipdf_default_discount_type === 'percent') ? 'selected="selected"' : '' ; ?> value="percent">%</option>
                                    </select>
                                </div>
                                <p class="text-error m-0"><?php echo isset($errors[$Config::INPUTS_PREFIX.'discount']) ? esc_html(sanitize_text_field($errors[$Config::INPUTS_PREFIX.'discount'])) : ''; ?></p>
                            </div>

                            <div class="flex flex-wrap mb-4">
                                <div class="w-1/2 flex items-center">
                                    <?php esc_html_e( 'Tax base', 'kirilkirkov-pdf-invoice-manager' ); ?>
                                </div>
                                <div class="w-1/2 flex justify-right items-center">
                                    <span class="tax-txt-base <?php echo $disabled_js_calc && (int)$disabled_js_calc === 1 ? 'hidden' : ''; ?>"><?php echo isset($invoice[$Config::INPUTS_PREFIX.'tax_base']) ? esc_html(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'tax_base'])) : '0.00'; ?></span> 
                                    <span class="currency-text"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_selected_currency)); ?></span>
                                    <input type="<?php echo $disabled_js_calc && (int)$disabled_js_calc === 1 ? 'text' : 'hidden'; ?>" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'tax_base']) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'tax_base'])) : '0.00'; ?>" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'tax_base'; ?>" class="tax-input-base">
                                </div>
                            </div>
                            
                            <?php 
                            // used for below input and reason field
                            $kirilkirkovwipdf_default_no_vat = get_option($Config::INPUTS_PREFIX.'default_no_vat');
                            if(isset($invoice[$Config::INPUTS_PREFIX.'no_vat'])) {
                                $kirilkirkovwipdf_default_no_vat = $invoice[$Config::INPUTS_PREFIX.'no_vat'];
                            }
                            ?>
                            <div class="flex flex-wrap mb-4 items-center <?php echo $kirilkirkovwipdf_default_no_vat && (int)$kirilkirkovwipdf_default_no_vat === 1 ? 'hidden' : ''; ?> no-vat-selected">
                                <div class="w-1/2 flex items-center">
                                    <span class="mr-2"><?php esc_html_e( 'VAT:', 'kirilkirkov-pdf-invoice-manager' ); ?></span>
                                    <?php 
                                        $kirilkirkovwipdf_default_vat_percent = get_option( $Config::INPUTS_PREFIX.'default_vat_percent' );
                                        if(isset($invoice[$Config::INPUTS_PREFIX.'vat_percent'])) {
                                            $kirilkirkovwipdf_default_vat_percent = $invoice[$Config::INPUTS_PREFIX.'vat_percent'];
                                        }                                  
                                    ?>
                                    <input class="w-80p text-right calc-field is-nan-validation vat-percent-input" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'vat_percent'; ?>" type="text" value="<?php echo esc_attr(sanitize_text_field($kirilkirkovwipdf_default_vat_percent)); ?>">
                                    <span class="ml-2">%</span>
                                </div>
                                <p class="text-error m-0"><?php echo isset($errors[$Config::INPUTS_PREFIX.'vat_percent']) ? esc_html(sanitize_text_field($errors[$Config::INPUTS_PREFIX.'vat_percent'])) : ''; ?></p>
                                
                                <div class="w-1/2 flex justify-right items-center">
                                    <span class="vat-txt-field <?php echo $disabled_js_calc && (int)$disabled_js_calc === 1 ? 'hidden' : ''; ?>"><?php echo isset($invoice[$Config::INPUTS_PREFIX.'vat_sum']) ? esc_html(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'vat_sum'])) : '0.00'; ?></span> 
                                    <span class="currency-text"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_selected_currency)); ?></span>
                                    <input type="<?php echo $disabled_js_calc && (int)$disabled_js_calc === 1 ? 'text' : 'hidden'; ?>" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'vat_sum'; ?>" class="vat-input-field" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'vat_sum']) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'vat_sum'])) : '0.00'; ?>">
                                </div>
                            </div>

                            <div class="flex flex-wrap mb-4">
                                <div class="w-1/2 flex items-center">
                                    <label>
                                        <input type="hidden" value="0" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'no_vat'; ?>">
                                        <input type="checkbox" value="1" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'no_vat'; ?>" class="no-vat calc-field" <?php echo $kirilkirkovwipdf_default_no_vat && (int)$kirilkirkovwipdf_default_no_vat === 1 ? 'checked' : ''; ?>>
                                        <?php esc_html_e( 'No Vat', 'kirilkirkov-pdf-invoice-manager' ); ?>
                                    </label>
                                </div>
                                <div class="w-1/2">
                                    <?php
                                        $kirilkirkovwipdf_default_vat_reason = get_option( $Config::INPUTS_PREFIX.'default_vat_reason' );
                                        if(isset($invoice[$Config::INPUTS_PREFIX.'no_vat_reason'])) {
                                            $kirilkirkovwipdf_default_vat_reason = $invoice[$Config::INPUTS_PREFIX.'no_vat_reason'];
                                        }
                                        if(isset($invoice[$Config::INPUTS_PREFIX.'no_vat_reason'])) {
                                            $kirilkirkovwipdf_default_remarks = $invoice[$Config::INPUTS_PREFIX.'no_vat_reason'];
                                        }
                                    ?>
                                    <div class="<?php echo $kirilkirkovwipdf_default_no_vat && (int)$kirilkirkovwipdf_default_no_vat === 1 ? '' : 'hidden'; ?> no-vat-reason">
                                        <div><label><b><?php esc_html_e( 'Reason for non-charging VAT:', 'kirilkirkov-pdf-invoice-manager' ); ?></b></label></div>
                                        <input type="text" value="<?php echo esc_attr(sanitize_text_field($kirilkirkovwipdf_default_vat_reason)); ?>" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'no_vat_reason'; ?>" />
                                    </div>
                                </div>
                            </div>

                            <div class="flex items-center total flex-wrap mb-4">
                                <div class="w-1/2">
                                    <span><?php esc_html_e( 'Total', 'kirilkirkov-pdf-invoice-manager' ); ?></span>
                                </div>
                                <div class="w-1/2 flex items-center justify-right">
                                    <span class="total-txt-field <?php echo $disabled_js_calc && (int)$disabled_js_calc === 1 ? 'hidden' : ''; ?>"><?php echo isset($invoice[$Config::INPUTS_PREFIX.'final_total']) ? esc_html(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'final_total'])) : '0.00'; ?></span> 
                                    <span class="currency-text"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_selected_currency)); ?></span>
                                    <input type="<?php echo $disabled_js_calc && (int)$disabled_js_calc === 1 ? 'text' : 'hidden'; ?>" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'final_total'; ?>" class="total-input-field" value="<?php echo isset($invoice[$Config::INPUTS_PREFIX.'final_total']) ? esc_attr(sanitize_text_field($invoice[$Config::INPUTS_PREFIX.'final_total'])) : '0.00'; ?>">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="mb-4">
                        <?php
                            $kirilkirkovwipdf_default_remarks = get_option( $Config::INPUTS_PREFIX.'default_remarks' );
                            if(isset($invoice[$Config::INPUTS_PREFIX.'remarks'])) {
                                $kirilkirkovwipdf_default_remarks = $invoice[$Config::INPUTS_PREFIX.'remarks'];
                            }
                        ?>
                        <label><?php esc_html_e( 'Notes', 'kirilkirkov-pdf-invoice-manager' ); ?> <sup><?php esc_html_e( 'Visible for client', 'kirilkirkov-pdf-invoice-manager' ); ?></sup></label>
                        <textarea name="<?php echo esc_attr($Config::INPUTS_PREFIX).'remarks'; ?>"><?php echo esc_textarea(sanitize_text_field($kirilkirkovwipdf_default_remarks)); ?></textarea>
                    </div>

                    <div class="payment-method">
                        <label><?php esc_html_e( 'Payment method', 'kirilkirkov-pdf-invoice-manager' ); ?></label>
                        <select name="<?php echo esc_attr($Config::INPUTS_PREFIX).'payment_method'; ?>">
                        <?php 
                            $kirilkirkovwipdf_payment_methods = get_option($Config::INPUTS_PREFIX.'payment_methods');
                            $kirilkirkovwipdf_selected_payment_method = esc_html__('Credit or Debit Card', 'kirilkirkov-pdf-invoice-manager');
                            if($kirilkirkovwipdf_payment_methods && is_array($kirilkirkovwipdf_payment_methods) && count($kirilkirkovwipdf_payment_methods)) {
                                $kirilkirkovwipdf_selected_payment_method = reset($kirilkirkovwipdf_payment_methods);
                            }
                            if(isset($invoice[$Config::INPUTS_PREFIX.'payment_method'])) {
                                $kirilkirkovwipdf_selected_payment_method = $invoice[$Config::INPUTS_PREFIX.'payment_method'];
                            }
                            if($kirilkirkovwipdf_payment_methods && is_array($kirilkirkovwipdf_payment_methods) && count($kirilkirkovwipdf_payment_methods)) {
                                $kirilkirkovwipdf_has_selection = false;
                                foreach($kirilkirkovwipdf_payment_methods as $kirilkirkovwipdf_p) {
                                    $kirilkirkovwipdf_selected = '';
                                    if($kirilkirkovwipdf_selected_payment_method === $p) {
                                        $kirilkirkovwipdf_selected = 'selected="selected"';
                                        $kirilkirkovwipdf_has_selection = true;
                                    }
                        ?>
                                <option <?php selected($kirilkirkovwipdf_selected_payment_method, $kirilkirkovwipdf_p); ?> value="<?php echo esc_attr(sanitize_text_field($kirilkirkovwipdf_p)); ?>"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_p)); ?></option>
                        <?php   }
                            }
                            // if selected value is not in the array
                            if($kirilkirkovwipdf_has_selection === false) {
                        ?>
                            <option selected="selected" value="<?php echo esc_attr(sanitize_text_field($kirilkirkovwipdf_selected_payment_method)); ?>"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_selected_payment_method)); ?></option>
                        <?php } ?>
                        </select>
                    </div>

                </div>
                <div class="bg-white p-2 border-wp-color border-1">
                    <button type="submit">
                        <?php 
                        // No nonce verification needed here – just reading if is edit for showing preview link.
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended 
                        if(isset($_GET['edit'])) { ?>
                            <?php esc_html_e( 'Update', 'kirilkirkov-pdf-invoice-manager' ); ?>
                        <?php } else { ?>
                            <?php esc_html_e( 'Create', 'kirilkirkov-pdf-invoice-manager' ); ?>
                        <?php } ?>
                    </button>
                    <div class="save_periodic_inv">
                        <label>
                            <input type="hidden" value="0" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'save_periodic_inv'; ?>">
                            <input type="checkbox" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'save_periodic_inv'; ?>" 
                            <?php 
                                // No nonce verification needed here – only reading a safe GET param for filtering.
                                // phpcs:ignore WordPress.Security.NonceVerification.Recommended
                                echo (isset($invoice[$Config::INPUTS_PREFIX.'save_periodic_inv']) && (int)$invoice[$Config::INPUTS_PREFIX.'save_periodic_inv'] > 0) || isset($_GET['save_periodic_inv']) ? 'checked' : ''; ?> class="maturity-date" value="1">
                            <?php esc_html_e( 'Issue periodically', 'kirilkirkov-pdf-invoice-manager' ); ?>
                        </label>
                    </div>
                </div>
                
                <div class="mx-auto flex">
                    <div class="w-full md:w-1/2 mt-4 bg-amber-50 p-2 border-wp-color border-1">
                        <label><?php esc_html_e( 'Notes', 'kirilkirkov-pdf-invoice-manager' ); ?> <sup><?php esc_html_e( 'Visible for admin only', 'kirilkirkov-pdf-invoice-manager' ); ?></sup></label>
                        <textarea class="bg-transparent border-0" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'adata[private_remarks]'; ?>"><?php echo $adata && is_array($adata) && isset($adata['private_remarks']) ? esc_textarea($adata['private_remarks']) : ''; ?></textarea>
                    </div>
                </div>
                
            </form>
        </div>

        <?php 
            require 'Create/SelectClientModal.php';
            require 'Create/SelectItemModal.php'; 
            require 'Create/SelectUserModal.php'; 
        ?>

    </div>
</div>