<?php
/**
 * Provide a admin area view for the plugin
 *
 * This file is used to markup the admin-facing aspects of the plugin.
 *
 * @link       https://yourwebsite.com
 * @since      1.0.0
 *
 * @package    Subscription_System
 * @subpackage Subscription_System/admin/partials
 */

// If this file is called directly, abort.
if (!defined('WPINC')) {
    die;
}

// Check if there's a category_add_failed error
$has_add_error = false;
$settings_errors = get_settings_errors('subscription_messages');
foreach ($settings_errors as $error) {
    if ($error['code'] === 'category_add_failed') {
        $has_add_error = true;
        break;
    }
}
?>

<div class="subscription-system-admin-page">
    <div class="wrap">
        <h1 class="wp-heading-inline"><?php echo esc_html__('Plan Categories', 'subscription-system'); ?></h1>
        <a href="#" class="page-title-action" id="show-add-category"><?php echo esc_html__('Add New Category', 'subscription-system'); ?></a>
        <hr class="wp-header-end">
        
        <?php settings_errors('subscription_messages'); ?>

        <!-- Troubleshooting link - only shown when needed -->
        <?php 
        $show_notice = empty($categories) || $has_add_error;
        if ($show_notice) : 
        ?>
            <div class="notice notice-info is-dismissible">
                <p>
                    <?php echo esc_html__('If you\'re having trouble adding categories, you may need to recreate the database table.', 'subscription-system'); ?>
                    <a href="<?php echo esc_url(wp_nonce_url(admin_url('edit.php?post_type=subscription&page=subscription-plan-categories&action=recreate_table'), 'recreate_categories_table')); ?>" class="button button-small">
                        <?php echo esc_html__('Recreate Categories Table', 'subscription-system'); ?>
                    </a>
                </p>
            </div>
        <?php endif; ?>
        
        <!-- Add New Category Form -->
        <div id="add-category-form" class="postbox" style="display: none; margin: 20px 0; padding: 0; background: #fff;">
            <div class="postbox-header">
                <h2 class="hndle" style="padding: 8px 12px;"><?php echo esc_html__('Add New Category', 'subscription-system'); ?></h2>
            </div>
            <div class="inside" style="padding: 12px;">
                <form method="post" action="">
                    <?php wp_nonce_field('subscription_add_category', 'subscription_nonce'); ?>
                    <table class="form-table" role="presentation">
                        <tr>
                            <th scope="row" style="padding: 15px 10px 15px 0;"><label for="category_name"><?php echo esc_html__('Category Name', 'subscription-system'); ?></label></th>
                            <td style="padding: 15px 10px;"><input type="text" name="category_name" id="category_name" class="regular-text" style="width: 25em;" required></td>
                        </tr>
                        <tr>
                            <th scope="row" style="padding: 15px 10px 15px 0;"><label for="category_description"><?php echo esc_html__('Category Description', 'subscription-system'); ?></label></th>
                            <td style="padding: 15px 10px;"><textarea name="category_description" id="category_description" class="large-text" style="width: 25em;" rows="4"></textarea></td>
                        </tr>
                    </table>
                    <p class="submit" style="padding: 10px 0 0 0; margin-top: 0;">
                        <input type="submit" name="add_category" class="button button-primary" value="<?php echo esc_attr__('Add Category', 'subscription-system'); ?>">
                        <button type="button" class="button" id="cancel-add-category" onclick="jQuery('#add-category-form').hide(); return false;"><?php echo esc_html__('Cancel', 'subscription-system'); ?></button>
                    </p>
                </form>
            </div>
        </div>
        
        <!-- Edit Category Form (hidden by default, shown via JavaScript) -->
        <div id="edit-category-form" class="postbox" style="display: none; margin: 20px 0; padding: 0; background: #fff;">
            <div class="postbox-header">
                <h2 class="hndle" style="padding: 8px 12px;"><?php echo esc_html__('Edit Category', 'subscription-system'); ?></h2>
            </div>
            <div class="inside" style="padding: 12px;">
                <form method="post" action="">
                    <?php wp_nonce_field('subscription_edit_category', 'subscription_nonce'); ?>
                    <input type="hidden" name="category_id" id="edit_category_id">
                    <table class="form-table" role="presentation">
                        <tr>
                            <th scope="row" style="padding: 15px 10px 15px 0;"><label for="edit_category_name"><?php echo esc_html__('Category Name', 'subscription-system'); ?></label></th>
                            <td style="padding: 15px 10px;"><input type="text" name="category_name" id="edit_category_name" class="regular-text" style="width: 25em;" required></td>
                        </tr>
                        <tr>
                            <th scope="row" style="padding: 15px 10px 15px 0;"><label for="edit_category_description"><?php echo esc_html__('Category Description', 'subscription-system'); ?></label></th>
                            <td style="padding: 15px 10px;"><textarea name="category_description" id="edit_category_description" class="large-text" style="width: 25em;" rows="4"></textarea></td>
                        </tr>
                    </table>
                    <p class="submit" style="padding: 10px 0 0 0; margin-top: 0;">
                        <input type="submit" name="edit_category" class="button button-primary" value="<?php echo esc_attr__('Update Category', 'subscription-system'); ?>">
                        <button type="button" class="button" id="cancel-edit"><?php echo esc_html__('Cancel', 'subscription-system'); ?></button>
                    </p>
                </form>
            </div>
        </div>
        
        <!-- Categories List Table -->
        <div class="tablenav top">
            <div class="alignleft actions">
                <!-- You can add bulk actions here if needed -->
            </div>
            <br class="clear">
        </div>
        
        <table class="wp-list-table widefat fixed striped">
            <thead>
                <tr>
                    <th scope="col" class="manage-column column-id column-primary" style="width: 5%;"><?php echo esc_html__('ID', 'subscription-system'); ?></th>
                    <th scope="col" class="manage-column column-code-id" style="width: 10%;"><?php echo esc_html__('Code ID', 'subscription-system'); ?></th>
                    <th scope="col" class="manage-column column-name" style="width: 20%;"><?php echo esc_html__('Category Name', 'subscription-system'); ?></th>
                    <th scope="col" class="manage-column column-description" style="width: 45%;"><?php echo esc_html__('Description', 'subscription-system'); ?></th>
                    <th scope="col" class="manage-column column-actions" style="width: 20%; text-align: right;"><?php echo esc_html__('Actions', 'subscription-system'); ?></th>
                </tr>
            </thead>
            <tbody id="the-list">
                <?php if (empty($categories)) : ?>
                    <tr>
                        <td colspan="5" class="colspanchange"><?php echo esc_html__('No categories found.', 'subscription-system'); ?></td>
                    </tr>
                <?php else : ?>
                    <?php foreach ($categories as $category) : ?>
                        <tr>
                            <td class="column-id column-primary" data-colname="<?php echo esc_attr__('ID', 'subscription-system'); ?>"><?php echo esc_html($category->id); ?></td>
                            <td class="column-code-id" data-colname="<?php echo esc_attr__('Code ID', 'subscription-system'); ?>"><code><?php echo esc_html($category->id); ?></code></td>
                            <td class="column-name" data-colname="<?php echo esc_attr__('Category Name', 'subscription-system'); ?>"><?php echo esc_html($category->category_name); ?></td>
                            <td class="column-description" data-colname="<?php echo esc_attr__('Description', 'subscription-system'); ?>">
                                <?php echo esc_html(isset($category->description) ? $category->description : 
                                    (isset($category->category_description) ? $category->category_description : '')); ?>
                            </td>
                            <td class="column-actions" data-colname="<?php echo esc_attr__('Actions', 'subscription-system'); ?>" style="text-align: right;">
                                <button type="button" class="button button-small edit-category" 
                                        data-id="<?php echo esc_attr($category->id); ?>" 
                                        data-name="<?php echo esc_attr($category->category_name); ?>"
                                        data-description="<?php echo esc_attr(isset($category->description) ? $category->description : 
                                            (isset($category->category_description) ? $category->category_description : '')); ?>">
                                    <?php echo esc_html__('Edit', 'subscription-system'); ?>
                                </button>
                                <button type="button" class="button button-small delete-category" 
                                        data-id="<?php echo esc_attr($category->id); ?>"
                                        data-nonce="<?php echo esc_attr(wp_create_nonce('delete_category_' . $category->id)); ?>">
                                    <?php echo esc_html__('Delete', 'subscription-system'); ?>
                                </button>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
            <tfoot>
                <tr>
                    <th scope="col" class="manage-column column-id column-primary"><?php echo esc_html__('ID', 'subscription-system'); ?></th>
                    <th scope="col" class="manage-column column-code-id"><?php echo esc_html__('Code ID', 'subscription-system'); ?></th>
                    <th scope="col" class="manage-column column-name"><?php echo esc_html__('Category Name', 'subscription-system'); ?></th>
                    <th scope="col" class="manage-column column-description"><?php echo esc_html__('Description', 'subscription-system'); ?></th>
                    <th scope="col" class="manage-column column-actions" style="text-align: right;"><?php echo esc_html__('Actions', 'subscription-system'); ?></th>
                </tr>
            </tfoot>
        </table>
        
        <div class="tablenav bottom">
            <div class="alignleft actions">
                <!-- You can add bulk actions here if needed -->
            </div>
            <br class="clear">
        </div>
    </div>
