<?php 

// Enable error reporting for debugging
/*ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);*/



include('database/connection.php');
session_start();
if(!$_SESSION['email'] )
{
    header("Location: login.php");
}

if($_REQUEST['id'] && $_REQUEST['id'] != '')
{
    $id = $_REQUEST['id'];
    
    // Query to check if user exists
    $query = "SELECT * FROM agent_leads WHERE id=".$id;
    $result = $link->query($query);
    
    if ($result->num_rows == 1) {
        // User is authenticated, store user info in session
        $lead = $result->fetch_assoc();
    }
        
    
    
    
} 


if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['action']) && $_POST['action'] == 'edit_lead') {
    // Sanitize input data
    $user_id = $_SESSION['user_id'];
    $id = $link->real_escape_string($_POST['id']);
    $first_name = $link->real_escape_string($_POST['first_name']);
    $last_name = $link->real_escape_string($_POST['last_name']);
    $email = $link->real_escape_string($_POST['email']);
    $phone_number = $link->real_escape_string($_POST['phone_number']);
    $dicision_make = $link->real_escape_string($_POST['dicision_make']);
    $doctor_speciality = $link->real_escape_string($_POST['doctor_speciality']);
    $average_monthly_collection = $link->real_escape_string($_POST['average_monthly_collection']);
    $state = $link->real_escape_string($_POST['state']);
    $current_billing_percentage = $link->real_escape_string($_POST['current_billing_percentage']);
    $software_name = $link->real_escape_string($_POST['software_name']);
    $practice = $link->real_escape_string($_POST['practice']);
    $billing_service = $link->real_escape_string($_POST['billing_service']);
    $call_back_time = $link->real_escape_string($_POST['call_back_time']);

    // Construct the SQL query
    $sql = "
    UPDATE agent_leads
    SET 
        first_name = '$first_name',
        last_name = '$last_name',
        phone_number = '$phone_number',
        dicision_make = '$dicision_make',
        doctor_speciality = '$doctor_speciality',
        average_monthly_collection = '$average_monthly_collection',
        state = '$state',
        current_billing_percentage = '$current_billing_percentage',
        software_name = '$software_name',
        practice = '$practice',
        billing_service = '$billing_service',
        call_back_time = '$call_back_time',
        edit_request = false
    WHERE id = '$id';
";


    // Execute the query
    if ($link->query($sql) === TRUE) {
        $_SESSION['success'] = "Lead updated successfully";
        header("Location: " . "edit_lead.php?id=".$id);
        exit();
        
        //echo "New record created successfully";
    } else {
        
        $_SESSION['error'] = "Some error occured!";
        header("Location: " . "edit_lead.php?id=".$id);
        exit();
        
        
        //echo "Error: " . $sql . "<br>" . $link->error;
    }

    // Close the database connection
    $link->close();
}


