<?php

include_once("../include/main.inc.php");

check_session('MyCpanel','index.php');

################ Paging Starts From Here ##################

@extract($_REQUEST);

if($parentID=="") {
	$parentID=0;
}

$heading="Manage Enquiry";

$record_type="Enquiry";

$script_name="manage-enquiry.php";

$table_name="tbl_enquiry";

if (isset($_POST['delete'])) {
   if (!empty($_POST['en_id'])) {
       $catid = $_POST['en_id'];
       foreach ($catid as $id) {
           mysqli_query($dbconn, "DELETE FROM $table_name WHERE en_id = '$id'");
       }
       $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been deleted successfully</div>';
   } else {
       $_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
   }
   header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
   exit;
}

########################################################
admin_header("Manage $record_type");
$qry= db_query("select * from $table_name order by en_id desc");	
?>
<div class="content-wrapper">
          <div class="page-header">
            <h3 class="page-title mb-3">
			<?=$heading;?>
            </h3>
          </div>
          <div class="card">
            <div class="card-body">
              <div class="row">
                <div class="col-12">

              <form method="post" action="export_enquiries_excel.php" class="form-inline mb-3" onsubmit="return validateDateRange();">
                <div class="form-group mr-2">
                  <label for="from_date" class="mr-1">From:</label>
                  <input type="date" name="from_date" id="from_date" class="form-control form-control-sm">
                </div>
                <div class="form-group mr-2">
                  <label for="to_date" class="mr-1">To:</label>
                  <input type="date" name="to_date" id="to_date" class="form-control form-control-sm">
                </div>
                <button type="submit" name="export_excel" class="btn btn-success btn-sm">
                  <i class="fas fa-file-excel mr-1"></i> Download Excel
                </button>
              </form>

                  <form action="" method="post">
                    <div class="table-responsive">
                      <!-- <table id="order-listing" class="table table-bordered table-hover text-uppercase"> -->
                        <table id="order-listing" class="table table-bordered table-hover">
                        <thead>
                          <tr>
                            <th>
                              <div class="form-check mt-0 ">
                                <label class="form-check-label" style="white-space: nowrap;">
                                  <input type="checkbox" class="form-check-input" name="check_all" id="check_all" value="1" onclick="checkall(this.form)">Sno.<i class="input-helper"></i> 
                                </label>
                              </div>
                            </th>
                            <!-- <th>Sno.</th> -->
                            <th>Name</th>
                            <th>Contact</th>
                            <th>Enquiry For</th>
                            <th>Date</th>
                            <th>Enquiry Source</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php
                        $sl=$start+1;
                        while($arr=mysqli_fetch_array($qry)) {
                          if($arr['en_zcode']!=''){  
                            $mobileNumbers= '+'.$arr[en_zcode].' - '.$arr[en_mobile];
                           }else{ 
                            $mobileNumbers=$arr['en_mobile'];
                         }
                        ?>
                        <tr>
                          <td class="text-muted small mt-1" ><input type="checkbox" name="en_id[]" value="<?= $arr['en_id'] ?>"> <?= $sl; ?></td>
                          <!-- <td><?= $i++ +1; ?></td> -->
                          
                          <td>
                            <p class="ordern"><?= $arr['en_name'] ?></p>
                            <?= empty($arr['en_org_name'])?'':'(Company: '.$arr['en_org_name'].')'; ?>
                          </td>
                          <td style="white-space: nowrap; line-height: 1.2;">
                            <p class="m-0"><i class="fas fa-phone-volume text-primary me-1"></i>  <a href="tel:<?=$mobileNumbers; ?>" class="href"><?=$mobileNumbers; ?></a></p>

                            <?php if ($arr['en_country_name'] != '') { ?>
                            <p class="m-0"><i class="fas fa-globe-asia text-info me-1" style="color: black;"></i> <?= $arr['en_country_name']?></p>
                            <?php
               } else { ?>
            <?php
               } ?>
                            <?php if ($arr['en_email'] != '') { ?>
                            <p class="m-0"><i class="fas fa-envelope me-1" style="color: black;"></i> <a href="mailto:<?= $arr['en_email']?>" class="href"><?= $arr['en_email']?></a></p>
                            <?php
               } else { ?>
            <?php
               } ?>



                            <?php if ($arr['en_country'] != '') { ?>
                            <p class="m-0"><i class="fas fa-map-marker-alt text-danger me-1"></i> <?= $arr['en_country']?></p>
                            <?php
               } else { ?>
            <?php
               } ?>


<?php if ($arr['en_quantity'] != '') { ?>
    <p class="m-0">
        <i class="fa fa-cubes"></i> <strong>Quantity:</strong> <?= $arr['en_quantity'] ?>
    </p>
<!-- <?php 
//} else { ?>
    <p class="m-0 text-muted">
        <i class="fa fa-box-open"></i> <strong>Quantity:</strong> Not Available
    </p> -->
<?php } ?>
                          </td>
                         
                          </td>
                          <td  style="white-space: normal;">
                            <p class="productid-dcs"><?= $arr['enquiry_for']?></p>
                            <p class="productid-dcs"><i class="fa fa-comments"></i>: <?= $arr['en_comment']?></p>
                          </td>
                           <td>
    <div style="white-space: nowrap;"><i class="fa fa-calendar-alt mr-1" ></i><?= date('d M Y', strtotime($arr['en_date'])) ?></div>
    <div class="text-muted"><i class="fa fa-clock mr-1"></i><?= date('H:i a', strtotime($arr['en_date'])) ?></div>
  </td>
                          <td>
                            <p class="productid-dcs"><b>Url:</b> <?= $arr['en_url'] ?></p>
                            <p class="productid-dcs"><b>IP:</b> <?= $arr['en_ip'] ?></p>
                            <?php if ($arr['system_detail'] != '') { ?>
                            <p class="productid-dcs"><b>System:</b> <?= $arr['system_detail'] ?></p>
                            <?php
               } else { ?>
            <?php
               } ?>
                          </td>
						</tr>

 <?php  $sl++; }?>
                       
                      </tbody>
                    </table>
                    <div class="btn-grp">
						<button type="submit" name="delete" onclick="return select_chk()" class="btn btn-danger btn-icon-text btn-sm">
							<i class="fas fa-trash-alt mr-1"></i>Delete
                        </button>
                        </div>
                  </div>
                                 </form>
                </div>
              </div>
            </div>
          </div>
        </div>
        <script language="javascript">

checked=false;
function checkall(frm1){
	var aa= frm1;
	if(checked == false){
		checked = true
	}
	else{
		checked = false
	}
	for (var i =0; i < aa.elements.length; i++){
		aa.elements[i].checked = checked;
	}
} 
function select_chk(){
var chks = document.getElementsByName('en_id[]');
var hasChecked = false;
for (var i = 0; i < chks.length; i++){
if (chks[i].checked){
  hasChecked = true;
  break;
  }
}
if (hasChecked == false){
alert("Please Select At Least One.");
return false;
}
}
</script>



<script>
  function validateDateRange() {
    const from = document.getElementById("from_date").value;
    const to = document.getElementById("to_date").value;

    if (from && !to) {
      alert("select both 'From' and 'To' dates to continue.");
      return false;
    }

    return true;
  }
</script>
                  <?php
                  admin_footer();
                  exit;
                  ?>