<?php
session_start();
require_once '../config.php';

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

function e($v){ return htmlspecialchars($v ?? '', ENT_QUOTES); }

$msg="";

/* GET LIMIT */

$res=$conn->query("SELECT * FROM stock_limits LIMIT 1");
$limit=null;

if($res->num_rows){
$limit=$res->fetch_assoc();
}

/* ADD LIMIT */

if(isset($_POST['set_limit'])){

if(!$limit){

$stock=(int)$_POST['stock_limit'];

$stmt=$conn->prepare("INSERT INTO stock_limits(stock_limit) VALUES(?)");
$stmt->bind_param("i",$stock);
$stmt->execute();
$stmt->close();

header("Location: stock-limit.php");
exit;

}else{
$msg="Delete existing limit first.";
}

}

/* DELETE */

if(isset($_GET['delete'])){

$conn->query("DELETE FROM stock_limits");

header("Location: stock-limit.php");
exit;

}

?>

<!doctype html>
<html>
<head>

<meta charset="utf-8">
<title>Stock Limit</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<style>

/* PAGE */

body{
font-family:Inter,Arial;
background:#eef2f7;
margin:0;
padding:90px 30px;
}

/* CONTAINER */

.container{
margin-left:260px;
max-width:900px;
}

/* CARD */

.card{
background:#fff;
border-radius:18px;
padding:28px;
box-shadow:0 10px 25px rgba(0,0,0,0.06);
margin-bottom:30px;
}

/* TITLE */

.title{
display:flex;
align-items:center;
gap:10px;
font-size:26px;
font-weight:700;
color:#0f172a;
margin-bottom:5px;
}

.title::before{
content:"";
width:6px;
height:24px;
background:#0f172a;
border-radius:4px;
display:inline-block;
}

.subtitle{
color:#64748b;
margin-bottom:20px;
font-size:14px;
}

/* INPUT */

.input{
width:100%;
padding:14px;
border-radius:12px;
border:1px solid #e2e8f0;
font-size:14px;
}

/* BUTTON */

.btn{
margin-top:14px;
background:#334155;
color:#fff;
border:none;
padding:10px 18px;
border-radius:25px;
cursor:pointer;
font-size:14px;
display:inline-flex;
align-items:center;
gap:6px;
}

.btn:hover{
background:#1e293b;
}

/* TABLE */

.table{
width:100%;
border-collapse:separate;
border-spacing:0 16px;
}

.table th{
background:#334155;
color:#fff;
padding:16px;
text-align:left;
font-size:14px;
}

.table th:first-child{
border-radius:12px 0 0 12px;
}

.table th:last-child{
border-radius:0 12px 12px 0;
text-align:center;
}

.table td{
background:#f8fafc;
padding:18px;
font-size:16px;
}

.limit-box{
font-weight:700;
font-size:18px;
}

/* DELETE BUTTON */

.btn-delete{
background:#e2e8f0;
border:none;
padding:10px 12px;
border-radius:10px;
cursor:pointer;
}

.btn-delete:hover{
background:#cbd5e1;
}

/* MOBILE */

@media(max-width:900px){

.container{
margin-left:0;
}

}

</style>

</head>

<body>

<?php include 'agencysidebar.php'; ?>
<?php include '../header.php'; ?>

<div class="container">

<!-- SET STOCK -->

<div class="card">

<div class="title">Set Stock</div>

<div class="subtitle">
Fill your required stock limit you want to set.
</div>

<?php if($msg) echo "<p style='color:red'>$msg</p>"; ?>

<form method="post" id="limitForm">

<label style="font-weight:600;margin-bottom:8px;display:block;">
Stock Limit
</label>

<input class="input"
type="number"
name="stock_limit"
value="<?php echo $limit['stock_limit'] ?? ''; ?>"
required
>

<button type="button" class="btn" onclick="checkLimit()">
<i class="fa fa-plus"></i> SET
</button>

<button type="submit" name="set_limit" id="realSubmit" style="display:none"></button>

</form>

</div>


<!-- TABLE -->

<div class="card">

<table class="table">

<thead>

<tr>
<th>Stock Limit</th>
<th>Action</th>
</tr>

</thead>

<tbody>

<?php if($limit): ?>

<tr>

<td class="limit-box">

<?php echo $limit['stock_limit']; ?>

</td>

<td style="text-align:center;">

<a href="?delete=1"
onclick="return confirm('Delete stock limit?')">

<button class="btn-delete">

<i class="fa fa-trash"></i>

</button>

</a>

</td>

</tr>

<?php else: ?>

<tr>
<td colspan="2">No stock limit set</td>
</tr>

<?php endif; ?>

</tbody>

</table>

</div>

</div>
<script>

function checkLimit(){

var limitExists = <?php echo $limit ? 'true' : 'false'; ?>;

if(limitExists){

alert("Stock limit already exists. Please delete it first.");

}else{

document.getElementById("realSubmit").click();

}

}

</script>
</body>
</html>