@php
$success = session('success');
$ticketNo = session('ticket_no');

if (!$success && request()->isMethod('POST')) {
    $data = request()->validate([
        'name' => 'required|string|max:255',
        'email' => 'required|email|max:255',
        'contact_number' => 'required|string|max:50',
        'subject' => 'required|string|max:255',
        'cat' => 'required|string|max:255',
        'feedback' => 'required|string',
        'data_protection' => 'required|accepted',
    ]);

    $data['ticket_no'] = \App\Models\Backend\ContentFeedbackMain::generateTicketNo();
    $data['status'] = 'PENDING';
    $data['type'] = 'COMPLAINT';
    $data['feedback'] = 'Phone: ' . $data['contact_number'] . "\n\n" . $data['feedback'];
    $data['created_user_type'] = 'guest';

    $feedback = \App\Models\Backend\ContentFeedbackMain::create($data);
    $ticketNo = $feedback->ticket_no;
    $success = true;

    session()->flash('success', true);
    session()->flash('ticket_no', $ticketNo);
}
@endphp

<style>
/* ---------- Page background ---------- */
.contact-page{
  background:
    url('/assets/redd-plus/contact_footer_bg.png') center bottom / 100% auto no-repeat,
    url('/assets/redd-plus/contact_hero_bg.png') center top / 100% auto no-repeat,
    #fff;
  min-height:calc(100vh - 64px);
  display:flex;flex-direction:column;
}
.contact-page > .container{flex:1;}

/* ---------- Left column ---------- */
.contact-title{
  color:var(--green-800);
  font-weight:700;
  font-size:clamp(2.3rem,4.5vw,3.2rem);
}
.contact-sub{font-size:1.15rem;font-weight:500;max-width:320px;}
.info-item{
  display:flex;gap:1.1rem;
  padding:1.4rem 0;
  border-bottom:1px solid #e8e4d8;
  max-width:400px;
}
.info-item:last-child{border-bottom:0;}
.info-icon{
  width:52px;height:52px;flex:0 0 52px;
  background:var(--green-100);
  border:1px solid var(--green-200);
  border-radius:50%;
  display:flex;align-items:center;justify-content:center;
  font-size:1.35rem;color:var(--green-800);
}
.info-item h6{color:var(--green-800);font-weight:700;margin-bottom:.35rem;}
.info-item p{margin:0;font-weight:500;font-size:.92rem;line-height:1.55;}

