<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<div class="edocman-lite-document-detail">
    <?php
    // Show breadcrumb navigation.
    $edocman_lite_current_url    = remove_query_arg( 'doc_id' );
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    $cat_id                      = isset( $_GET['cat_id'] ) ? absint( wp_unslash( $_GET['cat_id'] ) ) : 0;
    $edocman_lite_categories_url = remove_query_arg( array( 'cat_id', 'doc_id' ) );
    $edocman_lite_category       = null;

    if ( $cat_id > 0 ) {
        if ( ! isset( $edocman_lite_database ) ) {
            $edocman_lite_database = new Edocman_Lite_Database();
        }
        $edocman_lite_category = $edocman_lite_database->get_category( $cat_id );
        // Validate: only use category if it is published.
        if ( $edocman_lite_category && empty( $edocman_lite_category->published ) ) {
            $edocman_lite_category = null;
        }
    }

    // ✅ Helper: safe date format — validate strtotime() before use.
    $edocman_lite_created_timestamp = ! empty( $document->created_time ) ? strtotime( $document->created_time ) : false;
    $edocman_lite_created_display   = ( false !== $edocman_lite_created_timestamp )
        ? date_i18n( get_option( 'date_format' ), $edocman_lite_created_timestamp )
        : '';

    // ✅ Build image base URL from plugin config — không hardcode tên thư mục.
    $edocman_lite_upload_dir      = wp_upload_dir();
    $edocman_lite_documents_path  = Edocman_Lite_Helper::get_config( 'documents_path', $edocman_lite_upload_dir['basedir'] . '/edocman-lite' );
    $edocman_lite_documents_url   = str_replace(
        wp_normalize_path( $edocman_lite_upload_dir['basedir'] ),
        $edocman_lite_upload_dir['baseurl'],
        wp_normalize_path( $edocman_lite_documents_path )
    );
    ?>

    <!-- Breadcrumb Navigation -->
    <div class="edocman-breadcrumb">
        <a href="<?php echo esc_url( $edocman_lite_categories_url ); ?>">
            <i class="dashicons dashicons-admin-home"></i>
            <?php esc_html_e( 'All Categories', 'edocman-lite' ); ?>
        </a>
        <span class="separator">/</span>
        <?php if ( $edocman_lite_category ) : ?>
            <a href="<?php echo esc_url( add_query_arg( 'cat_id', $cat_id, $edocman_lite_categories_url ) ); ?>">
                <i class="dashicons dashicons-category"></i>
                <?php echo esc_html( $edocman_lite_category->title ); ?>
            </a>
            <span class="separator">/</span>
        <?php endif; ?>
        <span class="current"><?php echo esc_html( $document->title ); ?></span>
    </div>

    <!-- Document Header -->
    <div class="document-header">
        <div class="document-header-content">
            <div class="document-title-section">
                <h1 class="document-title"><?php echo esc_html( $document->title ); ?></h1>

                <?php if ( ! empty( $document->short_description ) ) : ?>
                    <p class="document-subtitle"><?php echo esc_html( $document->short_description ); ?></p>
                <?php endif; ?>
            </div>

            <div class="document-actions-header">
                <?php if ( Edocman_Lite_Helper::get_config( 'allow_download', 1 ) ) : ?>
                    <a href="<?php echo esc_url( add_query_arg( array(
                        'edocman_lite_download' => '1',
                        'doc_id'                => $document->id,
                        'nonce'                 => wp_create_nonce( 'edocman_lite_download_' . $document->id ),
                    ) ) ); ?>" class="download-btn primary" target="_blank">
                        <i class="dashicons dashicons-download"></i>
                        <?php esc_html_e( 'Download Now', 'edocman-lite' ); ?>
                    </a>
                <?php endif; ?>
            </div>
        </div>

        <!-- Document Meta Bar -->
        <div class="document-meta">
            <?php if ( $edocman_lite_created_display ) : ?>
                <span class="meta-item date">
                    <i class="dashicons dashicons-calendar-alt"></i>
                    <span class="meta-label"><?php esc_html_e( 'Created:', 'edocman-lite' ); ?></span>
                    <span class="meta-value"><?php echo esc_html( $edocman_lite_created_display ); ?></span>
                </span>
            <?php endif; ?>

            <?php if ( ! empty( $document->file_size ) ) : ?>
                <span class="meta-item size">
                    <i class="dashicons dashicons-database"></i>
                    <span class="meta-label"><?php esc_html_e( 'Size:', 'edocman-lite' ); ?></span>
                    <span class="meta-value"><?php echo esc_html( $document->file_size ); ?></span>
                </span>
            <?php endif; ?>

            <span class="meta-item downloads">
                <i class="dashicons dashicons-download"></i>
                <span class="meta-label"><?php esc_html_e( 'Downloads:', 'edocman-lite' ); ?></span>
                <span class="meta-value"><?php echo esc_html( number_format_i18n( absint( $document->downloads ?? 0 ) ) ); ?></span>
            </span>

            <?php if ( isset( $document->hits ) ) : ?>
                <span class="meta-item views">
                    <i class="dashicons dashicons-visibility"></i>
                    <span class="meta-label"><?php esc_html_e( 'Views:', 'edocman-lite' ); ?></span>
                    <span class="meta-value"><?php echo esc_html( number_format_i18n( absint( $document->hits ) ) ); ?></span>
                </span>
            <?php endif; ?>

            <?php if ( ! empty( $document->original_filename ) ) : ?>
                <span class="meta-item format">
                    <i class="dashicons dashicons-media-document"></i>
                    <span class="meta-label"><?php esc_html_e( 'Format:', 'edocman-lite' ); ?></span>
                    <span class="meta-value"><?php echo esc_html( strtoupper( pathinfo( $document->original_filename, PATHINFO_EXTENSION ) ) ); ?></span>
                </span>
            <?php endif; ?>
        </div>
    </div>

    <!-- Document Content -->
    <div class="document-content-wrapper">
        <div class="document-main-content">

            <!-- Document Image/Preview -->
            <?php if ( ! empty( $document->image ) ) : ?>
                <div class="document-preview-section">
                    <div class="document-preview-container">
                        <img src="<?php echo esc_url( rtrim( $edocman_lite_documents_url, '/' ) . '/' . ltrim( $document->image, '/' ) ); ?>"
                             alt="<?php echo esc_attr( $document->title ); ?>"
                             class="document-preview-image">
                    </div>
                </div>
            <?php endif; ?>

            <!-- Document Description -->
            <?php if ( ! empty( $document->description ) ) : ?>
                <div class="document-description-section">
                    <h2 class="section-title">
                        <i class="dashicons dashicons-text-page"></i>
                        <?php esc_html_e( 'Description', 'edocman-lite' ); ?>
                    </h2>
                    <div class="document-description">
                        <?php echo wp_kses_post( $document->description ); ?>
                    </div>
                </div>
            <?php endif; ?>

            <!-- Additional Information -->
            <div class="document-info-section">
                <h2 class="section-title">
                    <i class="dashicons dashicons-info"></i>
                    <?php esc_html_e( 'Document Information', 'edocman-lite' ); ?>
                </h2>

                <div class="info-grid">
                    <div class="info-card">
                        <div class="info-header">
                            <i class="dashicons dashicons-media-document"></i>
                            <h3><?php esc_html_e( 'File Details', 'edocman-lite' ); ?></h3>
                        </div>
                        <div class="info-content">
                            <?php if ( ! empty( $document->original_filename ) ) : ?>
                                <div class="info-row">
                                    <span class="info-label"><?php esc_html_e( 'Filename:', 'edocman-lite' ); ?></span>
                                    <span class="info-value"><?php echo esc_html( $document->original_filename ); ?></span>
                                </div>
                            <?php endif; ?>

                            <?php if ( ! empty( $document->file_size ) ) : ?>
                                <div class="info-row">
                                    <span class="info-label"><?php esc_html_e( 'File Size:', 'edocman-lite' ); ?></span>
                                    <span class="info-value"><?php echo esc_html( $document->file_size ); ?></span>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>

                    <div class="info-card">
                        <div class="info-header">
                            <i class="dashicons dashicons-chart-line"></i>
                            <h3><?php esc_html_e( 'Statistics', 'edocman-lite' ); ?></h3>
                        </div>
                        <div class="info-content">
                            <div class="info-row">
                                <span class="info-label"><?php esc_html_e( 'Downloads:', 'edocman-lite' ); ?></span>
                                <span class="info-value highlight"><?php echo esc_html( number_format_i18n( absint( $document->downloads ?? 0 ) ) ); ?></span>
                            </div>
                            <?php if ( isset( $document->hits ) ) : ?>
                                <div class="info-row">
                                    <span class="info-label"><?php esc_html_e( 'Views:', 'edocman-lite' ); ?></span>
                                    <span class="info-value highlight"><?php echo esc_html( number_format_i18n( absint( $document->hits ) ) ); ?></span>
                                </div>
                            <?php endif; ?>
                            <?php if ( $edocman_lite_created_display ) : ?>
                                <div class="info-row">
                                    <span class="info-label"><?php esc_html_e( 'Added:', 'edocman-lite' ); ?></span>
                                    <span class="info-value"><?php echo esc_html( $edocman_lite_created_display ); ?></span>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Sidebar -->
        <div class="document-sidebar">

            <!-- Download Card -->
            <div class="sidebar-card download-card">
                <div class="card-header">
                    <h3><i class="dashicons dashicons-download"></i> <?php esc_html_e( 'Download', 'edocman-lite' ); ?></h3>
                </div>
                <div class="card-content">
                    <div class="download-info">
                        <?php if ( ! empty( $document->file_size ) ) : ?>
                            <div class="download-size">
                                <i class="dashicons dashicons-database"></i>
                                <?php echo esc_html( $document->file_size ); ?>
                            </div>
                        <?php endif; ?>

                        <?php if ( ! empty( $document->original_filename ) ) : ?>
                            <div class="download-format">
                                <i class="dashicons dashicons-media-document"></i>
                                <?php echo esc_html( strtoupper( pathinfo( $document->original_filename, PATHINFO_EXTENSION ) ) ); ?>
                            </div>
                        <?php endif; ?>
                    </div>

                    <?php if ( Edocman_Lite_Helper::get_config( 'allow_download', 1 ) ) : ?>
                        <a href="<?php echo esc_url( add_query_arg( array(
                            'edocman_lite_download' => '1',
                            'doc_id'                => $document->id,
                            'nonce'                 => wp_create_nonce( 'edocman_lite_download_' . $document->id ),
                        ) ) ); ?>" class="download-btn-large" target="_blank">
                            <i class="dashicons dashicons-download"></i>
                            <?php esc_html_e( 'Download Now', 'edocman-lite' ); ?>
                        </a>
                    <?php endif; ?>

                    <div class="download-count">
					<?php
					echo wp_kses_post(
						sprintf(
							/* translators: %s: number of downloads formatted as bold HTML */
							esc_html__( 'Downloaded %s times', 'edocman-lite' ),
							'<strong>' . absint( $document->downloads ?? 0 ) . '</strong>'
						)
					);
					?>
					</div>

                </div>
            </div>

            <!-- Category Card -->
            <?php if ( $edocman_lite_category ) : ?>
                <div class="sidebar-card category-card">
                    <div class="card-header">
                        <h3><i class="dashicons dashicons-category"></i> <?php esc_html_e( 'Category', 'edocman-lite' ); ?></h3>
                    </div>
                    <div class="card-content">
                        <div class="category-info">
                            <?php if ( ! empty( $edocman_lite_category->image ) ) : ?>
                                <div class="category-thumb">
                                    <img src="<?php echo esc_url( rtrim( $edocman_lite_documents_url, '/' ) . '/categories/thumb/' . ltrim( $edocman_lite_category->image, '/' ) ); ?>"
                                         alt="<?php echo esc_attr( $edocman_lite_category->title ); ?>">
                                </div>
                            <?php endif; ?>

                            <div class="category-details">
                                <h4><?php echo esc_html( $edocman_lite_category->title ); ?></h4>
                                <?php if ( ! empty( $edocman_lite_category->description ) ) : ?>
                                    <p><?php echo esc_html( wp_trim_words( $edocman_lite_category->description, 20 ) ); ?></p>
                                <?php endif; ?>
                            </div>
                        </div>

                        <a href="<?php echo esc_url( add_query_arg( array( 'cat_id' => $edocman_lite_category->id ), remove_query_arg( 'doc_id' ) ) ); ?>"
                           class="view-category-btn">
                            <i class="dashicons dashicons-arrow-right-alt"></i>
                            <?php esc_html_e( 'View All Documents', 'edocman-lite' ); ?>
                        </a>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Back Link -->
    <div class="document-back-link">
        <a href="<?php echo esc_url( $edocman_lite_current_url ); ?>" class="button">
            <?php esc_html_e( '← Back to Documents', 'edocman-lite' ); ?>
        </a>
    </div>
</div>
