<?php
/**
 * Provide a admin area view for the work items page
 *
 * @link       https://example.com
 * @since      1.0.0
 *
 * @package    Subscription_System
 */

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

<div class="wrap">
    <h1><?php echo esc_html__('Work Items', 'subscription-system'); ?></h1>
    
    <?php settings_errors('subscription_messages'); ?>
    
    <div class="subscription-admin-container">
        <div class="subscription-admin-main">
            <div class="subscription-admin-card">
                <h2><?php echo esc_html__('All Work Items', 'subscription-system'); ?></h2>
                
                <table class="wp-list-table widefat fixed striped">
                    <thead>
                        <tr>
                            <th><?php echo esc_html__('ID', 'subscription-system'); ?></th>
                            <th><?php echo esc_html__('Name', 'subscription-system'); ?></th>
                            <th><?php echo esc_html__('Description', 'subscription-system'); ?></th>
                            <th><?php echo esc_html__('Channel', 'subscription-system'); ?></th>
                            <th><?php echo esc_html__('Status', 'subscription-system'); ?></th>
                            <th><?php echo esc_html__('Created At', 'subscription-system'); ?></th>
                            <th><?php echo esc_html__('Actions', 'subscription-system'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (!empty($work_items)) : ?>
                            <?php foreach ($work_items as $work_item) : ?>
                                <tr>
                                    <td><?php echo esc_html($work_item->id); ?></td>
                                    <td><?php echo esc_html($work_item->name); ?></td>
                                    <td><?php echo esc_html($work_item->description); ?></td>
                                    <td><?php echo esc_html($work_item->channel_name ? $work_item->channel_name : esc_html__('None', 'subscription-system')); ?></td>
                                    <td>
                                        <span class="status-badge status-<?php echo esc_attr(strtolower($work_item->status)); ?>">
                                            <?php echo esc_html(ucfirst($work_item->status)); ?>
                                        </span>
                                    </td>
                                    <td><?php echo esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($work_item->created_at))); ?></td>
                                    <td>
                                        <a href="#" class="edit-work-item" 
                                           data-id="<?php echo esc_attr($work_item->id); ?>" 
                                           data-name="<?php echo esc_attr($work_item->name); ?>" 
                                           data-description="<?php echo esc_attr($work_item->description); ?>" 
                                           data-status="<?php echo esc_attr($work_item->status); ?>"
                                           data-channel-id="<?php echo esc_attr($work_item->channel_id); ?>">
                                            <?php echo esc_html__('Edit', 'subscription-system'); ?>
                                        </a> | 
                                        <a href="<?php echo esc_url(wp_nonce_url(admin_url('edit.php?post_type=subscription&page=subscription-work-items&action=delete&work_item_id=' . $work_item->id), 'delete_work_item_' . $work_item->id)); ?>" class="delete-work-item" onclick="return confirm('<?php echo esc_js(esc_html__('Are you sure you want to delete this work item?', 'subscription-system')); ?>')">
                                            <?php echo esc_html__('Delete', 'subscription-system'); ?>
                                        </a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else : ?>
                            <tr>
                                <td colspan="7"><?php echo esc_html__('No work items found.', 'subscription-system'); ?></td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
        
        <div class="subscription-admin-sidebar">
            <div class="subscription-admin-card">
                <h2><?php echo esc_html__('Add New Work Item', 'subscription-system'); ?></h2>
                
                <form method="post" action="">
                    <?php wp_nonce_field('subscription_add_work_item', 'subscription_nonce'); ?>
                    
                    <div class="form-field">
                        <label for="work_item_name"><?php echo esc_html__('Name', 'subscription-system'); ?> <span class="required">*</span></label>
                        <input type="text" id="work_item_name" name="work_item_name" required>
                    </div>
                    
                    <div class="form-field">
                        <label for="work_item_description"><?php echo esc_html__('Description', 'subscription-system'); ?></label>
                        <textarea id="work_item_description" name="work_item_description" rows="5"></textarea>
                    </div>
                    
                    <div class="form-field">
                        <label for="channel_id"><?php echo esc_html__('Channel', 'subscription-system'); ?></label>
                        <select id="channel_id" name="channel_id">
                            <option value="0"><?php echo esc_html__('-- Select Channel --', 'subscription-system'); ?></option>
                            <?php foreach ($channels as $channel) : ?>
                                <?php if ($channel->status === 'active') : ?>
                                    <option value="<?php echo esc_attr($channel->id); ?>"><?php echo esc_html($channel->name); ?></option>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div class="form-field">
                        <label for="work_item_status"><?php echo esc_html__('Status', 'subscription-system'); ?></label>
                        <select id="work_item_status" name="work_item_status">
                            <option value="active"><?php echo esc_html__('Active', 'subscription-system'); ?></option>
                            <option value="inactive"><?php echo esc_html__('Inactive', 'subscription-system'); ?></option>
                            <option value="completed"><?php echo esc_html__('Completed', 'subscription-system'); ?></option>
                        </select>
                    </div>
                    
                    <p class="submit">
                        <input type="submit" name="add_work_item" class="button button-primary" value="<?php echo esc_attr__('Add Work Item', 'subscription-system'); ?>">
                    </p>
                </form>
            </div>
            
            <div class="subscription-admin-card" id="edit-work-item-form" style="display: none;">
                <h2><?php echo esc_html__('Edit Work Item', 'subscription-system'); ?></h2>
                
                <form method="post" action="">
                    <?php wp_nonce_field('subscription_edit_work_item', 'subscription_nonce'); ?>
                    <input type="hidden" id="edit_work_item_id" name="work_item_id">
                    
                    <div class="form-field">
                        <label for="edit_work_item_name"><?php echo esc_html__('Name', 'subscription-system'); ?> <span class="required">*</span></label>
                        <input type="text" id="edit_work_item_name" name="work_item_name" required>
                    </div>
                    
                    <div class="form-field">
                        <label for="edit_work_item_description"><?php echo esc_html__('Description', 'subscription-system'); ?></label>
                        <textarea id="edit_work_item_description" name="work_item_description" rows="5"></textarea>
                    </div>
                    
                    <div class="form-field">
                        <label for="edit_channel_id"><?php echo esc_html__('Channel', 'subscription-system'); ?></label>
                        <select id="edit_channel_id" name="channel_id">
                            <option value="0"><?php echo esc_html__('-- Select Channel --', 'subscription-system'); ?></option>
                            <?php foreach ($channels as $channel) : ?>
                                <?php if ($channel->status === 'active') : ?>
                                    <option value="<?php echo esc_attr($channel->id); ?>"><?php echo esc_html($channel->name); ?></option>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div class="form-field">
                        <label for="edit_work_item_status"><?php echo esc_html__('Status', 'subscription-system'); ?></label>
                        <select id="edit_work_item_status" name="work_item_status">
                            <option value="active"><?php echo esc_html__('Active', 'subscription-system'); ?></option>
                            <option value="inactive"><?php echo esc_html__('Inactive', 'subscription-system'); ?></option>
                            <option value="completed"><?php echo esc_html__('Completed', 'subscription-system'); ?></option>
                        </select>
                    </div>
                    
                    <p class="submit">
                        <input type="submit" name="edit_work_item" class="button button-primary" value="<?php echo esc_attr__('Update Work Item', 'subscription-system'); ?>">
                        <button type="button" class="button cancel-edit"><?php echo esc_html__('Cancel', 'subscription-system'); ?></button>
                    </p>
                </form>
            </div>
        </div>
    </div>
