<?php 
  include('database/connection.php');
  session_start();
  if(!$_SESSION['email'] )
  {
    header("Location: login.php");
  }
?>
<!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">LEADS</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-primaryy">
              <div class="card-header">
                <!--<h3 class="card-title">LEADS</h3>-->
                
                <!-- Developer test -->
                <div class="overlay-wrapper d-none">
                  <div class="overlay">
                    <i class="fas fa-3x fa-sync-alt fa-spin"></i>
                    <div class="text-bold pt-2">Loading...</div>
                  </div>
                </div>
                
                
                <!-- Import Export Leads Start --> 
                <div class="row">
                    <div class="col-md-12">
                        <form method="POST" enctype="multipart/form-data" id="importLeads">
                        <div class="input-group">
                        	<input type="file" name="file" class="form-control" id="customFile" required="required">
                        	<div class="input-group-append">
                                <button type="submit" class="btn btn-success importLeads">
                                    <i class="fa fa-upload"></i>&nbsp;IMPORT LEADS
                                </button>
                                <!--<a href="https://totalairportparking.co.uk/api/full-calendar/rate-band/27/export-rate" class="btn btn-info">
                                    <i class="fa fa-download"></i>&nbsp;EXPORT
                                </a>-->
                            </div>
                        </div>
                        </form>
                    </div>
                </div> <hr />
                <!-- Import Export Leads End -->
                
                
                <div class="row">
                    <div class="col-md-12">
                        <form id="search-form" method="get">
            			    <div class="input-group">
            			        
            			        <!--<select class="form-control" name="services" id="exampleFormControlSelect1">-->
                       <!--             <option value="">Services</option>-->
                       <!--             <option value="Book Writing">Book Writing </option>-->
                       <!--             <option value="E-book Writing &amp; Publishing"> E-book Writing &amp; Publishing</option>-->
                       <!--             <option value="Book Editing &amp; Proofreading">Book Editing &amp; Proofreading</option>-->
                       <!--             <option value="Book Cover Design"> Book Cover Design </option>-->
                       <!--             <option value="Book Illustrations">Book Illustrations </option>-->
                       <!--             <option value="Book Publishing">Book Publishing </option>-->
                       <!--             <option value="Book Printing">Book Printing </option>-->
                       <!--             <option value="Book Marketing">Book Marketing</option>-->
                       <!--             <option value="Author Website">Author Website</option>-->
                       <!--             <option value="Amazon Publishing ">Amazon Publishing </option>-->
                       <!--         </select>-->
            			        <input type="date" name="start_date" value="Start date" class="form-control">
            			        <input type="date" name="end_date" value="End date" class="form-control">
            			        
            			        
            			        
                                <div class="input-group-append">
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fa fa-search"></i>
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                
                <!-- Developer test -->
                
                
              </div>
              <!-- /.card-header -->
              <!-- form start -->
              <div class="card-body table-responsive">
              <p id="showMessage" style="color:green"></p>
               <table id="example1" class="table table-bordered table-striped">
                  <thead>
                  <tr>
                    <th>Sr.</th>
                    <!--<th>Name</th>
                    <th>Email</th>
                    <th>Phone</th>-->
                    <th>Customer Info</th>
                    <th>Services</th>
                    <!--<th width="10%">URL</th>-->
                    <th>Date</th>
                    <th>Ip</th>
                    
                    <th>Status</th>
                    <th>Note</th>
                    <th>Action</th>
                    
                  </tr>
                  </thead>
                  <tbody>
                   <?php
                        
                        $service = isset($_GET['services']) ? $_GET['services'] : '';
                        $start_date = isset($_GET['start_date']) ? $_GET['start_date'] : '';
                        $end_date = isset($_GET['end_date']) ? $_GET['end_date'] : '';
            
                        // Fetch leads based on the search criteria
                        //$sql = "SELECT * FROM leads where `page_name` IS NULL ORDER BY `id` DESC";
                        //$sql = "SELECT * FROM leads WHERE leads LIKE '%$search%' AND `page_name` IS NULL ORDER BY `id` DESC";
                        
                        //$sql = "SELECT * FROM leads WHERE leads LIKE '%$search%' AND `page_name` IS NULL AND `created_at` BETWEEN '$start_date' AND '$end_date' ORDER BY `id` DESC";
                        
                        $sql = "SELECT * FROM leads where `page_name` IS NULL";
                        
                        if($service)
                            $sql .= " AND leads LIKE '%$service%'";
                        
                        
                        if($start_date && $end_date)
                            $sql .= " AND DATE(date) BETWEEN '$start_date' AND '$end_date'";
                        
                        
                        $sql .= " ORDER BY `id` DESC";
                         
                        if($result = mysqli_query($link, $sql)){
                          if(mysqli_num_rows($result) > 0){
                            $i = 1;
                            while($row = mysqli_fetch_array($result)){
                                
                                //$data = json_decode($row['leads']);
                                
                                $jsonString = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $row['leads']);
                                $data = json_decode($jsonString);
                                
                              ?>
                              <tr>
                                <td><?php echo $i; ?></td>
                                <td>
                                    <i class="fas fa-user-circle mr-2 text-primary"></i>&nbsp;<b><?php echo $data->name??null; ?></b><br>
                                    <a href="tel:<?php echo $data->phone??null; ?>">
                                        <i class="fas fa-phone mr-2 text-primary"></i>&nbsp;<?php echo $data->phone??null; ?>
                                    </a> <br />
                                    <a href="javascript:;" data-toggle="modal" data-target="#modal-lgg<?php echo $row["id"]; ?>">
                                        <i class="fas fa-envelope mr-2 text-primary"></i>&nbsp;<?php echo $data->email??null; ?>
                                    </a>
                                </td>
                                
                                
                                <!-- Modal window for Email start -->
                                <div class="modal fade" id="modal-lgg<?php echo $row["id"]; ?>">
                                    <div class="modal-dialog modal-lg">
                                        <div class="modal-content">
                                            
                                            <div class="overlay-wrapper d-none">
                                              <div class="overlay">
                                                <i class="fas fa-3x fa-sync-alt fa-spin"></i>
                                                <div class="text-bold pt-2">Loading...</div>
                                              </div>
                                            </div>
                                            
                                            <!-- Compose form start -->
                                            <form class="send-email-form" action="send_email.php" method="post">
                                                 <input type="hidden" name="action" value="send_email">
                                            <!-- Compose email start -->
                                            <div class="card card-primary card-outline">
                                               <div class="card-header">
                                                  <h3 class="card-title">Compose New Message</h3>
                                               </div>
                                               <div class="card-body">
                                                  <div class="form-group">
                                                     <input class="form-control" name="to_email" value="<?php echo $data->email??null; ?>" placeholder="To:" required>
                                                  </div>
                                                  <div class="form-group">
                                                     <input class="form-control" name="subject" placeholder="Subject:" required>
                                                  </div>
                                                  <div class="form-group">
                                                     <textarea  name="message" class="email_message" required></textarea>
                                                  </div>
                                               </div>
                                               <div class="card-footer">
                                                  <div class="float-right">
                                                     <!--<button type="button" class="btn btn-default"><i class="fas fa-pencil-alt"></i> Draft</button>-->
                                                     <button type="submit" class="btn btn-primary"><i class="far fa-envelope"></i> Send</button>
                                                  </div>
                                                  <button type="reset" data-dismiss="modal" class="btn btn-default"><i class="fas fa-times"></i> Discard</button>
                                               </div>
                                            </div>
                                            <!-- Compose email end -->
                                            
                                            </form>
                                            <!-- Compose /form end -->
                                        </div>
                                    </div>
                                    
                                </div>
                                <!-- Modal window for Email end -->
                                
                                
                                
                                
                                
                                <td> 
                                    <?php //echo $data->service??null; ?> 
                                    <?php if(is_array($data->service)){ echo implode(',',$data->service); }else{ echo $data->service; } ?> 
                                </td>
                                <!--<td>-->
                                <!--    <?php if($data->message){ ?>-->
                                <!--        <?php echo $data->message; ?> -->
                                <!--        <a data-toggle="modal" data-target="#read-more-<?php echo $row["id"]; ?>"><i class="fas fa-plus-circle mr-2 text-primary"></i></a> -->
                                <!--    <?php } ?>-->
                                <!--</td>-->
                                
                                <!-- Read More modal window -->
                                <div class="modal fade" id="read-more-<?php echo $row["id"]; ?>">
                                    <div class="modal-dialog modal-lg">
                                        <div class="modal-content">
                                            
                                            <form class="note-form note-form-<?php echo $row["id"]; ?>" id="<?php echo $row["id"]; ?>">
                                                <div class="modal-header">
                                                    <h4 class="modal-title">Message</h4>
                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                        <span aria-hidden="true">&times;</span>
                                                    </button>
                                                </div>
                                                <div class="modal-body">
                                                    <textarea class="form-control" rows="10" readonly=""><?php echo $data->message??null; ?> </textarea>
                                                </div>
                                                <!--<div class="modal-footer justify-content-between">
                                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                                    <button type="submit" class="btn btn-primary">Save changes</button>
                                                </div>-->
                                            </form>
                                        </div>
                                    </div>
                                    
                                </div>
                                <!-- Read More modal window -->
                                
                                
                                <!--<td> <?php //echo date('h:i:s A | Y-m-d', strtotime($row['date'])); ?></td>-->
                                <td>
                                    <i class="fas fa-clock mr-2 text-primary"></i>&nbsp;<?php echo date('h:i:s A', strtotime($row['date'])); ?><br>
                                    <i class="fas fa-calendar mr-2 text-primary"></i>&nbsp;<?php echo date('Y-m-d', strtotime($row['date'])); ?>
                                </td>
                                
                                <td> <?php echo $data->ip??null; ?></td>
                               
                                <td class="action_td_<?php echo $row["id"]; ?>">
                                    <?php
                                    $status = $row["status"]; // Assuming 3 represents Wednesday
                                    $class = "secondary";
                                    $value = "Choose";
                                    
                                    switch ($status) {
                                      case "paid":
                                        $class = "success";
                                        $value = "Paid";
                                        break;
                                        
                                      case "follow_up":
                                        $class = "warning";
                                        $value = "Follow UP";
                                        break;
                                        
                                      case "call_back":
                                        $class = "info";
                                        $value = "Call Back";
                                        break;
                                      
                                      /**/
                                      case "today":
                                            $class = "info";
                                            $value = "Today";
                                            break;
                                      case "yesterday":
                                            $class = "info";
                                            $value = "Yesterday";
                                            break;
                                      case "week_to_date":
                                            $class = "info";
                                            $value = "Week To Date";
                                            break;
                                      case "month_to_date":
                                            $class = "info";
                                            $value = "Month To Date";
                                            break;
                                      case "custom":
                                            $class = "info";
                                            $value = "Custom";
                                            break;
                                      /**/
                                      
                                      
                                      case "dead_lead":
                                        $class = "danger";
                                        $value = "Dead Lead";
                                        break;
                                        
                                      default:
                                        $class = "default";
                                        $value = "Choose";
                                        break;
                                    }
                                    ?>
                                    
                                    <!--<div class="dropdown">
                                      <button class="btn btn-<?php echo $class; ?> dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
                                        <?php echo $value; ?>  
                                      </button>
                                      <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
                                        <li><a class="dropdown-item" href="#">Action</a></li>
                                        <li><a class="dropdown-item" href="#">Another action</a></li>
                                        <li><a class="dropdown-item" href="#">Something else here</a></li>
                                      </ul>
                                    </div>-->
                                    
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-<?php echo $class; ?>"><?php echo $value; ?> </button>
                                        <button type="button" class="btn btn-<?php echo $class; ?> dropdown-toggle dropdown-icon" data-toggle="dropdown">
                                            <span class="sr-only">Toggle Dropdown</span>
                                        </button>
                                    <div class="dropdown-menu" role="menu">
                                        <a class="dropdown-item status-change" data-id="<?php echo $row["id"]; ?>" data-value="paid" href="javascirpt:;">Paid</a>
                                        <a class="dropdown-item status-change" data-id="<?php echo $row["id"]; ?>" data-value="follow_up" href="javascirpt:;">Follow UP</a>
                                        <a class="dropdown-item status-change" data-id="<?php echo $row["id"]; ?>" data-value="call_back" href="javascirpt:;">Call Back</a>
                                        
                                        <a class="dropdown-item status-change" data-id="<?php echo $row["id"]; ?>" data-value="today" href="javascirpt:;">Today</a>
                                        <a class="dropdown-item status-change" data-id="<?php echo $row["id"]; ?>" data-value="yesterday" href="javascirpt:;">Yesterday</a>
                                        <a class="dropdown-item status-change" data-id="<?php echo $row["id"]; ?>" data-value="week_to_date" href="javascirpt:;">Week To Date</a>
                                        <a class="dropdown-item status-change" data-id="<?php echo $row["id"]; ?>" data-value="month_to_date" href="javascirpt:;">Month To Date</a>
                                        <a class="dropdown-item status-change" data-id="<?php echo $row["id"]; ?>" data-value="custom" href="javascirpt:;">Custom</a>
                                        
                                        
                                        <a class="dropdown-item status-change" data-id="<?php echo $row["id"]; ?>" data-value="dead_lead" href="javascirpt:;">Dead Lead</a>
                                        <!--<div class="dropdown-divider"></div>
                                        <a class="dropdown-item" href="#">Separated link</a>-->
                                    </div>
                                    </div>
                                    
                                    
                                    
                               </td>
                               
                                <td>
                                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-lg<?php echo $row["id"]; ?>">
                                       <i class="fas fa-sticky-note"></i> NOTES
                                    </button>
                                    
                               </td>
                               
                                <?php if($_SESSION['user_role']=='admin'){ ?>
                                <td>
                                    <a class="btn btn-danger btn-lg deleteButton" href="javascript:;" data-record-id="<?php echo $row["id"]; ?>">
                                        <i class="fas fa-trash"></i>
                                    </a>
                                </td>
                                <?php } ?>
                                
                              </tr>
                              
                                
                                <!-- Modal window for note start -->
                                <div class="modal fade" id="modal-lg<?php echo $row["id"]; ?>">
                                    <div class="modal-dialog modal-lg">
                                        <div class="modal-content">
                                            
                                            <div class="overlay-wrapper d-none">
                                              <div class="overlay">
                                                <i class="fas fa-3x fa-sync-alt fa-spin"></i>
                                                <div class="text-bold pt-2">Loading...</div>
                                              </div>
                                            </div>
                                            
                                            <form class="note-form note-form-<?php echo $row["id"]; ?>" id="<?php echo $row["id"]; ?>">
                                                <div class="modal-header">
                                                    <h4 class="modal-title">LEAD NOTES <?php echo $row["id"]; ?></h4>
                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                        <span aria-hidden="true">&times;</span>
                                                    </button>
                                                </div>
                                                <div class="modal-body">
                                                    
                                                    <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
                                                    <input type="hidden" name="action" value="add_note">
                                                    <textarea class="form-control show_note show_note_<?php echo $row["id"]; ?>" rows="10" readonly=""><?php echo $row["note"]; ?></textarea>
                                                    <div class="input-group input-group-sm mt-2">
                                                      <input name="note" class="form-control form-control-sm" placeholder="Enter note here ..." required="">
                                                      <!--<div class="input-group-append">
                                                        <button type="submit" class="btn btn-danger">Add</button>
                                                      </div>-->
                                                    </div>
                                                    
                                                </div>
                                                <div class="modal-footer justify-content-between">
                                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                                    <button type="submit" class="btn btn-primary">Save changes</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                    
                                </div>
                                <!-- Modal window for note end -->
                                
                                
                                
                                
                              
                              
                              <?php
                              $i++;
                            }
                          }else{
                            ?> <option value="">No data found</option> <?php
                          }
                        }
                      ?>
                 
                 
                  </tfoot>
                </table>
                </div>
            </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://codesitex.com">codesitex</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>
    <!-- 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>



       $(function () {
      $('.email_message').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)');
        
        
      });
    </script>



