<?php
/**
 * Time Tracking Week View
 *
 * @package Freelance_Studio
 */

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

$modal_id = 'fs-week-time-entry';
?>

<div class="wrap fs-wrap">
    <h1><?php esc_html_e('Time Tracking', 'freelance-studio'); ?></h1>
    
    <!-- Tab Navigation -->
    <div class="fs-tab-nav">
        <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-time-tracking') ); ?>" class="fs-tab"><?php esc_html_e('Timer view', 'freelance-studio'); ?></a>
        <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-time-tracking&action=week') ); ?>" class="fs-tab active"><?php esc_html_e('Week view', 'freelance-studio'); ?></a>
        <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-time-tracking&action=reports') ); ?>" class="fs-tab"><?php esc_html_e('Reports', 'freelance-studio'); ?></a>
    </div>

    <!-- Week Navigation -->
    <div class="fs-week-nav">
        <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-time-tracking&action=week&week=' . ($week_offset - 1)) ); ?>" class="button">
            <span class="dashicons dashicons-arrow-left-alt2"></span> <?php esc_html_e('Previous', 'freelance-studio'); ?>
        </a>
        
        <span class="fs-week-range">
            <strong><?php echo esc_html(gmdate('M j', strtotime($week_start))); ?></strong>
            –
            <strong><?php echo esc_html(gmdate('M j, Y', strtotime($week_end))); ?></strong>
        </span>
        
        <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-time-tracking&action=week&week=' . ($week_offset + 1)) ); ?>" class="button">
            <?php esc_html_e('Next', 'freelance-studio'); ?> <span class="dashicons dashicons-arrow-right-alt2"></span>
        </a>
        
        <?php if ($week_offset != 0): ?>
            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-time-tracking&action=week') ); ?>" class="button">
                <?php esc_html_e('Today', 'freelance-studio'); ?>
            </a>
        <?php endif; ?>
    </div>

    <!-- Week Summary -->
    <div class="fs-week-summary">
        <div class="fs-summary-stat">
            <span class="fs-summary-label"><?php esc_html_e('Week Total', 'freelance-studio'); ?></span>
            <span class="fs-summary-value"><?php echo esc_html(FS_Time_Entry::format_duration($week_total_seconds)); ?></span>
        </div>
        <div class="fs-summary-stat">
            <span class="fs-summary-label"><?php esc_html_e('Billable', 'freelance-studio'); ?></span>
            <span class="fs-summary-value"><?php echo esc_html(FS_Time_Entry::format_duration($week_billable_seconds)); ?></span>
        </div>
    </div>

    <!-- Week Grid -->
    <div class="fs-week-grid">
        <?php foreach ($days as $date => $day): ?>
            <div class="fs-day-column <?php echo $date === gmdate('Y-m-d') ? 'fs-today' : ''; ?>">
                <div class="fs-day-header">
                    <span class="fs-day-name"><?php echo esc_html($day['day_short']); ?></span>
                    <span class="fs-day-date"><?php echo esc_html($day['month'] . ' ' . $day['day_num']); ?></span>
                    <span class="fs-day-total"><?php echo esc_html(FS_Time_Entry::format_duration($day['total_seconds'])); ?></span>
                </div>
                
                <div class="fs-day-entries">
                    <?php if (empty($day['entries'])): ?>
                        <div class="fs-no-entries">
                            <span class="dashicons dashicons-clock"></span>
                        </div>
                    <?php else: ?>
                        <?php foreach ($day['entries'] as $entry): ?>
                            <div class="fs-day-entry <?php echo $entry->billable ? 'fs-billable' : 'fs-non-billable'; ?> <?php echo $entry->invoiced ? 'fs-invoiced' : ''; ?>">
                                <div class="fs-entry-duration">
                                    <?php echo esc_html(FS_Time_Entry::format_duration($entry->duration)); ?>
                                </div>
                                <div class="fs-entry-project">
                                    <?php echo esc_html($entry->project_name ?: __('No project', 'freelance-studio')); ?>
                                </div>
                                <?php if ($entry->task_name): ?>
                                    <div class="fs-entry-task">
                                        <?php echo esc_html($entry->task_name); ?>
                                    </div>
                                <?php endif; ?>
                                <?php if ($entry->description): ?>
                                    <div class="fs-entry-desc">
                                        <?php echo esc_html(wp_trim_words($entry->description, 5)); ?>
                                    </div>
                                <?php endif; ?>
                                <div class="fs-entry-actions">
                                    <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-time-tracking&action=edit&id=' . $entry->id) ); ?>" title="<?php esc_attr_e('Edit', 'freelance-studio'); ?>">
                                        <span class="dashicons dashicons-edit"></span>
                                    </a>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                    
                    <!-- Quick add button -->
                    <button type="button" class="fs-quick-add-btn" data-date="<?php echo esc_attr($date); ?>">
                        <span class="dashicons dashicons-plus-alt2"></span>
                    </button>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>

<?php 
// Include the unified time entry modal form
include JEEB_FS_PLUGIN_DIR . 'includes/views/partials/time-entry-modal-form.php';
?>

<script>
jQuery(document).ready(function($) {
    // Quick add button - open modal with date
    $('.fs-quick-add-btn').on('click', function() {
        var date = $(this).data('date');
        if (typeof openTimeEntryModal === 'function') {
            openTimeEntryModal({ date: date });
        }
    });
});
</script>
