<?php
/**
 * Unified Task Modal Partial
 *
 * Shared task create/edit modal used across Kanban, Project, and Client views.
 *
 * Expected variables:
 * - $task_modal_context: 'kanban' | 'project' | 'client' (required)
 * - $task_modal_projects: Array of projects for dropdown (required for kanban/client)
 * - $task_modal_milestones: Array of milestones (optional, project context only)
 * - $task_modal_project_id: Fixed project ID (project context only)
 * - $task_modal_client_id: Fixed client ID (client context only)
 *
 * @package Freelance_Studio
 */

if (!defined('ABSPATH')) {
    exit;
}

$context = isset($task_modal_context) ? $task_modal_context : 'kanban';
$modal_projects = isset($task_modal_projects) ? $task_modal_projects : array();
$modal_milestones = isset($task_modal_milestones) ? $task_modal_milestones : array();
$fixed_project_id = isset($task_modal_project_id) ? intval($task_modal_project_id) : 0;
$fixed_client_id = isset($task_modal_client_id) ? intval($task_modal_client_id) : 0;

// Show project dropdown in kanban and client contexts (not project, since it's fixed)
$show_project_field = ($context !== 'project');

// Show milestone field only in project context when milestones exist
$show_milestone_field = ($context === 'project' && !empty($modal_milestones));

// Show client-visible checkbox only in project context
$show_client_visible = ($context === 'project');

// Get role label for assignee dropdown
$my_role = get_option('fs_business_type', 'freelancer');
$role_labels = array(
    'freelancer' => __('Freelancer', 'freelance-studio'),
    'designer' => __('Designer', 'freelance-studio'),
    'developer' => __('Developer', 'freelance-studio'),
    'project_manager' => __('Project Manager', 'freelance-studio'),
    'consultant' => __('Consultant', 'freelance-studio'),
    'agency' => __('Agency', 'freelance-studio'),
);
$my_role_label = isset($role_labels[$my_role]) ? $role_labels[$my_role] : __('Freelancer', 'freelance-studio');

// Get priority options
$priorities = FS_Task::get_priorities();
?>

