<?php $companyName = $settings['company_name'] ?? 'FM ERP'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Maintenance Request — <?= esc($companyName) ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<link href="<?= base_url('assets/css/custom.css') ?>" rel="stylesheet">
<style>body{padding-top:0!important}</style>
</head>
<body>
<div class="public-bg py-4">
<div style="max-width:680px;margin:0 auto;padding:0 1rem">
  <div class="text-center text-white mb-4">
    <div class="auth-logo mx-auto mb-3"><i class="bi bi-buildings-fill"></i></div>
    <h2 class="fw-bold"><?= esc($companyName) ?></h2>
    <p class="opacity-75">Submit a Maintenance Request</p>
  </div>

  <?php if(session()->getFlashdata('success')): ?>
  <div class="alert alert-success border-0 rounded-3 mb-3"><i class="bi bi-check-circle-fill me-2"></i><?= session()->getFlashdata('success') ?></div>
  <?php endif; ?>
  <?php if(session()->getFlashdata('errors')): ?>
  <div class="alert alert-danger border-0 rounded-3 mb-3"><?php foreach((array)session()->getFlashdata('errors') as $e): ?><div><?= esc($e) ?></div><?php endforeach; ?></div>
  <?php endif; ?>

  <div class="public-card">
    <h4 class="fw-bold mb-1" style="color:#0a3d6b">Submit Maintenance Request</h4>
    <p class="text-muted small mb-4">Fill in the form below. You will receive a ticket number to track your request.</p>
    <?= form_open_multipart(base_url('request')) ?>
    <div class="row g-3">
      <div class="col-md-6"><label class="form-label">Your Name *</label><input type="text" name="requester_name" class="form-control" value="<?= old('requester_name') ?>" required></div>
      <div class="col-md-6"><label class="form-label">Phone Number</label><input type="tel" name="requester_phone" class="form-control" value="<?= old('requester_phone') ?>" placeholder="+974 XXXX XXXX"></div>
      <div class="col-12"><label class="form-label">Email Address</label><input type="email" name="requester_email" class="form-control" value="<?= old('requester_email') ?>"></div>
      <div class="col-md-6"><label class="form-label">Facility / Location</label><select name="facility_id" class="form-select"><option value="">— Select if known —</option><?php foreach($facilities as $f): ?><option value="<?= $f['id'] ?>"><?= esc($f['name']) ?></option><?php endforeach; ?></select></div>
      <div class="col-md-6"><label class="form-label">Issue Category</label><select name="category" class="form-select"><option value="">— Select —</option><?php foreach(['HVAC','Electrical','Plumbing','Fire Safety','Security','Civil / Structural','IT / Telecom','Cleaning','Other'] as $c): ?><option value="<?= $c ?>"><?= $c ?></option><?php endforeach; ?></select></div>
      <div class="col-md-6"><label class="form-label">Priority</label><select name="priority" class="form-select"><option value="low">Low — Minor inconvenience</option><option value="medium" selected>Medium — Needs attention</option><option value="high">High — Affecting operations</option><option value="critical">Critical — Emergency</option></select></div>
      <div class="col-12"><label class="form-label">Description *</label><textarea name="description" class="form-control" rows="4" placeholder="Please describe the issue in detail. Include location, time it started, and any relevant details..." required><?= old('description') ?></textarea></div>
      <div class="col-12"><label class="form-label">Attach Image (optional)</label><input type="file" name="image" class="form-control" accept="image/*"></div>
      <div class="col-12"><button type="submit" class="btn btn-fm-primary w-100 py-3 fw-bold"><i class="bi bi-send-fill me-2"></i>Submit Request</button></div>
    </div>
    <?= form_close() ?>
    <hr class="my-4">
    <div class="text-center"><p class="small text-muted mb-2">Already have a ticket number?</p>
    <form class="d-flex gap-2 justify-content-center" action="" onsubmit="var t=this.ticket.value.trim();if(t){window.location='<?= base_url('track/') ?>'+t;}return false;">
      <input type="text" name="ticket" class="form-control form-control-sm" placeholder="e.g. MR-2024-0001" style="max-width:200px">
      <button type="submit" class="btn btn-fm-outline btn-sm">Track</button>
    </form>
    </div>
    <div class="text-center mt-3"><a href="<?= base_url('login') ?>" class="small text-muted">Staff Login →</a></div>
  </div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body></html>