?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>codesitex | Dashboard</title>

    <!-- Google Font: Source Sans Pro -->
    <link
      rel="stylesheet"
      href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback"
    />
    <!-- Font Awesome -->
    <link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css" />
    <!-- Ionicons -->
    <link
      rel="stylesheet"
      href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css"
    />
    <!-- Tempusdominus Bootstrap 4 -->
    <link
      rel="stylesheet"
      href="plugins/tempusdominus-bootstrap-4/css/tempusdominus-bootstrap-4.min.css"
    />
    <!-- iCheck -->
    <link
      rel="stylesheet"
      href="plugins/icheck-bootstrap/icheck-bootstrap.min.css"
    />
    <!-- JQVMap -->
    <link rel="stylesheet" href="plugins/jqvmap/jqvmap.min.css" />
    <!-- Theme style -->
    <link rel="stylesheet" href="dist/css/adminlte.min.css" />
    <!-- overlayScrollbars -->
    <link
      rel="stylesheet"
      href="plugins/overlayScrollbars/css/OverlayScrollbars.min.css"
    />
    <!-- Daterange picker -->
    <link rel="stylesheet" href="plugins/daterangepicker/daterangepicker.css" />
    <!-- summernote -->
    <link rel="stylesheet" href="plugins/summernote/summernote-bs4.min.css" />
    <link rel="stylesheet" href="plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
     <link rel="stylesheet" href="plugins/datatables-responsive/css/responsive.bootstrap4.min.css">
    <style>
      .my_image {
        border-radius: 50%;
        height: 140%;
        width: 38px;
      }
    </style>
    
    <!-- Toastr -->
    <link rel="stylesheet" href="plugins/toastr/toastr.min.css">
    
    
  </head>
  <!--<body class="hold-transition sidebar-mini layout-fixed">-->
  <body class="hold-transition sidebar-collapsee">
    <div class="wrapper">
      <!-- Preloader -->
      <div
        class="preloader flex-column justify-content-center align-items-center"
      >
        <img
          class="animation__shake"
          src="dist/img/AdminLTELogo.png"
          alt="AdminLTELogo"
          height="60"
          width="60"
        />
      </div>

      <!-- Navbar -->
      <nav class="main-header navbar navbar-expand navbar-white navbar-light">
        <!-- Left navbar links -->
       <ul class="navbar-nav">
          <li class="nav-item">
            <a class="nav-link" data-widget="pushmenu" href="#" role="button"
              ><i class="fas fa-bars"></i
            ></a>
          </li>
          <li class="nav-item d-none d-sm-inline-block">
            <a href="index.php" class="nav-link">Leads</a>
          </li>
          <li class="nav-item d-none d-sm-inline-block">
            <a href="agent_leads.php" class="nav-link">Agent Leads</a>
          </li>
          <li class="nav-item d-none d-sm-inline-block">
            <a href="allowed_ip_address.php" class="nav-link">Allowed IP Addresses</a>
          </li>
          
          <li class="nav-item dropdown">
            <a id="dropdownSubMenu1" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="nav-link dropdown-toggle">Landing Pages</a>
            <ul aria-labelledby="dropdownSubMenu1" class="dropdown-menu border-0 shadow">
                <li><a href="new_landing.php" class="dropdown-item">Landing Page </a></li>
                <!--<li><a href="publishing_landing.php" class="dropdown-item">Publishing</a></li>-->
                <!--<li><a href="picing_landing.php" class="dropdown-item">Pricing</a></li>-->
            </ul>
          </li>
          
          <!--<li class="nav-item dropdown">-->
          <!--  <a id="dropdownSubMenu2" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="nav-link dropdown-toggle">Questionnaire</a>-->
          <!--  <ul aria-labelledby="dropdownSubMenu2" class="dropdown-menu border-0 shadow">-->
          <!--      <li><a href="web.php" class="dropdown-item">Web </a></li>-->
          <!--      <li><a href="logo.php" class="dropdown-item">Logo</a></li>-->
          <!--      <li><a href="seo.php" class="dropdown-item">SEO</a></li>-->
          <!--      <li><a href="smm.php" class="dropdown-item">SMM</a></li>-->
          <!--      <li><a href="content.php" class="dropdown-item">Content</a></li>-->
          <!--      <li><a href="book-cover.php" class="dropdown-item">Book Cover</a></li>-->
          <!--      <li><a href="publishing.php" class="dropdown-item">Publishing </a></li>-->
          <!--      <li><a href="ebook.php" class="dropdown-item">Ebook</a></li>-->
          <!--      <li><a href="illustrations.php" class="dropdown-item">Illustrations</a></li>-->
          <!--      <li><a href="wikipedia.php" class="dropdown-item">Wikipedia </a></li>-->
                
          <!--  </ul>-->
          <!--</li>-->
          <?php if($_SESSION['user_role']=='admin'){ ?>
          <li class="nav-item dropdown">
            <a id="dropdownSubMenu3" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="nav-link dropdown-toggle">User Management</a>
            <ul aria-labelledby="dropdownSubMenu3" class="dropdown-menu border-0 shadow">
                <li><a href="add_user.php" class="dropdown-item">Add User </a></li>
                <li><a href="user.php" class="dropdown-item">View Users</a></li>
            </ul>
          </li>
          <?php } ?>
          <li class="nav-item d-none d-sm-inline-block">
            <a href="login.php" class="nav-link">Logout</a>
          </li>
        </ul>

        <!-- Right navbar links -->
        <ul class="navbar-nav ml-auto">
          <!-- Navbar Search -->
          <!--<li class="nav-item">-->
          <!--  <a-->
          <!--    class="nav-link"-->
          <!--    data-widget="navbar-search"-->
          <!--    href="#"-->
          <!--    role="button"-->
          <!--  >-->
          <!--    <i class="fas fa-search"></i>-->
          <!--  </a>-->
            
          <!--</li>-->

          <!-- Messages Dropdown Menu -->
          <!--<li class="nav-item dropdown">-->
          <!--  <a class="nav-link" data-toggle="dropdown" href="#">-->
          <!--    <i class="far fa-comments"></i>-->
          <!--    <span class="badge badge-danger navbar-badge">3</span>-->
          <!--  </a>-->
          <!--  <div class="dropdown-menu dropdown-menu-lg dropdown-menu-right">-->
          <!--    <a href="#" class="dropdown-item">-->
                <!-- Message Start -->
          <!--      <div class="media">-->
          <!--        <img-->
          <!--          src="dist/img/user1-128x128.jpg"-->
          <!--          alt="User Avatar"-->
          <!--          class="img-size-50 mr-3 img-circle"-->
          <!--        />-->
          <!--        <div class="media-body">-->
          <!--          <h3 class="dropdown-item-title">-->
          <!--            Brad Diesel-->
          <!--            <span class="float-right text-sm text-danger"-->
          <!--              ><i class="fas fa-star"></i-->
          <!--            ></span>-->
          <!--          </h3>-->
          <!--          <p class="text-sm">Call me whenever you can...</p>-->
          <!--          <p class="text-sm text-muted">-->
          <!--            <i class="far fa-clock mr-1"></i> 4 Hours Ago-->
          <!--          </p>-->
          <!--        </div>-->
          <!--      </div>-->
                <!-- Message End -->
          <!--    </a>-->
          <!--    <div class="dropdown-divider"></div>-->
          <!--    <a href="#" class="dropdown-item">-->
                <!-- Message Start -->
          <!--      <div class="media">-->
          <!--        <img-->
          <!--          src="dist/img/user8-128x128.jpg"-->
          <!--          alt="User Avatar"-->
          <!--          class="img-size-50 img-circle mr-3"-->
          <!--        />-->
          <!--        <div class="media-body">-->
          <!--          <h3 class="dropdown-item-title">-->
          <!--            John Pierce-->
          <!--            <span class="float-right text-sm text-muted"-->
          <!--              ><i class="fas fa-star"></i-->
          <!--            ></span>-->
          <!--          </h3>-->
          <!--          <p class="text-sm">I got your message bro</p>-->
          <!--          <p class="text-sm text-muted">-->
          <!--            <i class="far fa-clock mr-1"></i> 4 Hours Ago-->
          <!--          </p>-->
          <!--        </div>-->
          <!--      </div>-->
                <!-- Message End -->
          <!--    </a>-->
          <!--    <div class="dropdown-divider"></div>-->
          <!--    <a href="#" class="dropdown-item">-->
                <!-- Message Start -->
          <!--      <div class="media">-->
          <!--        <img-->
          <!--          src="dist/img/user3-128x128.jpg"-->
          <!--          alt="User Avatar"-->
          <!--          class="img-size-50 img-circle mr-3"-->
          <!--        />-->
          <!--        <div class="media-body">-->
          <!--          <h3 class="dropdown-item-title">-->
          <!--            Nora Silvester-->
          <!--            <span class="float-right text-sm text-warning"-->
          <!--              ><i class="fas fa-star"></i-->
          <!--            ></span>-->
          <!--          </h3>-->
          <!--          <p class="text-sm">The subject goes here</p>-->
          <!--          <p class="text-sm text-muted">-->
          <!--            <i class="far fa-clock mr-1"></i> 4 Hours Ago-->
          <!--          </p>-->
          <!--        </div>-->
          <!--      </div>-->
                <!-- Message End -->
          <!--    </a>-->
          <!--    <div class="dropdown-divider"></div>-->
          <!--    <a href="#" class="dropdown-item dropdown-footer"-->
          <!--      >See All Messages</a-->
          <!--    >-->
          <!--  </div>-->
          <!--</li>-->
          <!-- Notifications Dropdown Menu -->
          
          <li class="nav-item">
            <a class="nav-link" data-widget="fullscreen" href="#" role="button">
              <i class="fas fa-expand-arrows-alt"></i>
            </a>
          </li>
          <!--<li class="nav-item">
            <a
              class="nav-link"
              data-widget="control-sidebar"
              data-controlsidebar-slide="true"
              href="#"
              role="button"
            >
              <i class="fas fa-th-large"></i>
            </a>
          </li>-->
        </ul>
      </nav>
      <!-- /.navbar -->

      <!-- Main Sidebar Container -->
      
      <?php include 'includes/aside.php' ?>

      <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
          <div class="container-fluid">
            <div class="row mb-2">
              <div class="col-sm-6">
                <h1 class="m-0">Edit Lead</h1>
              </div>
              <!-- /.col -->
              <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                  <li class="breadcrumb-item"><a href="https://codesitex.com/">Back To The Brand</a></li>
                 
                </ol>
              </div>
              <!-- /.col -->
            </div>
            <!-- /.row -->
          </div>
          <!-- /.container-fluid -->
        </div>
        <!-- /.content-header -->

        <!-- Main content -->
        <section class="content">
          <div class="container-fluid">
          <div class="row">
          <!-- left column -->
          <div class="col-md-12">
            <!-- general form elements -->
            <div class="card card-primary">
              <div class="card-header">
                <h3 class="card-title">Lead <small>Information</small></h3>
              </div>
              <!-- /.card-header -->
              
              <form action="" id="edit_lead_form" method="post">
              
              <div class="card-body table-responsive">
                    
                    <div class="row">
                        <div class="col-sm-6">
                          <!-- text input -->
                          <div class="form-group">
                            <label>First Name</label>
                            <input name="first_name" type="text" class="form-control" placeholder="Enter ..." value="<?php echo $lead["first_name"] ?? ''; ?>"  required>
                          </div>
                        </div>
                        
                        <div class="col-sm-6">
                          <div class="form-group">
                            <label>Last Name</label>
                            <input name="last_name" type="text" class="form-control" placeholder="Enter ..." value="<?php echo $lead["last_name"] ?? ''; ?>" required>
                          </div>
                        </div>
                        <div class="col-sm-6">
                          <div class="form-group">
                            <label>Email</label>
                            <input name="email" type="email" class="form-control" value="<?php echo $lead["email"] ?? '' ?>" disabled placeholder="Enter ..." required>
                          </div>
                        </div>
                        
                        <div class="col-sm-6">
                          <div class="form-group">
                            <label>Number</label>
                            <input name="phone_number" id="phone_number" type="number" class="form-control" value="<?php echo $lead["phone_number"] ?? '' ?>" placeholder="Enter *****" required>
                          </div>
                        </div>
                        
                        <div class="col-sm-6">
                          <div class="form-group">
                            <label>Dicision Maker</label>
                            <input name="dicision_make" type="text" class="form-control" value="<?php echo $lead["dicision_make"] ?? '' ?>" placeholder="Enter *****" required>
                          </div>
                        </div>
                        
                        <div class="col-sm-6">
                          <div class="form-group">
                            <label>Doctor Speciality</label>
                            <input name="doctor_speciality" type="text" class="form-control" value="<?php echo $lead["doctor_speciality"] ?? '' ?>" placeholder="Enter *****" required>
                          </div>
                        </div>
                        
                        <div class="col-sm-6">
                          <div class="form-group">
                            <label>Whats your average monthly collection</label>
                            <input name="average_monthly_collection" type="text" class="form-control" value="<?php echo $lead["average_monthly_collection"] ?? '' ?>" placeholder="Enter *****" required>
                          </div>
                        </div>
                        
                         <div class="col-sm-6">
                          <div class="form-group">
                            <label>State</label>
                            <input name="state" type="text" class="form-control" value="<?php echo $lead["state"] ?? '' ?>" placeholder="Enter *****" required>
                          </div>
                        </div>
                        
                          <div class="col-sm-6">
                            <label>What percentage your current medical billing company charges you</label>
                          <div class="input-group">
                            <div class="input-group-prepend">
