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

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

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

    $stmt = $pdo->prepare("
        INSERT INTO meter_issues 
        (company_id, meter_number, issue_title, issue_type, created_by)
        VALUES (?, ?, ?, ?, ?)
    ");

    $stmt->execute([
        $_SESSION['company_id'],
        $_POST['meter_number'],
        $_POST['issue_title'],
        $_POST['issue_type'],
        $_SESSION['user_id']
    ]);

    $issue_id = $pdo->lastInsertId();

    logMeterEvent($pdo, $issue_id, 'CREATED', 'Issue created');

    header("Location: view.php?id=" . $issue_id);
    exit;
}

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

<div class="content">

<h2>Create Meter Issue</h2>

<form method="POST">

<label>Meter Number</label>
<input type="text" name="meter_number" required>

<label>Issue Title</label>
<input type="text" name="issue_title">

<label>Issue Type</label>
<input type="text" name="issue_type">

<button class="btn-primary">Save</button>

</form>

</div>

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