<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<div class="wrap">
    <h1 class="wp-heading-inline"><?php esc_html_e('Documents', 'edocman-lite'); ?></h1>
    <a href="<?php echo esc_url( admin_url('admin.php?page=edocman-lite-documents&action=add') ); ?>" class="page-title-action">
        <?php esc_html_e('Add New', 'edocman-lite'); ?>
    </a>
    
    <hr class="wp-header-end">
    
    <form method="get" action="<?php echo esc_url( admin_url('admin.php') ); ?>" id="filter-form">
        <input type="hidden" name="page" value="edocman-lite-documents">
        
        <div class="tablenav top">
            <div class="alignleft actions">
                <select name="category_filter">
                    <option value="0"><?php esc_html_e('All Categories', 'edocman-lite'); ?></option>
                    <?php if (!empty($categories)): ?>
                        <?php foreach ($categories as $cat): ?>
                            <option value="<?php echo absint( $cat->id ); ?>" <?php selected($category_filter, $cat->id); ?>>
                                <?php echo esc_html($cat->title); ?>
                            </option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
                <input type="submit" class="button" value="<?php esc_html_e('Filter', 'edocman-lite'); ?>">
            </div>
            
            <p class="search-box">
                <input type="search" name="s" value="<?php echo esc_attr($search); ?>" placeholder="<?php esc_html_e('Search documents...', 'edocman-lite'); ?>">
                <input type="submit" class="button" value="<?php esc_html_e('Search Documents', 'edocman-lite'); ?>">
            </p>
        </div>
    </form>
    
    <form method="post" id="bulk-form">
        <input type="hidden" name="page" value="edocman-lite-documents">
        <?php wp_nonce_field('bulk-documents'); ?>
        
        <div class="tablenav top">
            <div class="alignleft actions bulkactions">
                <label for="bulk-action-selector-top" class="screen-reader-text">
                    <?php esc_html_e('Select bulk action', 'edocman-lite'); ?>
                </label>
                <select name="action" id="bulk-action-selector-top">
                    <option value="-1"><?php esc_html_e('Bulk Actions', 'edocman-lite'); ?></option>
                    <option value="delete"><?php esc_html_e('Delete', 'edocman-lite'); ?></option>
                    <option value="publish"><?php esc_html_e('Publish', 'edocman-lite'); ?></option>
                    <option value="unpublish"><?php esc_html_e('Unpublish', 'edocman-lite'); ?></option>
                </select>
                <input type="submit" id="doaction" class="button action" value="<?php esc_html_e('Apply', 'edocman-lite'); ?>">
            </div>
        </div>
    
    <table class="wp-list-table widefat fixed striped">
        <thead>
            <tr>
                <th scope="col" class="manage-column column-cb check-column">
                    <input type="checkbox" id="cb-select-all-1">
                </th>
                <th scope="col" class="manage-column column-title column-primary">
                    <?php esc_html_e('Title', 'edocman-lite'); ?>
                </th>
                <th scope="col" class="manage-column">
                    <?php esc_html_e('File', 'edocman-lite'); ?>
                </th>
                <th scope="col" class="manage-column">
                    <?php esc_html_e('Size', 'edocman-lite'); ?>
                </th>
                <th scope="col" class="manage-column">
                    <?php esc_html_e('Downloads', 'edocman-lite'); ?>
                </th>
                <th scope="col" class="manage-column">
                    <?php esc_html_e('Status', 'edocman-lite'); ?>
                </th>
                <th scope="col" class="manage-column">
                    <?php esc_html_e('Date', 'edocman-lite'); ?>
                </th>
            </tr>
        </thead>
        <tbody>
            <?php if (!empty($documents)): ?>
                <?php foreach ($documents as $edocman_lite_document): ?>
                    <tr>
                        <th scope="row" class="check-column">
                            <input type="checkbox" name="document_ids[]" value="<?php echo absint( $edocman_lite_document->id ); ?>">
                        </th>
                        <td class="title column-title has-row-actions column-primary" data-colname="<?php esc_html_e('Title', 'edocman-lite'); ?>">
                            <strong><?php echo esc_html($edocman_lite_document->title); ?></strong>
                            <div class="row-actions">
                                <span class="edit">
                                    <a href="<?php echo esc_url( admin_url('admin.php?page=edocman-lite-documents&action=edit&id=' . absint( $edocman_lite_document->id ) ) ); ?>">
                                        <?php esc_html_e('Edit', 'edocman-lite'); ?>
                                    </a> |
                                </span>
                                <span class="trash">
                                    <a href="#" class="delete-document" data-id="<?php echo absint( $edocman_lite_document->id ); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce('edocman_lite_nonce') ); ?>">
                                        <?php esc_html_e('Delete', 'edocman-lite'); ?>
                                    </a>
                                </span>
                            </div>
                        </td>
                        <td data-colname="<?php esc_html_e('File', 'edocman-lite'); ?>">
                            <?php if (!empty($edocman_lite_document->original_filename)): ?>
                                <span class="dashicons <?php echo esc_attr( Edocman_Lite_Helper::get_file_icon($edocman_lite_document->original_filename) ); ?>"></span>
                                <?php echo esc_html($edocman_lite_document->original_filename); ?>
                            <?php endif; ?>
                        </td>
                        <td data-colname="<?php esc_html_e('Size', 'edocman-lite'); ?>">
                            <?php echo esc_html($edocman_lite_document->file_size); ?>
                        </td>
                        <td data-colname="<?php esc_html_e('Downloads', 'edocman-lite'); ?>">
                            <?php echo intval($edocman_lite_document->downloads); ?>
                        </td>
                        <td data-colname="<?php esc_html_e('Status', 'edocman-lite'); ?>">
                            <?php echo wp_kses_post( $edocman_lite_document->published ? '<span class="dashicons dashicons-yes-alt" style="color: green;"></span>' : '<span class="dashicons dashicons-dismiss" style="color: red;"></span>' ); ?>
                        </td>
                        <td data-colname="<?php esc_html_e('Date', 'edocman-lite'); ?>">
                            <?php echo $edocman_lite_document->created_time ? esc_html( date_i18n( get_option('date_format'), strtotime( $edocman_lite_document->created_time ) ) ) : ''; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="7"><?php esc_html_e('No documents found.', 'edocman-lite'); ?></td>
                </tr>
            <?php endif; ?>
        </tbody>
        <tfoot>
            <tr>
                <th scope="col" class="manage-column column-cb check-column">
                    <input type="checkbox" id="cb-select-all-2">
                </th>
                <th scope="col" class="manage-column column-title column-primary">
                    <?php esc_html_e('Title', 'edocman-lite'); ?>
                </th>
                <th scope="col" class="manage-column">
                    <?php esc_html_e('File', 'edocman-lite'); ?>
                </th>
                <th scope="col" class="manage-column">
                    <?php esc_html_e('Size', 'edocman-lite'); ?>
                </th>
                <th scope="col" class="manage-column">
                    <?php esc_html_e('Downloads', 'edocman-lite'); ?>
                </th>
                <th scope="col" class="manage-column">
                    <?php esc_html_e('Status', 'edocman-lite'); ?>
                </th>
                <th scope="col" class="manage-column">
                    <?php esc_html_e('Date', 'edocman-lite'); ?>
                </th>
            </tr>
        </tfoot>
    </table>
    
    <div class="tablenav bottom">
        <div class="alignleft actions bulkactions">
            <label for="bulk-action-selector-bottom" class="screen-reader-text">
                <?php esc_html_e('Select bulk action', 'edocman-lite'); ?>
            </label>
            <select name="action2" id="bulk-action-selector-bottom">
                <option value="-1"><?php esc_html_e('Bulk Actions', 'edocman-lite'); ?></option>
                <option value="delete"><?php esc_html_e('Delete', 'edocman-lite'); ?></option>
                <option value="publish"><?php esc_html_e('Publish', 'edocman-lite'); ?></option>
                <option value="unpublish"><?php esc_html_e('Unpublish', 'edocman-lite'); ?></option>
            </select>
            <input type="submit" id="doaction2" class="button action" value="<?php esc_html_e('Apply', 'edocman-lite'); ?>">
        </div>
    </div>
    </form>
