@extends('admin.layouts.app')

@section('title', '공지사항 수정')
@section('page_title', '공지사항 수정')
@section('page_desc', '공지/숨김/첨부파일/링크를 관리합니다.')

@push('head')
    <style>
        /* notice edit only - match admins */
        .lbtn{padding:8px 12px;font-size:13px;border-radius:12px;line-height:1.1;text-decoration:none;display:inline-flex;align-items:center;justify-content:center;gap:6px;
            border:1px solid rgba(255,255,255,.10);background:rgba(255,255,255,.06);color:inherit;cursor:pointer;}
        .lbtn:hover{background:rgba(255,255,255,.10);text-decoration:none;}
        .lbtn--primary{background:rgba(59,130,246,.88);border-color:rgba(59,130,246,.95);color:#fff;}
        .lbtn--primary:hover{background:rgba(59,130,246,.98);}
        .lbtn--danger{background:rgba(244,63,94,.88);border-color:rgba(244,63,94,.95);color:#fff;}
        .lbtn--danger:hover{background:rgba(244,63,94,.98);}
        .lbtn--ghost{background:transparent;}
        .lbtn--sm{padding:7px 10px;font-size:12px;border-radius:11px;}
        .lbtn--wide{padding:10px 14px;font-weight:800;}

        .pill{display:inline-flex;align-items:center;gap:6px;padding:6px 10px;border-radius:999px;font-size:12px;
            border:1px solid rgba(255,255,255,.10);background:rgba(255,255,255,.06);}
        .pill--ok{border-color:rgba(34,197,94,.35);background:rgba(34,197,94,.12);}
        .pill--bad{border-color:rgba(244,63,94,.35);background:rgba(244,63,94,.10);}
        .pill--warn{border-color:rgba(245,158,11,.35);background:rgba(245,158,11,.12);}
        .pill--muted{opacity:.9;}

        .mono{padding:4px 8px;border-radius:10px;background:rgba(255,255,255,.06);border:1px solid rgba(255,255,255,.10);
            font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;font-size:12px;}

        .actions{position:sticky;bottom:10px;z-index:5;margin-top:12px;
            display:flex;justify-content:space-between;gap:12px;flex-wrap:wrap;align-items:center;
            padding:12px;border-radius:16px;border:1px solid rgba(255,255,255,.10);background:rgba(0,0,0,.25);backdrop-filter:blur(10px);}
        .actions__right{display:flex;gap:8px;flex-wrap:wrap;align-items:center;}
        .wrap{max-width:980px;}
        .checks{display:flex;flex-wrap:wrap;gap:10px;}
    </style>
@endpush

@section('content')
    @php
        $isHidden = (($row->hiding ?? 'N') === 'Y');
        $isPinned = ((int)($row->first_sign ?? 0) > 0);
        $meta = '#'.((int)($row->seq ?? 0)).' / reg: '.($row->regdate ?? '-').' / hit: '.($row->hit ?? 0);
    @endphp

    {{-- 상단 요약 --}}
    <div class="a-card" style="padding:16px; margin-bottom:16px;">
        <div style="display:flex; justify-content:space-between; align-items:flex-start; gap:12px; flex-wrap:wrap;">
            <div>
                <div style="font-weight:900; font-size:16px;">공지사항 수정</div>
                <div class="a-muted" style="font-size:12px; margin-top:4px;">
                    {{ $meta }}
                    <span style="margin-left:8px;">
                        @if($isPinned) <span class="pill pill--ok">● 공지</span> @endif
                        @if($isHidden) <span class="pill pill--bad">● 숨김</span> @endif
                    </span>
                </div>
            </div>

            <a class="lbtn lbtn--ghost lbtn--sm"
               href="{{ route('admin.notice.index', request()->only(['field','q','page'])) }}">
                ← 목록
            </a>
        </div>
    </div>

    {{-- 수정 폼 --}}
    <form id="noticeEditForm"
          method="POST"
          action="{{ route('admin.notice.update', ['id'=>(int)$row->seq] + request()->only(['field','q','page'])) }}"
          enctype="multipart/form-data"
          onsubmit="this.querySelector('button[type=submit][data-submit=save]')?.setAttribute('disabled','disabled');">
        @csrf
        @method('PUT')

        <div class="a-card wrap" style="padding:16px;">
            <div class="a-field">
                <label class="a-label">옵션</label>
                <div class="checks">
                    <label class="a-check" style="margin:0;">
                        <input type="checkbox" name="first_sign" value="1" {{ (int)old('first_sign', $row->first_sign) > 0 ? 'checked' : '' }}>
                        <span>공지(게시물 상위노출)</span>
                    </label>

                    <label class="a-check" style="margin:0;">
                        <input type="checkbox" name="hiding" value="1" {{ old('hiding', $isHidden) ? 'checked' : '' }}>
                        <span>숨김</span>
                    </label>
                </div>
            </div>

            <div class="a-field">
                <label class="a-label">제목</label>
                <input class="a-input" name="subject" value="{{ old('subject', $row->subject) }}" maxlength="80" placeholder="제목을 입력하세요">
                @error('subject') <div class="a-error">{{ $message }}</div> @enderror
            </div>

            <div class="a-field">
                <label class="a-label">내용</label>
                <textarea class="a-input" name="content" rows="12" placeholder="내용을 입력하세요">{{ old('content', $row->content) }}</textarea>
                @error('content') <div class="a-error">{{ $message }}</div> @enderror
            </div>

            <div class="a-field">
                <label class="a-label"># 링크1</label>
                <input class="a-input" name="link_01" value="{{ old('link_01', $row->link_01) }}" maxlength="200" placeholder="https://">
                @error('link_01') <div class="a-error">{{ $message }}</div> @enderror
            </div>

            <div class="a-field">
                <label class="a-label"># 링크2</label>
                <input class="a-input" name="link_02" value="{{ old('link_02', $row->link_02) }}" maxlength="200" placeholder="https://">
                @error('link_02') <div class="a-error">{{ $message }}</div> @enderror
            </div>

            <div class="a-field">
                <label class="a-label"># 첨부파일1</label>
                <input class="a-input" type="file" name="file_01">
                @if(($row->file_01 ?? '') !== '')
                    <div style="margin-top:8px; display:flex; gap:8px; align-items:center; flex-wrap:wrap;">
                        <span class="a-muted" style="font-size:12px;">현재:</span>
                        <a class="lbtn lbtn--ghost lbtn--sm"
                           href="{{ route('admin.notice.file', ['id'=>(int)$row->seq, 'slot'=>1]) }}">
                            다운로드
                        </a>
                        <span class="mono">{{ $row->file_01 }}</span>
                    </div>
                @endif
                @error('file_01') <div class="a-error">{{ $message }}</div> @enderror
            </div>

            <div class="a-field">
                <label class="a-label"># 첨부파일2</label>
                <input class="a-input" type="file" name="file_02">
                @if(($row->file_02 ?? '') !== '')
                    <div style="margin-top:8px; display:flex; gap:8px; align-items:center; flex-wrap:wrap;">
                        <span class="a-muted" style="font-size:12px;">현재:</span>
                        <a class="lbtn lbtn--ghost lbtn--sm"
                           href="{{ route('admin.notice.file', ['id'=>(int)$row->seq, 'slot'=>2]) }}">
                            다운로드
                        </a>
                        <span class="mono">{{ $row->file_02 }}</span>
                    </div>
                @endif
                @error('file_02') <div class="a-error">{{ $message }}</div> @enderror
            </div>
        </div>
    </form>

    {{-- 하단 액션바 --}}
    <div class="actions">
        <a class="lbtn lbtn--ghost"
           href="{{ route('admin.notice.index', request()->only(['field','q','page'])) }}">
            ← 뒤로가기
        </a>

        <div class="actions__right">
            <form method="POST"
                  action="{{ route('admin.notice.destroy', ['id'=>(int)$row->seq]) }}"
                  style="display:inline;"
                  onsubmit="return confirm('이 공지사항을 삭제할까요?');">
                @csrf
                @method('DELETE')
                <button class="lbtn lbtn--danger" type="submit">삭제</button>
            </form>

            <button class="lbtn lbtn--primary lbtn--wide"
                    form="noticeEditForm"
                    type="submit"
                    data-submit="save">
                저장
            </button>
        </div>
    </div>
@endsection
