<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly ?>

<?php
    use EHxMe\Classes\Settings;
    use EHxMe\Helpers\Helper;
    use EHxMe\Shortcodes\FormShortcode;
?>

<div class="ehx-alert-element d-none">
    <div class="ehx-alert ehx-alert-primary text-center rounded-0" role="alert" id="ehx-alert-message">
        <i>&quest;</i>
        <span><?php esc_html_e('Item added to cart successfully', 'ehx-members'); ?></span>
    </div>
    <div class="ehx-alert-close">&#10006;</div>
</div>

<div class="ehx-container">
    <div class="ehx-content">

        <?php if($ehx_mode == 'profile'): ?>
            <div>
                <ul class="ehx-tabs">
                    <li><a href="#" class="ehx-tab-link" id="ehx-tab-link" data-tab="subscription"><?php esc_html_e('Subscriptions', 'ehx-members'); ?></a></li>
                    <li><a href="#" class="ehx-tab-link active" id="ehx-tab-link" data-tab="form"><?php esc_html_e('Edit Profile', 'ehx-members'); ?></a></li>
                    <li><a href="#" class="ehx-tab-link" id="ehx-tab-link" data-tab="payment"><?php esc_html_e('Payments', 'ehx-members'); ?></a></li>
                    <?php if($is_active_donate_plugin): ?>
                        <li><a href="#" class="ehx-tab-link" id="ehx-tab-link" data-tab="donation"><?php esc_html_e('Donations', 'ehx-members'); ?></a></li>
                    <?php endif ?>
                    <?php if($is_active_event_plugin): ?>
                        <li><a href="#" class="ehx-tab-link" id="ehx-tab-link" data-tab="booking"><?php esc_html_e('Bookings', 'ehx-members'); ?></a></li>
                    <?php endif ?>
                    <li><a href="<?php echo esc_url(wp_logout_url( home_url())); ?>" class="ehx-tab-link"><?php esc_html_e('Logout', 'ehx-members'); ?></a></li>
                </ul>
            </div>
        <?php endif ?>
        
        <div class="ehx-tab-contents">

            <div class="ehx-tab-content" id="tab-subscription">
                <div class="">
                    <p><?php esc_html_e('You do not have any subscriptions attached to your account.', 'ehx-members') ?></p>
                </div>
            </div>

            <div class="ehx-tab-content active" id="tab-form">

                <form action="#" method="POST" class="ehxme-form" id="ehxme_form_submit" <?php if($load_stripe): ?> enctype="application/x-www-form-urlencoded" <?php else: ?> enctype="multipart/form-data" <?php endif ?> <?php if($ehx_mode == 'profile'): ?> data-reset="false" <?php endif ?>>

                    <input type="hidden" name="action" value="<?php echo esc_html(FormShortcode::NONCE_ACTION); ?>">
                    <?php wp_nonce_field(FormShortcode::NONCE_ACTION, Helper::NONCE_NAME); ?>

                    <div class="ehx-user-details">
                        <?php
                            if (gettype($ehx_custom_fields) == 'array') {
                                foreach ($ehx_custom_fields as $index => $field) {
                                    FormShortcode::inputField($field);

                                    if ($field['_type'] == 'password' && isset($field['_force_confirm_pass']) && $field['_force_confirm_pass']) {
                                        FormShortcode::inputField(array_merge($field, ['_label' => $field['_label_confirm_pass'], '_metakey' => 'confirm_password']));
                                    }
                                }
                            }
                        ?>

                        <?php if($load_stripe): ?>
                            <div class="ehx-input-box ehx-input-box-full">
                                <label class="ehx-details" for="payment"><?php esc_html_e('Payment', 'ehx-members') ?> <?php echo esc_html("(£{$ehx_form['ehx_amount']})") ?> </label>
                                <div style="border: 1px solid #b2b4b7; padding: 12px 8px;">
                                    <div 
                                        id="card_element"
                                        data-key="<?php echo esc_attr(Settings::extractSettingValue('stripe_client_key')) ?>"
                                    ></div>
                                </div>
                            </div>
                        <?php endif ?>

                        <input type="hidden" name="form_id" value="<?php echo esc_html($form_id); ?>">

                    </div>

                    <?php if (isset($ehx_form['register_use_gdpr']) && $ehx_form['register_use_gdpr']): ?>
                        <div class="ehx-input-box ehx-input-box-full">
                            <div class="ehx-gender-details">
                                <div class="ehx-category">
                                    <div>
                                        <input type="checkbox" id="privacy_policy" required>
                                        <label for="privacy_policy" style="align-items: inherit;">
                                            <span class="ehx-dot ehx-dot-checkbox" style="margin-top: 7px;width: 30px;height:20px;"></span>
                                            <span class="ehx-gender"><?php echo esc_html($ehx_form['register_use_gdpr_agreement']) ?></span>
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endif ?>

                    <!-- Submit button -->
                    <div>
                        <button type="submit" class="ehx-button" data-submit="<?php esc_html_e('Please Wait...', 'ehx-members') ?>">
                            <div class="ehx-btn-loader" id="ehx-loader"></div>
                            <span id="ehx-btn-text"><?php echo !empty($ehx_form['primary_btn_word']) ? esc_html($ehx_form['primary_btn_word']) : esc_html__('Submit', 'ehx-members') ?></span>
                        </button>
                    </div>
                </form>
            </div>

            <div class="ehx-tab-content" id="tab-payment">
                <div class="">
                    <p><?php esc_html_e('Payments.', 'ehx-members') ?></p>

                    <table class="ehx-datatable">
                        <thead>
                            <tr>
                                <th><?php esc_html_e('Amount', 'ehx-members') ?></th>
                                <th><?php esc_html_e('Charge', 'ehx-members') ?></th>
                                <th><?php esc_html_e('Payment Method', 'ehx-members') ?></th>
                                <th><?php esc_html_e('Status', 'ehx-members') ?></th>
                                <th><?php esc_html_e('Date', 'ehx-members') ?></th>
                            </tr>
                        </thead>

                        <tbody>
                            <?php foreach($payments as $payment): ?>
                                <tr>
                                    <td><?php echo esc_html(Helper::currencyFormat($payment->amount)) ?></td>
                                    <td><?php echo esc_html(Helper::currencyFormat($payment->charge)) ?></td>
                                    <td><?php echo esc_html($payment->method) ?></td>
                                    <td><?php echo esc_html($payment->status) ?></td>
                                    <td><?php echo esc_html($payment->created_at) ?></td>
                                </tr>
                            <?php endforeach ?>
                        </tbody>
                        
                    </table>
                </div>
            </div>

            <?php if($is_active_donate_plugin): ?>
                <div class="ehx-tab-content" id="tab-donation">
                    <div class="">
                        <p><?php esc_html_e('Donations.', 'ehx-members') ?></p>

                        <table class="ehx-datatable">
                            <thead>
                                <tr>
                                    <th><?php esc_html_e('Date & Time', 'ehx-members') ?></th>
                                    <th><?php esc_html_e('Amount', 'ehx-members') ?></th>
                                    <th><?php esc_html_e('Campaign', 'ehx-members') ?></th>
                                    <th><?php esc_html_e('Gift Aid', 'ehx-members') ?></th>
                                    <th><?php esc_html_e('Recurring', 'ehx-members') ?></th>
                                    <th><?php esc_html_e('Payment Status', 'ehx-members') ?></th>
                                </tr>
                            </thead>

                            <tbody>
                                <?php foreach($donations as $donation): ?>
                                    <tr>
                                        <td><?php echo esc_html(wp_date('d F Y', strtotime($donation->created_at))) ?></td>
                                        <td><?php echo esc_html(Helper::currencyFormat($donation->total_amount)) ?></td>
                                        <td><?php echo esc_html($donation->post_title) ?></td>
                                        <td><?php echo esc_html($donation->gift_aid ? 'True':'False') ?></td>
                                        <td><?php echo esc_html($donation->recurring) ?></td>
                                        <td><?php echo esc_html($donation->payment_status) ?></td>
                                    </tr>
                                <?php endforeach ?>
                            </tbody>
                            
                        </table>
                        
                    </div>
                </div>
            <?php endif ?>

            <?php if($is_active_event_plugin): ?>
                <div class="ehx-tab-content" id="tab-booking">
                    <div class="">
                        <p><?php esc_html_e('Bookings.', 'ehx-members') ?></p>

                        <table class="ehx-datatable">
                            <thead>
                                <tr>
                                    <th><?php esc_html_e('Date & Time', 'ehx-members') ?></th>
                                    <th><?php esc_html_e('Event', 'ehx-members') ?></th>
                                    <th><?php esc_html_e('Ticket Price', 'ehx-members') ?></th>
                                    <th><?php esc_html_e('Qty', 'ehx-members') ?></th>
                                </tr>
                            </thead>

                            <tbody>
                                <?php foreach($bookings as $booking): ?>
                                    <tr>
                                        <td><?php echo esc_html(wp_date('d F Y', strtotime($booking->created_at))) ?></td>
                                        <td><?php echo esc_html($booking->post_title) ?></td>
                                        <td><?php echo esc_html(Helper::currencyFormat($booking->ticket_price)) ?></td>
                                        <td><?php echo esc_html($booking->quantity) ?></td>
                                    </tr>
                                <?php endforeach ?>
                            </tbody>
                            
                        </table>
                        
                    </div>
                </div>
            <?php endif ?>

        </div>

    </div>
</div>