<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/../../../includes/auth.php';
requirePermission('manage_network_jobs');

$BASE = app_base_path();
$user_id = $_SESSION['user_id'] ?? 0;

$error = "";

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

    $client_name   = $_POST['client_name'] ?? null;
    $client_email  = $_POST['client_email'] ?? null;
    $client_phone  = $_POST['client_phone'] ?? null;
    $province      = $_POST['province'] ?? '';
    $city          = $_POST['city'] ?? '';
    $suburb        = $_POST['suburb'] ?? '';
    $street        = $_POST['street_address'] ?? null;
    $category      = $_POST['category'] ?? '';
    $title         = $_POST['title'] ?? '';
    $description   = $_POST['description'] ?? '';

    if (!$province || !$city || !$suburb || !$category || !$title || !$description) {
        $error = "Please complete all required fields.";
    } else {

        $stmt = $pdo->prepare("
            INSERT INTO network_jobs
            (source, client_name, client_email, client_phone,
             province, city, suburb, street_address,
             category, title, description,
             status, created_by, created_at)
            VALUES
            ('INTERNAL', ?, ?, ?,
             ?, ?, ?, ?,
             ?, ?, ?,
             'SUBMITTED', ?, NOW())
        ");

        $stmt->execute([
            $client_name,
            $client_email,
            $client_phone,
            $province,
            $city,
            $suburb,
            $street,
            $category,
            $title,
            $description,
            $user_id
        ]);

        $job_id = $pdo->lastInsertId();

        header("Location: " . $BASE . "/operations/network/view.php?id=" . $job_id);
        exit;
    }
}

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

?>

<div class="main-content">
    <div class="page-header">
        <h2>Create Network Job</h2>
        <p class="page-subtitle">Register a new installer marketplace job.</p>
    </div>

    <?php if ($error): ?>
        <div class="card" style="background:#ffe6e6;color:#900;">
            <?= htmlspecialchars($error) ?>
        </div>
    <?php endif; ?>

    <div class="card">
        <form method="POST">

            <h4>Client Details (Optional)</h4>

            <label>Client Name</label>
            <input type="text" name="client_name" class="input">

            <label>Email</label>
            <input type="email" name="client_email" class="input">

            <label>Phone</label>
            <input type="text" name="client_phone" class="input">

            <hr>

            <h4>Location *</h4>

            <label>Province</label>
            <input type="text" name="province" required class="input">

            <label>City</label>
            <input type="text" name="city" required class="input">

            <label>Suburb</label>
            <input type="text" name="suburb" required class="input">

            <label>Street Address</label>
            <input type="text" name="street_address" class="input">

            <hr>

            <h4>Job Details *</h4>

            <label>Category</label>
            <select name="category" required class="input">
                <option value="">Select Category</option>
                <option value="ELECTRICAL">Electrical</option>
                <option value="PLUMBING">Plumbing</option>
                <option value="METER_INSTALL">Meter Install</option>
                <option value="OTHER">Other</option>
            </select>

            <label>Job Title</label>
            <input type="text" name="title" required class="input">

            <label>Description</label>
            <textarea name="description" required class="input"></textarea>

            <br><br>

            <button type="submit" class="btn-primary">+ Create Network Job</button>
            <a href="<?= $BASE ?>/operations/network/index.php" class="btn-secondary">Cancel</a>

        </form>
    </div>
</div>

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