<?php
session_start();
require("php_con.php");
require("check_user.php");
require("function.php");

if(isset($_GET['remove'])){
    $cs_id = mysqli_real_escape_string($connation, $_GET['remove']);
    
    $sale_check = mysqli_query($connation, "SELECT * FROM sale_details sd INNER JOIN customer_details cd ON sd.sale_details_customer=cd.cs_mobile WHERE cd.cs_id='$cs_id'");
    
    if(!mysqli_num_rows($sale_check) > 0){    
        // Delete customer photo if exists
        $customer_query = mysqli_query($connation, "SELECT cs_photo FROM customer_details WHERE cs_id='$cs_id'");
        $customer = mysqli_fetch_assoc($customer_query);
        if (!empty($customer['cs_photo'])) {
            $photo_path = "customer_photos/" . $customer['cs_photo'];
            if (file_exists($photo_path)) {
                unlink($photo_path);
            }
        }
        
        mysqli_query($connation, "DELETE FROM customer_details WHERE cs_id='$cs_id'");
        echo "<script>window.location='customer';</script>";
    } else {
        echo "<script>window.location='customer?error';</script>";
    }
}

if(isset($_POST['search_text'])){
    $_SESSION['search_text'] = mysqli_real_escape_string($connation, $_POST['search_text']);
    echo "<script>window.location='customer';</script>";
}

if(isset($_GET['clear'])){
    unset($_SESSION['search_text']);
    unset($_SESSION['page']);
    echo "<script>window.location='customer';</script>";
}

if(isset($_GET['page'])){
    $page = $_GET['page'];
} else {
    $page = 1;
}
$per_page = 20;
$start_page = ($page-1) * $per_page;
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title><?php echo $system_name; ?></title>
<link rel="stylesheet" href="main_css.css">
    
<script src="data_table/js/jquery-3.5.1.js"></script>
<script src="data_table/js/jquery.dataTables.min.js"></script>
<script src="data_table/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">
    
<link rel="stylesheet" href="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
    
<link rel="stylesheet" href="alert/sweetalert2.min.css">
<script src="alert/sweetalert2.min.js"></script>
</head>
<style>
    .table tr td{
        font-size: 14px;
        padding: 5px;
    }
    .table tr th{
        font-size: 14px;
        padding: 5px;
    }
    .customer-photo {
        width: 50px;
        height: 50px;
        object-fit: cover;
        border-radius: 50%;
    }
</style>
<body>
<script>
window.addEventListener("load", function(){
    var load_screen = document.getElementById('load_screen');
    document.body.removeChild(load_screen);
});
</script>

<div id="load_screen"><div id="loading"><img src="image/loading.gif" width="80px;">Please Wait...</div></div>
    
<?php require("header.php"); ?>
    
<div class="contaner">

    
<strong>Customer Details</strong>
    
<?php require("sub_menu/customer.php"); ?>
    
    <div class="row">
        
<form method="post" class="mb-2 w-100">
    <input name="search_text" type="text" autofocus="autofocus" class="form-control form-control-sm" id="search_text" placeholder="Search Customer" list="product_list">
</form>
        
