<?php
session_start();
include_once "../config/database.php";

if(!isset($_SESSION['user_id'])){
    header("Location: /beauty_pos/login.php");
    exit;
}

$search=isset($_GET['search']) ? mysqli_real_escape_string($conn,$_GET['search']) : "";

$limit=10;

$page=isset($_GET['page'])?(int)$_GET['page']:1;

if($page<1)$page=1;

$offset=($page-1)*$limit;

$where="";

if($search!=""){
$where=" WHERE currency_name LIKE '%$search%' OR currency_code LIKE '%$search%'";
}

$total=mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) total FROM currencies $where"));

$totalPages=ceil($total['total']/$limit);

$query=mysqli_query($conn,"
SELECT *
FROM currencies
$where
ORDER BY is_default DESC,id DESC
LIMIT $offset,$limit
");

include_once "../includes/header.php";
include_once "../includes/sidebar.php";
?>

<div class="overlay" id="overlay"></div>

<div class="main-content">

<?php include_once "../includes/topbar.php"; ?>

<div class="container-fluid">

<div class="d-flex justify-content-between align-items-center mb-3">

<h4>Currencies</h4>

<a href="/beauty_pos/currencies/add.php" class="btn btn-primary">
<i class="fa fa-plus"></i> Add Currency
</a>

</div>

<form method="GET" class="mb-3">

<div class="row g-2">

<div class="col-md-10">
<input
type="text"
name="search"
class="form-control"
placeholder="Search..."
value="<?php echo $search; ?>">
</div>

<div class="col-md-2">
<button class="btn btn-dark w-100">Search</button>
</div>

</div>

</form>

<div class="card-box table-box rounded-4 shadow">

<div class="table-responsive">

<table class="table table-hover">

<thead class="table-dark">

<tr>

<th>#</th>
<th>Name</th>
<th>Code</th>
<th>Symbol</th>
<th>Default</th>
<th>Status</th>
<th>Action</th>

</tr>

</thead>

<tbody>

<?php

$i=$offset+1;

while($row=mysqli_fetch_assoc($query)){

?>

<tr>

<td><?php echo $i++; ?></td>

<td><?php echo $row['currency_name']; ?></td>

<td><?php echo $row['currency_code']; ?></td>

<td><?php echo $row['currency_symbol']; ?></td>

<td>

<?php

echo $row['is_default']
?'<span class="badge bg-success">Yes</span>'
:'<span class="badge bg-secondary">No</span>';

?>

</td>

<td>

<?php

echo $row['status']
?'<span class="badge bg-success">Active</span>'
:'<span class="badge bg-danger">Inactive</span>';

?>

</td>

<td>

<a href="/beauty_pos/currencies/edit.php?id=<?php echo $row['id']; ?>" class="btn btn-warning btn-sm">
<i class="fa fa-edit"></i>
</a>

<a href="/beauty_pos/currencies/delete.php?id=<?php echo $row['id']; ?>" class="btn btn-danger btn-sm" onclick="return confirm('Delete Currency?')">
<i class="fa fa-trash"></i>
</a>

</td>

</tr>

<?php } ?>

</tbody>

</table>

</div>

</div>

</div>

<?php include_once "../includes/footer.php"; ?>

</div>