<?php
require_once '../config.php'; // Include your configuration file if needed


if(isset($_GET['employee_life_cycle_id'])){
    $employee_id = $_GET['employee_life_cycle_id'];

    $sql = "SELECT 
        t1.loi_id,
        t2.created_at as letter_of_intent_issue,

        t1.joining_date,

        t3.duration_start as internship_start,
        t3.duration_end as internship_end,
        t3.issue_date as internship_complete_issue,

        t1.probation_date,
        t1.con_after_prob,

        t1.full_time_date,
        t4.issue_date as full_time_issue,

        t4.issue_date as appraisal_issue,

        t1.working_status,
        t1.working_status_date,
        t1.notice_period,

        t6.issue_date as relieving_issue

        FROM users t1 
        LEFT JOIN letters_of_intent t2 ON t2.id=t1.loi_id
        LEFT JOIN (
            SELECT
                il.duration_start, 
                il.duration_end, 
                icl.issue_date,
                icl.employee_id
            FROM internship_complete_letter icl
            INNER JOIN internship_letter il ON il.employee_id = icl.employee_id
        ) t3 ON t3.employee_id=t1.employee_id
        LEFT JOIN full_time_letter t4 ON t4.employee_id=t1.employee_id
        LEFT JOIN (
            SELECT 
                employee_id, MAX(issue_date) as issue_date
            FROM appraisal_letter 
            GROUP BY employee_id
        ) t5 ON t5.employee_id=t1.employee_id
        LEFT JOIN relieving_letter t6 ON t6.employee_id=t1.employee_id
        WHERE t1.employee_id = '$employee_id';";
    $result = mysqli_query($db, $sql);
    $user = mysqli_fetch_assoc($result);

?>

    <style>
        .stage-card {
            background: white;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 15px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            position: relative;
        }

        .stage-card:hover {
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
            transform: translateY(-2px);
        }

        .stage-card.completed {
            background: #e8f5e9;
        }

        .stage-header {
            display: flex;
            align-items: center;
            gap: 15px;
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f0f0;
        }

        .stage-icon {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 22px;
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.2);
            flex-shrink: 0;
        }

        .stage-info {
            flex: 1;
        }

        .stage-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
            margin: 0;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .stage-desc {
            font-size: 13px;
            color: #666;
            margin: 5px 0 0 0;
        }

        .stage-number {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            background: #e9ecef;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 14px;
            font-weight: 700;
            color: #667eea;
            flex-shrink: 0;
        }

        .complete-indicator {
            font-size: 28px;
            color: #43e97b;
            flex-shrink: 0;
        }

        .incomplete-indicator {
            font-size: 28px;
            color: #ddd;
            flex-shrink: 0;
        }

        .detail-list {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .detail-list li {
            padding: 8px 0;
            font-size: 13px;
            color: #555;
            display: flex;
            align-items: center;
        }

        .detail-list li i {
            color: #667eea;
            margin-right: 10px;
            font-size: 10px;
        }

        .badge-status {
            font-size: 11px;
            padding: 4px 10px;
            border-radius: 20px;
        }

        .completion-date {
            font-size: 12px;
            color: #43e97b;
            margin-top: 10px;
            font-weight: 600;
            padding-top: 10px;
            border-top: 1px dashed #e0e0e0;
        }

        .stage-1 {
            background: linear-gradient(135deg, #667eea, #764ba2);
        }

        .stage-2 {
            background: linear-gradient(135deg, #f093fb, #f5576c);
        }

        .stage-3 {
            background: linear-gradient(135deg, #ffa726, #fb8c00);
        }

        .stage-4 {
            background: linear-gradient(135deg, #4facfe, #00f2fe);
        }

        .stage-5 {
            background: linear-gradient(135deg, #43e97b, #38f9d7);
        }

        .stage-6 {
            background: linear-gradient(135deg, #fa709a, #fee140);
        }

        .stage-7 {
            background: linear-gradient(135deg, #30cfd0, #330867);
        }

        .stage-8 {
            background: linear-gradient(135deg, #a8edea, #fed6e3);
        }

        .offcanvas-body {
            padding: 20px;
        }
    </style>

    <?php if(!empty($user['relieving_issue'])){ ?>
    <!-- Relieving -->
    <div class="stage-card completed" data-stage="8">
        <div class="stage-header">
            <div class="stage-icon stage-8">
                <i class="fas fa-handshake"></i>
            </div>
            <div class="stage-info">
                <div class="stage-title">
                    Relieving
                    <span class="badge bg-secondary badge-status">End</span>
                </div>
                <p class="stage-desc">Exit formalities & clearance</p>
            </div>
            <i class="fas fa-circle complete-indicator"></i>
        </div>
        <div class="completion-date">Completed on: <?php echo !empty($user['relieving_issue']) ? date('d M Y', strtotime($user['relieving_issue'])) : ''; ?></div>
    </div>
    <?php } ?>

    <?php if(!empty($user['working_status_date'])){ ?>
    <!-- Resignation -->
    <div class="stage-card completed" data-stage="7">
        <div class="stage-header">
            <div class="stage-icon stage-7">
                <i class="fas fa-user-minus"></i>
            </div>
            <div class="stage-info">
                <div class="stage-title"><?php echo !empty($user['working_status']) ? $user['working_status'] : 'Resignation'; ?></div>
                <p class="stage-desc">Notice period: <?php echo !empty($user['notice_period']) ? $user['notice_period'] : '0'; ?> days</p>
            </div>
            <i class="fas fa-circle complete-indicator"></i>
        </div>
        <div class="completion-date">Completed on: <?php echo !empty($user['working_status_date']) ? date('d M Y', strtotime($user['working_status_date'])) : ''; ?></div>
    </div>
    <?php } ?>

    <?php if(!empty($user['appraisal_issue'])){ ?>
    <!-- Appraisal -->
    <div class="stage-card completed" data-stage="6">
        <div class="stage-header">
            <div class="stage-icon stage-6">
                <i class="fas fa-chart-line"></i>
            </div>
            <div class="stage-info">
                <div class="stage-title">
                    Appraisal
                    <span class="badge bg-warning badge-status">Annual</span>
                </div>
                <p class="stage-desc">Performance review & growth</p>
            </div>
            <i class="fas fa-circle complete-indicator"></i>
        </div>
        <div class="completion-date">Completed on: <?php echo !empty($user['appraisal_issue']) ? date('d M Y', strtotime($user['appraisal_issue'])) : ''; ?></div>
    </div>
    <?php } ?>

    <?php if(!empty($user['full_time_issue'])){ ?>
    <!-- Full Time -->
    <div class="stage-card completed" data-stage="5">
        <div class="stage-header">
            <div class="stage-icon stage-5">
                <i class="fas fa-check-circle"></i>
            </div>
            <div class="stage-info">
                <div class="stage-title">Full Time</div>
                <p class="stage-desc">Confirmed permanent position</p>
            </div>
            <i class="fas fa-check-circle complete-indicator"></i>
        </div>
        <div class="completion-date">Completed on: <?php echo !empty($user['full_time_issue']) ? date('d M Y', strtotime($user['full_time_issue'])) : ''; ?></div>
    </div>
    <?php } ?>

    <?php if($user['con_after_prob']=='Yes'){ ?>
    <!-- Probation -->
    <div class="stage-card completed" data-stage="4">
        <div class="stage-header">
            <div class="stage-icon stage-4">
                <i class="fas fa-hourglass-half"></i>
            </div>
            <div class="stage-info">
                <div class="stage-title">
                    Probation
                    <?php
                    // Calculate probation duration
                    if (!empty($user['joining_date']) && !empty($user['probation_date'])) {
                        $start = new DateTime($user['joining_date']);
                        $end = new DateTime($user['probation_date']);
                        $interval = $start->diff($end);
                        $months = $interval->m + ($interval->y * 12);
                        echo "<span class='badge bg-info badge-status'>{$months} months</span>";
                    }
                    ?>
                </div>
                <p class="stage-desc">Performance evaluation period</p>
            </div>
            <i class="fas fa-check-circle complete-indicator"></i>
        </div>
        <div class="completion-date">Completed on: <?php echo !empty($user['probation_date']) ? date('d M Y', strtotime($user['probation_date'])) : ''; ?></div>
    </div>
    <?php } ?>

    <?php if(!empty($user['internship_complete_date'])){ ?>
    <!-- Internship -->
    <div class="stage-card completed" data-stage="3">
        <div class="stage-header">
            <div class="stage-icon stage-3">
                <i class="fas fa-graduation-cap"></i>
            </div>
            <div class="stage-info">
                <div class="stage-title">
                    Internship
                    <?php
                    // Calculate internship duration
                    if (!empty($user['internship_start']) && !empty($user['internship_end'])) {
                        $start = new DateTime($user['internship_start']);
                        $end = new DateTime($user['internship_end']);
                        $interval = $start->diff($end);
                        $months = $interval->m + ($interval->y * 12);
                        echo "<span class='badge bg-warning badge-status'>{$months} months</span>";
                    }
                    ?>
                </div>
                <p class="stage-desc">Learning and hands-on experience period</p>
            </div>
            <i class="fas fa-check-circle complete-indicator"></i>
        </div>
        <div class="completion-date">Completed on: <?php echo !empty($user['internship_complete_date']) ? date('d M Y', strtotime($user['internship_complete_date'])) : ''; ?></div>
    </div>
    <?php } ?>

    <?php if(!empty($user['joining_date'])){ ?>
    <!-- Joining -->
    <div class="stage-card completed" data-stage="2">
        <div class="stage-header">
            <div class="stage-icon stage-2">
                <i class="fas fa-door-open"></i>
            </div>
            <div class="stage-info">
                <div class="stage-title">Joining</div>
                <p class="stage-desc">Onboarding and first day formalities</p>
            </div>
            <i class="fas fa-check-circle complete-indicator"></i>
        </div>
        <div class="completion-date">Completed on: <?php echo !empty($user['joining_date']) ? date('d M Y', strtotime($user['joining_date'])) : ''; ?></div>
    </div>
    <?php } ?>

    <?php if(!empty($user['letter_of_intent_issue'])){ ?>
    <!-- Letter of Intent -->
    <div class="stage-card completed" data-stage="1">
        <div class="stage-header">
            <div class="stage-icon stage-1">
                <i class="fas fa-file-signature"></i>
            </div>
            <div class="stage-info">
                <div class="stage-title">
                    Letter of Intent
                    <span class="badge bg-primary badge-status">Start</span>
                </div>
                <p class="stage-desc">Initial employment offer acceptance</p>
            </div>
            <i class="fas fa-check-circle complete-indicator"></i>
        </div>
        <div class="completion-date">Completed on: <?php echo !empty($user['letter_of_intent_issue']) ? date('d M Y', strtotime($user['letter_of_intent_issue'])) : ''; ?></div>
    </div>
    <?php } ?>

        
<?php } ?>