<?php
require_once __DIR__ . '/../../includes/auth.php';
requireLogin();

require_once __DIR__ . '/../../config/database.php';

if (!isset($_GET['id'])) {
    die("Meter ID missing.");
}

$meterId = (int)$_GET['id'];

/* ===============================
   LOAD METER
================================= */
$stmt = $pdo->prepare("SELECT * FROM meters WHERE id = ?");
$stmt->execute([$meterId]);
$meter = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$meter) {
    die("Meter not found.");
}

/* ===============================
   HANDLE INSTALL
================================= */
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $companyId = (int)$_POST['company_id'];
    $tariffId  = (int)$_POST['tariff_id'];

    $stmt = $pdo->prepare("
        UPDATE meters
        SET company_id = ?,
            tariff_id = ?,
            current_holder_company_id = ?,
            status = 'INSTALLED',
            installed_at = CURDATE()
        WHERE id = ?
    ");

    $stmt->execute([
        $companyId,
        $tariffId,
        $companyId,
        $meterId
    ]);

    header("Location: inventory.php");
    exit;
}

/* ===============================
   LOAD COMPANIES
================================= */
$companies = $pdo->query("SELECT id, company_name FROM companies ORDER BY company_name ASC")->fetchAll(PDO::FETCH_ASSOC);

/* ===============================
   LOAD TARIFFS
================================= */
$tariffs = $pdo->query("SELECT id, name FROM tariffs ORDER BY name ASC")->fetchAll(PDO::FETCH_ASSOC);

?>

<?php require_once __DIR__ . '/../../includes/header.php'; ?>
<?php require_once __DIR__ . '/../../includes/sidebar.php'; ?>

<div class="main-content">

    <div class="page-header">
        <h2>Install Meter: <?= htmlspecialchars($meter['meter_number']) ?></h2>
    </div>

    <div class="stat-card">

        <form method="POST">

            <div style="margin-bottom:15px;">
                <label>Company</label>
                <select name="company_id" required>
                    <option value="">Select Company</option>
                    <?php foreach ($companies as $company): ?>
                        <option value="<?= $company['id'] ?>">
                            <?= htmlspecialchars($company['company_name']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div style="margin-bottom:15px;">
                <label>Tariff</label>
                <select name="tariff_id" required>
                    <option value="">Select Tariff</option>
                    <?php foreach ($tariffs as $tariff): ?>
                        <option value="<?= $tariff['id'] ?>">
                            <?= htmlspecialchars($tariff['name']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <button type="submit" class="btn">
                Confirm Installation
            </button>

        </form>

    </div>

</div>

<?php require_once __DIR__ . '/../../includes/footer.php'; ?>