<?php
require('header.php');
include "dbconfig.php";
$link =opencon();
$team_manager_session=$_SESSION['team_manager'];
?>

<div class="breadcome-area mg-b-30 small-dn">
                <div class="container-fluid">
                    <div class="row">

                                                
                        <div class="col-lg-12">
                            <div class="breadcome-list shadow-reset">
                                <div class="row">
                             <div class="col-lg-12">
                                 <!--<h4 style="text-align: center;font-size: 26px;background-color: antiquewhite;height: 36px;margin-top: -20px;">Search</h4>-->
                                  </div>
                               </div>
                                <div class="row" style="
    width: 100%;
">
                                    <form method='post'>
    <div class="col-lg-3"  >
                 <label for="">Student Name</label>
                     <input type="text" name="client_name" class="form-control">
            </div>
             <div class="col-lg-3"  >
                 <label for="">Student Number</label>
                     <input type="number" name="client_number" class="form-control">
            </div>
             <div class="col-lg-3"  >
                 <label for="">Student Email</label>
                     <input type="text" name="client_email" class="form-control">
            </div> 
            <div class="col-lg-3"  >
                 <label for="">Student status</label>
                       <select name="client_status" id="client_status" class="form-control">
                           <option value="">Select</option>
                        <option value="ELIGIBILITY COMPLETED">ELIGIBILITY COMPLETED</option>            
<option value="ADMISSION COMPLETED">ADMISSION COMPLETED</option>
<option value="SURE PIPELINE">SURE PIPELINE</option>
<option value="UNDER PROJECTION">UNDER PROJECTION</option>
<option value="HIGH FEES">HIGH FEES</option>
<option value="COURSE NOT AVAILABLE">COURSE NOT AVAILABLE</option>
<option value="UNIVERSITY NOT AVAILABLE">UNIVERSITY NOT AVAILABLE</option>
<option value="STUDENT NOT ELIGIBLE">STUDENT NOT ELIGIBLE</option>
                                    </select>
            </div>
            

    
             <div class="col-lg-3"  style="margin-top: 2%;">
                 <label for="">Lead Given Date From</label>
                     <input type="Date" name="lead_given_date_from" class="form-control">
            </div>
            <div class="col-lg-3"  style="margin-top: 2%;">
                 <label for="">Lead Given Date To</label>
                     <input type="Date" name="lead_given_date_to" class="form-control">
            </div>
            
            
          
            
  <div class="col-lg-3"  style="margin-top: 4%;">
                 
                     <button type="submit" name="btn1" class=" btn btn-primary">Search</button>
            </div>
            </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- Breadcome End-->
          
         
            <!-- stockprice, feed area end-->
            <div class="admin-dashone-data-table-area mg-b-15">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="sparkline8-list shadow-reset">
                                <div class="sparkline8-hd">
                                    <div class="main-sparkline8-hd">
                                        <h1>Lead List</h1>
                                        <div class="sparkline8-outline-icon">
                                            <span class="sparkline8-collapse-link"><i class="fa fa-chevron-up"></i></span>
                                            <span><i class="fa fa-wrench"></i></span>
                                            <span class="sparkline8-collapse-close"><i class="fa fa-times"></i></span>
                                        </div>
                                    </div>
                                </div>
                                
                                                
                             
                                
                                
                                <div class="sparkline8-graph">
                                   
                              
                                    
                                     
                                    
                                    <div class="datatable-dashv1-list custom-datatable-overright">
                                        
                                        
                                        
                                        
                                        <div id="toolbar">
                                            <select class="form-control">
                                                <option value="">Export Basic</option>
                                                <option value="all">Export All</option>
                                                <option value="selected">Export Selected</option>
                                            </select>
                                        </div>
                                        <table id="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="true" data-key-events="true" data-show-toggle="true" data-resizable="true" data-cookie="true" data-cookie-id-table="saveId" data-show-export="true" data-click-to-select="true" data-toolbar="#toolbar">
                                            <thead>
                                                <tr>
                                                      <th><input type='checkbox' id='checkAll' style="margin-top: 2px" onclick="checkall(this)">CHECK ALL</th>
                                                       
                                                       
                                                    <th>S.No</th>
                                                   
                                               <th>Status</th>
                                                <th>Student Name</th>
                                                   <th>Mobile Number</th>

                                                <th>Alternate Number</th>
                                                <th>Email</th>
                                                <th>Location</th>
                                                <th>Qualification</th>
                                                <th>Applied Course</th>
                                                <th>Date of Birth</th>
                                             
                                               <th>Assigned To</th>
                                               <th>Assigned Date</th>

                                      
                                       <th>Status Updated Date</th>
                                   
                                            
                                                </tr>
                                            </thead>
                                             <tbody>
                                                 
                                                 
                                               <?php
                                               
                                               if(isset($_POST['btn1'])){
   $client_name=$_POST['client_name'];
   $client_number=$_POST['client_number'];
   $client_email=$_POST['client_email'];
   $client_status=$_POST['client_status'];
   $lead_generated_by=$_POST['lead_generated_by'];
    $lead_given_to=$_POST['lead_given_to'];
   $from=$_POST['lead_given_date_from'];
   $to=$_POST['lead_given_date_to'];
   $location=$_POST['location'];
   if($location=="0")
   {
       $where9=" and lead_location=''";
   }
   else if($location=="1")
   {
       $where9=" and lead_location!=''";
   }
   else
   {
       
   }
   $where7="";
   

   if($client_name!='')
   {
       $where1=" and student_name='$client_name'";
   }
   if($client_number!='')
   {
       $where2=" and mobilenumber='$client_number'";
   }
   if($client_email!='')
   {
       $where3=" and email='$client_email'";
   }
   if($client_status!='')
   {
       $where4=" and status='$client_status'";
   }
   if($lead_generated_by!='')
   {
       $where5=" and lead_generated_by='$lead_generated_by'";
   }
   if($lead_given_to!='')
   {
       $where6=" and lead_given_to='$lead_given_to'";
   }
    if($from!='')
   {
       $where7=" and lead_given_date between '$from' and '$to'";
   }
   
}


