<?php

require 'config.php';
session_start();
session_write_close();

if(
    !in_array(
        strtolower($_SESSION['role'] ?? ''),
        ['superadmin','admin','hr']
    )
){
    die("Access Denied");
}

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

    $asset_name = trim($_POST['asset_name']);
    $asset_tag = trim($_POST['asset_tag']);
    $category = trim($_POST['category']);
    $serial_number = trim($_POST['serial_number']);

    $stmt = $conn->prepare("
        INSERT INTO assets
        (
            asset_name,
            asset_tag,
            category,
            serial_number,
            status,
            created_at
        )
        VALUES
        (
            ?,?,?,?,
            'Available',
            NOW()
        )
    ");

    $stmt->bind_param(
        "ssss",
        $asset_name,
        $asset_tag,
        $category,
        $serial_number
    );

    $stmt->execute();

    header("Location:add_asset.php?saved=1");
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>

<title>Add Asset</title>

<script src="https://cdn.tailwindcss.com"></script>

</head>

<body class="flex bg-gray-100 min-h-screen">

<?php include 'sidebar.php'; ?>

<div class="flex-1 p-8" id="main-content">

<h1 class="text-3xl font-bold mb-6">
Add New Asset
</h1>

<?php if(isset($_GET['saved'])): ?>

<div class="bg-green-100 border border-green-300 text-green-700 p-4 rounded-xl mb-5">
Asset added successfully.
</div>

<?php endif; ?>

<div class="bg-white p-6 rounded-2xl shadow max-w-3xl">

<form method="POST">

<div class="grid md:grid-cols-2 gap-5">

<div>

<label class="block mb-2 font-medium">
Asset Name
</label>

<input
type="text"
name="asset_name"
required
class="w-full border p-3 rounded-lg">

</div>

<div>

<label class="block mb-2 font-medium">
Asset Tag
</label>

<input
type="text"
name="asset_tag"
required
class="w-full border p-3 rounded-lg">

</div>

<div>

<label class="block mb-2 font-medium">
Category
</label>

<select
name="category"
required
class="w-full border p-3 rounded-lg">

<option value="">Select</option>

<option>Laptop</option>
<option>Desktop</option>
<option>Printer</option>
<option>Phone</option>
<option>Router</option>
<option>Access Card</option>
<option>Radio</option>
<option>POS Terminal</option>
<option>Key</option>
<option>Television</option>
<option>Other</option>

</select>

</div>

<div>

<label class="block mb-2 font-medium">
Serial Number
</label>

<input
type="text"
name="serial_number"
class="w-full border p-3 rounded-lg">

</div>

</div>

<button
type="submit"
name="save"
class="mt-6 bg-blue-600 text-white px-8 py-3 rounded-xl">

Save Asset

</button>

</form>

</div>

</div>

</body>
</html>