<?php
include "header.php";
?>

<!-- Main Working Space Start -->

<div class="col-md-12">
<div
          class="mt-4 grid grid-cols-12 gap-4 px-[var(--margin-x)] transition-all duration-[.25s] sm:mt-5 sm:gap-5 lg:mt-6 lg:gap-6"
        >
        <div style="">
        <div class="card mt-5">
        <div class="col-md-12 card-header ">
            <div class="row">
              <div class="col-md-2">
                <h3 class=""> Assign Lead </h3>
              </div>
              <div class="col-md-6">
              <nav style="--bs-breadcrumb-divider: url(&#34;data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='8'%3E%3Cpath d='M2.5 0L1 1.5 3.5 4 1 6.5 2.5 8l4-4-4-4z' fill='%236c757d'/%3E%3C/svg%3E&#34;);" aria-label="breadcrumb">
                <ol class="breadcrumb mt-2">
                  <li class="breadcrumb-item"><a href="index.php">Leads</a></li>
                  <li class="breadcrumb-item active" aria-current="page">Assign Leads</li>
                </ol>
              </nav>
              </div>
            </div>
          </div>
          <!-- <div id="liveAlertPlaceholder" style="width: 300px; border-radius: 20px;"></div> -->
            <div class="card-body">
              <form action="" method='POST'>
              <div class="col-md-12">
                <div class="row">
                  <div class="col-md-4">
                    <input type="date" autocomplete="off" class="form-control date" name='from' to='from'>
                  </div>
                  <div class="col-md-4">
                    <input type="date" autocomplete="off" name="to" id="to" class="form-control">
                  </div>
                  <div class="col-md-4">
                    <button type='submit' name='btn' id='btn' class="btn btn-primary">Search</button>
                  </div>
                </div>
              </div>
              </form> 
            </div>
        </div>
        <div>
          <div class="card my-4">
          <div class="col-md-12 card-header ">
            <div class="row">
              <div class="col-md-8">
                <h3 class=""> Lead List</h3>
              </div>
              <div class="col-md-3" style="float: right;">
                <input type="search" autocomplete="off" id="search" placeholder="Search Employee Name" class="form-control">
              </div>
              <div class="col-md-1">
                <!-- <button class="btn btn-primary" type="submit" name="btn" id="btn">Assign</button> -->
              </div>
            <div class="card-body">
              <div>
                <table class="table table-striped">
                  <thead>
                    <tr>
                      <th>S.No</th>
                      <th>Date</th>
                      <th>Name</th>
                      <th>Contact No</th>
                      <th>Email ID</th>
                      <th>Lead Status</th>
                      <th>Lead Source</th>
                      <th>Micro Source</th>
                      <th>Select</th>
                      <!-- <th>Action</th> -->
                    </tr>
                  </thead>
                  <tbody>
                    <!-- <form action="assign_array_update.php" method="post"> -->
                  <?php
                  include "dbconfig.php";
                  $link =opencon();
                  if( isset($_POST['from']) && isset($_POST['to'])){
                    $from = $_POST['from'];
                    $to = $_POST['to'];

                    // echo $from;
                    // echo $to;
                  
// if (isset($_POST['btn'])) { // Assuming 'btn' is the name of a form input element
  // echo $from;
  // echo $to;
    $sql = "SELECT * FROM new_lead WHERE Date BETWEEN '$from' AND '$to'";
    // echo $sql;
    $sql_exe = mysqli_query($link, $sql);
    $count = 1;
    while ($fetch_qr = mysqli_fetch_array($sql_exe)) {
      echo "<tr>
              <td>" . $count . "</td>
              <td>" . $fetch_qr['Date'] . "</td>
              <td>" . $fetch_qr['Name'] . "</td>
              <td>" . $fetch_qr['Contact_No'] . "</td>
              <td>" . $fetch_qr['Email'] . "</td>
              <td>" . $fetch_qr['Lead_Status'] . "</td>
              <td>" . $fetch_qr['Lead_Source'] . "</td>
              <td>" . $fetch_qr['Lead_Micro_Source'] . "</td>
              <td><input type='checkbox' id='" . $fetch_qr['id'] . "' class='child-checkbox' autocomplete='off'></td>
          </tr>";
      $count++;
  }
  
} 
else {
    $sql2 = "SELECT * FROM new_lead Where Team_Manager='' AND Team_Leader='' AND Telecaller=''";
    $sql2_exe = mysqli_query($link, $sql2);
    $count = 1;
    while ($fetch_qr = mysqli_fetch_array($sql2_exe)) {
      echo "<tr>
              <td>" . $count . "</td>
              <td>" . $fetch_qr['Date'] . "</td>
              <td>" . $fetch_qr['Name'] . "</td>
              <td>" . $fetch_qr['Contact_No'] . "</td>
              <td>" . $fetch_qr['Email'] . "</td>
              <td>" . $fetch_qr['Lead_Status'] . "</td>
              <td>" . $fetch_qr['Lead_Source'] . "</td>
              <td>" . $fetch_qr['Lead_Micro_Source'] . "</td>
              <td><input type='checkbox' id='" . $fetch_qr['id'] . "' class='child-checkbox' autocomplete='off'></td>
          </tr>";
      $count++;
  }
  
}

                  
?>

                  </tbody>
                  <tfoot>
                    <tr>
                        <td><button type="reset" value="button" id="refreshButton" class="btn btn-danger" style="float: left;">Reset</button></td>
                        <td colspan="2">
                            <select id="ddl" name="manager" class="form-select" onchange="configureDropDownLists(this, document.getElementById('ddl2'))">
                                <option value="empty">--Select Manager--</option>
                                <option value="AKILA">AKILA</option>
                            </select>
                        </td>
                        <td colspan="2"><select id="ddl2" name="leader" class="form-select" onchange="configureDropDownLists2(this, document.getElementById('ddl3'))"><option value="">--Select Leader--</option></select></td>
                        <td colspan="2"><select name="telecaller" id="ddl3" class="form-select"><option value="">--Select Telecaller--</option></select></td>
                        <!-- <td></td> -->
                        <td xolspan="2"><center><button class="btn btn-success" type="submit" id="saveLead" style="float: right;">Update</button></center></td>
                    </tr>
                  </tfoot>
                </table>
              </div>
            </div>
            
          </div>
          
        </div>
        
      </div>
            

            
          </div>