$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'hrdant7r_market';
$DATABASE_PASS = 'gAw=6Y79}FDN';
$DATABASE_NAME = 'hrdant7r_market';
// Try and connect using the info above.
$conn = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

if($team_manager_session!="Admin")
    {
        $tm_condition=" and assigned_to='$team_manager_session'";
    }

$sql107=explode("[]","SELECT * FROM `final_leads_education` where id!='' ".$tm_condition.$where1.$where2.$where3.$where4.$where5.$where6.$where7.$where8.$where9."[]");

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

$sql107=explode("[]",$_SESSION[$session]);
}

// print_r($sql107);
$count=count($sql107);

$prabuinc=0;
$i=0;
$count1=1;
while($prabuinc < $count)
{
    $sql1=$sql107[$prabuinc];
    

 $result = $conn->query($sql1);
if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
    
    
$fileno=$row["fileno"];
$mainid=$row["id"];
echo "<tr>


<td>
<input type='checkbox' class='check-input' name='fileno[]' value='".$row["id"]."'>

</td>

<td>".$count1."</td>";
?>

<td>
    
    <select name='call_status' class='form-control call_status<?=$count1;?> ' style='width: 167px;'>
                         <?php
                         if($row["call_status"]!='')
                         {
                         ?>
                         
                         <option value="<?=$row["call_status"];?>"><?=$row["call_status"];?></option>
                         
                         <?php
                         }
                         ?>
 
<option value="Not connected">Not connected</option>
<option value="Connected">Connected</option>




</select>
<br>
    
    <select name='lead_status' class='form-control query_status<?=$count1;?> ' style='width: 167px;' onchange="statuschange(this,<?=$count1;?>)">
                         <?php
                         if($row["client_status"]!='')
                         {
                         ?>
                         
                         <option value="<?=$row["client_status"];?>"><?=$row["client_status"];?></option>
                         
                         <?php
                         }
                         ?>

<option value="">Select</option>

<option value="Interested">Interested</option>


<option value="Not Interested">Not Interested</option>

</select>
<br>
  <select name='query_status' class='form-control lead_status<?=$count1;?> ' style='width: 167px;'>
                         <?php
                         if($row["status"]!='')
                         {
                         ?>
                         
                         <option value="<?=$row["status"];?>"><?=$row["status"];?></option>
                         
                         <?php
                         }
                         ?>

<option value="">Select</option>
<option value="ELIGIBILITY COMPLETED">ELIGIBILITY COMPLETED</option>            
<option value="ADMISSION COMPLETED">ADMISSION COMPLETED</option>
<option value="SURE PIPELINE">SURE PIPELINE</option>
<option value="UNDER PROJECTION">UNDER PROJECTION</option>
<option value="HIGH FEES">HIGH FEES</option>
<option value="COURSE NOT AVAILABLE">COURSE NOT AVAILABLE</option>
<option value="UNIVERSITY NOT AVAILABLE">UNIVERSITY NOT AVAILABLE</option>
<option value="STUDENT NOT ELIGIBLE">STUDENT NOT ELIGIBLE</option>

</select>
<br>
<textarea class='form-control remarks<?=$count1;?> '><?=$row["remarks"];?></textarea>

<button type="button" id='basicSuccessAnimation' class='btn btn-success query_statusubmit$i'  onclick=submitapproval_call1('<?=$mainid;?>',this,<?=$count1;?>)  style='margin-top: 10%;'>submit</button>
</td>

<?php
echo "<td>".$row["student_name"]."</td>
<td>".$row["mobilenumber"]."</td>
<td>".$row["alternate_number"]."</td>
<td>".$row["email"]."</td>
<td>".$row["location"]."</td>
<td>".$row["qualification"]."</td>
<td>".$row["course_applied"]."</td>
<td>".$row["dob"]."</td>
<td>".$row["assigned_to"]."</td>
<td>".$row["assigned_date"]."</td>";
$count1++;



echo "<td>".$row["status_date"]."</td>



</tr>";
}

}
$prabuinc++;
}




