<?php
require('header.php');

include "dbconfig.php";
$link = opencon();

// Fetch distinct project names from the database
$query = "SELECT DISTINCT Project_Name FROM project";
$result = mysqli_query($link, $query);

$projectOptions = [];
while ($row = mysqli_fetch_assoc($result)) {
    $projectOptions[] = $row['Project_Name'];
}

// Fetch distinct project names from the database
$query = "SELECT DISTINCT project_location FROM lead_location";
$result = mysqli_query($link, $query);
$Project_Location = [];
while ($row = mysqli_fetch_assoc($result)) {
    $Project_Location[] = $row['project_location'];
}


// Fetch distinct directions from the database
$query = "SELECT DISTINCT Direction FROM project_sites";
$result = mysqli_query($link, $query);

$directionOptions = [];
while ($row = mysqli_fetch_assoc($result)) {
    $directionOptions[] = $row['Direction'];
}

if($_SESSION["fullname"]=="SWATHIKA" || $_SESSION["fullname"]=="GOBIKA" ||  $_SESSION["fullname"]=="MARY PRISILLA")
{
    $where8=" and name='".$_SESSION["fullname"]."'";
}
?>

<style>
    label {
        font-size:16px;
    }
    input,select{
           border-radius: 5px !important;
    }
    table {
  border-collapse: collapse;
  border-radius: 5px;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.25);
  overflow: hidden;
  font-family: "Quicksand", sans-serif;
  font-weight: bold;
  font-size: 14px;
}

th {
  background: #009578;
  color: #ffffff;
  text-align: left;
}

th,
td {
  padding: 10px 20px;
  border:1px solid black;
}

tr:nth-child(even) {
  background: #eeeeee;
}

</style>

<script>
    function leadtypechange(x)
{
    
    if(x.value!="Database")
    {
         document.getElementById("bulk-file").style.display="none";
        document.getElementById("single-entry").style.display="block";
        document.getElementById("bulk-entry").style.display="none";
    }
    else
    {
         document.getElementById("bulk-file").style.display="block";
         document.getElementById("single-entry").style.display="none";
        document.getElementById("bulk-entry").style.display="block";
    }
}
</script>
<div class="breadcome-area mg-b-30 small-dn">
                <div class="container-fluid" >
                    <div class="row">
                        <div class="col-lg-12">
                             <div class="admin-dashone-data-table-area mg-b-15">
                  <div class="sparkline8-graph" style="
    text-align: left;
">
    <?php
     $filename="https://hrdandrecruitments.com/marketing/LEAD.csv";
 $btn_name="bulk_lead_upload";
 if(isset($_GET['status']))
 {
     $filename="LEAD_STATUS.csv";
     $btn_name="bulk_lead_status";
 }
 if(isset($_POST["bulk_lead_upload"]))
{

$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);
          if(!$conn){
          die('Could not Connect My Sql:' .mysqli_error());
      }
      
   $sheet_name= $_POST["sheetname"];
      $created=date('Y-m-d');
      
      
          $file = $_FILES['file']['tmp_name'];
          $handle = fopen($file, "r");
          $c = 0;
        $filesop = fgetcsv($handle, 1000, ",");
          while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
                    {
 $v1 = $filesop[0];//s.no
 $v2 = $filesop[1];//
 $v3 = $filesop[2];//
 $v4 = $filesop[3]; //
 $v5= $filesop[4];//
 $v6 = $filesop[5];//
 $v7 = $filesop[6];//cpllection File no
 $v8 = $filesop[7];//application Date
 $v9 = $filesop[8]; //payment date
 $v10= $filesop[9];//student Name
 $v11= $filesop[10];//student Name
$sql="insert into final_leads_education (student_name,mobilenumber,alternate_number,email,location,qualification,course_applied,dob,lead_source,lead_given_by,lead_given_date) values('$v1','$v2','$v3','$v4','$v5','$v6','$v7','$v8','$v9','$v10','$v11')";



// echo $sql.";<br>";


         $stmt = mysqli_query($conn,$sql);
        //   mysqli_stmt_execute($stmt);
         $c = $c + 1;
           }

             if($sql){
              
                 header("Refresh:0; url=lead_list.php");
                 echo "<script>alert('success')</script>";
             } 
         else
         {
             header("Refresh:0; url=lead_list.php");
             echo "<script>alert('error')</script>";
              
          }

}
    ?>
                                    
 <div class="modal bulkcheck"  tabindex="-1" style="display:block;top: 30%;overflow:scroll;z-index:-1" aria-modal="true" role="dialog">
                          <div class="modal-dialog modal-dialog-centered" role="document">
                            <div class="modal-content" style="
    padding: 10px;
