@extends('admin.layouts.app')

@section('title', '1:1 문의 상세')
@section('page_title', '1:1 문의 상세')
@section('page_desc', '업무 배정/처리/보류/완료 및 답변 발송')

@push('head')
    <style>
        .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--warn{background:rgba(245,158,11,.88);border-color:rgba(245,158,11,.95);color:#fff;}
        .lbtn--warn:hover{background:rgba(245,158,11,.98);}
        .lbtn--ghost{background:transparent;}
        .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--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;}
        .wrap{max-width:1200px;}
        .grid{display:grid;grid-template-columns:1fr;gap:12px;}
        @media (min-width: 1100px){ .grid{grid-template-columns: 1fr 1fr;} }
        .actionsTop{display:flex;justify-content:space-between;gap:12px;flex-wrap:wrap;align-items:center;}
        .actionsRight{display:flex;gap:8px;flex-wrap:wrap;align-items:center;margin-left:auto;}
        .sectionTitle{font-weight:900;margin-bottom:8px;}
    </style>
@endpush

@section('content')
    @php
        $st = (string)$row->state;
        $stLabel = $stateLabels[$st][0] ?? $st;
        $stCls = $stateLabels[$st][1] ?? 'pill--muted';
    @endphp

    <div class="a-card wrap" style="padding:16px; margin-bottom:16px;">
        <div class="actionsTop">
            <div>
                <div class="a-muted" style="font-size:12px;">접수분류 / 처리상태</div>
                <div style="display:flex; align-items:center; gap:10px; margin-top:6px; flex-wrap:wrap;">
                    <span class="pill pill--muted">{{ $row->enquiry_code ?? '-' }}</span>
                    <span class="pill {{ $stCls }}">● {{ $stLabel }}</span>
                    <span class="mono">#{{ (int)$row->seq }} / year {{ (int)$year }}</span>
                </div>
            </div>

            <div class="actionsRight">
                <a class="lbtn lbtn--ghost" href="{{ route('admin.qna.index', request()->only(['year','enquiry_code','state','q','q_type','date_from','date_to','my_work','page'])) }}">← 목록</a>

                @if($actions['can_assign'])
                    <form method="POST" action="{{ route('admin.qna.assign',['seq'=>$row->seq,'year'=>$year]) }}">
                        @csrf
                        <button class="lbtn lbtn--primary" type="submit">내 업무로 배정</button>
                    </form>
                @endif

                @if($actions['can_start'])
                    <form method="POST" action="{{ route('admin.qna.start',['seq'=>$row->seq,'year'=>$year]) }}">
                        @csrf
                        <button class="lbtn lbtn--primary" type="submit">해당업무시작</button>
                    </form>
                @endif

                @if($actions['can_return'])
                    <form method="POST" action="{{ route('admin.qna.return',['seq'=>$row->seq,'year'=>$year]) }}" onsubmit="return confirm('업무를 반납할까요?');">
                        @csrf
                        <button class="lbtn lbtn--warn" type="submit">업무 반납하기</button>
                    </form>
                @endif

                @if($actions['can_complete'])
                    <form method="POST" action="{{ route('admin.qna.complete',['seq'=>$row->seq,'year'=>$year]) }}" onsubmit="return confirm('업무를 완료 처리할까요? (사용자에게 발송됩니다)');">
                        @csrf
                        <button class="lbtn lbtn--primary" type="submit">업무처리완료</button>
                    </form>
                @endif
            </div>
        </div>
    </div>

    <div class="wrap grid">

        <div class="a-card" style="padding:16px;">
            <div class="sectionTitle">제목</div>
            <div style="font-weight:900;">{{ $row->enquiry_title }}</div>

            <div style="margin-top:14px;">
                <div class="sectionTitle">회원정보</div>
                @if($member)
                    <div class="a-muted" style="font-size:13px; line-height:1.7;">
                        회원번호 : <b>{{ (int)$member->mem_no }}</b><br>
                        이메일 : <b>{{ $member->email }}</b><br>
                        성명 : <b>{{ $member->name }}</b><br>
                        전화번호 : <b>{{ $member->cell_phone }}</b>
                    </div>
                @else
                    <div class="a-muted">회원 정보를 찾을 수 없습니다.</div>
                @endif
            </div>

            <div style="margin-top:18px;">
                <div class="sectionTitle">내용</div>
                <div class="a-input" style="min-height:110px; white-space:pre-wrap;">{{ $row->enquiry_content }}</div>
            </div>

            <div style="margin-top:18px;">
                <div class="sectionTitle">관리자 답변</div>

                @if($actions['can_answer'])
                    <form method="POST" action="{{ route('admin.qna.answer.save',['seq'=>$row->seq,'year'=>$year]) }}">
                        @csrf
                        <textarea class="a-input" name="answer_content" rows="10" placeholder="관리자 답변을 입력해 주세요">{{ old('answer_content', $row->answer_content) }}</textarea>
                        @error('answer_content') <div class="a-error">{{ $message }}</div> @enderror

                        <div class="a-muted" style="margin-top:10px; font-size:12px;">
                            SMS답변 (업무완료 시 SMS요청 회원에게 발송됩니다)
                        </div>
                        <textarea class="a-input" name="answer_sms" rows="3" maxlength="500">{{ old('answer_sms', $row->answer_sms ?? '[핀포유] 1:1 문의 답변이 등록되었습니다. 1:1문의내역에서 확인 가능합니다. 감사합니다') }}</textarea>
                        @error('answer_sms') <div class="a-error">{{ $message }}</div> @enderror

                        <div style="margin-top:10px; display:flex; gap:8px; justify-content:flex-end;">
                            <button class="lbtn lbtn--primary" type="submit">저장</button>
                        </div>
                    </form>
                @else
                    <div class="a-input" style="min-height:110px; white-space:pre-wrap;">
                        {{ $row->answer_content ?: '등록된 답변이 없습니다.' }}
                    </div>

                    <div class="a-muted" style="margin-top:8px; font-size:12px;">
                        SMS: {{ $row->answer_sms ?: '-' }}
                    </div>
                @endif
            </div>

            @if($actions['can_postpone'])
                <div style="margin-top:18px;">
                    <div class="sectionTitle">업무 보류</div>
                    <form method="POST" action="{{ route('admin.qna.postpone',['seq'=>$row->seq,'year'=>$year]) }}">
                        @csrf
                        <div style="display:flex; gap:8px; flex-wrap:wrap;">
                            <select class="a-input" name="defer_code" style="width:160px;">
                                <option value="">보류코드(선택)</option>
                                @foreach($deferCodes as $c)
                                    <option value="{{ $c['code'] }}">{{ $c['code'] }} · {{ $c['title'] }}</option>
                                @endforeach
                            </select>
                            <input class="a-input" name="defer_comment" style="flex:1; min-width:240px;" placeholder="보류 사유를 입력해 주세요">
                            <button class="lbtn lbtn--danger" type="submit" onclick="return confirm('업무를 보류할까요?');">업무 보류하기</button>
                        </div>
                        @error('defer_comment') <div class="a-error">{{ $message }}</div> @enderror
                    </form>
                </div>
            @endif
        </div>

        <div class="a-card" style="padding:16px;">
            <div class="sectionTitle">최근 구매기록(10건)</div>
            @if($orders)
                <div style="overflow:auto;">
                    <table class="a-table">
                        <thead>
                        <tr>
                            <th>주문</th>
                            <th>상품</th>
                            <th>금액</th>
                            <th>결제일시</th>
                            <th>상태</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($orders as $o)
                            <tr>
                                <td class="a-muted">{{ $o->order_no ?? ($o->seq ?? '-') }}</td>
                                <td>{{ $o->product_name ?? '-' }}</td>
                                <td class="a-muted">{{ $o->price ?? '-' }}</td>
                                <td class="a-muted">{{ $o->regdate ?? '-' }}</td>
                                <td class="a-muted">{{ $o->stat_pay ?? '-' }}</td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
            @else
                <div class="a-muted">구매기록을 불러올 수 없습니다(테이블/컬럼 확인 필요).</div>
            @endif

            <div style="margin-top:18px;">
                <div class="sectionTitle">변경이력</div>
                @if($stateLog)
                    <div style="overflow:auto;">
                        <table class="a-table">
                            <thead>
                            <tr>
                                <th style="width:140px;">처리자</th>
                                <th style="width:80px;">전</th>
                                <th style="width:80px;">후</th>
                                <th>내용</th>
                                <th style="width:160px;">일자</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($stateLog as $lg)
                                @php
                                    $aid = (int)($lg['admin_num'] ?? 0);
                                    $a = $admins[$aid] ?? null;
                                    $before = (string)($lg['state_before'] ?? '');
                                    $after  = (string)($lg['state_after'] ?? '');
                                @endphp
                                <tr>
                                    <td class="a-muted">
                                        {{ $a ? ($a['name'].' ('.$a['email'].')') : ('admin#'.$aid) }}
                                    </td>
                                    <td class="a-muted">{{ $stateLabels[$before][0] ?? $before }}</td>
                                    <td class="a-muted">{{ $stateLabels[$after][0] ?? $after }}</td>
                                    <td>{{ $lg['why'] ?? '' }}</td>
                                    <td class="a-muted">{{ $lg['when'] ?? '' }}</td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <div class="a-muted">변경이력이 없습니다.</div>
                @endif
            </div>

            <div style="margin-top:18px;">
                <div class="sectionTitle">내부 메모</div>
                <form method="POST" action="{{ route('admin.qna.memo.add',['seq'=>$row->seq,'year'=>$year]) }}">
                    @csrf
                    @php $canMemo = (bool)($actions['canMemo'] ?? false); @endphp

                    <textarea class="a-input" name="memo" rows="3"
                              {{ $canMemo ? '' : 'disabled' }}
                              placeholder="{{ $canMemo ? '내부 메모를 입력하세요' : '업무 배정 후(내 업무일 때) 메모 작성이 가능합니다.' }}"></textarea>

                    <button class="lbtn lbtn--primary" type="submit" {{ $canMemo ? '' : 'disabled' }}>
                        메모 저장
                    </button>
                </form>

                @if($memoLog)
                    <div style="margin-top:10px; display:flex; flex-direction:column; gap:8px;">
                        @foreach($memoLog as $m)
                            @php
                                $aid = (int)($m['admin_num'] ?? 0);
                                $a = $admins[$aid] ?? null;
                            @endphp
                            <div class="a-card" style="padding:10px;">
                                <div class="a-muted" style="font-size:12px;">
                                    {{ $a ? ($a['name'].' ('.$a['email'].')') : ('admin#'.$aid) }}
                                    · {{ $m['when'] ?? '' }}
                                </div>
                                <div style="margin-top:4px; white-space:pre-wrap;">{{ $m['memo'] ?? '' }}</div>
                            </div>
                        @endforeach
                    </div>
                @endif
            </div>

        </div>

    </div>
@endsection
