<?php

include './classes/classses.php';

$cf=new CenterFunction();

$alllpendings= $cf->pending_leads();

// echo $alllpendings;

// print_r($alllpendings);

// $alllpendings = // Assume this is your array containing all the lead data

$perPage = 5;
$page = isset($_POST['page']) ? (int)$_POST['page'] : 1;
$query = isset($_POST['query']) ? $_POST['query'] : '';

$start = ($page - 1) * $perPage;

$filteredLeads = array_filter($alllpendings, function($lead) use ($query) {
    return stripos($lead['lead_first_name'], $query) !== false || 
           stripos($lead['lead_last_name'], $query) !== false || 
           stripos($lead['lead_phone_1'], $query) !== false;
});

$totalLeads = count($filteredLeads);
$leadsToShow = array_slice($filteredLeads, $start, $perPage);

$tableData = '';
foreach ($leadsToShow as $lead) {
    $leadStatus = '';
    switch ($lead['lead_status']) {
        case '1': $leadStatus = '<td class="order-success"><span>Enrolled</span></td>'; break;
        case '0': $leadStatus = '<td class="order-warning"><span>Pending Verification</span></td>'; break;
        case '3': $leadStatus = '<td class=""><span class="order-notprospective">Not Prospective</span></td>'; break;
        case '2': $leadStatus = '<td><span class="order-rejected">Rejected</span></td>'; break;
        case '4': $leadStatus = '<td><span class="order-convertable">Convertable</span></td>'; break;
        case '5': $leadStatus = '<td><span class="order-future">Future Intake</span></td>'; break;
        case '6': $leadStatus = '<td><span class="order-completed">Completed</span></td>'; break;
        case '7': $leadStatus = '<td><span class="order-registered">Registered</span></td>'; break;
        default: $leadStatus = '<td class="order-secondary"><span>Undefined</span></td>'; break;
    }

    $campusLogo = $lead['campus_id'] == 1 ? '<img src="./assets/images/aic/logo.png">' : '<img src="./assets/images/aic/ímc logo.png">';

    $tableData .= "
        <tr>
            <td data-field='number'>{$lead['lead_first_name']} {$lead['lead_last_name']}</td>
            <td data-field='date'>{$lead['lead_created_date']}</td>
            <td data-field='text'>{$lead['lead_phone_1']}</td>
            $leadStatus
            <td data-field='number'>{$lead['user_first_name']}</td>
            <td data-field='text'>$campusLogo</td>
            <td><span onclick='toggleInfo({$lead['lead_id']})'><i class='fa fa-eye' title='view'></i></span></td>
            </tr>
            <tr class='additional-info_{$lead['lead_id']}' style='display: none;'>
            <td colspan='6'>
                 {$lead['lead_first_name']} {$lead['lead_last_name']}
                 <hr>
            
                <div class='grid-container'>
                    <div class='grid-item'>Lead ID : <span class='value_info'>{$lead['lead_id']}</span></div>
                    <div class='grid-item'>First Name: <span class='value_info'>{$lead['lead_first_name']}</span></div>
                    <div class='grid-item'>Last Name :<span class='value_info'>{$lead['lead_last_name']}</span></div>
                    <div class='grid-item'>Contact 1 :<span class='value_info'>{$lead['lead_phone_1']}</span></div>
                    <div class='grid-item'>Contact 2 :<span class='value_info'>{$lead['lead_phone_2']}</span></div>
                    <div class='grid-item'>Email :<span class='value_info'>{$lead['lead_email']}</span></div>
                    <div class='grid-item'>Address : <span class='value_info'>{$lead['lead_address']}</span></div>
                    <div class='grid-item'>Lead Source :<span class='value_info'>{$lead['lead_source']}</span></div>
                    <div class='grid-item'>Campus :<span class='value_info'>$campusLogo </span></div>
                    <div class='grid-item'>Branch :<span class='value_info'>{$lead['branch_name']}</span></div>
                    <div class='grid-item'>Counselor:<span class='value_info'>{$lead['user_first_name']} {$lead['user_last_name']}</span></div>
                    <div class='grid-item'>Lead Created :<span class='value_info'>{$lead['user_created_date']}</span></div>
                    <div class='grid-item'>Preffered Language :<span class='value_info'>{$lead['user_created_date']}</span></div>
                    <div class='grid-item'><button class='btn_approve'>Approve</button> <button class='btn_reject'>Reject</button></div>
                    
            </div>
            
            </td>
        </tr>
    ";
}

$pagination = '';
$totalPages = ceil($totalLeads / $perPage);
for ($i = 1; $i <= $totalPages; $i++) {
    $activeClass = ($i == $page) ? 'active' : '';
    $pagination .= "<li class='page-item $activeClass'><a class='page-link' href='#' data-page='$i'>$i</a></li>";
}

$response = [
    'tableData' => $tableData,
    'pagination' => $pagination
];

echo json_encode($response);
?>