<?php
    //Prevent direct access
    defined('ABSPATH') || exit;
    $currency_symbol    = function_exists('get_woocommerce_currency_symbol') ? get_woocommerce_currency_symbol() : '&#36;';
?>
<div class="sagc-redeem-gift-code">
    <div class="col-50 sa-br">
        <div class="sagc-redeem-wrapper">
            <h2><?php esc_html_e('Redeem Your Gift Code', 'smart-gift-card'); ?></h2>
            <?php if ( $msg ): ?>
                <p><?php echo esc_html( $msg ); ?></p>
            <?php endif; ?>

            <form method="post" action="">
                <?php wp_nonce_field('sgc_smart_gift_redeem_code_action', 'sgc_smart_gift_redeem_code_nonce'); ?>
                <input type="text" name="gift_code" placeholder="Enter your gift code" required>
                <button type="submit" class="sagc-btn sagc-btn-primary">Redeem</button>
            </form>
        </div>

        <div class="sagc-settings-wrapper">
            <h2><?php esc_html_e('Settings', 'smart-gift-card'); ?></h2>
            <?php if ( $settings_msg ): ?>
                <p><?php echo esc_html( $settings_msg ); ?></p>
            <?php endif; ?>
            <form method="post" action="">
                <?php wp_nonce_field('use_gift_balance_action', 'use_gift_balance_nonce'); ?>
                <input type="hidden" name="gift_balance_use_or_not" value="true">
                <div class="form-row">
                    <label for="use_gift_balance">
                        <input type="checkbox" name="use_gift_balance" id="use_gift_balance" value="yes" <?php echo esc_attr($checked); ?> />
                        <?php esc_html_e('Use Gift Wallet Balance on checkout', 'smart-gift-card'); ?>
                    </label>
                </div>

                <button type="submit" class="sagc-btn sagc-btn-primary"><?php esc_html_e('Save', 'smart-gift-card'); ?></button>
            </form>
        </div>
    </div>
    <div class="col-50">
        <div class="sagc-5-redeem-wrapper">
            <h2><?php esc_html_e('Last 5 Redeem', 'smart-gift-card'); ?></h2>
            <?php
            if ( empty( $redeem_list ) ):
            ?>
                <div class="sagc-alert">
                    <span class="sa-text-center"><?php esc_html_e('No gift card redeemed', 'smart-gift-card'); ?></span>
                </div>
            <?php else: ?>
                <table class="sagc-table">
                    <thead>
                        <tr>
                            <th><?php esc_html_e('Date', 'smart-gift-card'); ?></th>
                            <th><?php esc_html_e('Gift Code', 'smart-gift-card'); ?></th>
                            <th><?php esc_html_e('Amount', 'smart-gift-card'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        foreach ( $redeem_list as $item ):
                            $timestamp = strtotime(  $item['date'] );
                        ?>
                            <tr>
                                <td><?php echo esc_html( date_i18n('F j, Y g:i a',  $timestamp ) ); ?> </td>
                                <td><?php echo esc_html( $item['gift_code'] ); ?></td>
                                <td><?php echo esc_html( $currency_symbol . $item['amount'] ); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>
        <div class="available-gift-balance">
            <h2><?php esc_html_e('Available Gift Balance:', 'smart-gift-card'); ?> <?php echo esc_html($currency_symbol . $user_balance); ?></h2>
        </div>
    </div>
</div>