<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<div class="wrap">
    <h1 class="wp-heading-inline"><?php esc_html_e('Categories', 'edocman-lite'); ?></h1>
    <a href="<?php echo esc_url( admin_url('admin.php?page=edocman-lite-categories&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' ) ); ?>">
        <input type="hidden" name="page" value="edocman-lite-categories">
        
        <p class="search-box">
            <input type="search" name="s" value="<?php echo esc_attr($search); ?>" placeholder="<?php esc_html_e('Search categories...', 'edocman-lite'); ?>">
            <input type="submit" class="button" value="<?php esc_html_e('Search Categories', 'edocman-lite'); ?>">
        </p>
    </form>
    
    <table class="wp-list-table widefat fixed striped">
        <thead>
            <tr>
                <th scope="col" class="manage-column column-cb check-column">
                    <input type="checkbox">
                </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('Description', '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($categories_tree)): ?>
                <?php foreach ($categories_tree as $edocman_lite_category): ?>
                    <tr>
                        <th scope="row" class="check-column">
                            <input type="checkbox" name="category_ids[]" value="<?php echo absint( $edocman_lite_category->id ); ?>">
                        </th>
                        <td class="title column-title has-row-actions column-primary" data-colname="<?php esc_html_e('Title', 'edocman-lite'); ?>">
                            <strong>
                                <?php 
                                $edocman_lite_indent = str_repeat('— ', isset($edocman_lite_category->level) ? $edocman_lite_category->level : 0);
                                echo esc_html($edocman_lite_indent . $edocman_lite_category->title); 
                                ?>
                            </strong>
                            <div class="row-actions">
                                <span class="edit">
                                    <a href="<?php echo esc_url( admin_url('admin.php?page=edocman-lite-categories&action=edit&id=' . absint( $edocman_lite_category->id ) ) ); ?>">
                                        <?php esc_html_e('Edit', 'edocman-lite'); ?>
                                    </a> |
                                </span>
                                <span class="trash">
                                    <a href="#" class="delete-category" data-id="<?php echo absint( $edocman_lite_category->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('Description', 'edocman-lite'); ?>">
                            <?php echo esc_html( wp_trim_words( wp_strip_all_tags( $edocman_lite_category->description ), 10 ) ); ?>
                        </td>
                        <td data-colname="<?php esc_html_e('Status', 'edocman-lite'); ?>">
                            <?php echo wp_kses_post( $edocman_lite_category->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_category->created_time ? esc_html( date_i18n( get_option( 'date_format' ), strtotime( $edocman_lite_category->created_time ) ) ) : ''; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="5"><?php esc_html_e('No categories found.', 'edocman-lite'); ?></td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<script>
jQuery(document).ready(function($) {
    $('.delete-category').on('click', function(e) {
        e.preventDefault();
        if (!confirm('<?php esc_html_e('Are you sure you want to delete this category?', 'edocman-lite'); ?>')) {
            return;
        }
        
        var categoryId = $(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_category',
                category_id: categoryId,
                nonce: nonce
            },
            success: function(response) {
                if (response.success) {
                    row.fadeOut(function() {
                        row.remove();
                    });
                } else {
                    alert(response.data.message);
                }
            }
        });
    });
});
</script>
