<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<div class="mb-4 flex flex-wrap items-center justify-between invoices-types--menu p-2 wp-filter <?php 
// No nonce verification needed here – just reading if is edit for hiding the menu.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
echo isset($_GET['edit']) ? 'hidden' : ''; ?>">
    <div class="flex flex-wrap items-center">
        <label><b><?php esc_html_e( 'Invoice Type:', 'kirilkirkov-pdf-invoice-manager' ); ?></b></label>

        <div class="px-2">
            <?php 
                $kirilkirkovwipdf_inv_types__ = $Config::get_invoice_types();
                foreach($kirilkirkovwipdf_inv_types__ as $kirilkirkovwipdf_inv_k => $kirilkirkovwipdf_inv_v) {
                    $kirilkirkovwipdf_selected = false;
                    if((isset($invoice[$Config::INPUTS_PREFIX.'invoice_type']) 
                        && $invoice[$Config::INPUTS_PREFIX.'invoice_type'] === $kirilkirkovwipdf_inv_k) 
                        || (!isset($invoice[$Config::INPUTS_PREFIX.'invoice_type']) 
                        && $kirilkirkovwipdf_inv_k === 'invoice')
                        || (isset($invoice[$Config::INPUTS_PREFIX.'invoice_type']) && 
                        !isset($kirilkirkovwipdf_inv_types__[$invoice[$Config::INPUTS_PREFIX.'invoice_type']])
                        && $kirilkirkovwipdf_inv_k === 'invoice')) {

                        $kirilkirkovwipdf_selected = true; 
            ?>
                <input type="hidden" name="<?php echo esc_attr($Config::INPUTS_PREFIX.'invoice_type'); ?>" value="<?php echo esc_attr(sanitize_text_field($kirilkirkovwipdf_inv_k)); ?>">
            <?php }
            ?>
                <a href="<?php echo esc_url(admin_url('admin.php?page=' . $Config::SUB_MENU_CREATE_INVOICE_PAGE_SLUG . '&invoice_type=' . $kirilkirkovwipdf_inv_k )); ?>" class="button media-button px-2 <?php echo esc_attr($kirilkirkovwipdf_selected === true ? 'active' : ''); ?>"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_inv_v)); ?></a>
            <?php
            } ?>
        </div>
        <p class="text-error m-0"><?php echo isset($errors[$Config::INPUTS_PREFIX.'invoice_type']) ? esc_html(sanitize_text_field($errors[$Config::INPUTS_PREFIX.'invoice_type'])) : ''; ?></p>
    </div>
    
    <div class="flex items-center">
        <?php 
        // TOGGLE OPTIONS BUTTONS FOR WOOCOMMERCE
        if($Woo->hasActiveWoocommerce()) { ?>
            <a href="javascript:;" class="button media-button px-2 flex items-center mx-1 kkwi-create-inv-toggle-woo-options">
                <?php esc_html_e( 'WooCommerce', 'kirilkirkov-pdf-invoice-manager' ); ?>
                <span class="dashicons dashicons-arrow-down"></span>
            </a>
        <?php } ?>
        <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>
</div>

<?php 
// WOOCOMMERCE OPTIONS
if($Woo->hasActiveWoocommerce()) {
// No nonce verification needed here – just reading if is valid Woo order id.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$kirilkirkovwipdf_get_data_wc_order_id = isset($_GET[$Config::INPUTS_PREFIX.'get_data_wc_order_id']) && trim(sanitize_text_field(wp_unslash($_GET[$Config::INPUTS_PREFIX.'get_data_wc_order_id']))) != '' ? trim(sanitize_text_field(wp_unslash($_GET[$Config::INPUTS_PREFIX.'get_data_wc_order_id']))) : null;    
?>
<div class="kkwi-create-inv-woo-options <?php echo $kirilkirkovwipdf_get_data_wc_order_id || isset($errors['wc_id']) ? '' : 'none' ?>">
    <div class="bg-white p-2 border-wp-color border-1 mb-4 text-label-color">
        <div class="mb-4"><b><?php esc_html_e( 'WooCommerce:', 'kirilkirkov-pdf-invoice-manager' ); ?></b></div>
        
        <div class="flex items-center mb-2">
            <label class="mr-2"><?php esc_html_e( 'Order ID:', 'kirilkirkov-pdf-invoice-manager' ); ?></label>
            <input type="text" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'get_data_wc_order_id'; ?>" value="<?php 
            // No nonce verification needed here – just reading the get_data_wc_order_id for prefilling the input.
            // phpcs:ignore WordPress.Security.NonceVerification.Recommended
            echo $kirilkirkovwipdf_get_data_wc_order_id ? esc_attr(sanitize_text_field(wp_unslash($_GET[$Config::INPUTS_PREFIX.'get_data_wc_order_id']))) : '' ?>" class="max-w-42 kkwi-create-inv-woo-options-get-data-input" />
            <button type="button" class="button media-button px-2 mx-2 kkwi-create-inv-woo-options-get-data-button"><?php esc_html_e( 'Get Data', 'kirilkirkov-pdf-invoice-manager' ); ?></button>
        </div>
        <?php if($kirilkirkovwipdf_get_data_wc_order_id && !$isValidGetDataWcOrder) { ?>
            <p class="text-error m-0 mb-4"><?php esc_html_e( 'Invalid WooCommerce Order ID', 'kirilkirkov-pdf-invoice-manager' ); ?></p>
        <?php } ?>
        <p class="text-error m-0 mb-4"><?php echo isset($errors['wc_id']) ? esc_html(sanitize_text_field($errors['wc_id'])) : ''; ?></p>

        <div class="flex items-center">
            <input type="checkbox" name="<?php echo esc_attr($Config::INPUTS_PREFIX.'attach_to_wc_order'); ?>" id="<?php echo esc_attr($Config::INPUTS_PREFIX.'attach_to_wc_order'); ?>" value="1" />
            <label for="<?php echo esc_attr($Config::INPUTS_PREFIX.'attach_to_wc_order'); ?>" class="ml-2"><?php esc_html_e( 'Attach invoice to WooCommerce order', 'kirilkirkov-pdf-invoice-manager' ); ?></label>
        </div>
    </div>
</div>
<?php } ?>