@extends('layouts.admin')
@section('title', 'Edit Bulletin')

@section('content')

<div style="display:flex;align-items:center;gap:.75rem;margin-bottom:1.5rem">
  <a href="{{ route('admin.bulletin.index') }}" class="btn btn-secondary btn-sm"><svg width="14" height="14" fill="none" stroke="currentColor" viewBox="0 0 24 24" style="flex-shrink:0"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/></svg> Bulletin</a>
  <span style="color:var(--muted-foreground)">/</span>
  <span style="font-size:.8125rem;color:var(--muted-foreground)">Edit: {{ Str::limit($bulletin->title, 40) }}</span>
</div>

<div style="display:grid;grid-template-columns:1fr 280px;gap:1.25rem;align-items:start">

  <form method="POST" action="{{ route('admin.bulletin.update', $bulletin) }}" id="bulletin-form">
    @csrf @method('PUT')
    <div class="card" style="padding:1.5rem;margin-bottom:1rem">
      <div style="margin-bottom:1.25rem">
        <label>Title</label>
        <input type="text" name="title" value="{{ old('title', $bulletin->title) }}" required
          class="input" style="font-size:.9375rem;font-weight:600">
      </div>
      <div style="margin-bottom:1.25rem">
        <label>Excerpt</label>
        <textarea name="excerpt" rows="2" class="input" style="resize:none">{{ old('excerpt', $bulletin->excerpt) }}</textarea>
      </div>
      <div>
        <label>Content</label>
        <textarea name="content" rows="16" required
          class="input" style="resize:vertical;font-family:monospace;font-size:.8125rem;line-height:1.7">{{ old('content', $bulletin->content) }}</textarea>
      </div>
    </div>
  </form>

  <div style="display:flex;flex-direction:column;gap:1rem">

    <div class="card" style="padding:1.125rem">
      <div style="font-size:.875rem;font-weight:600;color:var(--foreground);margin-bottom:.875rem">Save</div>
      <div style="display:flex;flex-direction:column;gap:.5rem">
        <button type="submit" form="bulletin-form" class="btn btn-primary btn-sm" style="justify-content:center">
          Save Changes
        </button>
        @if($bulletin->is_published)
        <form method="POST" action="{{ route('admin.bulletin.unpublish', $bulletin) }}">
          @csrf
          <button class="btn btn-secondary btn-sm" style="width:100%;justify-content:center;color:#d97706">Unpublish</button>
        </form>
        @else
        <form method="POST" action="{{ route('admin.bulletin.publish', $bulletin) }}">
          @csrf
          <button class="btn btn-secondary btn-sm" style="width:100%;justify-content:center;color:#166534">Publish</button>
        </form>
        @endif
      </div>
    </div>

    <div class="card" style="padding:1.125rem">
      <div style="font-size:.875rem;font-weight:600;color:var(--foreground);margin-bottom:.875rem">Settings</div>

      <div style="margin-bottom:.875rem">
        <label>Type</label>
        <select name="type" form="bulletin-form" class="select">
          @foreach([
            'alert'        => '🚨 Alert',
            'announcement' => '📢 Announcement',
            'release'      => '🚀 Release',
            'update'       => '🔄 Update',
            'maintenance'  => '🔧 Maintenance',
            'warning'      => '⚠️ Warning',
          ] as $val => $lbl)
          <option value="{{ $val }}" @selected(old('type', $bulletin->type) === $val)>{{ $lbl }}</option>
          @endforeach
        </select>
      </div>

      <div style="margin-bottom:.875rem">
        <label>Related Product</label>
        <select name="product_id" form="bulletin-form" class="select">
          <option value="">None</option>
          @foreach($products as $product)
          <option value="{{ $product->id }}" @selected(old('product_id', $bulletin->product_id) == $product->id)>{{ $product->name }}</option>
          @endforeach
        </select>
      </div>

      <label style="display:flex;align-items:center;gap:.625rem;cursor:pointer;margin:0;
        padding:.625rem .75rem;border-radius:var(--radius);background:var(--muted);border:1px solid var(--border)">
        <input type="checkbox" name="is_pinned" value="1" form="bulletin-form"
          @checked(old('is_pinned', $bulletin->is_pinned)) style="accent-color:#6366f1;width:15px;height:15px">
        <div>
          <div style="font-size:.8125rem;font-weight:500;color:var(--foreground)">Pin to top</div>
          <div style="font-size:.75rem;color:var(--muted-foreground);margin-top:1px">Shows in the Pinned section</div>
        </div>
      </label>
    </div>

    <div class="card" style="padding:1.125rem">
      @foreach([
        'Status'    => $bulletin->is_published ? 'Published' : 'Draft',
        'Views'     => number_format($bulletin->views_count),
        'Created'   => $bulletin->created_at->format('M d, Y'),
        'Published' => $bulletin->published_at?->format('M d, Y') ?? 'Not yet',
      ] as $k => $v)
      <div style="display:flex;justify-content:space-between;font-size:.8125rem;
        padding:.4rem 0;border-bottom:1px solid var(--border)">
        <span style="color:var(--muted-foreground)">{{ $k }}</span>
        <span style="font-weight:500;color:var(--foreground)">{{ $v }}</span>
      </div>
      @endforeach
    </div>

    <form method="POST" action="{{ route('admin.bulletin.destroy', $bulletin) }}"
          onsubmit="return confirm('Delete this bulletin permanently?')">
      @csrf @method('DELETE')
      <button class="btn btn-danger btn-sm" style="width:100%;justify-content:center">Delete Bulletin</button>
    </form>
  </div>
</div>

@endsection