<div id="fs-task-modal" class="fs-modal" data-context="<?php echo esc_attr($context); ?>">
    <div class="fs-modal-content fs-modal--large">
        <div class="fs-modal-header">
            <h2 class="fs-modal-title"><?php esc_html_e('Add Task', 'freelance-studio'); ?></h2>
            <button type="button" class="fs-modal-close">&times;</button>
        </div>
        <form id="fs-task-form">
            <div class="fs-modal-body">
                <input type="hidden" id="fs-task-id" name="id" value="">
                
                <!-- Status: hidden input for new tasks, visible dropdown for editing -->
                <input type="hidden" id="fs-task-status" name="status" value="pending">
                <div id="fs-task-status-row" class="fs-form-row" style="display: none;">
                    <div class="fs-form-group fs-form-group-full">
                        <label for="fs-task-status-select"><?php esc_html_e('Status', 'freelance-studio'); ?></label>
                        <select id="fs-task-status-select">
                            <?php 
                            $task_statuses = FS_View_Context::get_task_status_labels();
                            $editable_statuses = array_diff_key($task_statuses, array('archived' => 1, 'cancelled' => 1));
                            foreach ($editable_statuses as $skey => $slabel): ?>
                                <option value="<?php echo esc_attr($skey); ?>"><?php echo esc_html($slabel); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                
                <?php if ($fixed_project_id): ?>
                    <input type="hidden" id="fs-task-project" name="project_id" value="<?php echo esc_attr($fixed_project_id); ?>">
                <?php endif; ?>
                
                <?php if ($fixed_client_id): ?>
                    <input type="hidden" id="fs-task-client" name="client_id" value="<?php echo esc_attr($fixed_client_id); ?>">
                <?php endif; ?>
                
                <div class="fs-form-row">
                    <div class="fs-form-group fs-form-group-full">
                        <label for="fs-task-title"><?php esc_html_e('Task Title', 'freelance-studio'); ?> <span class="required">*</span></label>
                        <input type="text" id="fs-task-title" name="title" required>
                    </div>
                </div>
                
                <div class="fs-form-row">
                    <div class="fs-form-group fs-form-group-full">
                        <label for="fs-task-description"><?php esc_html_e('Description', 'freelance-studio'); ?></label>
                        <textarea id="fs-task-description" name="description" rows="3"></textarea>
                    </div>
                </div>
                
                <?php if ($show_project_field): ?>
                <div class="fs-form-row fs-form-row-2col">
                    <div class="fs-form-group">
                        <label for="fs-task-project"><?php esc_html_e('Project', 'freelance-studio'); ?></label>
                        <select id="fs-task-project" name="project_id">
                            <option value=""><?php esc_html_e('— No Project —', 'freelance-studio'); ?></option>
                            <?php foreach ($modal_projects as $proj_item): ?>
                                <option value="<?php echo esc_attr($proj_item->id); ?>">
                                    <?php echo esc_html($proj_item->name); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="fs-form-group">
                        <label for="fs-task-priority"><?php esc_html_e('Priority', 'freelance-studio'); ?></label>
                        <select id="fs-task-priority" name="priority">
                            <?php foreach ($priorities as $key => $label): ?>
                                <option value="<?php echo esc_attr($key); ?>" <?php selected($key, 'medium'); ?>>
                                    <?php echo esc_html($label); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                <?php else: ?>
                <div class="fs-form-row fs-form-row-2col">
                    <div class="fs-form-group">
                        <label for="fs-task-assigned"><?php esc_html_e('Assign To', 'freelance-studio'); ?></label>
                        <select id="fs-task-assigned" name="assigned_to">
                            <?php /* translators: 1. me value */ ?>
                            <option value="freelancer"><?php echo wp_kses_post( sprintf( __('Me (%s)', 'freelance-studio'), esc_html($my_role_label))); ?></option>
                            <optgroup label="<?php esc_attr_e('Team Members', 'freelance-studio'); ?>">
                                <option value="client"><?php esc_html_e('Client', 'freelance-studio'); ?></option>
                                <option value="team_designer"><?php esc_html_e('Designer', 'freelance-studio'); ?></option>
                                <option value="developer"><?php esc_html_e('Developer', 'freelance-studio'); ?></option>
                                <option value="contractor"><?php esc_html_e('Contractor', 'freelance-studio'); ?></option>
                                <option value="copywriter"><?php esc_html_e('Copywriter', 'freelance-studio'); ?></option>
                            </optgroup>
                        </select>
                    </div>
                    <div class="fs-form-group">
                        <label for="fs-task-priority"><?php esc_html_e('Priority', 'freelance-studio'); ?></label>
                        <select id="fs-task-priority" name="priority">
                            <?php foreach ($priorities as $key => $label): ?>
                                <option value="<?php echo esc_attr($key); ?>" <?php selected($key, 'medium'); ?>>
                                    <?php echo esc_html($label); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                <?php endif; ?>
                
                <div class="fs-form-row fs-form-row-2col">
                    <div class="fs-form-group">
                        <label for="fs-task-due"><?php esc_html_e('Due Date', 'freelance-studio'); ?></label>
                        <input type="date" id="fs-task-due" name="due_date">
                    </div>
                    <div class="fs-form-group">
                        <label for="fs-task-hours"><?php esc_html_e('Estimated Hours', 'freelance-studio'); ?></label>
                        <input type="number" id="fs-task-hours" name="estimated_hours" step="0.25" min="0">
                    </div>
                </div>
                
                <?php if ($show_project_field): ?>
                <div class="fs-form-row">
                    <div class="fs-form-group fs-form-group-full">
                        <label for="fs-task-assigned"><?php esc_html_e('Assigned To', 'freelance-studio'); ?></label>
                        <select id="fs-task-assigned" name="assigned_to">
                            <?php /* translators: 1. me value */ ?>
                            <option value="freelancer"><?php echo wp_kses_post( sprintf( __('Me (%s)', 'freelance-studio'), esc_html($my_role_label))); ?></option>
                            <optgroup label="<?php esc_attr_e('Team Members', 'freelance-studio'); ?>">
                                <option value="client"><?php esc_html_e('Client', 'freelance-studio'); ?></option>
                                <option value="team_designer"><?php esc_html_e('Designer', 'freelance-studio'); ?></option>
                                <option value="developer"><?php esc_html_e('Developer', 'freelance-studio'); ?></option>
                                <option value="contractor"><?php esc_html_e('Contractor', 'freelance-studio'); ?></option>
                                <option value="copywriter"><?php esc_html_e('Copywriter', 'freelance-studio'); ?></option>
                            </optgroup>
                        </select>
                    </div>
                </div>
                <?php endif; ?>
                
                <?php if ($show_milestone_field): ?>
                <div class="fs-form-row">
                    <div class="fs-form-group fs-form-group-full">
                        <label for="fs-task-milestone"><?php esc_html_e('Milestone', 'freelance-studio'); ?></label>
                        <select id="fs-task-milestone" name="milestone_id">
                            <option value=""><?php esc_html_e('— No Milestone —', 'freelance-studio'); ?></option>
                            <?php foreach ($modal_milestones as $milestone): ?>
                                <option value="<?php echo esc_attr($milestone->id); ?>"><?php echo esc_html($milestone->name); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                <?php endif; ?>
                
                <?php if ($show_client_visible): ?>
                <div class="fs-form-row">
                    <div class="fs-form-group fs-form-group-full">
                        <label class="fs-checkbox-label">
                            <input type="checkbox" id="fs-task-client-visible" name="is_client_visible" value="1" checked>
                            <?php esc_html_e('Visible to client in portal', 'freelance-studio'); ?>
                        </label>
                    </div>
                </div>
                <?php endif; ?>
                
                <div class="fs-form-row">
                    <div class="fs-form-group fs-form-group-full fs-recurring-toggle-row">
                        <label class="fs-checkbox-label">
                            <input type="checkbox" id="fs-task-non-billable" name="is_non_billable" value="1">
                            <?php esc_html_e('Non-billable task', 'freelance-studio'); ?>
                            <span class="fs-non-billable-hint" title="<?php esc_attr_e('Time tracked on this task will automatically be marked as non-billable', 'freelance-studio'); ?>">ⓘ</span>
                        </label>
                    </div>
                </div>
                
                <div class="fs-form-row">
                    <div class="fs-form-group fs-form-group-full fs-recurring-toggle-row">
                        <label class="fs-checkbox-label">
                            <input type="checkbox" id="fs-task-recurring" name="is_recurring" value="1">
                            <?php esc_html_e('Recurring task', 'freelance-studio'); ?>
                            <span class="fs-recurring-icon" title="<?php esc_attr_e('Task will automatically reappear on the set schedule when completed', 'freelance-studio'); ?>">↻</span>
                        </label>
                    </div>
                </div>
                
                <div id="fs-task-recurring-options" class="fs-recurring-options" style="display: none;">
                    <div class="fs-form-row fs-form-row-2col">
                        <div class="fs-form-group">
                            <label for="fs-task-frequency"><?php esc_html_e('Repeat Every', 'freelance-studio'); ?></label>
                            <select id="fs-task-frequency" name="recurring_frequency">
                                <?php foreach (FS_Task::get_recurring_frequencies() as $key => $label): ?>
                                    <option value="<?php echo esc_attr($key); ?>"><?php echo esc_html($label); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="fs-form-group">
                            <label for="fs-task-recur-end"><?php esc_html_e('End Date (optional)', 'freelance-studio'); ?></label>
                            <input type="date" id="fs-task-recur-end" name="recurring_end_date">
                        </div>
                    </div>
                    <div id="fs-task-recurring-days-row" class="fs-form-group fs-form-group-full fs-recurring-days-row" style="display: none;">
                        <label><?php esc_html_e('On These Days', 'freelance-studio'); ?></label>
                        <div class="fs-day-picker">
                            <?php
                            $days = array(
                                'mon' => __('M', 'freelance-studio'),
                                'tue' => __('T', 'freelance-studio'),
                                'wed' => __('W', 'freelance-studio'),
                                'thu' => __('T', 'freelance-studio'),
                                'fri' => __('F', 'freelance-studio'),
                                'sat' => __('S', 'freelance-studio'),
                                'sun' => __('S', 'freelance-studio'),
                            );
                            foreach ($days as $key => $label): ?>
                                <label class="fs-day-chip" title="<?php echo esc_attr(ucfirst($key)); ?>">
                                    <input type="checkbox" name="recurring_days[]" value="<?php echo esc_attr($key); ?>">
                                    <span><?php echo esc_html($label); ?></span>
                                </label>
                            <?php endforeach; ?>
                        </div>
                    </div>
                    <p class="fs-recurring-hint"><?php esc_html_e('A new task will be created automatically each time this one is completed.', 'freelance-studio'); ?></p>
                    <div class="fs-form-row" style="margin-top: 8px;">
                        <div class="fs-form-group fs-form-group-full">
                            <label class="fs-checkbox-label">
                                <input type="checkbox" id="fs-task-hide-until-due" name="hide_until_due" value="1">
                                <?php esc_html_e('Hide until due date', 'freelance-studio'); ?>
                            </label>
                            <p class="description" style="margin-left: 24px; margin-top: 4px;"><?php esc_html_e('Next task won\'t appear in lists until its due date arrives.', 'freelance-studio'); ?></p>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="fs-modal-footer">
                <button type="button" class="fs-btn fs-btn-secondary fs-modal-cancel"><?php esc_html_e('Cancel', 'freelance-studio'); ?></button>
                <button type="submit" class="fs-btn fs-btn-primary fs-task-save-btn"><?php esc_html_e('Add Task', 'freelance-studio'); ?></button>
            </div>
        </form>
    </div>
</div>
