<?php
include "db.php";

// Currency ke hisab se unit price format karne ka helper
// PKR/Rs = 2 decimals, OMR = 3 decimals
function format_unit_price($row) {
    $currency = (isset($row['currency']) && strtoupper($row['currency']) === 'OMR') ? 'OMR' : 'PKR';
    if ($currency === 'OMR') {
        return 'OMR ' . number_format((float)$row['unit_price'], 3);
    }
    return 'Rs. ' . number_format((float)$row['unit_price'], 2);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap" rel="stylesheet">
<style>
body{
    background:#F5F5F5;
    font-family:'Poppins',sans-serif;
    margin:0;
}
/* Bigger Layout */
.page-wrapper{
    width:85%;
    margin:30px auto;
}
.page-card{
    background:#fff;
    border-radius:8px;
    box-shadow:0 4px 18px rgba(0,0,0,0.08);
    padding:24px;
}
/* Header */
.page-header{
    background:#F5F5F5;
    padding:16px 20px;
    border-bottom:3px solid #F37021;
    margin:-24px -24px 24px -24px;
}
.page-header h2{
    margin:0;
    font-size:18px;
    color:#0C1F3A;
}
/* Table */
table{
    width:100%;
    border-collapse:collapse;
    font-size:14px;
}
thead{
    background:#0C1F3A;
    color:#fff;
}
th, td{
    padding:12px;
    text-align:left;
}
td{
    border-bottom:1px solid #E0E0E0;
}
tbody tr:hover{
    background:#F9F9F9;
}
/* Price Highlight */
.price{
    font-weight:600;
    color:#0C1F3A;
}
/* Buttons Same Line */
.actions{
    display:flex;
    gap:6px;
    flex-wrap:nowrap;
}
.action-btn{
    padding:6px 12px;
    border-radius:4px;
    font-size:12px;
    color:#fff;
    text-decoration:none;
    white-space:nowrap;
}
.edit-btn{ background:#0C1F3A; }
.delete-btn{ background:#F37021; }
.no-data{
    text-align:center;
    padding:20px;
    color:#777;
}
/* Stock Summary Section */
.summary-card{
    background:#fff;
    border-radius:8px;
    box-shadow:0 4px 18px rgba(0,0,0,0.08);
    padding:24px;
    margin-bottom:24px;
}
.summary-header{
    background:#F5F5F5;
    padding:16px 20px;
    border-bottom:3px solid #0C1F3A;
    margin:-24px -24px 24px -24px;
}
.summary-header h2{
    margin:0;
    font-size:18px;
    color:#0C1F3A;
}
.stock-qty{
    font-weight:700;
    color:#F37021;
    font-size:15px;
}
.stock-low{
    color:#E53E3E;
}
.summary-header{
    display:flex;
    justify-content:space-between;
    align-items:center;
}
.reset-stock-btn{
    background:#E53E3E;
    color:#fff;
    border:none;
    padding:8px 16px;
    border-radius:6px;
    font-size:12px;
    font-weight:600;
    cursor:pointer;
    font-family:'Poppins',sans-serif;
}
.reset-stock-btn:hover{
    background:#C53030;
}
/* Responsive */
@media(max-width:768px){
    .page-wrapper{
        width:95%;
    }
}
</style>
</head>
<body>
<div class="page-wrapper">

<div class="summary-card">
<div class="summary-header">
    <h2>📊 Current Stock Summary</h2>
    <button type="button" class="reset-stock-btn" onclick="startResetFlow()">🗑️ Reset All Stock</button>
</div>
<table>
<thead>
<tr>
    <th>Product ID</th>
    <th>Product Name</th>
    <th>Total Stock Available</th>
</tr>
</thead>
<tbody>
<?php
$stock_result = $conn->query("SELECT product_id, product_name, stock_quantity FROM products ORDER BY product_name ASC");
if($stock_result && $stock_result->num_rows > 0){
    while($p = $stock_result->fetch_assoc()){
        $qty_class = ($p['stock_quantity'] <= 0) ? 'stock-qty stock-low' : 'stock-qty';
        echo "<tr>
            <td>{$p['product_id']}</td>
            <td>{$p['product_name']}</td>
            <td class='{$qty_class}'>{$p['stock_quantity']} units</td>
        </tr>";
    }
}else{
    echo "<tr>
            <td colspan='3' class='no-data'>
                No Products Found
            </td>
          </tr>";
}
?>
</tbody>
</table>
</div>

<div class="page-card">
<div class="page-header">
    <h2>📦 Inventory Items</h2>
</div>
<table>
<thead>
<tr>
    <th>ID</th>
    <th>Name</th>
    <th>Quantity</th>
    <th>Unit Price</th>
    <th>Date</th>
    <th>Time</th>
    <th>Description</th>
    <th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$result = $conn->query("SELECT * FROM inventory ORDER BY id DESC");
if($result->num_rows > 0){
    while($row = $result->fetch_assoc()){
        $unit_price_display = format_unit_price($row);

        // Date aur Time ko readable format mein show kar rahe hain
        $date_display = !empty($row['date']) ? date('d-M-Y', strtotime($row['date'])) : '-';
        $time_display = !empty($row['time']) ? date('h:i A', strtotime($row['time'])) : '-';

        echo "<tr>
            <td>{$row['id']}</td>
            <td>{$row['name']}</td>
            <td>{$row['quantity']}</td>
            <td class='price'>{$unit_price_display}</td>
            <td>{$date_display}</td>
            <td>{$time_display}</td>
            <td>{$row['description']}</td>
            <td>
                <div class='actions'>
                    <a href='inventory_edit.php?id={$row['id']}'
                       class='action-btn edit-btn'>Edit</a>
                    <a href='inventory_delete.php?id={$row['id']}'
                       class='action-btn delete-btn'
                       onclick='return confirm(\"Are you sure?\")'>Delete</a>
                </div>
            </td>
        </tr>";
    }
}else{
    echo "<tr>
            <td colspan='8' class='no-data'>
                No Inventory Items Found
            </td>
          </tr>";
}
?>
</tbody>
</table>
</div>
</div>

<!-- Hidden form used to securely POST the reset request to reset_stock.php -->
<form id="resetStockForm" action="reset_stock.php" method="POST" style="display:none;">
    <input type="hidden" name="do_reset" value="1">
    <input type="hidden" name="password" id="resetPasswordField">
</form>

<script>
function startResetFlow() {
    var pwd = prompt("Ye action tamam products ka stock 0 kar dega.\nJaari rakhne ke liye admin password likhein:");

    // User ne cancel dabaya
    if (pwd === null) {
        return;
    }

    if (pwd.trim() === "") {
        alert("Password khali nahi ho sakta.");
        return;
    }

    var sure = confirm("Kya aap wakai sabhi products ka Total Stock 0 karna chahte hain?\nYe action wapas nahi ho sakta.");
    if (!sure) {
        return;
    }

    document.getElementById('resetPasswordField').value = pwd;
    document.getElementById('resetStockForm').submit();
}
</script>

</body>
</html>