<?php
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../config/database.php';

requirePermission('trade_jobs.manage');

$companyId = $_SESSION['company_id'] ?? null;
$userId    = $_SESSION['user_id'] ?? null;

if (!$companyId) {
    die("Company isolation error.");
}

$error = null;

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

    $title       = trim($_POST['title'] ?? '');
    $priority    = $_POST['priority'] ?? 'MEDIUM';
    $description = trim($_POST['description'] ?? '');

    if (!$title) {
        $error = "Title is required.";
    }

    if (!$error) {

        $jobNumber = 'TJ-' . date('Y') . '-' . str_pad(rand(1,9999), 4, '0', STR_PAD_LEFT);

        $stmt = $pdo->prepare("
            INSERT INTO trade_jobs 
            (company_id, job_number, title, description, status, priority, created_at, updated_at)
            VALUES (?, ?, ?, ?, 'PENDING', ?, NOW(), NOW())
        ");

        $stmt->execute([
            $companyId,
            $jobNumber,
            $title,
            $description,
            $priority
        ]);

        $newId = $pdo->lastInsertId();

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

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

<div class="main-content">

    <div class="page-header">
        <h2>Create Trade Job</h2>
        <p class="page-subtitle">
            Register a new field service assignment.
        </p>
    </div>

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

    <div class="card modern-card">

        <form method="POST" class="form-grid">

            <div class="form-group full-width">
                <label>Job Title</label>
                <input 
                    type="text" 
                    name="title" 
                    class="form-control modern-input" 
                    placeholder="Enter job title..."
                    required
                >
            </div>

            <div class="form-group half-width">
                <label>Priority</label>
                <select name="priority" class="form-control modern-input">
                    <option value="LOW">LOW</option>
                    <option value="MEDIUM" selected>MEDIUM</option>
                    <option value="HIGH">HIGH</option>
                    <option value="URGENT">URGENT</option>
                </select>
            </div>

            <div class="form-group full-width">
                <label>Description</label>
                <textarea 
                    name="description" 
                    rows="4" 
                    class="form-control modern-input"
                    placeholder="Enter job description..."
                ></textarea>
            </div>

            <div class="form-actions full-width">
                <button type="submit" class="btn btn-primary modern-btn">
                    + Create Job
                </button>

                <a href="index.php" class="btn btn-light modern-btn-light">
                    Cancel
                </a>
            </div>

        </form>

    </div>

</div>

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