<span class="input-group-text">%</span>
</div>
                            <input name="current_billing_percentage" type="number" class="form-control" value="<?php echo $lead["current_billing_percentage"] ?? '' ?>" placeholder="Enter *****" required>
                          </div>
                        </div>
                        
                          <div class="col-sm-6">
                          <div class="form-group">
                            <label>What software are you using ?</label>
                            <input name="software_name" type="text" class="form-control" value="<?php echo $lead["software_name"] ?? '' ?>" placeholder="Enter *****" required>
                          </div>
                        </div>
                        
                       
                        
                        <div class="col-sm-6">
                          <div class="form-group">
                               <h5>Practice Type</h5>
                           <div class="form-check">
                                <input class="form-check-input" type="radio" name="practice" value="individual" <?php if($lead['practice'] == "individual") echo "checked" ?> id="individual" required>
                                <label class="form-check-label" for="individual">Individual Practice</label>
                            </div>
                           <div class="form-check">
                                <input class="form-check-input" type="radio" name="practice" value="group" <?php if($lead['practice'] == "group") echo "checked" ?> id="group" required>
                                <label class="form-check-label" for="group">Group Practice</label>
                            </div>
                          </div>
                        </div>
                       
                        <div class="col-sm-6">
                          <div class="form-group">
                               <h5>Billing Service</h5>
                           <div class="form-check">
                                <input class="form-check-input" type="radio" name="billing_service" <?php if($lead['billing_service'] == "inhouse") echo "checked" ?> value="inhouse" id="inhouse" required>
                                <label class="form-check-label" for="inhouse">Inhouse </label>
                            </div>
                           <div class="form-check">
                                <input class="form-check-input" type="radio" name="billing_service" <?php if($lead['billing_service'] == "outsourcing") echo "checked" ?> value="outsourcing" id="outsourcing" required>
                                <label class="form-check-label" for="outsourcing">Outsourcing</label>
                            </div>
                          </div>
                        </div>

                        <div class="col-sm-6">
                          <div class="form-group">
                            <label>Call back time</label>
                            <input name="call_back_time" type="time" class="form-control" value="<?php echo $lead["call_back_time"] ?? '' ?>" placeholder="Enter *****" required>
                          </div>
                        </div>
                        
                    </div>
              
              </div>
              
              <input type="hidden" name="action" value="edit_lead">
              <input type="hidden" name="id" value="<?php echo $lead["id"] ?? '' ?>">
              
              <div class="card-footer">
                  <button type="submit" class="btn btn-primary">Submit</button>
              </div>
              
              </form>
            </div>
            <!-- /.card -->

           

            
            

          </div>
          <!--/.col (left) -->
         
        </div>
          </div>
          <!-- /.container-fluid -->
        </section>
        <!-- /.content -->
      </div>
      <!-- /.content-wrapper -->
      <footer class="main-footer">
        <strong
          >Copyright &copy; <?php echo date("Y"); ?>
          <a href="https://adminlte.io">codesitex.com</a>.</strong
        >
        All rights reserved.
        <div class="float-right d-none d-sm-inline-block">
          <b>Version</b> 3.2.0
        </div>
      </footer>

      <!-- Control Sidebar -->
      <aside class="control-sidebar control-sidebar-dark">
        <!-- Control sidebar content goes here -->
      </aside>
      <!-- /.control-sidebar -->
    </div>
    <!-- ./wrapper -->

    <!-- jQuery -->
    <!-- <script src="plugins/jquery/jquery.min.js"></script> -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.21.0/jquery.validate.min.js" integrity="sha512-KFHXdr2oObHKI9w4Hv1XPKc898mE4kgYx58oqsc/JqqdLMDI4YjOLzom+EMlW8HFUd0QfjfAvxSL6sEq/a42fQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <!-- jQuery UI 1.11.4 -->
    <script src="plugins/jquery-ui/jquery-ui.min.js"></script>
    
    
    <!-- Resolve conflict in jQuery UI tooltip with Bootstrap tooltip -->
    <script>
      $.widget.bridge("uibutton", $.ui.button);
    </script>
    <!-- Bootstrap 4 -->
    <script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
    <!-- ChartJS -->
    <script src="plugins/chart.js/Chart.min.js"></script>
    <!-- Sparkline -->
    <script src="plugins/sparklines/sparkline.js"></script>
    <!-- JQVMap -->
    <script src="plugins/jqvmap/jquery.vmap.min.js"></script>
    <script src="plugins/jqvmap/maps/jquery.vmap.usa.js"></script>
    <!-- jQuery Knob Chart -->
    <script src="plugins/jquery-knob/jquery.knob.min.js"></script>
    <!-- daterangepicker -->
    <script src="plugins/moment/moment.min.js"></script>
    <script src="plugins/daterangepicker/daterangepicker.js"></script>
    <!-- Tempusdominus Bootstrap 4 -->
    <script src="plugins/tempusdominus-bootstrap-4/js/tempusdominus-bootstrap-4.min.js"></script>
    <!-- Summernote -->
    <script src="plugins/summernote/summernote-bs4.min.js"></script>
    <!-- overlayScrollbars -->
    <script src="plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
    <!-- AdminLTE App -->
    <script src="dist/js/adminlte.js"></script>
    <!-- AdminLTE for demo purposes -->
    <script src="dist/js/demo.js"></script>
    <!-- AdminLTE dashboard demo (This is only for demo purposes) -->
    <script src="dist/js/pages/dashboard.js"></script>
  
    <!-- Toastr -->
    <script src="plugins/toastr/toastr.min.js"></script>


    <script>
    
     $(document).ready(function(event) {
              $('#edit_lead_form').on("submit", function(event) {
        var phoneNumber = $("#phone_number").val();
        if(phoneNumber.length != 10 ){
            event.preventDefault();
            toastr.error("Phone number must be 10 digits");
        }
        });
           
    });
    
    $(function () {
      $('#selec2').summernote()

        // CodeMirror.fromTextArea(document.getElementById("codeMirrorDemo"), {
        //   mode: "htmlmixed",
        //   theme: "monokai"
        // });
      })
    </script>
    
    <script src="plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
    <script src="plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
    
    <script>
      $(function () {
        $("#example1").DataTable({
          "responsive": true, "lengthChange": false, "autoWidth": false,
          "buttons": ["copy", "csv", "excel", "pdf", "print"],
          "ordering": false
        }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
        
        
      });
      
        
        <?php if ($_SESSION['success']) { ?>
            toastr.success('<?php echo $_SESSION['success']; ?>');
        <?php unset($_SESSION['success']); // Clear the notification after displaying
        }
        
        if ($_SESSION['error']) { ?>
            toastr.error('<?php echo $_SESSION['error']; ?>');
        <?php unset($_SESSION['error']); // Clear the notification after displaying
            }
        ?>
      
    </script>
    
  </body>
</html>
