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

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

    $data['ticket_no'] = \App\Models\Backend\ContentFeedbackMain::generateTicketNo();
    $data['status'] = 'PENDING';
    $data['type'] = $data['type'] ?: 'INQUIRY';
    $data['cat'] = $data['cat'] ?: 'OTHERS';
    $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;
}

/* ---------- Footer ---------- */
.footer-contactbar{padding:2rem 0 1.5rem;}
.footer-contactbar a{
  color:var(--ink);text-decoration:none;font-weight:600;font-size:.9rem;
  display:inline-flex;align-items:center;gap:.5rem;
}
.footer-contactbar a:hover{color:var(--green-800);}
.footer-contactbar .bi{color:var(--green-800);font-size:1.1rem;}
.f-divider{width:1px;height:26px;background:#cfc9b8;}

.map-card{
  background:#fff;
  border:1px solid #ece8db;
  border-radius:1rem;
  box-shadow:0 8px 24px rgba(46,74,53,.10);
  padding:2rem;
  text-align:center;
}
.map-card .map-icon{
  width:64px;height:64px;
  background:var(--green-100);
  border:1px solid var(--green-200);
  border-radius:50%;
  display:flex;align-items:center;justify-content:center;
  margin:0 auto 1rem;
  font-size:1.6rem;color:var(--green-800);
}
.map-card h5{color:var(--green-800);font-weight:700;font-size:1rem;margin-bottom:.35rem;}
.map-card p{margin:0;font-weight:500;font-size:.9rem;color:var(--ink);line-height:1.6;}
.map-card .btn-map{
  margin-top:1rem;
  background:var(--green-800);color:#fff;border:0;
  border-radius:50rem;padding:.55rem 1.8rem;font-weight:600;font-size:.9rem;
  display:inline-flex;align-items:center;gap:.5rem;
  transition:all .2s;
}
.map-card .btn-map:hover{background:var(--green-900);color:#fff;}

@media (max-width: 991.98px){
  .contact-page{background-size:auto 300px, auto 500px;}
}
</style>

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

      <!-- ===== Left: contact info ===== -->
      <div class="col-lg-6">
        <h1 class="contact-title mb-2">Contact Us</h1>
        <p class="contact-sub mb-4">We welcome your feedback, enquiries and collaboration.</p>

        <div class="info-item">
          <div class="info-icon"><i class="bi bi-geo-alt"></i></div>
          <div>
            <h6>Contact Information</h6>
            <p>REDD+ Malaysia Secretariat<br>
               NRES Complex, Lot 4G2, Precinct 4<br>
               Federal Government Administrative Centre<br>
               62574 Putrajaya, Malaysia</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 class="info-item">
          <div class="info-icon"><i class="bi bi-clock"></i></div>
          <div>
            <h6>Office Hours</h6>
            <p>Monday &ndash; Friday<br>8:30 am &ndash; 5:30 pm (GMT+8)</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 message has been submitted successfully.</p>
              @if($ticketNo)
                <span class="ticket">Ticket No: {{ $ticketNo }}</span>
              @endif
            </div>
          @else
            <h2 class="mb-4">General Inquiry</h2>
            <form method="POST" action="{{ request()->url() }}">
              @csrf
              <input type="hidden" name="type" value="INQUIRY">
              <input type="hidden" name="cat" value="OTHERS">

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

              <div class="mb-3">
                <label for="email" class="visually-hidden">Email Address</label>
                <input type="email" class="form-control @error('email') is-invalid @enderror" id="email" name="email" value="{{ old('email') }}" placeholder="Email Address">
                @error('email')<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-4">
                <label for="feedback" class="visually-hidden">Your Message</label>
                <textarea class="form-control @error('feedback') is-invalid @enderror" id="feedback" name="feedback" rows="6" placeholder="Your Message *" required>{{ old('feedback') }}</textarea>
                @error('feedback')<span class="invalid-feedback">{{ $message }}</span>@enderror
              </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 class="container pb-4">
    <div class="row justify-content-center">
      <div class="col-12">
        <div class="map-embed">
          <iframe
            src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3984.1234!2d101.685!3d2.9144!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x31cdb5b5b5b5b5b5%3A0x1234567890abcdef!2sMinistry%20of%20Natural%20Resources%20and%20Environmental%20Sustainability!5e0!3m2!1sen!2smy!4v1"
            width="100%"
            height="380"
            style="border:0;border-radius:1rem;"
            allowfullscreen=""
            loading="lazy"
            referrerpolicy="no-referrer-when-downgrade"
          ></iframe>
        </div>
      </div>
    </div>
  </div>
      </div>
    </div>
  </div>

  <!-- ===== Footer ===== -->
  
<style>
.footer-redd-article {
  background: url('/assets/redd-plus/faq_footer_bg.png') center bottom/cover no-repeat;
  padding: 4rem 0 2.5rem;
}
.footer-redd-article a {
  color: var(--ink); text-decoration: none; font-weight: 600; font-size: .85rem;
}
.footer-redd-article a:hover { color: var(--green-800); }
.footer-redd-article .bi { color: var(--green-800); font-size: 1.1rem; }
.footer-redd-article .f-divider { width: 1px; height: 26px; background: #cfc9b8; }
</style>

    <footer class="footer-redd-article">
      <div class="container">
        <div class="d-flex flex-wrap align-items-center justify-content-center gap-4">
          <img src="/assets/redd-plus/logo.png" alt="REDD+ Malaysia" height="46">
          <span class="f-divider d-none d-md-inline-block"></span>
          <a href="https://redd.nres.gov.my"><i class="bi bi-globe2"></i> redd.nres.gov.my</a>
          <span class="f-divider d-none d-md-inline-block"></span>
          <a href="mailto:redd@nres.gov.my"><i class="bi bi-envelope"></i> redd@nres.gov.my</a>
          <span class="f-divider d-none d-md-inline-block"></span>
          <a href="tel:+60380008000"><i class="bi bi-telephone"></i> +603 8000 8000</a>
        </div>
      </div>
    </footer>
</div>