">
                              <div class="modal-header" style="
    font-weight: bold;
">
                                <h5 class="modal-title" id="modalCenterTitle" style="
    font-weight: bold;
">UPLOAD FILE</h5>
                                
                              </div>
                              <div class="modal-body">
                                  <div class="row">
                                  <div class="col mb-3">
                                    <label for="nameWithTitle" class="form-label" style="font-weight: 700;">Download Sample file</label>
                                    <br>
                                    <button type="button" class="btn btn-primary " onclick="sampledownload()">Download</button>
                                  </div>
                                </div>
                                <br>
                <form action=''  method="post" enctype="multipart/form-data" >
                                
                                <div class="row">
                                  <div class="col mb-3">
                                    <label  class="form-label">Choose File</label>
                                    <input type="file" name="file"  class="form-control" required >
                                  </div>
                                  <div class="col mb-3">
                                    <label  class="form-label">Date</label>
                                    <input type="date" name="sheetname"  class="form-control" required >
                                  </div>
                                </div>
                              </div>
                              <div id="uploadresult">
                              </div>
                              <div class="modal-footer">
                                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal" onclick=showstatus('bulkcheck','none')>
                                  Close
                                </button>
                                <button type="submit" class="btn btn-primary"  name="<?php echo $btn_name;?>">BULK UPLOAD</button>
                              </div>
                              </form>
                            </div>
                          </div>
                        </div>
                 </div>   
                                   </div>
            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- Breadcome End-->
        
            <!-- Breadcome start-->
         
            <!-- Breadcome End-->
            <!-- stockprice, feed area end-->
           
            
   <script src="https://cdn.jsdelivr.net/npm/papaparse@5.2.0/papaparse.min.js"></script>
   
   <script>
       class TableCsv {
  /**
   * @param {HTMLTableElement} root The table element which will display the CSV data.
   */
  constructor(root) {
    this.root = root;
  }
  

  /**
   * Clears existing data in the table and replaces it with new data.
   *
   * @param {string[][]} data A 2D array of data to be used as the table body
   * @param {string[]} headerColumns List of headings to be used
   */
  update(data, headerColumns = []) {
    this.clear();
    this.setHeader(headerColumns);
    this.setBody(data);
  }

  /**
   * Clears all contents of the table (incl. the header).
   */
  clear() {
    this.root.innerHTML = "";
  }

  /**
   * Sets the table header.
   *
   * @param {string[]} headerColumns List of headings to be used
   */
  setHeader(headerColumns) {
    this.root.insertAdjacentHTML(
      "afterbegin",
      `
            <thead>
                <tr>
                    ${headerColumns.map((text) => `<th>${text}</th>`).join("")}
                </tr>
            </thead>
        `
    );
  }

  /**
   * Sets the table body.
   *
   * @param {string[][]} data A 2D array of data to be used as the table body
   */
  setBody(data) {
    const rowsHtml = data.map((row) => {
      return `
                <tr>
                    ${row.map((text) => `<td>${text}</td>`).join("")}
                </tr>
            `;
    });

    this.root.insertAdjacentHTML(
      "beforeend",
      `
            <tbody>
                ${rowsHtml.join("")}
            </tbody>
        `
    );
  }
}

const tableRoot = document.querySelector("#csvRoot");
const csvFileInput = document.querySelector("#csvFileInput");
const tableCsv = new TableCsv(tableRoot);

csvFileInput.addEventListener("change", (e) => {
  Papa.parse(csvFileInput.files[0], {
    delimiter: ",",
    skipEmptyLines: true,
    complete: (results) => {
      tableCsv.update(results.data.slice(1), results.data[0]);
    }
  });
});


 function sampledownload()
      {
          window.open("<?php echo $filename;?>");
      }
   </script>

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