</div>

<script>
jQuery(document).ready(function($) {
    // Handle select all checkboxes
    $('#cb-select-all-1, #cb-select-all-2').on('click', function() {
        var checked = $(this).prop('checked');
        $('input[name="document_ids[]"]').prop('checked', checked);
        $('#cb-select-all-1, #cb-select-all-2').prop('checked', checked);
    });
    
    // Handle bulk form submission
    $('#bulk-form').on('submit', function(e) {
        // Get the selected action from either dropdown
        var actionTop = $('#bulk-action-selector-top').val();
        var actionBottom = $('#bulk-action-selector-bottom').val();
        var action = actionTop !== '-1' ? actionTop : actionBottom;
        
        if (action === '-1') {
            e.preventDefault();
            alert('<?php esc_html_e('Please select an action.', 'edocman-lite'); ?>');
            return false;
        }
        
        var checkedBoxes = $('input[name="document_ids[]"]:checked');
        if (checkedBoxes.length === 0) {
            e.preventDefault();
            alert('<?php esc_html_e('Please select at least one document.', 'edocman-lite'); ?>');
            return false;
        }
        
        if (action === 'delete') {
            if (!confirm('<?php esc_html_e('Are you sure you want to delete the selected documents?', 'edocman-lite'); ?>')) {
                e.preventDefault();
                return false;
            }
        }
    });
    
    // Handle individual delete links
    $('.delete-document').on('click', function(e) {
        e.preventDefault();
        if (!confirm('<?php esc_html_e('Are you sure you want to delete this document?', 'edocman-lite'); ?>')) {
            return;
        }
        
        var documentId = $(this).data('id');
        var nonce = $(this).data('nonce');
        var row = $(this).closest('tr');
        
        $.ajax({
            url: edocman_lite_ajax.ajax_url,
            type: 'POST',
            data: {
                action: 'edocman_lite_delete_document',
                document_id: documentId,
                nonce: nonce
            },
            success: function(response) {
                if (response.success) {
                    row.fadeOut(function() {
                        row.remove();
                    });
                } else {
                    alert(response.data.message);
                }
            }
        });
    });
});
</script>
