<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>TM - Dashboard</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="css/styles.css" rel="stylesheet">
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
  <div class="container bg2">
    <header>
      <div class="row align-items-center">
        <div class="col-6 text-start">
        <?php 
 
 if( $_SESSION['formtype']=="abr")  {   echo '<a href="abr.php"> <img src="images/lpt-left-logo.png" class="img-fluid" alt="Left logo"></a>'; }
 if( $_SESSION['formtype']=="drl") { echo  '<a href="drl.php"> <img src="images/lpt-left-logo.png" class="img-fluid" alt="Left logo"></a>'; }
 if( $_SESSION['formtype']=="form") { echo  '<a href="form.php"> <img src="images/lpt-left-logo.png" class="img-fluid" alt="Left logo"></a>'; }

    ?>
        </div>
        <div class="col-6 text-end">
          <img src="images/topmenu-logo.png" class="img-fluid" alt="Topmenu logo">
        </div>
      </div>
    </header>

    <div class="content mt-3">
      <form method="post" id="campForm" action="register.php"> <!-- Fixed ID -->

        <div class="row mb-3 ">
          <div class="col-1 col-md-3"></div>
          <div class="col-10 col-md-6">
            <div class="box">Camp Registration</div>
          </div>
          <div class="col-1 col-md-3"></div>
        </div>

        <div class="row">
          <div class="mb-3">
            <label for="doctorname" class="form-label">Doctor Name</label>
            <input type="text" class="form-control" id="doctorname" name="doctorname" required>
          </div>
        </div>

        <div class="row">
          <div class="mb-3">
            <label for="no_of_patients_screened" class="form-label">No of patients screened</label>
            <input type="text" class="form-control" id="no_of_patients_screened" name="no_of_patients_screened" required>
          </div>
        </div>
        <div class="row">
          <div class="col-2"></div>
          <div class="col-8"><center>
          <button id="submit_form" class="btn w-100" type="button">
            <img src="images/submit_btn.png" alt="Submit" class="img-fluid">
          </button>        </center>
          </div>
          <div class="col-2"></div>
</div></div> 
      </form>
    
    <div class="row text-center">
       <div class="col-2 text-center"></div>
       <div class="col-8 text-center"><a href="my-tracker1.php"><img src="images/view-my-tracker.png" alt="View my tracker" class="img-fluid"></a></div>
       <div class="col-2"></div>
    </div>



  

  
    <footer>
      <div class="row">
        <div class="col-6 col-md-6 padding-zero text-center" style="background:#ECE9F8">
        <div class="mt-4"><a href="#"  data-bs-toggle="modal" data-bs-target="#findUserModal" style="color:#54308E;font-weight:bold;text-decoration:none;"> Find Your User Code</a></div>
        </div>
        <div class="col-6 col-md-6 padding-zero text-center" style="background:#54308E">
        <img src="images/contact-us.png" class="img-fluid" style="cursor:pointer;" data-bs-toggle="modal" data-bs-target="#contactModal">
        </div>
      </div>
    </footer>
  </div>


  

<!-- Contact Us Modal -->
<div class="modal fade" id="contactModal" tabindex="-1" aria-labelledby="contactModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="contactModalLabel">Contact Us</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
      <p><strong>Name:</strong> Ravindra Veer</p>
        <p><strong>Support Email:</strong> ravindra.veer@exicongroup.com</p>
        <p><strong>Phone:</strong> +91-9284759607,+91-8446704280 (Whatsapp) </p>
        <p><strong>Contact Time:</strong> Time, 9 AM to 6 PM</p>
        <!-- Add any other contact details here -->
      </div>
    </div>
  </div>
</div>





<!-- Find User Code Modal -->
<div class="modal fade" id="findUserModal" tabindex="-1" aria-labelledby="findUserModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="findUserModalLabel">Find Your User Code</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
      <div class="mb-3">
  <label for="fullnameInput" class="form-label">Full Name (partial allowed)</label>
  <input type="text" class="form-control" id="fullnameInput" placeholder="Enter full or partial name">
</div>
<div class="mb-3">
  <label for="stateInput" class="form-label">State (partial allowed)</label>
  <input type="text" class="form-control" id="stateInput" placeholder="Enter full or partial state">
</div>
        <div id="userDetails" class="d-none">
          <p><strong>User ID:</strong> <span id="userId"></span></p>
          <p><strong>Full Name:</strong> <span id="fullName"></span></p>
          <p><strong>State:</strong> <span id="userState"></span></p>
        </div>
        <div id="userError" class="text-danger d-none">TM ID not found.</div>
      </div>
      <div class="modal-footer">
        <button type="button" id="fetchUserBtn" class="btn btn-primary">Fetch Details</button>
      </div>
    </div>
  </div>
</div>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

  <script>
  $(document).ready(function(){
    $('#submit_form').click(function(e){
      e.preventDefault(); // Prevent the default form submission

      // Retrieve the values of the fields
      var doctorName = $('#doctorname').val().trim();
      var patientsScreened = $('#no_of_patients_screened').val().trim();

      // Validate the fields
      if (doctorName === '') {
        alert('Please enter the Doctor Name.');
        $('#doctorname').focus();
        return;
      }

      if (patientsScreened === '') {
        alert('Please enter the Number of Patients Screened.');
        $('#no_of_patients_screened').focus();
        return;
      }

      // If validation passes, serialize the form data
      var formData = $('#campForm').serialize();

      // Proceed with the AJAX request
      $.ajax({
        url: 'register.php',
        type: 'POST',
        data: formData,
        success: function(response){  
          if(response == "1") {
            window.location.href = 'thank-you.php';
          } else {
            alert('Something went wrong: ' + response);
          }
        },
        error: function(xhr, status, error){
          alert('Error submitting form. Please try again.');
        }
      });
    });
  });
</script>


<script>
document.getElementById('fetchUserBtn').addEventListener('click', function () {
  const fullname = document.getElementById('fullnameInput').value.trim();
  const state = document.getElementById('stateInput').value.trim();

  if (!fullname && !state) {
    alert('Please enter Full Name or State to search.');
    return;
  }

  fetch('get-user-by-tmid.php', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: new URLSearchParams({ fullname, state })
  })
  .then(res => res.json())
  .then(data => {
    const userDetails = document.getElementById('userDetails');
    const userError = document.getElementById('userError');

    if (data.success) {
      let output = '<ul class="list-group">';
      data.data.forEach(user => {
        output += `<li class="list-group-item">
          <strong>User ID:</strong> ${user.userid}<br>
          <strong>Full Name:</strong> ${user.fullname}<br>
          <strong>State:</strong> ${user.state}
        </li>`;
      });
      output += '</ul>';
      userDetails.innerHTML = output;
      userDetails.classList.remove('d-none');
      userError.classList.add('d-none');
    } else {
      userDetails.classList.add('d-none');
      userError.textContent = data.message || 'No users found.';
      userError.classList.remove('d-none');
    }
  })
  .catch(() => {
    alert('Something went wrong. Please try again.');
  });
});

</script>
</body>
</html>
