<?php
    session_start();
    if (!isset($_SESSION['username'])) {
        $_SESSION['login_msg'] = "Please login first to continue.";
        header("Location: login.php");
        exit();
    }
    
    require_once('include/config.php');
    require_once('include/startup.php');                           
    $alltables = new AllTables();
    
    
    // View Service Requests
    $sql="SELECT * FROM `service_requests` WHERE service_id = 2 && cmp_id = $G_companyid";
    $result = $conn->query($sql);
    
    $powerconrequest="";
    $i = 1; // ✅ Declare counter BEFORE loop
    
    while($rows = $result->fetch_assoc()) {
        
        $cardId = $rows['id']; // unique DB id
        
        $status = $rows['status'];
        
        if($status == 1){
            $currentstatus = "pending";
        }
        elseif($status == 2){
            $currentstatus = "approved";
        }
        elseif($status == 3){
            $currentstatus = "rejected";
        }
        else{
            $currentstatus = "pending";
        }
         
        $powerconrequest .= " <tr>
                                <!--<th class='text-center' scope='row'>$cardId</th>-->
                                <th class='text-center' scope='row'>$i</th>
                                <td><div class='$currentstatus'></div></td>
                                <td>". $rows['request']."</td>
                                <td class='text-center'>". $rows['quantity']."</td>
                                <td class='text-end'><span>&#8377;</span>". $rows['amount'].".00</td>
                                <td class='text-center'>
                                    <div class='btn-group'>
                                        <button class='btn btn btn-light btn-sm btn-sm' type='button' data-bs-toggle='dropdown' aria-expanded='false'>
                                            <i class='fa-solid fa-gear'></i>
                                        </button>
                                        <ul class='dropdown-menu text-center'>
                                            <li>
                                                <button type='button' class='btn btn-primary btn-sm edit-btn' data-id='$cardId' data-request='". $rows['request']."' data-quantity='". $rows['quantity']."' data-amount='". $rows['amount']."'>
                                                    Edit
                                                </button>
                                            </li>
                                        </ul>
                                    </div>
                                </td>
                            </tr>";
                            
        $i++; // ✅ Increment after each row
    }


    include('include/header.php');   
    include_once('power-connection1.html');
    include('include/footer.php');         
?>    