<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<div class="edocman-lite-documents">
    <?php 
    // Show breadcrumb navigation
    $edocman_lite_current_url = remove_query_arg(array('cat_id', 'doc_id'));
    ?>
    
    <?php if ($category): ?>
        <div class="category-header">
            <div class="category-breadcrumb">
                <a href="<?php echo esc_url($edocman_lite_current_url); ?>" class="back-to-categories">
                    <i class="dashicons dashicons-arrow-left-alt"></i>
                    <?php esc_html_e('Back to Categories', 'edocman-lite'); ?>
                </a>
            </div>
            
            <div class="category-info">
                <?php if (!empty($category->image)): ?>
                    <?php 
                    $edocman_lite_upload_dir = wp_upload_dir();
                    $edocman_lite_image_url = $edocman_lite_upload_dir['baseurl'] . '/edocman/categories/thumb/' . $category->image;
                    ?>
                    <div class="category-featured-image">
                        <img src="<?php echo esc_url($edocman_lite_image_url); ?>" 
                             alt="<?php echo esc_attr($category->title); ?>">
                    </div>
                <?php endif; ?>
                
                <div class="category-details">
                    <h1 class="category-title"><?php echo esc_html($category->title); ?></h1>
                    
                    <?php if (!empty($category->description)): ?>
                        <div class="category-description">
                            <?php echo wp_kses_post($category->description); ?>
                        </div>
                    <?php endif; ?>
                    
                    <div class="category-meta">
                        <span class="document-count">
                            <i class="dashicons dashicons-media-document"></i>
                            <?php
                            /* translators: %d: number of documents */
                            echo esc_html( sprintf( _n('%d document', '%d documents', count($documents), 'edocman-lite'), absint( count($documents) ) ) ); ?>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    <?php endif; ?>
    
    <?php if (!empty($documents)): ?>
        <div class="documents-list">
            <?php if ($category): ?>
                <div class="documents-header">
                    <h2><?php esc_html_e('Documents in this category', 'edocman-lite'); ?></h2>
                </div>
            <?php endif; ?>
            
            <div class="documents-grid">
                <?php foreach ($documents as $edocman_lite_document): ?>
                    <div class="document-item">
                        <!-- Document Image/Icon -->
                        <div class="document-thumbnail">
                            <?php if (!empty($edocman_lite_document->image)): ?>
                                <?php 
                                $edocman_lite_doc_upload_dir = wp_upload_dir();
                                $edocman_lite_doc_image_url = $edocman_lite_doc_upload_dir['baseurl'] . '/edocman/' . $edocman_lite_document->image;
                                ?>
                                <div class="document-image" onclick="window.location.href='<?php echo esc_url( add_query_arg('doc_id', $edocman_lite_document->id) ); ?>'">
                                    <img src="<?php echo esc_url($edocman_lite_doc_image_url); ?>" 
                                         alt="<?php echo esc_attr($edocman_lite_document->title); ?>">
                                    <div class="image-overlay">
                                        <i class="dashicons dashicons-visibility"></i>
                                    </div>
                                </div>
                            <?php else: ?>
                                <!-- Fallback to file icon if no image -->
                                <div class="document-icon" onclick="window.location.href='<?php echo esc_url( add_query_arg('doc_id', $edocman_lite_document->id) ); ?>'">
                                    <?php
                                    $edocman_lite_file_ext = pathinfo($edocman_lite_document->original_filename, PATHINFO_EXTENSION);
                                    $edocman_lite_icon_class = Edocman_Lite_Helper::get_file_icon($edocman_lite_document->original_filename);
                                    ?>
                                    <i class="<?php echo esc_attr($edocman_lite_icon_class); ?>"></i>
                                    <span class="file-extension"><?php echo esc_html(strtoupper($edocman_lite_file_ext)); ?></span>
                                    <div class="icon-overlay">
                                        <i class="dashicons dashicons-visibility"></i>
                                    </div>
                                </div>
                            <?php endif; ?>
                        </div>
                        
                        <!-- Document Info -->
                        <div class="document-content">
                            <h3 class="document-title">
                                <a href="<?php echo esc_url( add_query_arg('doc_id', $edocman_lite_document->id) ); ?>">
                                    <?php echo esc_html($edocman_lite_document->title); ?>
                                </a>
                            </h3>
                            
                            <?php if ($atts['show_description'] && !empty($edocman_lite_document->short_description)): ?>
                                <div class="document-short-description">
                                    <?php echo esc_html($edocman_lite_document->short_description); ?>
                                </div>
                            <?php elseif ($atts['show_description'] && !empty($edocman_lite_document->description)): ?>
                                <div class="document-description">
                                    <?php 
                                    $edocman_lite_description = wp_strip_all_tags($edocman_lite_document->description);
                                    if (strlen($edocman_lite_description) > 120) {
                                        $edocman_lite_description = substr($edocman_lite_description, 0, 120) . '...';
                                    }
                                    echo esc_html($edocman_lite_description);
                                    ?>
                                </div>
                            <?php endif; ?>
                            
                            <div class="document-meta">
                                <?php if (!empty($edocman_lite_document->file_size)): ?>
                                    <span class="file-size">
                                        <i class="dashicons dashicons-database"></i>
                                        <?php echo esc_html($edocman_lite_document->file_size); ?>
                                    </span>
                                <?php endif; ?>
                                
                                <span class="upload-date">
                                    <i class="dashicons dashicons-calendar-alt"></i>
                                    <?php echo esc_html( date_i18n(get_option('date_format'), strtotime($edocman_lite_document->created_time)) ); ?>
                                </span>
                                
                                <span class="download-count">
                                    <i class="dashicons dashicons-download"></i>
                                    <?php
                                    /* translators: %d: number of downloads */
                                    echo esc_html( sprintf( __('%d downloads', 'edocman-lite'), absint( $edocman_lite_document->downloads ?? 0 ) ) ); ?>
                                </span>
                                
                                <?php if (isset($edocman_lite_document->hits)): ?>
                                    <span class="document-hits">
                                        <i class="dashicons dashicons-visibility"></i>
                                        <?php
                                        /* translators: %d: number of views */
                                        echo esc_html( sprintf( __('%d views', 'edocman-lite'), absint( $edocman_lite_document->hits ) ) ); ?>
                                    </span>
                                <?php endif; ?>
                            </div>
                            
                            <div class="document-actions">
                                <?php
                                $edocman_lite_download_url = add_query_arg(
                                    array(
                                        'edocman_lite_download' => '1',
                                        'doc_id'                => $edocman_lite_document->id,
                                        'nonce'                 => wp_create_nonce('edocman_lite_download_' . $edocman_lite_document->id),
                                    )
                                );
                                $edocman_lite_preview_url = add_query_arg(
                                    array(
                                        'edocman_lite_view' => '1',
                                        'doc_id'            => $edocman_lite_document->id,
                                        'nonce'             => wp_create_nonce('edocman_lite_view_' . $edocman_lite_document->id),
                                    )
                                );
                                ?>
                                <a href="<?php echo esc_url($edocman_lite_download_url); ?>" 
                                   class="download-btn" target="_blank">
                                    <i class="dashicons dashicons-download"></i>
                                    <?php esc_html_e('Download', 'edocman-lite'); ?>
                                </a>
                                <a href="<?php echo esc_url( add_query_arg('doc_id', $edocman_lite_document->id) ); ?>" 
                                   class="view-details-btn">
                                    <i class="dashicons dashicons-visibility"></i>
                                    <?php esc_html_e('View Details', 'edocman-lite'); ?>
                                </a>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    <?php else: ?>
        <div class="no-documents">
            <div class="no-documents-icon">
                <i class="dashicons dashicons-media-document"></i>
            </div>
            <h3><?php esc_html_e('No documents found', 'edocman-lite'); ?></h3>
            <p><?php esc_html_e('This category does not contain any documents yet.', 'edocman-lite'); ?></p>
            <?php if ($category): ?>
                <a href="<?php echo esc_url($edocman_lite_current_url); ?>" class="back-btn">
                    <?php esc_html_e('Browse other categories', 'edocman-lite'); ?>
                </a>
            <?php endif; ?>
        </div>
    <?php endif; ?>
</div>
