<?php
session_start();
if (!isset($_SESSION['admin_id']) && $_SESSION['role'] != 'admin') {
    header("Location: ../login.php");
    exit();
}
include '../../include/config.php';

// Handle add/edit/delete
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['add_equipment'])) {
        $name = $_POST['name'];
        $type = $_POST['type'];
        $description = $_POST['description'];
        $total_shares = intval($_POST['total_shares']);
        $share_price = floatval($_POST['share_price']);
        $expected_roi = floatval($_POST['expected_roi']);
        $period = intval($_POST['period']);
        
        $conn->query("
            INSERT INTO hps_invest_equipment (equipment_name, equipment_type, description, total_shares, available_shares, share_price, expected_roi, investment_period_months, status) 
            VALUES ('$name', '$type', '$description', '$total_shares', '$total_shares', '$share_price', '$expected_roi', '$period', 'active')
        ");
        $success = "Equipment added!";
    }
    
    if (isset($_POST['delete_equipment'])) {
        $id = intval($_POST['id']);
        $conn->query("DELETE FROM hps_invest_equipment WHERE id = '$id'");
        $success = "Equipment deleted!";
    }
}

$equipment = $conn->query("SELECT * FROM hps_invest_equipment ORDER BY created_at DESC");

$page_title = 'Equipment Management';
include '../../include/header.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Equipment Management - HPS</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-4">
    <div class="d-flex justify-content-between mb-4">
        <h2><i class="fas fa-microscope me-2"></i> Medical Equipment for Investment</h2>
        <div>
            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addModal"><i class="fas fa-plus"></i> Add Equipment</button>
            <a href="dashboard.php" class="btn btn-secondary">← Back</a>
        </div>
    </div>

    <?php if(isset($success)): ?>
        <div class="alert alert-success"><?= $success ?></div>
    <?php endif; ?>

    <div class="card shadow">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered">
                    <thead class="table-light">
                        <tr><th>ID</th><th>Name</th><th>Type</th><th>Total Shares</th><th>Available</th><th>Price/Share</th><th>ROI</th><th>Period</th><th>Status</th><th>Action</th></tr>
                    </thead>
                    <tbody>
                        <?php while($eq = $equipment->fetch_assoc()): ?>
                        <tr>
                            <td><?= $eq['id'] ?></td>
                            <td><?= htmlspecialchars($eq['equipment_name']) ?></td>
                            <td><?= ucfirst($eq['equipment_type']) ?></td>
                            <td><?= number_format($eq['total_units']) ?></td>
                            <td><?= number_format($eq['available_units']) ?></td>
                            <td>₦<?= number_format($eq['unit_price'], 2) ?></td>
                            <td><span class="text-success"><?= $eq['expected_roi'] ?>%</span></td>
                            <td><?= $eq['lock_period_months'] ?> months</td
                            <td><span class="badge bg-<?= $eq['status'] == 'active' ? 'success' : 'secondary' ?>"><?= ucfirst($eq['status']) ?></span></td
                            <td>
                                <form method="POST" class="d-inline" onsubmit="return confirm('Delete?')">
                                    <input type="hidden" name="id" value="<?= $eq['id'] ?>">
                                    <button type="submit" name="delete_equipment" class="btn btn-sm btn-danger"><i class="fas fa-trash"></i></button>
                                </form>
                             </td
                          </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Add Modal -->
<div class="modal fade" id="addModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5>Add Medical Equipment</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <div class="modal-body">
                    <input type="text" name="name" class="form-control mb-2" placeholder="Equipment Name" required>
                    <select name="type" class="form-select mb-2" required>
                        <option value="diagnostic">Diagnostic</option>
                        <option value="therapeutic">Therapeutic</option>
                        <option value="monitoring">Monitoring</option>
                        <option value="surgical">Surgical</option>
                    </select>
                    <textarea name="description" class="form-control mb-2" placeholder="Description" rows="2"></textarea>
                    <input type="number" name="total_shares" class="form-control mb-2" placeholder="Total Shares" required>
                    <input type="number" name="share_price" class="form-control mb-2" placeholder="Price per Share (₦)" required>
                    <input type="number" name="expected_roi" class="form-control mb-2" placeholder="Expected ROI (%)" required>
                    <input type="number" name="period" class="form-control mb-2" placeholder="Investment Period (months)" required>
                </div>
                <div class="modal-footer">
                    <button type="submit" name="add_equipment" class="btn btn-primary">Add Equipment</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<?php include '../../include/footer.php'; ?>