<!doctype html>
<html lang="id">

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
  <title>Edit Permintaan Peminjaman - {{ config('app.name', 'Laravel') }}</title>
  <link rel="icon" type="image/svg+xml" href="{{ asset('favicon.svg') }}">

  <script src="https://cdn.tailwindcss.com"></script>
  <script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
  <script src="https://unpkg.com/lucide@latest"></script>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">

  <style>
    :root {
      --bg: #eef4ff;
      --panel: rgba(255, 255, 255, 0.92);
      --text: #172554;
      --muted: #64748b;
      --line: #dbe6f7;
      --primary: #4f46e5;
      --secondary: #2563eb;
      --cyan: #38bdf8;
      --amber: #f59e0b;
      --shadow-soft: 0 28px 55px rgba(59, 84, 145, 0.12);
      --shadow-card: 0 18px 32px rgba(148, 163, 184, 0.12);
      --font-main: 'Inter', system-ui, -apple-system, sans-serif;
    }

    * { box-sizing: border-box; }

    body {
      margin: 0;
      font-family: var(--font-main);
      color: var(--text);
      background:
        radial-gradient(circle at top left, rgba(79, 70, 229, 0.10), transparent 22%),
        radial-gradient(circle at top right, rgba(56, 189, 248, 0.10), transparent 24%),
        linear-gradient(180deg, #f8fbff 0%, var(--bg) 100%);
      -webkit-font-smoothing: antialiased;
    }

    .app { display: flex; min-height: 100vh; }

    .content {
      flex: 1;
      width: 100%;
      padding: 28px;
    }

    .shell {
      max-width: 1320px;
      margin: 0 auto;
      display: flex;
      flex-direction: column;
      gap: 20px;
    }

    .surface {
      background: var(--panel);
      border: 1px solid rgba(255, 255, 255, 0.82);
      box-shadow: var(--shadow-soft);
      backdrop-filter: blur(16px);
    }

    .hero {
      position: relative;
      overflow: hidden;
      border-radius: 32px;
      padding: 28px;
      background:
        radial-gradient(circle at 86% 14%, rgba(79, 70, 229, 0.14), transparent 25%),
        radial-gradient(circle at 14% 18%, rgba(56, 189, 248, 0.12), transparent 24%),
        linear-gradient(135deg, rgba(255, 255, 255, 0.98) 0%, rgba(243, 247, 255, 0.96) 100%);
    }

    .hero::after {
      content: "";
      position: absolute;
      right: -70px;
      bottom: -120px;
      width: 310px;
      height: 310px;
      border-radius: 999px;
      background: rgba(79, 70, 229, 0.08);
    }

    .hero-inner {
      position: relative;
      z-index: 1;
      display: flex;
      align-items: flex-start;
      justify-content: space-between;
      gap: 24px;
    }

    .pill {
      display: inline-flex;
      align-items: center;
      gap: 8px;
      padding: 8px 14px;
      border-radius: 999px;
      background: linear-gradient(135deg, rgba(79, 70, 229, 0.10), rgba(37, 99, 235, 0.08));
      color: var(--primary);
      font-size: 11px;
      font-weight: 900;
      letter-spacing: 0.16em;
      text-transform: uppercase;
      border: 1px solid rgba(99, 102, 241, 0.12);
    }

    .hero-title {
      margin: 16px 0 10px;
      font-size: clamp(34px, 4vw, 54px);
      line-height: 0.94;
      letter-spacing: -0.06em;
      font-weight: 900;
      color: #172554;
    }

    .hero-text {
      margin: 0;
      max-width: 720px;
      color: var(--muted);
      font-size: 14px;
      line-height: 1.8;
    }

    .btn-primary,
    .btn-soft {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      gap: 8px;
      min-height: 46px;
      padding: 0 18px;
      border-radius: 18px;
      text-decoration: none;
      border: 0;
      cursor: pointer;
      font-size: 14px;
      font-weight: 900;
      transition: all 0.2s ease;
    }

    .btn-primary {
      color: #fff;
      background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
      box-shadow: 0 18px 28px rgba(79, 70, 229, 0.24);
    }

    .btn-soft {
      color: var(--text);
      background: rgba(255, 255, 255, 0.84);
      border: 1px solid var(--line);
    }

    .btn-primary:hover,
    .btn-soft:hover {
      transform: translateY(-1px);
    }

    .layout-grid {
      display: grid;
      grid-template-columns: minmax(0, 0.85fr) minmax(0, 1.15fr);
      gap: 20px;
      align-items: start;
    }

    .panel {
      overflow: hidden;
      border-radius: 30px;
      background:
        radial-gradient(circle at top right, rgba(99, 102, 241, 0.10), transparent 23%),
        linear-gradient(135deg, rgba(255, 255, 255, 0.98) 0%, rgba(243, 247, 255, 0.96) 100%);
    }

    .panel-head {
      display: flex;
      align-items: flex-start;
      justify-content: space-between;
      gap: 14px;
      padding: 22px 24px 0;
    }

    .panel-eyebrow {
      color: var(--muted);
      font-size: 11px;
      font-weight: 900;
      letter-spacing: 0.14em;
      text-transform: uppercase;
    }

    .panel-title {
      margin: 6px 0 0;
      color: #172554;
      font-size: 22px;
      font-weight: 900;
      letter-spacing: -0.04em;
    }

    .status-badge {
      display: inline-flex;
      align-items: center;
      gap: 8px;
      padding: 10px 14px;
      border-radius: 999px;
      font-size: 12px;
      font-weight: 900;
      color: #c2410c;
      background: #fff7ed;
      border: 1px solid #fed7aa;
      white-space: nowrap;
    }

    .product-preview {
      margin: 20px 24px 0;
      height: 320px;
      border-radius: 28px;
      border: 1px solid rgba(214, 226, 247, 0.98);
      background:
        radial-gradient(circle at top left, rgba(79, 70, 229, 0.10), transparent 26%),
        radial-gradient(circle at bottom right, rgba(56, 189, 248, 0.14), transparent 28%),
        linear-gradient(135deg, #f8fbff, #eef6ff);
      display: flex;
      align-items: center;
      justify-content: center;
      overflow: hidden;
    }

    .product-preview img {
      width: 100%;
      height: 100%;
      object-fit: contain;
      padding: 18px;
    }

    .empty-image {
      display: grid;
      place-items: center;
      gap: 10px;
      color: #8ba0bd;
      text-align: center;
      font-weight: 800;
    }

    .info-list {
      padding: 22px 24px 24px;
      display: grid;
      gap: 12px;
    }

    .info-row {
      display: grid;
      grid-template-columns: 150px minmax(0, 1fr);
      gap: 14px;
      padding: 14px;
      border-radius: 18px;
      background: rgba(248, 251, 255, 0.82);
      border: 1px solid rgba(219, 230, 247, 0.85);
    }

    .info-label {
      color: var(--muted);
      font-size: 11px;
      font-weight: 900;
      letter-spacing: 0.12em;
      text-transform: uppercase;
    }

    .info-value {
      color: #172554;
      font-size: 14px;
      font-weight: 800;
      line-height: 1.55;
      word-break: break-word;
    }

    .form-body {
      padding: 24px;
      display: grid;
      gap: 18px;
    }

    .field-card {
      padding: 18px;
      border-radius: 24px;
      background: rgba(248, 251, 255, 0.86);
      border: 1px solid rgba(219, 230, 247, 0.92);
    }

    .label {
      display: flex;
      align-items: center;
      gap: 8px;
      color: #334155;
      font-size: 13px;
      font-weight: 900;
      margin-bottom: 10px;
    }

    .input {
      width: 100%;
      border-radius: 18px;
      border: 1px solid #cbd5e1;
      background: rgba(255, 255, 255, 0.96);
      color: #0f172a;
      padding: 13px 14px;
      font-size: 14px;
      font-weight: 800;
      outline: none;
      transition: all 0.2s ease;
    }

    .input:focus {
      border-color: var(--primary);
      box-shadow: 0 0 0 5px rgba(79, 70, 229, 0.10);
    }

    .input[type="date"],
    .input[type="datetime-local"] {
      min-height: 58px;
      padding: 16px 18px;
      font-size: 16px;
      font-weight: 900;
      cursor: pointer;
      background: linear-gradient(180deg, #ffffff, #f8fbff);
    }

    .input[type="date"]::-webkit-calendar-picker-indicator,
    .input[type="datetime-local"]::-webkit-calendar-picker-indicator {
      width: 28px;
      height: 28px;
      padding: 6px;
      border-radius: 12px;
      cursor: pointer;
      background-color: #eef2ff;
    }

    .hint {
      margin-top: 8px;
      color: var(--muted);
      font-size: 12px;
      line-height: 1.6;
    }

    .error-text {
      margin-top: 8px;
      color: #dc2626;
      font-size: 13px;
      font-weight: 800;
    }

    .revision-note {
      margin: 0 24px 24px;
      padding: 18px;
      border-radius: 24px;
      background: linear-gradient(135deg, rgba(255, 251, 235, 0.96), rgba(255, 247, 237, 0.92));
      border: 1px solid #fed7aa;
      color: #92400e;
    }

    .footer-actions {
      display: flex;
      justify-content: flex-end;
      gap: 12px;
      padding: 20px 24px 24px;
      border-top: 1px solid rgba(219, 230, 247, 0.9);
      background: rgba(248, 251, 255, 0.72);
    }

    @media (max-width: 1100px) {
      .layout-grid { grid-template-columns: 1fr; }
    }

    @media (max-width: 1024px) {
      .app { display: block; }
      .content { padding: 88px 20px 22px; }
    }

    @media (max-width: 760px) {
      .hero,
      .panel { border-radius: 24px; }
      .hero-inner { flex-direction: column; }
      .hero-actions,
      .footer-actions { width: 100%; flex-direction: column-reverse; }
      .hero-actions a,
      .footer-actions a,
      .footer-actions button { width: 100%; }
      .product-preview { height: 250px; margin-inline: 16px; border-radius: 22px; }
      .panel-head,
      .info-list,
      .form-body,
      .footer-actions { padding-inline: 16px; }
      .info-row { grid-template-columns: 1fr; gap: 6px; }
      .revision-note { margin-inline: 16px; }
    }
  </style>
</head>

<body>
  @php
    $item = $borrowRequest->item;
    $itemImage = $item->image ?? null;
    $statusLabel = ucwords(str_replace('_', ' ', strtolower($borrowRequest->status ?? 'pending')));
    $borrowDate = optional($borrowRequest->tanggal_pinjam)->format('d M Y, H:i');
    $returnDateValue = old('tanggal_kembali', $borrowRequest->tanggal_kembali ? $borrowRequest->tanggal_kembali->format('Y-m-d\TH:i') : '');
  @endphp

  <div class="app">
    @include('partials.sidebar')

    <main class="content">
      <div class="shell">
        <section class="hero surface">
          <div class="hero-inner">
            <div>
              <span class="pill">
                <i data-lucide="pencil-line" class="w-4 h-4"></i>
                {{ $restricted ? 'Revisi Permintaan' : 'Edit Permintaan' }}
              </span>
              <h1 class="hero-title">{{ $item->nama ?? 'Peminjaman Barang' }}</h1>
              <p class="hero-text">
                {{ $restricted
                  ? 'Perbarui jumlah atau jadwal kembali sesuai arahan staf, lalu kirim ulang agar permintaan bisa diproses lagi.'
                  : 'Sesuaikan detail peminjaman barang dengan rapi sebelum permintaan dilanjutkan.'
                }}
              </p>
            </div>

            <div class="hero-actions flex gap-3">
              <a href="{{ route('borrow.show', $borrowRequest->id) }}" class="btn-soft">
                <i data-lucide="arrow-left" class="w-4 h-4"></i>
                Detail
              </a>
              <a href="{{ route('borrow.index') }}" class="btn-soft">
                <i data-lucide="list" class="w-4 h-4"></i>
                Daftar Pinjaman
              </a>
            </div>
          </div>
        </section>

        <section class="layout-grid">
          <article class="panel surface">
            <div class="panel-head">
              <div>
                <div class="panel-eyebrow">Barang Dipinjam</div>
                <h2 class="panel-title">Ringkasan Barang</h2>
              </div>
              <span class="status-badge">
                <i data-lucide="refresh-cw" class="w-4 h-4"></i>
                {{ $statusLabel }}
              </span>
            </div>

            <div class="product-preview">
              @if($itemImage && Storage::disk('public')->exists($itemImage))
                <img src="{{ asset('storage/' . $itemImage) }}" alt="{{ $item->nama ?? 'Foto barang' }}">
              @else
                <div class="empty-image">
                  <i data-lucide="image" class="w-12 h-12"></i>
                  <div>Belum ada foto barang.</div>
                </div>
              @endif
            </div>

            <div class="info-list">
              <div class="info-row">
                <div class="info-label">Nama Barang</div>
                <div class="info-value">{{ $item->nama ?? '-' }}</div>
              </div>
              <div class="info-row">
                <div class="info-label">Tanggal Pinjam</div>
                <div class="info-value">{{ $borrowDate ?? '-' }}</div>
              </div>
              <div class="info-row">
                <div class="info-label">Divisi Pemilik</div>
                <div class="info-value">{{ optional($item->divisi)->nama ?? '-' }}</div>
              </div>
              <div class="info-row">
                <div class="info-label">Deskripsi</div>
                <div class="info-value">{{ $item->deskripsi ?? 'Tidak ada deskripsi barang.' }}</div>
              </div>
            </div>
          </article>

          <article class="panel surface">
            <div class="panel-head">
              <div>
                <div class="panel-eyebrow">Form Perubahan</div>
                <h2 class="panel-title">Atur Ulang Permintaan</h2>
              </div>
            </div>

            <form action="{{ route('borrow.update', $borrowRequest->id) }}" method="POST" id="borrowEditScheduleForm">
              @csrf
              @method('PUT')

              <input type="hidden" name="item_id" value="{{ $borrowRequest->item_id }}">
              <input type="hidden" name="tanggal_pinjam" value="{{ $borrowRequest->tanggal_pinjam }}">

              <div class="form-body">
                <div class="field-card">
                  <label for="jumlah" class="label">
                    <i data-lucide="package-plus" class="w-4 h-4 text-indigo-600"></i>
                    Jumlah Barang
                  </label>
                  <input
                    type="number"
                    name="jumlah"
                    id="jumlah"
                    min="1"
                    value="{{ old('jumlah', $borrowRequest->jumlah) }}"
                    class="input"
                    placeholder="Masukkan jumlah barang"
                    required
                  >
                  <div class="hint">Jumlah akan dicek ulang dengan stok yang tersedia sebelum permintaan dikirim kembali.</div>
                  @error('jumlah')
                    <div class="error-text">{{ $message }}</div>
                  @enderror
                </div>

                <div class="field-card">
                  <label for="tanggal_kembali" class="label">
                    <i data-lucide="calendar-clock" class="w-4 h-4 text-blue-600"></i>
                    Tanggal & Jam Kembali
                  </label>
                  <input
                    name="tanggal_kembali"
                    id="tanggal_kembali"
                    type="hidden"
                    value="{{ $returnDateValue }}"
                  >
                  <div class="space-y-2">
                    <input type="date" id="borrow_edit_end_date" class="input cursor-pointer" required>
                    <div class="grid grid-cols-2 gap-2">
                      <input type="text" id="borrow_edit_end_hour" inputmode="numeric" maxlength="2" class="input text-center" placeholder="Jam" required>
                      <input type="text" id="borrow_edit_end_minute" inputmode="numeric" maxlength="2" class="input text-center" placeholder="Mnt" required>
                    </div>
                  </div>
                  <div id="borrow_edit_end_error" class="error-text hidden"></div>
                  <div class="hint">Tanggal kembali harus setelah jadwal pinjam: {{ $borrowDate ?? '-' }}.</div>
                  @error('tanggal_kembali')
                    <div class="error-text">{{ $message }}</div>
                  @enderror
                </div>
              </div>

              @if($restricted && $borrowRequest->comment)
                <div class="revision-note">
                  <div class="flex gap-3">
                    <div class="w-10 h-10 rounded-2xl bg-amber-100 text-amber-700 flex items-center justify-center flex-shrink-0">
                      <i data-lucide="message-square-warning" class="w-5 h-5"></i>
                    </div>
                    <div>
                      <div class="text-sm font-black">Catatan dari Staf</div>
                      <div class="mt-2 text-sm leading-7">{{ $borrowRequest->comment }}</div>
                    </div>
                  </div>
                </div>
              @endif

              <div class="footer-actions">
                <a href="{{ route('borrow.show', $borrowRequest->id) }}" class="btn-soft">
                  <i data-lucide="x" class="w-4 h-4"></i>
                  Batal
                </a>
                <button type="submit" class="btn-primary">
                  <i data-lucide="send" class="w-4 h-4"></i>
                  Simpan Perubahan
                </button>
              </div>
            </form>
          </article>
        </section>
      </div>
    </main>
  </div>

  <script>
    if (window.lucide) {
      window.lucide.createIcons();
    }

    document.addEventListener('DOMContentLoaded', function () {
      const minReturn = @js($borrowRequest->tanggal_pinjam->format('Y-m-d\TH:i'));
      const form = document.getElementById('borrowEditScheduleForm');
      const endInput = document.getElementById('tanggal_kembali');
      const endDate = document.getElementById('borrow_edit_end_date');
      const endHour = document.getElementById('borrow_edit_end_hour');
      const endMinute = document.getElementById('borrow_edit_end_minute');
      const endError = document.getElementById('borrow_edit_end_error');

      if (!form || !endInput || !endDate || !endHour || !endMinute || !endError) {
        return;
      }

      const pad = value => String(value).padStart(2, '0');

      const showError = message => {
        endError.textContent = message || '';
        endError.classList.toggle('hidden', !message);
      };

      const setPartsFromValue = value => {
        if (!value || !value.includes('T')) {
          return;
        }

        const [datePart, timePart] = value.split('T');
        const [hourPart, minutePart] = timePart.split(':');

        endDate.value = datePart;
        endHour.value = hourPart;
        endMinute.value = minutePart;
      };

      const sanitizeNumber = (input, max) => {
        input.value = input.value.replace(/\D/g, '').slice(0, 2);

        if (input.value !== '' && Number(input.value) > max) {
          input.value = String(max);
        }
      };

      const composeDateTime = () => {
        if (!endDate.value || endHour.value === '' || endMinute.value === '') {
          return '';
        }

        return `${endDate.value}T${pad(endHour.value)}:${pad(endMinute.value)}`;
      };

      const syncDateTime = () => {
        endInput.value = composeDateTime();
        endDate.min = minReturn.slice(0, 10);

        if (endDate.value && endDate.value < endDate.min) {
          endDate.value = '';
          endInput.value = '';
        }

        if (!endInput.value) {
          showError('Lengkapi tanggal, jam, dan menit kembali.');
          return false;
        }

        if (endInput.value <= minReturn) {
          showError('Waktu kembali harus setelah jadwal pinjam.');
          return false;
        }

        showError('');
        return true;
      };

      setPartsFromValue(endInput.value);

      [endDate].forEach(input => {
        input.addEventListener('click', () => {
          if (typeof input.showPicker === 'function') {
            input.showPicker();
          }
        });

        input.addEventListener('keydown', event => {
          if (event.key === 'Backspace' || event.key === 'Delete') {
            input.value = '';
            syncDateTime();
            return;
          }

          event.preventDefault();
        });
        input.addEventListener('paste', event => event.preventDefault());
        input.addEventListener('drop', event => event.preventDefault());
      });

      [endHour, endMinute].forEach(input => {
        const max = input === endHour ? 23 : 59;

        input.addEventListener('input', () => {
          sanitizeNumber(input, max);
          syncDateTime();
        });

        input.addEventListener('blur', () => {
          if (input.value !== '') {
            input.value = pad(input.value);
          }

          syncDateTime();
        });
      });

      [endDate, endHour, endMinute].forEach(input => {
        input.addEventListener('input', syncDateTime);
        input.addEventListener('change', syncDateTime);
        input.addEventListener('focus', syncDateTime);
      });

      form.addEventListener('submit', event => {
        if (!syncDateTime()) {
          event.preventDefault();
        }
      });

      syncDateTime();
    });
  </script>
  @include('partials.custom-calendar')
</body>

</html>
