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

    <?php if ( isset( $_GET['created'] ) ) : ?>
        <div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Chatter user created successfully.', 'live-support-chat' ); ?></p></div>
    <?php endif; ?>
    <?php if ( isset( $_GET['deleted'] ) ) : ?>
        <div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'User deleted.', 'live-support-chat' ); ?></p></div>
    <?php endif; ?>
    <?php if ( isset( $_GET['error'] ) ) : ?>
        <div class="notice notice-error is-dismissible">
            <p>
            <?php
            switch ( sanitize_key( $_GET['error'] ) ) {
                case 'missing_fields':
                    esc_html_e( 'Please fill in all required fields.', 'live-support-chat' );
                    break;
                case 'user_exists':
                    esc_html_e( 'Username or email already exists.', 'live-support-chat' );
                    break;
                default:
                    esc_html_e( 'An error occurred. Please try again.', 'live-support-chat' );
            }
            ?>
            </p>
        </div>
    <?php endif; ?>

    <div class="lsc-two-col">

        <!-- Create user form -->
        <div class="lsc-card">
            <h2><?php esc_html_e( 'Add New Chatter', 'live-support-chat' ); ?></h2>
            <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
                <?php wp_nonce_field( 'lsc_create_user' ); ?>
                <input type="hidden" name="action" value="lsc_create_user" />

                <table class="form-table">
                    <tr>
                        <th><label for="lsc-username"><?php esc_html_e( 'Username', 'live-support-chat' ); ?> <span class="required">*</span></label></th>
                        <td><input type="text" name="username" id="lsc-username" class="regular-text" required /></td>
                    </tr>
                    <tr>
                        <th><label for="lsc-email"><?php esc_html_e( 'Email', 'live-support-chat' ); ?> <span class="required">*</span></label></th>
                        <td><input type="email" name="email" id="lsc-email" class="regular-text" required /></td>
                    </tr>
                    <tr>
                        <th><label for="lsc-password"><?php esc_html_e( 'Password', 'live-support-chat' ); ?> <span class="required">*</span></label></th>
                        <td><input type="password" name="password" id="lsc-password" class="regular-text" required autocomplete="new-password" /></td>
                    </tr>
                    <tr>
                        <th><label for="lsc-can-chat"><?php esc_html_e( 'Allow Chatting', 'live-support-chat' ); ?></label></th>
                        <td>
                            <label>
                                <input type="checkbox" name="can_chat" id="lsc-can-chat" value="1" checked />
                                <?php esc_html_e( 'Enable chat access', 'live-support-chat' ); ?>
                            </label>
                        </td>
                    </tr>
                </table>

                <?php submit_button( __( 'Create Chatter', 'live-support-chat' ) ); ?>
            </form>
        </div>

        <!-- Existing chatters list -->
        <div class="lsc-card">
            <h2><?php esc_html_e( 'Existing Chatters', 'live-support-chat' ); ?></h2>
            <?php if ( empty( $chatters ) ) : ?>
                <p><?php esc_html_e( 'No chatter users yet.', 'live-support-chat' ); ?></p>
            <?php else : ?>
                <table class="widefat striped">
                    <thead>
                        <tr>
                            <th><?php esc_html_e( 'Username', 'live-support-chat' ); ?></th>
                            <th><?php esc_html_e( 'Email', 'live-support-chat' ); ?></th>
                            <th><?php esc_html_e( 'Chat Access', 'live-support-chat' ); ?></th>
                            <th><?php esc_html_e( 'Actions', 'live-support-chat' ); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ( $chatters as $chatter ) :
                            $can_chat = (int) get_user_meta( $chatter->ID, 'lsc_can_chat', true );
                        ?>
                        <tr>
                            <td><?php echo esc_html( $chatter->user_login ); ?></td>
                            <td><?php echo esc_html( $chatter->user_email ); ?></td>
                            <td>
                                <?php if ( $can_chat ) : ?>
                                    <span class="lsc-badge lsc-badge-green"><?php esc_html_e( 'Enabled', 'live-support-chat' ); ?></span>
                                <?php else : ?>
                                    <span class="lsc-badge lsc-badge-gray"><?php esc_html_e( 'Disabled', 'live-support-chat' ); ?></span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <!-- Toggle chat access -->
                                <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="display:inline">
                                    <?php wp_nonce_field( 'lsc_toggle_user' ); ?>
                                    <input type="hidden" name="action"    value="lsc_toggle_user" />
                                    <input type="hidden" name="user_id"   value="<?php echo esc_attr( $chatter->ID ); ?>" />
                                    <input type="hidden" name="can_chat"  value="<?php echo esc_attr( $can_chat ); ?>" />
                                    <button type="submit" class="button button-small">
                                        <?php echo $can_chat ? esc_html__( 'Disable', 'live-support-chat' ) : esc_html__( 'Enable', 'live-support-chat' ); ?>
                                    </button>
                                </form>

                                <!-- Delete user -->
                                <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="display:inline" onsubmit="return confirm('<?php esc_attr_e( 'Delete this chatter?', 'live-support-chat' ); ?>')">
                                    <?php wp_nonce_field( 'lsc_delete_user' ); ?>
                                    <input type="hidden" name="action"  value="lsc_delete_user" />
                                    <input type="hidden" name="user_id" value="<?php echo esc_attr( $chatter->ID ); ?>" />
                                    <button type="submit" class="button button-small button-link-delete">
                                        <?php esc_html_e( 'Delete', 'live-support-chat' ); ?>
                                    </button>
                                </form>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>

    </div><!-- .lsc-two-col -->
</div>