</div>
<!-- Main Working Space End -->

<script>
  $(document).ready(function(){
    $("#search").keyup(function(){
      var a1=$(this).val();
      $('table tbody tr').hide();
      var len =$('table tbody tr:not(.notfound) td:nth-child(2):contains("'+a1+'")').length;
      if(len>0){
        $('table tbody tr:not(.notfound) td:contains("'+a1+'")').each(function(){
          $(this).closest('tr').show();
        });
      }else{
        $('.notfound').show();
      }
      // alert("working");
    });
  });
</script>
<script>
  $(document).ready(function(){
    $("#search").keyup(function(){
      var a1=$(this).val();
      $('table tbody tr').hide();
      var len =$('table tbody tr:not(.notfound) td:nth-child(3):contains("'+a1+'")').length;
      if(len>0){
        $('table tbody tr:not(.notfound) td:contains("'+a1+'")').each(function(){
          $(this).closest('tr').show();
        });
      }else{
        $('.notfound').show();
      }
      // alert("working");
    });
  });
</script>
<script>
    const alertPlaceholder = document.getElementById('liveAlertPlaceholder')
    const appendAlert = (message, type) => {
  const wrapper = document.createElement('div')
  wrapper.innerHTML = [
    `<div class="alert alert-${type} alert-dismissible" role="alert">`,
    `   <div>${message}</div>`,
    '   <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>',
    '</div>'
  ].join('')

  alertPlaceholder.append(wrapper)
}

