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

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=enquiries_" . date("Y-m-d") . ".xls");
header("Pragma: no-cache");
header("Expires: 0");


$cond = "WHERE 1=1";


if (!empty($_POST['keyword'])) {
    $keyword = mysqli_real_escape_string($dbconn, $_POST['keyword']);
    $cond .= " AND (en_country LIKE '%$keyword%' OR en_name LIKE '%$keyword%')";
}


if (!empty($_POST['from_date']) && !empty($_POST['to_date'])) {
    $from = mysqli_real_escape_string($dbconn, $_POST['from_date']);
    $to = mysqli_real_escape_string($dbconn, $_POST['to_date']);
    $cond .= " AND DATE(en_date) BETWEEN '$from' AND '$to'";
} elseif (!empty($_POST['from_date'])) {
    $from = mysqli_real_escape_string($dbconn, $_POST['from_date']);
    $cond .= " AND DATE(en_date) >= '$from'";
} elseif (!empty($_POST['to_date'])) {
    $to = mysqli_real_escape_string($dbconn, $_POST['to_date']);
    $cond .= " AND DATE(en_date) <= '$to'";
}


$qry = mysqli_query($dbconn, "SELECT * FROM tbl_enquiry $cond ORDER BY en_id DESC");

echo "<table border='1'>";
echo "<tr>
        <th>S.No.</th>
        <th>Name</th>
        <th>Email</th>
        <th>Country Code</th>
        <th>Mobile</th>
        <th>Country</th>
        <th>Quantity</th>
        <th>Enquiry Source</th>
        <th>Message</th>
        <th>Receive Date</th>
      </tr>";

$sn = 1;
while ($row = mysqli_fetch_assoc($qry)) {


   echo "<tr>";
echo "<td>".$sn++."</td>";
echo "<td>".$row['en_name']."</td>";
echo "<td>".$row['en_email']."</td>";
echo "<td>".$row['en_zcode'] ."</td>";
echo "<td>". $row['en_mobile']."</td>";


// if (!empty($row['en_zcode'])) {
//     $mobile = $row['en_zcode'] . ' - ' . $row['en_mobile'];
//     echo "<td>" . $mobile . "</td>";
// } else {
//     echo "<td>" . $row['en_mobile'] . "</td>";
// }


if (!empty($row['en_country_name'])) {
    echo "<td>".$row['en_country_name']."</td>";
} else {
    echo "<td>".$row['en_country']."</td>";
}

echo "<td>".$row['en_quantity']."</td>";
echo "<td>".$row['en_url']."</td>";
echo "<td>".$row['en_comment']."</td>";
echo "<td>".$row['en_date']."</td>";
echo "</tr>";
}
echo "</table>";
exit;
?>
