<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<div id="kirilkirkov" class="wrap">
    
    <div class="header p-4 flex items-center space-between">
        <div class="flex items-center">
            <img class="logo" src="<?php echo esc_url(plugins_url('../../Assets/Img/logo.svg', __FILE__ )); ?>" alt="<?php echo esc_attr( 'WP Invoices' ) ?>" />
        </div>
        <h1 class="text-white uppercase text-sm font-lighter p-0">
            <?php esc_html_e('Edit Periodic Invoice Options', 'kirilkirkov-pdf-invoice-manager' );?>
        </h1>
    </div>

    <div class="flex flex-wrap">
        <div class="w-full">
            <div class="section-body bg-white p-4">
                
                <?php if($successEdit === true) { ?>
                <div class="alert alert-success w-full md:w-1/4 mx-auto mb-4">
                    <?php esc_html_e('Periodic Invoice Options Saved', 'kirilkirkov-pdf-invoice-manager' ); ?>
                </div>
                <?php } ?>

                <?php if($successEdit === false) { ?>
                <div class="alert alert-danger w-full md:w-1/4 mx-auto mb-4">
                    <?php esc_html_e('There are errors received', 'kirilkirkov-pdf-invoice-manager' ); ?>
                </div>
                <?php } ?>
                
                <form class="max-w-96 mx-auto p-0 rounded-sm" method="post" action="">
                    <?php wp_nonce_field('periodic_invoices_edit_nonce', 'periodic_invoices_edit_nonce'); ?>
                    
                    <div class="form-row">  
                        <label><?php esc_html_e('Invoice Number', 'kirilkirkov-pdf-invoice-manager' ) ?> *</label>
                        <input type="text" readonly value="<?php echo isset($item['invoice_number']) ? esc_attr(sanitize_text_field($item['invoice_number'])) : ''; ?>" name="<?php echo esc_attr($Config::INPUTS_PREFIX).'name'; ?>">
                    </div>

                    <div class="form-row">
                        <p class="alert alert-info h-auto--important"><?php esc_html_e('Based on the field into the invoice - "Invoice Date", according to the "Reissue Type" you selected it will be reissued on the same day.', 'kirilkirkov-pdf-invoice-manager' ) ?></p>
                        
                        <div class="premium-feature-box-mini flex items-center flex-col justify-center mt-4 mb-2 gap-1">
                            <img class="w-6 h-6" src="<?php echo esc_url(plugins_url('../../Assets/Img/crown.svg', __FILE__ )); ?>" alt="<?php echo esc_attr( 'WP Invoices' ) ?>" />
                            <span><?php esc_html_e( 'Only in the Premium version can you choose different option than Weekly.', 'kirilkirkov-pdf-invoice-manager' ); ?></span>
                            <a href="<?php echo esc_url($Config::PREMIUM_LINK); ?>" class="kkinv-btn-primary" target="_blank"><?php esc_html_e( 'Get Premium Now', 'kirilkirkov-pdf-invoice-manager' ); ?></a>
                        </div>
                        
                        <div>
                            <label><?php esc_html_e('Reissue Type', 'kirilkirkov-pdf-invoice-manager' ) ?> *</label>
                        </div>
                        
                        <select name="<?php echo esc_attr($Config::INPUTS_PREFIX).'reissue_type'; ?>" class="w-full">
                            <?php foreach($reissue_types as $kirilkirkovwipdf_reissue_type) { 
                                $kirilkirkovwipdf_selected = false;
                                if(isset($item['reissue_type']) && $item['reissue_type'] === $kirilkirkovwipdf_reissue_type) {
                                    $kirilkirkovwipdf_selected = true;
                                }
                            ?>    
                                <option <?php selected($kirilkirkovwipdf_selected, true); ?> value="<?php echo esc_attr(sanitize_text_field($kirilkirkovwipdf_reissue_type)); ?>"><?php echo esc_html(sanitize_text_field(ucfirst($kirilkirkovwipdf_reissue_type))); ?></option>
                            <?php } ?>
                        </select>
                    </div>

                    <div class="flex items-center">  
                        <button class="button-primary" type="submit">
                            <?php esc_html_e('Save', 'kirilkirkov-pdf-invoice-manager' ); ?>
                        </button>
                    </div>  

                </form>
                
                <div class="w-full max-w-96 mx-auto p-0 rounded-sm mt-4">
                    <h3 class="text-center">
                        <?php esc_html_e('History of auto generated invoices', 'kirilkirkov-pdf-invoice-manager' ) ?>
                    </h3>
                    <table class="w-full max-w-96 mx-auto border-collapse">
                        <thead>
                            <tr>
                                <th class="text-left border-b-1 border-gray-300 p-2"><?php esc_html_e('Invoice Number', 'kirilkirkov-pdf-invoice-manager' ) ?></th>
                                <th class="text-right border-b-1 border-gray-300 p-2"><?php esc_html_e('Created At', 'kirilkirkov-pdf-invoice-manager' ) ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php if($history === false) { ?>
                        <tr>
                            <td class="text-center" colspan="2">
                                <?php esc_html_e('There are no generated invoices yet', 'kirilkirkov-pdf-invoice-manager' ) ?>
                            </td>
                        </tr>
                        <?php } else {
                        foreach($history as $kirilkirkovwipdf_k => $kirilkirkovwipdf_h) {
                            $kirilkirkovwipdf_edit_link = admin_url( 'admin.php?page=' . $Config::SUB_MENU_PREVIEW_INVOICE_PAGE_SLUG. '&view=' . $kirilkirkovwipdf_h['invoice_id'] );
                            $kirilkirkovwipdf_edit_link = '<a href="' . esc_url( $kirilkirkovwipdf_edit_link ) . '">' . $kirilkirkovwipdf_h['invoice_number'] . '</a>';
                        ?>
                        <tr>
                            <td class="text-left <?php if($kirilkirkovwipdf_k < (count($history) - 1)) { echo 'border-b-1 border-gray-300'; } ?> p-2"><?php echo esc_url($kirilkirkovwipdf_edit_link); ?></td>
                            <td class="text-right <?php if($kirilkirkovwipdf_k < (count($history)- 1)) { echo 'border-b-1 border-gray-300'; } ?> p-2"><?php echo esc_html(sanitize_text_field($kirilkirkovwipdf_h['created_at'])); ?></td>
                        </tr>
                        <?php } 
                        } 
                        ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>