<div class="table-responsive">
<table class="table list-table" style="width:100%; font-size: 14px; text-transform: capitalize;">
        <thead>
            <tr>
                <th>No.</th>
                <th>Photo</th>
                <th>&nbsp;</th>
                <th>PIN</th>
                <th>Name</th>
                <th>Mobile</th>
                <th>NIC</th>
                <th>Total Bill</th>
                <th>Balance</th>
                <th>Reg. Time</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $count_no = 0;
            if(isset($_SESSION['search_text'])){
                $cs_mobile = (int)$_SESSION['search_text'];
                $qury_str = "SELECT *
                FROM customer_details
                WHERE cs_name LIKE '%$_SESSION[search_text]%' OR cs_mobile='$cs_mobile' OR cs_nic='$_SESSION[search_text]' OR cs_address LIKE '%$_SESSION[search_text]%'
                ORDER BY cs_id DESC";
            } else {
                $qury_str = "SELECT * FROM customer_details ORDER BY cs_id DESC";
            }
            $list_qury = mysqli_query($connation, "$qury_str LIMIT $start_page,$per_page");
            while($list_resalt = mysqli_fetch_assoc($list_qury)){
                $count_no++;
            ?>
            <tr>
                <td><?php echo number_format($count_no,0); ?></td>
                <td>
                    <?php if(!empty($list_resalt['cs_photo'])): ?>
                        <img src="customer_photos/<?php echo $list_resalt['cs_photo']; ?>" alt="Customer Photo" class="customer-photo">
                    <?php else: ?>
                        <img src="image/default_avatar.png" alt="Default Avatar" class="customer-photo">
                    <?php endif; ?>
                </td>
                <td style="white-space: nowrap; text-align: center">
                <a href="edit-customer?cs_id=<?php echo $list_resalt['cs_id']; ?>" class="badge badge-primary" title="Edit Product"><i class="fa fa-edit"></i></a>
                <a href="customer_summery?sale_details_customer=<?php echo $list_resalt['cs_mobile']; ?>" class="badge badge-dark" title="View Bill" target="_blank"><i class="fa fa-tasks"></i></a>
                <a href="customer?remove=<?php echo $list_resalt['cs_id']; ?>" class="badge badge-danger" title="Remove Customer" onClick="JavaScript:return confirm('Are you sure you want to remove this customer?');"><i class="fa fa-trash"></i></a>
                </td>
                <td><?php echo $list_resalt['cs_pin']; ?></td>
                <td><?php echo $list_resalt['cs_name']; ?></td>
                <td><?php echo '0'.(int)$list_resalt['cs_mobile']; ?></td>
                <td><?php echo $list_resalt['cs_nic']; ?></td>
                <td align="center"><?php echo number_format((float)cus_bill($list_resalt['cs_mobile'])); ?></td>
                <td align="center">
                    <?php 
                    if(cus_balance($list_resalt['cs_mobile']) < 0){
                    ?>
                    <span class="badge badge-danger"><?php echo number_format((float)cus_balance($list_resalt['cs_mobile']),2); ?></span>
                    <?php } else { ?>
                    <span class="badge badge-success"><?php echo number_format((float)cus_balance($list_resalt['cs_mobile']),2); ?></span>
                    <?php } ?>
                
                </td>
                <td><?php echo date('Y-m-d h:i:s A',strtotime($list_resalt['cs_add_date'].' '.$list_resalt['cs_add_time'])) ?></td>
          </tr>
            <?php } ?>
        </tbody>
    </table>
    
<?php 
$total_record_qury = mysqli_query($connation, "$qury_str");
$total_record = mysqli_num_rows($total_record_qury);
$page_count = ceil($total_record/$per_page);
?>
<div class="text-right">
<span class="mr-2"><?php echo "Showing ".number_format((float)($start_page+1))."/".number_format((float)($start_page+$per_page))." page of ".number_format((float)$total_record)." entries"; ?></span>
<a href="customer?page=1" class="btn btn-sm btn-dark" <?php if($page<=1){echo "hidden='yes'";} ?>>First</a>
<a href="customer?page=<?php echo $page-1; ?>" class="btn btn-sm btn-dark" <?php if($page<=1){echo "hidden='yes'";} ?>>Previous</a>
<a href="customer?page=<?php echo $page+1; ?>" class="btn btn-sm btn-dark" <?php if($page>=$page_count){echo "hidden='yes'";} ?>>Next</a>
<a href="customer?page=<?php echo $page_count; ?>" class="btn btn-sm btn-dark" <?php if($page>=$page_count){echo "hidden='yes'";} ?>>Last</a>    
</div>
    
</div>

</div>
</div>    
    
</body>
</html>
<?php @mysqli_close($connation); ?>