/* ---------- Form card ---------- */
.form-card{
  background:#fdfcf9;
  border:1px solid #ece8db;
  border-radius:1rem;
  box-shadow:0 16px 40px rgba(46,74,53,.10);
  padding:2rem 2rem 2.25rem;
}
.form-card h2{
  color:var(--green-800);font-weight:700;font-size:1.35rem;
}
.form-card .form-control{
  border:1px solid #e2ddd0;
  border-radius:.6rem;
  padding:.8rem 1rem;
  font-size:.92rem;
  font-weight:500;
  color:var(--ink);
  background:#fff;
}
.form-card .form-control::placeholder{color:#9aa59c;}
.form-card .form-control:focus{
  border-color:var(--green-800);
  box-shadow:0 0 0 .2rem rgba(46,74,53,.12);
}
.btn-forest-contact{
  background:var(--green-800);color:#fff;
  border-radius:.6rem;padding:.7rem 1.6rem;font-weight:600;
}
.btn-forest-contact:hover{background:var(--green-900);color:#fff;}

.form-card .invalid-feedback{
  display:block;
  font-size:.8rem;
  color:#a94442;
  margin-top:.25rem;
}
.form-card .is-invalid{
  border-color:#a94442;
}
.form-card .is-invalid:focus{
  border-color:#a94442;
  box-shadow:0 0 0 .2rem rgba(169,68,66,.15);
}

/* ---------- Success message ---------- */
.contact-success{
  text-align:center;
  padding:3rem 1.5rem;
}
.contact-success i{
  font-size:3rem;
  color:var(--green-800);
  margin-bottom:1rem;
}
.contact-success h3{
  color:var(--green-800);
  font-weight:700;
  font-size:1.4rem;
  margin-bottom:.5rem;
}
.contact-success p{
  color:var(--ink);
  margin:0;
}
.contact-success .ticket{
  display:inline-block;
  background:var(--green-100);
  color:var(--green-900);
  padding:.35rem 1rem;
  border-radius:50rem;
  font-weight:700;
  margin-top:.75rem;
}
</style>

<div class="contact-page">
  <div class="container py-5">
    <div class="row gy-5">

      <!-- ===== Left: info ===== -->
      <div class="col-lg-6">
        <h1 class="contact-title mb-2">Reports, Issues &amp; Incidents</h1>
        <p class="contact-sub mb-4">Submit reports, raise issues or report incidents related to REDD+ safeguards implementation in Malaysia.</p>

        <div class="info-item">
          <div class="info-icon"><i class="bi bi-shield-exclamation"></i></div>
          <div>
            <h6>What to Report</h6>
            <p>Issues related to Safeguards A–G, including forest monitoring, stakeholder consultation, biodiversity, governance, and coordination.</p>
          </div>
        </div>

        <div class="info-item">
          <div class="info-icon"><i class="bi bi-envelope"></i></div>
          <div>
            <h6>Email</h6>
            <p><a href="mailto:redd@nres.gov.my" class="text-decoration-none" style="color:var(--ink);">redd@nres.gov.my</a></p>
          </div>
        </div>

        <div class="info-item">
          <div class="info-icon"><i class="bi bi-telephone"></i></div>
          <div>
            <h6>Phone</h6>
            <p><a href="tel:+60380008000" class="text-decoration-none" style="color:var(--ink);">+603 8000 8000</a></p>
          </div>
        </div>
      </div>

      <!-- ===== Right: form ===== -->
      <div class="col-lg-6">
        <div class="form-card">
          @if($success)
            <div class="contact-success">
              <i class="bi bi-check-circle-fill"></i>
              <h3>Thank You!</h3>
              <p>Your report has been submitted successfully. We will review and follow up accordingly.</p>
              @if($ticketNo)
                <span class="ticket">Ticket No: {{ $ticketNo }}</span>
              @endif
            </div>
          @else
            <h2 class="mb-4">Submit a Report</h2>
            <form method="POST" action="{{ request()->url() }}">
              @csrf
              <input type="hidden" name="type" value="COMPLAINT">

              <div class="mb-3">
                <label for="name" class="visually-hidden">Full Name</label>
                <input type="text" class="form-control @error('name') is-invalid @enderror" id="name" name="name" value="{{ old('name') }}" placeholder="Full Name *" required>
                @error('name')<span class="invalid-feedback">{{ $message }}</span>@enderror
              </div>

              <div class="mb-3">
                <label for="email" class="visually-hidden">Email</label>
                <input type="email" class="form-control @error('email') is-invalid @enderror" id="email" name="email" value="{{ old('email') }}" placeholder="Email *" required>
                @error('email')<span class="invalid-feedback">{{ $message }}</span>@enderror
              </div>

              <div class="mb-3">
                <label for="contact_number" class="visually-hidden">Contact Number</label>
                <input type="text" class="form-control @error('contact_number') is-invalid @enderror" id="contact_number" name="contact_number" value="{{ old('contact_number') }}" placeholder="Contact Number *" required>
                @error('contact_number')<span class="invalid-feedback">{{ $message }}</span>@enderror
              </div>

              <div class="mb-3">
                <label for="subject" class="visually-hidden">Subject</label>
                <input type="text" class="form-control @error('subject') is-invalid @enderror" id="subject" name="subject" value="{{ old('subject') }}" placeholder="Subject *" required>
                @error('subject')<span class="invalid-feedback">{{ $message }}</span>@enderror
              </div>

              <div class="mb-3">
                <label for="cat" class="visually-hidden">Choose Item</label>
                <select class="form-control @error('cat') is-invalid @enderror" id="cat" name="cat" required>
                  <option value="">— Select a safeguard —</option>
                  <option value="SAFEGUARD_A" {{ old('cat') == 'SAFEGUARD_A' ? 'selected' : '' }}>Safeguard A — National Forest Monitoring System</option>
                  <option value="SAFEGUARD_B" {{ old('cat') == 'SAFEGUARD_B' ? 'selected' : '' }}>Safeguard B — National REDD+ Strategy / Forest Reference Level</option>
                  <option value="SAFEGUARD_C" {{ old('cat') == 'SAFEGUARD_C' ? 'selected' : '' }}>Safeguard C — Stakeholder Consultation / Transparency</option>
                  <option value="SAFEGUARD_D" {{ old('cat') == 'SAFEGUARD_D' ? 'selected' : '' }}>Safeguard D — Biodiversity / Ecosystem Services</option>
                  <option value="SAFEGUARD_E" {{ old('cat') == 'SAFEGUARD_E' ? 'selected' : '' }}>Safeguard E — Governance / Forest Law Enforcement</option>
                  <option value="SAFEGUARD_F" {{ old('cat') == 'SAFEGUARD_F' ? 'selected' : '' }}>Safeguard F — Safeguards Information System (SIS)</option>
                  <option value="SAFEGUARD_G" {{ old('cat') == 'SAFEGUARD_G' ? 'selected' : '' }}>Safeguard G — Coordination / Institutional Arrangements</option>
                </select>
                @error('cat')<span class="invalid-feedback">{{ $message }}</span>@enderror
              </div>

              <div class="mb-4">
                <label for="feedback" class="visually-hidden">Your Inquiry</label>
                <textarea class="form-control @error('feedback') is-invalid @enderror" id="feedback" name="feedback" rows="6" placeholder="Your Inquiry *" required>{{ old('feedback') }}</textarea>
                @error('feedback')<span class="invalid-feedback">{{ $message }}</span>@enderror
              </div>

              <div class="mb-4">
                <div class="form-check">
                  <input class="form-check-input @error('data_protection') is-invalid @enderror" type="checkbox" id="data_protection" name="data_protection" value="1" {{ old('data_protection') ? 'checked' : '' }} required>
                  <label class="form-check-label" for="data_protection" style="font-weight:500;font-size:.88rem;">
                    I consent to the collection and processing of my personal data in accordance with the Personal Data Protection Act 2010.
                  </label>
                  @error('data_protection')<span class="invalid-feedback">{{ $message }}</span>@enderror
                </div>
              </div>

              <button type="submit" class="btn btn-forest-contact">
                Send Message <i class="bi bi-arrow-right ms-1"></i>
              </button>
            </form>
          @endif
        </div>
      </div>

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