?>
                                            </tbody>
                                        </table>
                                        
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <script>
// Function to handle checkbox changes and update localStorage
function handleCheckboxChange(event) {
    const checkbox = event.target;
    const id = checkbox.getAttribute('data-id');

    // Retrieve the current checkedIds from localStorage or initialize an empty array
    let checkedIds = JSON.parse(localStorage.getItem('checkedIds')) || [];

    if (checkbox.checked) {
        // If checked, add the id to the array
        if (!checkedIds.includes(id)) {  // Ensure the ID is not added multiple times
            checkedIds.push(id);
        }
    } else {
        // If unchecked, remove the id from the array
        const index = checkedIds.indexOf(id);
        if (index > -1) {
            checkedIds.splice(index, 1);
        }
    }

    // Save the updated checkedIds to localStorage
    localStorage.setItem('checkedIds', JSON.stringify(checkedIds));

    console.log('Checked IDs (after change):', checkedIds); // Debugging: You can see the updated array in the console
}

// Wait for the DOM to load and then add event listeners to checkboxes
document.addEventListener("DOMContentLoaded", function() {
    // Retrieve the checked IDs from localStorage and update the checkbox states
    const checkedIds = JSON.parse(localStorage.getItem('checkedIds')) || [];
    const checkboxes = document.querySelectorAll('.checkbox');

    checkboxes.forEach(checkbox => {
        const id = checkbox.getAttribute('data-id');
        // If the checkbox ID is in the stored checkedIds, check it
        if (checkedIds.includes(id)) {
            checkbox.checked = true;
        }

        // Add event listeners to each checkbox
        checkbox.addEventListener('click', handleCheckboxChange);
    });
});

// Button click handler to send data to the server
document.getElementById('showIdsButton').addEventListener('click', function() {
    // Retrieve the checkedIds from localStorage
    const checkedIds = JSON.parse(localStorage.getItem('checkedIds')) || [];

    // Check if no checkboxes are selected
    if (checkedIds.length === 0) {
        alert("No IDs selected");
        return;
    }

    // Get location input value
    const location = document.getElementById("update_location").value;

    // Send the selected IDs and location to the server using AJAX
    const xhr = new XMLHttpRequest();
    xhr.open('POST', 'process_ids.php', true); // Replace 'process_ids.php' with the correct PHP script
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    
    // Send the checked IDs as a query parameter (encoded as JSON) and the location
    xhr.send('location=' + encodeURIComponent(location) + '&ids=' + JSON.stringify(checkedIds));

    // Handle the response from the server
    xhr.onload = function() {
        if (xhr.status === 200) {
            // Handle the server response (e.g., display success or error)
            alert('Response from server: ' + xhr.responseText);
        } else {
            alert('Error: ' + xhr.statusText);
        }
    };
});


 function submitapproval_call1(id,x,y)
  {
      
    
      
      var call_status=document.getElementsByClassName("call_status"+y)[0].value;
      var lead_status=document.getElementsByClassName("lead_status"+y)[0].value;
      var stu_calls=document.getElementsByClassName("query_status"+y)[0].value;
    var remarks=document.getElementsByClassName("remarks"+y)[0].value;
    
     if(call_status!='')
     {
           x.innerHTML="Please Wait...";
         var data2 = new FormData();
        
        data2.append("client_status",stu_calls);
        data2.append("call_status",call_status);
        data2.append("lead_status",lead_status);
        data2.append("remarks",remarks);
       

        data2.append("lead_id",id);
        
    const xhttp = new XMLHttpRequest();
    xhttp.onload = function() {
        
      alert(this.responseText);
        x.innerHTML = "Submit";
        
            }
   
  xhttp.open("POST", "lead_status_update.php");
  xhttp.send(data2);
  
  
     }
     else
     {
         alert("Fields Are Required");
     }
     

 
   
   
  }
  


            </script>
<?php
require('footer.php');
?>