<?php
// Prevent direct access.
defined( 'ABSPATH' ) || exit;
?>
<div class="wrap lsc-admin-wrap">
    <h1 class="lsc-page-title">
        <span class="dashicons dashicons-format-chat"></span>
        <?php esc_html_e( 'Manage Chats', 'live-support-chat' ); ?>
    </h1>

    <!-- Filter tabs -->
    <ul class="subsubsub">
        <li>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=lsc-chats&status=open' ) ); ?>"
               class="<?php echo 'open' === $status_filter ? 'current' : ''; ?>">
                <?php esc_html_e( 'Open', 'live-support-chat' ); ?>
            </a> |
        </li>
        <li>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=lsc-chats&status=closed' ) ); ?>"
               class="<?php echo 'closed' === $status_filter ? 'current' : ''; ?>">
                <?php esc_html_e( 'Closed', 'live-support-chat' ); ?>
            </a>
        </li>
    </ul>

    <div class="lsc-card lsc-chats-admin-panel" id="lsc-admin-chats-panel">
        <?php if ( empty( $chats ) ) : ?>
            <p><?php esc_html_e( 'No chats found.', 'live-support-chat' ); ?></p>
        <?php else : ?>
            <table class="widefat striped">
                <thead>
                    <tr>
                        <th>#</th>
                        <th><?php esc_html_e( 'Visitor', 'live-support-chat' ); ?></th>
                        <th><?php esc_html_e( 'Email', 'live-support-chat' ); ?></th>
                        <th><?php esc_html_e( 'Status', 'live-support-chat' ); ?></th>
                        <th><?php esc_html_e( 'Started', 'live-support-chat' ); ?></th>
                        <th><?php esc_html_e( 'Last Update', 'live-support-chat' ); ?></th>
                        <th><?php esc_html_e( 'Actions', 'live-support-chat' ); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ( $chats as $chat ) : ?>
                    <tr>
                        <td><?php echo esc_html( $chat->id ); ?></td>
                        <td><?php echo esc_html( $chat->visitor_name ?: __( 'Anonymous', 'live-support-chat' ) ); ?></td>
                        <td><?php echo esc_html( $chat->visitor_email ?: '—' ); ?></td>
                        <td>
                            <?php if ( 'open' === $chat->status ) : ?>
                                <span class="lsc-badge lsc-badge-green"><?php esc_html_e( 'Open', 'live-support-chat' ); ?></span>
                            <?php else : ?>
                                <span class="lsc-badge lsc-badge-gray"><?php esc_html_e( 'Closed', 'live-support-chat' ); ?></span>
                            <?php endif; ?>
                        </td>
                        <td><?php echo esc_html( mysql2date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $chat->created_at ) ); ?></td>
                        <td><?php echo esc_html( mysql2date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $chat->updated_at ) ); ?></td>
                        <td>
                            <button class="button button-small lsc-view-chat-btn" data-chat-id="<?php echo esc_attr( $chat->id ); ?>">
                                <?php esc_html_e( 'View', 'live-support-chat' ); ?>
                            </button>

                            <?php if ( 'open' === $chat->status ) : ?>
                            <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="display:inline">
                                <?php wp_nonce_field( 'lsc_close_chat' ); ?>
                                <input type="hidden" name="action"  value="lsc_admin_close_chat" />
                                <input type="hidden" name="chat_id" value="<?php echo esc_attr( $chat->id ); ?>" />
                                <button type="submit" class="button button-small">
                                    <?php esc_html_e( 'Close', 'live-support-chat' ); ?>
                                </button>
                            </form>
                            <?php endif; ?>

                            <button class="button button-small button-link-delete lsc-delete-chat-btn" data-chat-id="<?php echo esc_attr( $chat->id ); ?>">
                                <?php esc_html_e( 'Delete', 'live-support-chat' ); ?>
                            </button>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </div>

    <!-- Chat detail modal -->
    <div id="lsc-chat-modal" class="lsc-modal" style="display:none">
        <div class="lsc-modal-content">
            <div class="lsc-modal-header">
                <h3 id="lsc-modal-title"><?php esc_html_e( 'Chat Messages', 'live-support-chat' ); ?></h3>
                <button id="lsc-modal-close" class="lsc-modal-close">&times;</button>
            </div>
            <div id="lsc-modal-messages" class="lsc-modal-messages"></div>
        </div>
    </div>
</div>
