<?php
/**
 * The main display page for FixReport logs.
 * (Other comments as before)
 */

// Security check
if ( ! defined( 'ABSPATH' ) ) {
	die( esc_html__( 'You cannot access this file directly!', 'fixreport-maintenance-logger' ) );
}

?>

<div class="wrap">

    <h1><?php esc_html_e( 'FixReport - Maintenance Logs', 'fixreport-maintenance-logger' ); ?></h1>

    <hr class="wp-header-end">

    <?php // === EDIT LOG ENTRY FORM === ?>
    <?php if ( isset($is_edit_mode) && $is_edit_mode && isset($log_to_edit) && $log_to_edit ) : ?>

        <h2><?php esc_html_e( 'Edit Log Entry', 'fixreport-maintenance-logger' ); ?> – ID: <?php echo esc_html( $log_to_edit->log_id ); ?></h2>

        <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
            <?php wp_nonce_field( 'fixreport_update_log_action_' . $log_to_edit->log_id, 'fixreport_update_log_nonce' ); ?>
            <input type="hidden" name="action" value="fixreport_update_log">
            <input type="hidden" name="fixreport_log_id" value="<?php echo esc_attr( $log_to_edit->log_id ); ?>">
            <table class="form-table" role="presentation">
                <tbody>
                    <tr>
                        <th scope="row"><label for="fixreport-edit-title"><?php esc_html_e( 'Title', 'fixreport-maintenance-logger' ); ?></label></th>
                        <td><input name="fixreport_title" type="text" id="fixreport-edit-title" value="<?php echo esc_attr( $log_to_edit->title ); ?>" class="regular-text" required></td>
                    </tr>
                    <tr>
                        <th scope="row"><label for="fixreport-edit-description"><?php esc_html_e( 'Description', 'fixreport-maintenance-logger' ); ?></label></th>
                        <td><textarea name="fixreport_description" id="fixreport-edit-description" rows="5" class="large-text" required><?php echo esc_textarea( $log_to_edit->description ); ?></textarea></td>
                    </tr>
                    <tr>
                        <th scope="row"><label for="fixreport-edit-type"><?php esc_html_e( 'Type', 'fixreport-maintenance-logger' ); ?></label></th>
                        <td>
                            <select name="fixreport_type" id="fixreport-edit-type" required>
                                <?php $types = array('Fix', 'Update', 'Maintenance', 'Security', 'Error', 'Other'); ?>
                                <?php foreach ($types as $type_option) : ?>
                                    <option value="<?php echo esc_attr($type_option); ?>" <?php selected( $log_to_edit->type, $type_option ); ?>>
                                        <?php
                                        $translated_type_option = $type_option;
                                        switch ( $type_option ) {
                                            case 'Fix': $translated_type_option = __( 'Fix', 'fixreport-maintenance-logger' ); break;
                                            case 'Update': $translated_type_option = __( 'Update', 'fixreport-maintenance-logger' ); break;
                                            case 'Maintenance': $translated_type_option = __( 'Maintenance', 'fixreport-maintenance-logger' ); break;
                                            case 'Security': $translated_type_option = __( 'Security', 'fixreport-maintenance-logger' ); break;
                                            case 'Error': $translated_type_option = __( 'Error Logged', 'fixreport-maintenance-logger' ); break;
                                            case 'Other': $translated_type_option = __( 'Other', 'fixreport-maintenance-logger' ); break;
                                        }
                                        echo esc_html( $translated_type_option );
                                        ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </td>
                    </tr>
                     <tr>
                        <th scope="row"><label for="fixreport-edit-priority"><?php esc_html_e( 'Priority', 'fixreport-maintenance-logger' ); ?></label></th>
                        <td>
                            <select name="fixreport_priority" id="fixreport-edit-priority" required>
                                <?php $priorities = array('Low', 'Medium', 'High', 'Critical'); ?>
                                <?php foreach ($priorities as $priority_option) : ?>
                                    <option value="<?php echo esc_attr($priority_option); ?>" <?php selected( $log_to_edit->priority, $priority_option ); ?>>
                                        <?php
                                        $translated_priority_option = $priority_option;
                                        switch ( $priority_option ) {
                                            case 'Low': $translated_priority_option = __( 'Low', 'fixreport-maintenance-logger' ); break;
                                            case 'Medium': $translated_priority_option = __( 'Medium', 'fixreport-maintenance-logger' ); break;
                                            case 'High': $translated_priority_option = __( 'High', 'fixreport-maintenance-logger' ); break;
                                            case 'Critical': $translated_priority_option = __( 'Critical', 'fixreport-maintenance-logger' ); break;
                                        }
                                        echo esc_html( $translated_priority_option );
                                        ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row"><label for="fixreport-edit-status"><?php esc_html_e( 'Status', 'fixreport-maintenance-logger' ); ?></label></th>
                        <td>
                            <select name="fixreport_status" id="fixreport-edit-status" required>
                                <?php $statuses = array('Open', 'In Progress', 'Resolved', 'Closed'); ?>
                                <?php foreach ($statuses as $status_option) : ?>
                                    <option value="<?php echo esc_attr($status_option); ?>" <?php selected( $log_to_edit->status, $status_option ); ?>>
                                        <?php
                                        $translated_status_option = $status_option;
                                        switch ( $status_option ) {
                                            case 'Open': $translated_status_option = __( 'Open', 'fixreport-maintenance-logger' ); break;
                                            case 'In Progress': $translated_status_option = __( 'In Progress', 'fixreport-maintenance-logger' ); break;
                                            case 'Resolved': $translated_status_option = __( 'Resolved', 'fixreport-maintenance-logger' ); break;
                                            case 'Closed': $translated_status_option = __( 'Closed', 'fixreport-maintenance-logger' ); break;
                                        }
                                        echo esc_html( $translated_status_option );
                                        ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </td>
                    </tr>
                </tbody>
            </table>
            <?php submit_button( __( 'Update Log Entry', 'fixreport-maintenance-logger' ), 'primary', 'submit_update_log' ); ?>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=' . (isset($current_page_slug) ? $current_page_slug : '') ) ); ?>" class="button button-secondary">
                <?php esc_html_e( 'Cancel', 'fixreport-maintenance-logger' ); ?>
            </a>
        </form>

    <?php else : // === ADD NEW LOG ENTRY FORM & LOGS TABLE === ?>

        <div id="fixreport-add-log-form" style="margin-bottom: 20px;">
            <h2><?php esc_html_e( 'Add New Log Entry', 'fixreport-maintenance-logger' ); ?></h2>
            <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
                <?php wp_nonce_field( 'fixreport_add_log_action', 'fixreport_add_log_nonce' ); ?>
                <input type="hidden" name="action" value="fixreport_add_log">
                <table class="form-table" role="presentation">
                    <tbody>
                        <tr>
                            <th scope="row"><label for="fixreport-title"><?php esc_html_e( 'Title', 'fixreport-maintenance-logger' ); ?></label></th>
                            <td><input name="fixreport_title" type="text" id="fixreport-title" value="" class="regular-text" required>
                                <p class="description"><?php esc_html_e( 'A brief summary.', 'fixreport-maintenance-logger' ); ?></p></td>
                        </tr>
                        <tr>
                            <th scope="row"><label for="fixreport-description"><?php esc_html_e( 'Description', 'fixreport-maintenance-logger' ); ?></label></th>
                            <td><textarea name="fixreport_description" id="fixreport-description" rows="5" class="large-text" required></textarea>
                                <p class="description"><?php esc_html_e( 'Detailed description.', 'fixreport-maintenance-logger' ); ?></p></td>
                        </tr>
                        <tr>
                            <th scope="row"><label for="fixreport-type"><?php esc_html_e( 'Type', 'fixreport-maintenance-logger' ); ?></label></th>
                            <td><select name="fixreport_type" id="fixreport-type" required>
                                    <option value=""><?php esc_html_e( '-- Select --', 'fixreport-maintenance-logger' ); ?></option>
                                    <option value="Fix"><?php esc_html_e( 'Fix', 'fixreport-maintenance-logger' ); ?></option>
                                    <option value="Update"><?php esc_html_e( 'Update', 'fixreport-maintenance-logger' ); ?></option>
                                    <option value="Maintenance"><?php esc_html_e( 'Maintenance', 'fixreport-maintenance-logger' ); ?></option>
                                    <option value="Security"><?php esc_html_e( 'Security', 'fixreport-maintenance-logger' ); ?></option>
                                    <option value="Error"><?php esc_html_e( 'Error Logged', 'fixreport-maintenance-logger' ); ?></option>
                                    <option value="Other"><?php esc_html_e( 'Other', 'fixreport-maintenance-logger' ); ?></option>
                                </select></td>
                        </tr>
                         <tr>
                            <th scope="row"><label for="fixreport-priority"><?php esc_html_e( 'Priority', 'fixreport-maintenance-logger' ); ?></label></th>
                            <td><select name="fixreport_priority" id="fixreport-priority" required>
                                    <option value="Medium"><?php esc_html_e( 'Medium', 'fixreport-maintenance-logger' ); ?></option>
                                    <option value="Low"><?php esc_html_e( 'Low', 'fixreport-maintenance-logger' ); ?></option>
                                    <option value="High"><?php esc_html_e( 'High', 'fixreport-maintenance-logger' ); ?></option>
                                    <option value="Critical"><?php esc_html_e( 'Critical', 'fixreport-maintenance-logger' ); ?></option>
                                </select></td>
                        </tr>
                        <tr>
                            <th scope="row"><label for="fixreport-status"><?php esc_html_e( 'Status', 'fixreport-maintenance-logger' ); ?></label></th>
                            <td> <select name="fixreport_status" id="fixreport-status" required>
                                    <option value="Resolved"><?php esc_html_e( 'Resolved', 'fixreport-maintenance-logger' ); ?></option>
                                    <option value="Open"><?php esc_html_e( 'Open', 'fixreport-maintenance-logger' ); ?></option>
                                    <option value="In Progress"><?php esc_html_e( 'In Progress', 'fixreport-maintenance-logger' ); ?></option>
                                    <option value="Closed"><?php esc_html_e( 'Closed', 'fixreport-maintenance-logger' ); ?></option>
                                </select></td>
                        </tr>
                    </tbody>
                </table>
                <?php submit_button( __( 'Add Log Entry', 'fixreport-maintenance-logger' ) ); ?>
            </form>
        </div>

        <hr>

        <div style="margin-bottom: 15px; text-align: right;">
            <?php
                $export_pdf_nonce = wp_create_nonce( 'fixreport_export_pdf_nonce_action' );
                $export_pdf_url = admin_url( 'admin-post.php?action=fixreport_export_pdf&_wpnonce=' . $export_pdf_nonce );
            ?>
            <a href="<?php echo esc_url( $export_pdf_url ); ?>" class="button button-secondary">
                <span class="dashicons dashicons-media-document" style="vertical-align: middle; margin-top: -3px;"></span>
                <?php esc_html_e( 'Export Logs to PDF', 'fixreport-maintenance-logger' ); ?>
            </a>
        </div>

        <h2><?php esc_html_e( 'Existing Logs', 'fixreport-maintenance-logger' ); ?></h2>

        <?php if ( isset($pagination_args) && $pagination_args['total_pages'] > 1 ) : ?>
            <div class="tablenav top">
                <div class="tablenav-pages">
                    <span class="displaying-num">
                        <?php
                        /* translators: %s: Number of items. */
                        printf( esc_html__( '%s items', 'fixreport-maintenance-logger' ), esc_html( number_format_i18n( $pagination_args['total_items'] ) ) );
                        ?>
                    </span>
                    <span class="pagination-links">
                        <?php
                        echo wp_kses_post( paginate_links( array(
                            'base'      => admin_url( 'admin.php?page=' . (isset($current_page_slug) ? $current_page_slug : '') . '%_%' ),
                            'format'    => '&paged=%#%',
                            'current'   => $pagination_args['current_page'],
                            'total'     => $pagination_args['total_pages'],
                            'prev_text' => esc_html__( '«', 'fixreport-maintenance-logger' ),
                            'next_text' => esc_html__( '»', 'fixreport-maintenance-logger' ),
                        ) ) );
                        ?>
                    </span>
                </div>
                <br class="clear" />
            </div>
        <?php endif; ?>

        <?php if ( isset($logs_data) && ! empty( $logs_data ) && is_array( $logs_data ) ) : ?>
            <table class="wp-list-table widefat fixed striped table-view-list posts">
                <thead>
                     <tr>
                        <th scope="col"><?php esc_html_e( 'Title', 'fixreport-maintenance-logger' ); ?></th>
                        <th scope="col"><?php esc_html_e( 'Type', 'fixreport-maintenance-logger' ); ?></th>
                        <th scope="col"><?php esc_html_e( 'Priority', 'fixreport-maintenance-logger' ); ?></th>
                        <th scope="col"><?php esc_html_e( 'Status', 'fixreport-maintenance-logger' ); ?></th>
                        <th scope="col"><?php esc_html_e( 'User', 'fixreport-maintenance-logger' ); ?></th>
                        <th scope="col"><?php esc_html_e( 'Date', 'fixreport-maintenance-logger' ); ?></th>
                     </tr>
                </thead>
                <tbody id="the-list">
                    <?php foreach ( $logs_data as $log ) : ?>
                        <?php
                            $user_info = get_userdata( $log->user_id );
                            $user_name = $user_info ? esc_html( $user_info->display_name ) : esc_html__( 'Unknown User', 'fixreport-maintenance-logger' );
                            $date_format = get_option( 'date_format', 'F j, Y' );
                            $time_format = get_option( 'time_format', 'g:i a' );
                            $log_date = mysql2date( $date_format . ' @ ' . $time_format, $log->created_at );
                            $delete_nonce_action = 'fixreport_delete_log_' . $log->log_id;
                            $delete_nonce = wp_create_nonce( $delete_nonce_action );
                            $delete_url = admin_url( 'admin-post.php?action=fixreport_delete_log&log_id=' . $log->log_id . '&_wpnonce=' . $delete_nonce );
                            $edit_nonce_action = 'fixreport_edit_log_view_' . $log->log_id;
                            $edit_nonce = wp_create_nonce( $edit_nonce_action );
                            $edit_url = add_query_arg( array(
                                'page'     => isset($current_page_slug) ? $current_page_slug : '',
                                'action'   => 'edit', 'log_id'   => $log->log_id, '_wpnonce' => $edit_nonce
                            ), admin_url( 'admin.php' ) );
                        ?>
                        <tr>
                            <td class="title column-title has-row-actions column-primary page-title" data-colname="<?php esc_attr_e( 'Title', 'fixreport-maintenance-logger' ); ?>">
                                <strong>
                                    <a class="row-title" href="<?php echo esc_url( $edit_url ); ?>" aria-label="<?php
                                        /* translators: %s: Title of the log entry. */
                                        printf(esc_attr__('Edit "%s"', 'fixreport-maintenance-logger'), esc_attr($log->title));
                                    ?>">
                                        <?php echo esc_html( $log->title ); ?>
                                    </a>
                                </strong>
                                <div class="row-actions">
                                    <span class="edit"><a href="<?php echo esc_url( $edit_url ); ?>"><?php esc_html_e( 'Edit', 'fixreport-maintenance-logger' ); ?></a> | </span>
                                    <span class="trash"><a href="<?php echo esc_url( $delete_url ); ?>" class="submitdelete" onclick="return confirm('<?php esc_attr_e( 'Are you sure you want to delete this log entry? This action cannot be undone.', 'fixreport-maintenance-logger' ); ?>');"><?php esc_html_e( 'Delete', 'fixreport-maintenance-logger' ); ?></a> | </span>
                                    <span class="view-description toggle-description" style="cursor:pointer;" data-log-id="<?php echo esc_attr($log->log_id); ?>"><?php esc_html_e( 'View Desc.', 'fixreport-maintenance-logger' ); ?></span>
                                </div>
                                <button type="button" class="toggle-row"><span class="screen-reader-text"><?php esc_html_e( 'Show more details', 'fixreport-maintenance-logger' ); ?></span></button>
                                <div id="log-description-<?php echo esc_attr($log->log_id); ?>" class="hidden-description" style="display:none; margin-top: 5px; padding: 5px; background-color: #f9f9f9; border: 1px solid #eee;">
                                    <strong><?php esc_html_e('Description:', 'fixreport-maintenance-logger'); ?></strong><br>
                                    <?php echo nl2br(esc_html($log->description)); ?>
                                </div>
                            </td>
                            <td data-colname="<?php esc_attr_e( 'Type', 'fixreport-maintenance-logger' ); ?>"><?php echo esc_html( $log->type ); ?></td>
                            <td data-colname="<?php esc_attr_e( 'Priority', 'fixreport-maintenance-logger' ); ?>"><?php echo esc_html( $log->priority ); ?></td>
                            <td data-colname="<?php esc_attr_e( 'Status', 'fixreport-maintenance-logger' ); ?>"><?php echo esc_html( $log->status ); ?></td>
                            <td data-colname="<?php esc_attr_e( 'User', 'fixreport-maintenance-logger' ); ?>"><?php echo esc_html( $user_name ); ?></td>
                            <td data-colname="<?php esc_attr_e( 'Date', 'fixreport-maintenance-logger' ); ?>"><?php echo esc_html( $log_date ); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
                <tfoot>
                     <tr>
                        <th scope="col"><?php esc_html_e( 'Title', 'fixreport-maintenance-logger' ); ?></th>
                        <th scope="col"><?php esc_html_e( 'Type', 'fixreport-maintenance-logger' ); ?></th>
                        <th scope="col"><?php esc_html_e( 'Priority', 'fixreport-maintenance-logger' ); ?></th>
                        <th scope="col"><?php esc_html_e( 'Status', 'fixreport-maintenance-logger' ); ?></th>
                        <th scope="col"><?php esc_html_e( 'User', 'fixreport-maintenance-logger' ); ?></th>
                        <th scope="col"><?php esc_html_e( 'Date', 'fixreport-maintenance-logger' ); ?></th>
                     </tr>
                </tfoot>
            </table>

            <?php if ( isset($pagination_args) && $pagination_args['total_pages'] > 1 ) : ?>
                <div class="tablenav bottom">
                     <div class="tablenav-pages">
                        <span class="displaying-num">
                            <?php /* translators: %s: Number of items. */ printf( esc_html__( '%s items', 'fixreport-maintenance-logger' ), esc_html( number_format_i18n( $pagination_args['total_items'] ) ) ); ?>
                        </span>
                        <span class="pagination-links">
                            <?php echo wp_kses_post( paginate_links( array(
                                'base' => admin_url( 'admin.php?page=' . (isset($current_page_slug) ? $current_page_slug : '') . '%_%' ),
                                'format' => '&paged=%#%', 'current' => $pagination_args['current_page'],
                                'total' => $pagination_args['total_pages'],
                                'prev_text' => esc_html__( '«', 'fixreport-maintenance-logger' ), 'next_text' => esc_html__( '»', 'fixreport-maintenance-logger' ),
                            ) ) ); ?>
                        </span>
                    </div>
                    <br class="clear" />
                </div>
            <?php endif; ?>

        <?php else : ?>
            <p><?php esc_html_e( 'No maintenance logs found.', 'fixreport-maintenance-logger' ); ?></p>
        <?php endif; ?>

    <?php endif; ?>

</div> <?php // End .wrap ?>

<?php // REMOVED: The inline <script> tag has been moved to an external JS file and enqueued. ?>