<script>
$(document).ready(function() {
  
  // Handle onchange event of the dropdown
  $(document).on("click", ".status-change", function (e) {
    
    var ID = $(this).attr('data-id');
    var selectedValue = $(this).attr('data-value');
    
    
    // Use the switch statement to perform different actions based on the value of the "action" variable
    switch (selectedValue) {
        
        case "paid":
            var bg_color = "success";
            var  dropdown_title = "Paid";
            break;
            
        case "follow_up":
            var bg_color = "warning";
            var dropdown_title = "Follow UP";
            break;
            
        case "call_back":
            var bg_color = "info";
            var dropdown_title = "Call Back";
            break;
            
        case "dead_lead":
            var bg_color = "danger";
            var dropdown_title = "Dead Lead";
            break;
        
        /**/
        case "today":
            var bg_color = "info";
            var dropdown_title = "Today";
            break;
        case "yesterday":
            var bg_color = "info";
            var dropdown_title = "Yesterday";
            break;
        case "week_to_date":
            var bg_color = "info";
            var dropdown_title = "Week To Date";
            break;
        case "month_to_date":
            var bg_color = "info";
            var dropdown_title = "Month To Date";
            break;
        case "custom":
            var bg_color = "info";
            var dropdown_title = "Custom";
            break;
        /**/
            
        default:
            var bg_color = "default";
            var dropdown_title = "Choose";
            break;
            
    }
    
    
     
    if (!confirm("Are you sure you want to perform this action?")) 
        return false;
    
    // Send AJAX request to update the record
    $.ajax({
      type: 'POST',
      url: 'dev_script.php', // Replace with the PHP file to handle the update
      data: { id: ID, status: selectedValue, action: 'update_status' },
      success: function(response) {
        // Handle the response from the server (if needed)
        if(response=='success')
        {
            //$(".action_td_"+ID).html('<div class="btn-group"><button type="button" class="btn btn-'+bg_color+'">'+dropdown_title+'</button><button type="button" class="btn btn-'+bg_color+' dropdown-toggle dropdown-icon" data-toggle="dropdown"><span class="sr-only">Toggle Dropdown</span></button><div class="dropdown-menu" role="menu"><a class="dropdown-item status-change" data-id="'+ID+'" data-value="paid" href="javascirpt:;">Paid</a><a class="dropdown-item status-change" data-id="'+ID+'" data-value="follow_up" href="javascirpt:;">Follow UP</a><a class="dropdown-item status-change" data-id="'+ID+'" data-value="call_back" href="javascirpt:;">Call Back</a><a class="dropdown-item status-change" data-id="'+ID+'" data-value="dead_lead" href="javascirpt:;">Dead Lead</a></div></div>');
            
            $(".action_td_" + ID).html(
              '<div class="btn-group">' +
                '<button type="button" class="btn btn-' + bg_color + '">' + dropdown_title + '</button>' +
                '<button type="button" class="btn btn-' + bg_color + ' dropdown-toggle dropdown-icon" data-toggle="dropdown">' +
                  '<span class="sr-only">Toggle Dropdown</span>' +
                '</button>' +
                '<div class="dropdown-menu" role="menu">' +
                  '<a class="dropdown-item status-change" data-id="' + ID + '" data-value="paid" href="javascript:;">Paid</a>' +
                  '<a class="dropdown-item status-change" data-id="' + ID + '" data-value="follow_up" href="javascript:;">Follow Up</a>' +
                  '<a class="dropdown-item status-change" data-id="' + ID + '" data-value="call_back" href="javascript:;">Call Back</a>' +
                  '<a class="dropdown-item status-change" data-id="' + ID + '" data-value="today" href="javascript:;">Today</a>' +
                  '<a class="dropdown-item status-change" data-id="' + ID + '" data-value="yesterday" href="javascript:;">Yesterday</a>' +
                  '<a class="dropdown-item status-change" data-id="' + ID + '" data-value="week_to_date" href="javascript:;">Week To Date</a>' +
                  '<a class="dropdown-item status-change" data-id="' + ID + '" data-value="month_to_date" href="javascript:;">Month To Date</a>' +
                  '<a class="dropdown-item status-change" data-id="' + ID + '" data-value="custom" href="javascript:;">Custom</a>' +
                  '<a class="dropdown-item status-change" data-id="' + ID + '" data-value="dead_lead" href="javascript:;">Dead Lead</a>' +
                '</div>' +
              '</div>'
            );
            
            toastr.success("Status: "+dropdown_title);    
        }
        else
        {
            toastr.error("Some error occured!");    
        }
            
      },
      error: function(xhr, status, error) {
        // Handle error (if any)
        toastr.error("Some error occured!!");
      }
    });
  });
  
   // Handle form submissions
  $('.note-form').on('submit', function(event) {
    event.preventDefault(); // Prevent default form submission
    var overlay = $(document).find('.overlay-wrapper');
    overlay.removeClass("d-none");
    
    // Get the form ID
    var formId = $(this).attr('id');
    // Get form data
    var formData = $(this).serialize();

    // Send AJAX request to insert data into the database
    $.ajax({
      type: 'POST',
      url: 'dev_script.php', // Replace with the PHP file to handle the insertion
      data: formData,
      success: function(response) {
        overlay.addClass("d-none");
        
        // Handle the response from the server (if needed)
        if(response!='error')
        {
            $('.show_note_'+formId).html(response);
            //form.find(".show_note").html(response);
            form.find(".show_note_"+formId).scrollTop($(".show_note_"+formId)[0].scrollHeight);
            toastr.success("Note added successfully!");
            
            //$('.note-form')[0].reset();
        }
        else
        {
            toastr.error("Some error occured!"); 
        }
      },
      error: function(xhr, status, error) {
        // Handle error (if any)
        toastr.error("Some error occured!"); 
      }
    });
  });
  
  // handle email submissions
  $('.send-email-form').on('submit', function(event) {
    event.preventDefault(); // Prevent default form submission
     var overlay = $(document).find('.overlay-wrapper');
    overlay.removeClass("d-none");
    
    // Get the form ID
   
    var formData = $(this).serialize();

    // Send AJAX request to insert data into the database
    $.ajax({
      type: 'POST',
      url: 'send_email.php', // Replace with the PHP file to handle the insertion
      data: formData,
      success: function(response) {
        overlay.addClass("d-none");
        
        console.log(response)
        // Handle the response from the server (if needed)
        if(response!='error')
        {
            toastr.success("Email sended successfully!");
            
            //$('.note-form')[0].reset();
        }
        else
        {
            toastr.error("Some error occured!"); 
        }
      },
      error: function(xhr, status, error) {
        // Handle error (if any)
        console.log(error)
        overlay.addClass("d-none");
        toastr.error("Some error occured!"); 
      }
    });
  });
  
  
  $(document).on("click", ".deleteButton", function () {
    var recordId = $(this).data("record-id");
    var closestTr = $(this).closest("tr");
    $.ajax({
      url: "dev_script.php", // Replace with your server-side delete script
      type: "POST",
      data: { id: recordId, action: 'delete_record' },
      success: function(response) {
        // Handle success, e.g., show a success message or update UI
        //alert("Record deleted successfully!");
        if(response=='success')
        {
            closestTr.fadeOut();
            toastr.success("Record deleted successfully!");
        }
        else
        {
            toastr.error("Some error occured!"); 
        }
            
      },
      error: function(xhr, status, error) {
        // Handle error, e.g., show an error message
        toastr.error("Some error occured!!"); 
      }
    });
  });
  
    // Import Leads File
    
    // Show the overlay when AJAX request starts
    $(document).ajaxStart(function() {
        $('.overlay-wrapper').removeClass('d-none');
    });

    // Hide the overlay when AJAX request completes
    $(document).ajaxStop(function() {
        $('.overlay-wrapper').addClass('d-none');
    });
    
    $(document).on("submit", "#importLeads", function (e) {
        e.preventDefault();
        
        //HoldOn.open();
        //var band_id = $(this).find('input[name=band_id]').val();
        var url = 'import.php';
        var formData = new FormData(this);
        
        $.ajax({
            type:'POST',
            url: url,
            data: formData,
            cache:false,
            contentType: false,
            processData: false,
            success: (response) => {
                
                //HoldOn.close();
                if(response.error)
                {
                  toastr.error(response.msg);
                }
                else
                {
                  //$(document).find(".importRateResponse").html(response);
                  toastr.success(response.msg); 
                  location.reload();
                }
                
            },
            error: function(data){
                //HoldOn.close();
                toastr.error("Some error occured!");
            }
        });
    });
  
  
});
</script>


  </body>
</html>