</div>

<style>
    .subscription-admin-container {
        display: flex;
        flex-wrap: wrap;
        margin-top: 20px;
    }
    
    .subscription-admin-main {
        flex: 2;
        margin-right: 20px;
    }
    
    .subscription-admin-sidebar {
        flex: 1;
        min-width: 300px;
    }
    
    .subscription-admin-card {
        background: #fff;
        border: 1px solid #ccd0d4;
        box-shadow: 0 1px 1px rgba(0,0,0,.04);
        margin-bottom: 20px;
        padding: 15px;
    }
    
    .form-field {
        margin-bottom: 15px;
    }
    
    .form-field label {
        display: block;
        margin-bottom: 5px;
        font-weight: 600;
    }
    
    .form-field input[type="text"],
    .form-field textarea,
    .form-field select {
        width: 100%;
    }
    
    .required {
        color: #dc3232;
    }
    
    .status-badge {
        display: inline-block;
        padding: 3px 8px;
        border-radius: 3px;
        font-size: 12px;
        font-weight: 600;
    }
    
    .status-active {
        background-color: #d4edda;
        color: #155724;
    }
    
    .status-inactive {
        background-color: #f8d7da;
        color: #721c24;
    }
    
    .status-completed {
        background-color: #cce5ff;
        color: #004085;
    }
</style>

<script>
jQuery(document).ready(function($) {
    // Edit work item
    $('.edit-work-item').on('click', function(e) {
        e.preventDefault();
        
        var id = $(this).data('id');
        var name = $(this).data('name');
        var description = $(this).data('description');
        var status = $(this).data('status');
        var channelId = $(this).data('channel-id');
        
        $('#edit_work_item_id').val(id);
        $('#edit_work_item_name').val(name);
        $('#edit_work_item_description').val(description);
        $('#edit_work_item_status').val(status);
        $('#edit_channel_id').val(channelId);
        
        $('#edit-work-item-form').show();
    });
    
    // Cancel edit
    $('.cancel-edit').on('click', function() {
        $('#edit-work-item-form').hide();
    });
});
</script> 