<?php

error_reporting(E_ALL);
ini_set('display_errors',1);

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

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

/*
====================================
SAVE ASSIGNMENT
====================================
*/

if($_SERVER['REQUEST_METHOD']=='POST'){

    $assetId = intval($_POST['asset_id']);
    $employeeId = intval($_POST['employee_id']);

    $assignedBy = intval($_SESSION['user_id']);

    $stmt = $conn->prepare("
        INSERT INTO asset_assignments
        (
            asset_id,
            employee_id,
            assigned_by,
            assigned_date,
            status
        )
        VALUES
        (
            ?, ?, ?, CURDATE(), 'Assigned'
        )
    ");

    $stmt->bind_param(
        "iii",
        $assetId,
        $employeeId,
        $assignedBy
    );

    $stmt->execute();

    /*
    ================================
    UPDATE ASSET STATUS
    ================================
    */

    $update = $conn->prepare("
        UPDATE assets_master
        SET asset_status='Assigned'
        WHERE id=?
    ");

    $update->bind_param(
        "i",
        $assetId
    );

    $update->execute();

    header(
        "Location: assign_asset.php?success=1"
    );

    exit;
}

/*
====================================
AVAILABLE ASSETS
====================================
*/

$assets = $conn->query("
SELECT *
FROM assets_master
WHERE asset_status='Available'
ORDER BY asset_name
");

/*
====================================
EMPLOYEES
====================================
*/

$employees = $conn->query("
SELECT
e.id,
e.name,
d.name department

FROM employees e

LEFT JOIN departments d
ON d.id=e.department_id

ORDER BY e.name
");
?>

<!DOCTYPE html>
<html>
<head>

<title>Assign 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">

    <!-- PAGE HEADER -->

    <div class="flex justify-between items-center mb-8">

        <div>

            <h1 class="text-3xl font-bold text-gray-800">
                Assign Asset
            </h1>

            <p class="text-gray-500 mt-1">
                Assign laptops, phones, routers, keys, radios and other company assets.
            </p>

        </div>

        <a
        href="assets.php"
        class="bg-gray-600 text-white px-5 py-3 rounded-lg">

            Back To Assets

        </a>

    </div>

    <!-- SUCCESS -->

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

        <div class="bg-green-100 border border-green-300 text-green-700 p-4 rounded-xl mb-6">

            Asset assigned successfully.

        </div>

    <?php endif; ?>

    <!-- FORM -->

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

        <form method="POST">

            <!-- ASSET -->

            <div class="mb-6">

                <label class="block font-medium text-gray-700 mb-2">

                    Select Asset

                </label>

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

                    <option value="">

                        Choose Asset

                    </option>

                    <?php while($asset=$assets->fetch_assoc()): ?>

                        <option value="<?= $asset['id'] ?>">

                            <?= htmlspecialchars(
                                $asset['asset_name']
                            ) ?>

                            -
                            <?= htmlspecialchars(
                                $asset['asset_type']
                            ) ?>

                            <?php if(!empty($asset['serial_number'])): ?>

                                (
                                <?= htmlspecialchars(
                                    $asset['serial_number']
                                ) ?>
                                )

                            <?php endif; ?>

                        </option>

                    <?php endwhile; ?>

                </select>

            </div>

            <!-- EMPLOYEE -->

            <div class="mb-6">

                <label class="block font-medium text-gray-700 mb-2">

                    Assign To Employee

                </label>

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

                    <option value="">

                        Choose Employee

                    </option>

                    <?php while($emp=$employees->fetch_assoc()): ?>

                        <option value="<?= $emp['id'] ?>">

                            <?= htmlspecialchars(
                                $emp['name']
                            ) ?>

                            -

                            <?= htmlspecialchars(
                                $emp['department']
                            ) ?>

                        </option>

                    <?php endwhile; ?>

                </select>

            </div>

            <!-- BUTTON -->

            <button
            type="submit"
            class="bg-blue-600 hover:bg-blue-700 text-white px-8 py-3 rounded-xl font-semibold">

                Assign Asset

            </button>

        </form>

    </div>

</div>

</body>
</html>