const alertTrigger = document.getElementById('liveAlertBtn')
if (alertTrigger) {
  alertTrigger.addEventListener('click', () => {
    appendAlert('Nice Employee assigned Successfully', 'info')
  })
}
  </script>

  <!-- Page Refreshing Button Start -->
  <script>
    // Get a reference to the refresh button
var refreshButton = document.getElementById("refreshButton");

// Add a click event listener to the button
refreshButton.addEventListener("click", function() {
    // Use location.reload() to refresh the page
    location.reload();
});

  </script>

  <!-- Page Refreshing Button End -->

  <script>
    $(".form-control date").change(function () {
      $("#from").val($(this).val()).trigger('click');
    });
  </script>
  <!-- Changing dropdown Start -->
  <script type="text/javascript">
        function configureDropDownLists(ddl1, ddl2) {
            var AKILA = ['BAKIYALAKSHMI', 'VIJAYAKUMARI'];

            switch (ddl1.value) {
                case 'AKILA':
                    ddl2.options.length = 0;
                    for (var i = 0; i < AKILA.length; i++) {
                        createOption(ddl2, AKILA[i], AKILA[i]);
                    }
                    break;
                default:
                    ddl2.options.length = 0;
                    break;
            }
        }

        function configureDropDownLists2(ddl2, ddl3) {
            var BAKIYALAKSHMI = ['PRADEEPRAJA', 'RAMAPRABA.G', 'YOKESH.M', 'SURUTHIKA'];
            var VIJAYAKUMARI = ['ANI JOLLA', 'MANIVEL.S', 'POONGODI M'];

            switch (ddl2.value) {
                // You can add more cases here if needed
                case 'BAKIYALAKSHMI':
                    ddl3.options.length = 0;
                    for (var i = 0; i < BAKIYALAKSHMI.length; i++) {
                        createOption(ddl3, BAKIYALAKSHMI[i], BAKIYALAKSHMI[i]);
                    }
                    break;
                case 'VIJAYAKUMARI':
                    ddl3.options.length = 0;
                    for (var i = 0; i < VIJAYAKUMARI.length; i++) {
                        createOption(ddl3, VIJAYAKUMARI[i], VIJAYAKUMARI[i]);
                    }
                    break;
                default:
                    ddl3.options.length = 0;
                    break;
            }
        }

        function createOption(ddl, text, value) {
            var opt = document.createElement('option');
            opt.value = value;
            opt.text = text;
            ddl.options.add(opt);
        }
    </script>
    <!-- Changing dropdown END -->
<script>
    $('#saveLead').click(function () {
        const checkedIds = [];
        $('.child-checkbox:checked').each(function () {
            checkedIds.push($(this).attr('id'));
        });

        const dropdownValue1 = document.querySelector('#ddl').value;
        const dropdownValue2 = document.querySelector('#ddl2').value;
        const dropdownValue3 = document.querySelector('#ddl3').value;

        if (checkedIds.length > 0 && dropdownValue1 !== '0' && dropdownValue2 !== '0' && dropdownValue3 !== '0') {
            var selectedemployee = $('#ddl').val();
            var selectedemployee2 = $('#ddl2').val();
            var selectedemployee3 = $('#ddl3').val();

            // Create an object to send as data
            var dataToSend = {
                checkedIds: checkedIds,
                selectedemployee: selectedemployee,
                selectedemployee2: selectedemployee2,
                selectedemployee3: selectedemployee3
            };

            $.ajax({
                url: 'assign_array_update.php', // Replace with your actual domain
                method: 'POST',
                data: dataToSend, // Send the data object
                dataType: 'json', // Expect JSON response
                success: function (response) {
                    // Handle the response from the server
                    if (response.success == 1) {
                        alert("Assigned Successfully");
                    } else {
                        alert("Something went wrong");
                    }
                }
            });
            // function myFunction(){
      alert("Files Updates Successfully");
      window.location.reload();
  // }
        } else {
            alert("Checkbox is not checked or select the employee");
        }
    });
</script>

<?php
include "footer.php";
?>