<?php
/**
 * Applications List View
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$cloudi7_icg_message = isset( $_GET['message'] ) ? sanitize_text_field( wp_unslash( $_GET['message'] ) ) : '';
?>

<div class="wrap cloudi7-icg-admin-wrap">
    <h1 class="wp-heading-inline"><?php esc_html_e( 'ID Card Applications', 'cloudi7-id-card-generator' ); ?></h1>
    <hr class="wp-header-end">

    <?php if ( 'approved' === $cloudi7_icg_message ) : ?>
        <div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Application approved and ID card generated.', 'cloudi7-id-card-generator' ); ?></p></div>
    <?php elseif ( 'rejected' === $cloudi7_icg_message ) : ?>
        <div class="notice notice-warning is-dismissible"><p><?php esc_html_e( 'Application rejected.', 'cloudi7-id-card-generator' ); ?></p></div>
    <?php elseif ( 'deleted' === $cloudi7_icg_message ) : ?>
        <div class="notice notice-info is-dismissible"><p><?php esc_html_e( 'Application deleted.', 'cloudi7-id-card-generator' ); ?></p></div>
    <?php endif; ?>

    <div class="cloudi7-icg-apps-table-container cloudi7-icg-card-box">
        <table class="wp-list-table widefat fixed striped">
            <thead>
                <tr>
                    <th class="column-primary"><?php esc_html_e( 'Applicant', 'cloudi7-id-card-generator' ); ?></th>
                    <th><?php esc_html_e( 'Contact Info', 'cloudi7-id-card-generator' ); ?></th>
                    <th><?php esc_html_e( 'Type', 'cloudi7-id-card-generator' ); ?></th>
                    <th><?php esc_html_e( 'Organization Details', 'cloudi7-id-card-generator' ); ?></th>
                    <th><?php esc_html_e( 'Applied On', 'cloudi7-id-card-generator' ); ?></th>
                    <th><?php esc_html_e( 'Status', 'cloudi7-id-card-generator' ); ?></th>
                    <th style="text-align:right;"><?php esc_html_e( 'Actions', 'cloudi7-id-card-generator' ); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php if ( $cloudi7_icg_apps ) : foreach ( $cloudi7_icg_apps as $cloudi7_icg_app_item ) : 
                    $cloudi7_icg_details = json_decode( $cloudi7_icg_app_item->org_details, true );
                    $cloudi7_icg_status_class = 'cloudi7-icg-status-' . $cloudi7_icg_app_item->status;
                ?>
                    <tr>
                        <td class="column-primary">
                            <div class="cloudi7-icg-applicant-box">
                                <?php if ( $cloudi7_icg_app_item->photo ) : ?>
                                    <img src="<?php echo esc_url( $cloudi7_icg_app_item->photo ); ?>" class="cloudi7-icg-mini-photo">
                                <?php endif; ?>
                                <strong><?php echo esc_html( $cloudi7_icg_app_item->name ); ?></strong>
                                <button type="button" class="toggle-row"><span class="screen-reader-text"><?php esc_html_e( 'Show more details', 'cloudi7-id-card-generator' ); ?></span></button>
                            </div>
                        </td>
                        <td>
                            <div><?php echo esc_html( $cloudi7_icg_app_item->email ); ?></div>
                            <div class="description"><?php echo esc_html( $cloudi7_icg_app_item->phone ); ?></div>
                        </td>
                        <td><span class="cloudi7-icg-type-badge"><?php echo esc_html( $cloudi7_icg_app_item->org_type ); ?></span></td>
                        <td>
                            <div class="cloudi7-icg-app-details-col">
                                <div><strong><?php esc_html_e( 'DOB:', 'cloudi7-id-card-generator' ); ?></strong> <?php echo esc_html( $cloudi7_icg_app_item->dob ); ?></div>
                                <div><strong><?php esc_html_e( 'Blood:', 'cloudi7-id-card-generator' ); ?></strong> <?php echo esc_html( $cloudi7_icg_app_item->blood_group ); ?></div>
                                <?php if ( $cloudi7_icg_app_item->gov_id ) : ?>
                                    <div><strong><?php esc_html_e( 'Gov ID:', 'cloudi7-id-card-generator' ); ?></strong> <?php echo esc_html( $cloudi7_icg_app_item->gov_id ); ?></div>
                                <?php endif; ?>
                                <?php if ( $cloudi7_icg_app_item->parent_name ) : ?>
                                    <div><strong><?php esc_html_e( 'Guardian:', 'cloudi7-id-card-generator' ); ?></strong> <?php echo esc_html( sprintf( /* translators: %1$s: Parent Name, %2$s: Parent Phone */ esc_html__( '%1$s (%2$s)', 'cloudi7-id-card-generator' ), $cloudi7_icg_app_item->parent_name, $cloudi7_icg_app_item->parent_phone ) ); ?></div>
                                <?php endif; ?>
                                <hr style="margin:5px 0; border:0; border-top:1px solid #eee;">
                                <?php foreach ( $cloudi7_icg_details as $cloudi7_icg_key => $cloudi7_icg_val ) : if ( ! empty( $cloudi7_icg_val ) ) : ?>
                                    <div><strong><?php echo esc_html( ucwords( str_replace( '_', ' ', $cloudi7_icg_key ) ) ); ?>:</strong> <?php echo esc_html( $cloudi7_icg_val ); ?></div>
                                <?php endif; endforeach; ?>
                            </div>
                        </td>
                        <td><?php echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $cloudi7_icg_app_item->created_at ) ) ); ?></td>
                        <td>
                            <span class="cloudi7-icg-badge <?php echo esc_attr( $cloudi7_icg_status_class ); ?>">
                                <?php echo esc_html( ucfirst( $cloudi7_icg_app_item->status ) ); ?>
                            </span>
                        </td>
                        <td style="text-align:right;">
                            <?php if ( 'pending' === $cloudi7_icg_app_item->status ) : ?>
                                <a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=cloudi7-icg-apps&action=approve_app&id=' . $cloudi7_icg_app_item->id ), 'cloudi7_icg_action', 'cloudi7_icg_nonce' ) ); ?>" class="button button-small action-approve" onclick="return confirm('<?php echo esc_js( esc_html__( 'Are you sure you want to approve this application and generate an ID card?', 'cloudi7-id-card-generator' ) ); ?>');"><?php esc_html_e( 'Approve', 'cloudi7-id-card-generator' ); ?></a>
                                <a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=cloudi7-icg-apps&action=reject_app&id=' . $cloudi7_icg_app_item->id ), 'cloudi7_icg_action', 'cloudi7_icg_nonce' ) ); ?>" class="button button-small action-reject" onclick="return confirm('<?php echo esc_js( esc_html__( 'Reject this application?', 'cloudi7-id-card-generator' ) ); ?>');"><?php esc_html_e( 'Reject', 'cloudi7-id-card-generator' ); ?></a>
                            <?php endif; ?>
                            <a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=cloudi7-icg-apps&action=delete_app&id=' . $cloudi7_icg_app_item->id ), 'cloudi7_icg_action', 'cloudi7_icg_nonce' ) ); ?>" class="button button-link-delete action-delete" onclick="return confirm('<?php echo esc_js( esc_html__( 'Delete this application permanently?', 'cloudi7-id-card-generator' ) ); ?>');"><span class="dashicons dashicons-trash"></span></a>
                        </td>
                    </tr>
                <?php endforeach; else : ?>
                    <tr><td colspan="7"><?php esc_html_e( 'No applications found.', 'cloudi7-id-card-generator' ); ?></td></tr>
                <?php endif; ?>
            </tbody>
        </table>

        <?php if ( $cloudi7_icg_total_pages > 1 ) : ?>
            <div class="cloudi7-icg-pagination">
                <?php
                echo wp_kses_post( paginate_links( array(
                    'base'      => add_query_arg( 'paged', '%#%' ),
                    'format'    => '',
                    'prev_text' => esc_html__( '&laquo; Previous', 'cloudi7-id-card-generator' ),
                    'next_text' => esc_html__( 'Next &raquo;', 'cloudi7-id-card-generator' ),
                    'total'     => $cloudi7_icg_total_pages,
                    'current'   => $cloudi7_icg_current_page,
                ) ) );
                ?>
            </div>
        <?php endif; ?>
    </div>
</div>