</div>

<script>
jQuery(document).ready(function($) {
    // Show/hide add category form
    $('#show-add-category').on('click', function(e) {
        e.preventDefault();
        $('#add-category-form').toggle();
        if ($('#add-category-form').is(':visible')) {
            $('#category_name').focus();
        }
    });
    
    // Edit category
    $('.edit-category').on('click', function() {
        var id = $(this).data('id');
        var name = $(this).data('name');
        var description = $(this).data('description');
        
        $('#edit_category_id').val(id);
        $('#edit_category_name').val(name);
        $('#edit_category_description').val(description);
        
        $('#edit-category-form').show();
        $('#edit_category_name').focus();
        $('html, body').animate({
            scrollTop: $('#edit-category-form').offset().top - 50
        }, 300);
    });
    
    // Delete category
    $('.delete-category').on('click', function() {
        var id = $(this).data('id');
        var nonce = $(this).data('nonce');
        
        if (confirm('<?php echo esc_js(__('Are you sure you want to delete this category?', 'subscription-system')); ?>')) {
            window.location.href = '<?php echo esc_url(admin_url('edit.php?post_type=subscription&page=subscription-plan-categories&action=delete&category_id=')); ?>' + id + '&_wpnonce=' + nonce;
        }
    });
    
    // Cancel edit
    $('#cancel-edit').on('click', function(e) {
        e.preventDefault();
        $('#edit-category-form').hide();
    });
});
</script> 