@extends('layouts.app')

@section('title', '계좌 수정')

@section('content')
<div class="container mx-auto px-4 py-6 max-w-2xl">
    {{-- 페이지 헤더 --}}
    <div class="mb-6">
        <a href="{{ route('finance.accounts.index') }}" class="text-sm text-gray-500 hover:text-gray-700 inline-flex items-center gap-1 mb-2">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/>
            </svg>
            계좌 목록으로
        </a>
        <h1 class="text-2xl font-bold text-gray-800">계좌 수정</h1>
        <p class="text-sm text-gray-500 mt-1">{{ $account->bank_name }} {{ $account->account_number }}</p>
    </div>

    {{-- 수정 폼 --}}
    <div class="bg-white rounded-lg shadow-sm p-6">
        <form id="accountForm"
              hx-put="{{ route('api.admin.bank-accounts.update', $account->id) }}"
              hx-headers='{"X-CSRF-TOKEN": "{{ csrf_token() }}", "Accept": "application/json"}'
              hx-target="#form-message"
              hx-swap="innerHTML"
              class="space-y-6">

            {{-- 메시지 영역 --}}
            <div id="form-message"></div>

            {{-- 은행명 --}}
            @php
                $banks = [
                    // 시중은행
                    'KB국민은행', '신한은행', '우리은행', '하나은행', 'SC제일은행', '한국씨티은행',
                    // 지방은행
                    '경남은행', '광주은행', '대구은행', '부산은행', '전북은행', '제주은행',
                    // 특수은행
                    'NH농협은행', 'IBK기업은행', 'KDB산업은행', '수협은행',
                    // 인터넷은행
                    '카카오뱅크', '케이뱅크', '토스뱅크',
                    // 외국계
                    'BNP파리바은행', 'BOA은행', 'HSBC은행', 'JP모간은행', '도이치은행',
                    // 기타
                    '새마을금고', '신협', '우체국', '저축은행', '산림조합', '기타',
                ];
                // 기존 값이 목록에 없으면 맨 앞에 추가
                $currentBank = $account->bank_name;
                $bankInList = in_array($currentBank, $banks);
            @endphp
            <div>
                <label for="bank_name" class="block text-sm font-medium text-gray-700 mb-1">
                    은행명 <span class="text-red-500">*</span>
                </label>
                <select name="bank_name" id="bank_name" required
                        class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500">
                    <option value="">선택하세요</option>
                    @if($currentBank && !$bankInList)
                        <option value="{{ $currentBank }}" selected>{{ $currentBank }}</option>
                    @endif
                    @foreach($banks as $bank)
                        <option value="{{ $bank }}" {{ $currentBank === $bank ? 'selected' : '' }}>{{ $bank }}</option>
                    @endforeach
                </select>
            </div>

            {{-- 계좌번호 --}}
            <div>
                <label for="account_number" class="block text-sm font-medium text-gray-700 mb-1">
                    계좌번호 <span class="text-red-500">*</span>
                </label>
                <input type="text" name="account_number" id="account_number" required
                       value="{{ $account->account_number }}"
                       class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500">
            </div>

            {{-- 예금주 --}}
            <div>
                <label for="account_holder" class="block text-sm font-medium text-gray-700 mb-1">
                    예금주 <span class="text-red-500">*</span>
                </label>
                <input type="text" name="account_holder" id="account_holder" required
                       value="{{ $account->account_holder }}"
                       class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500">
            </div>

            {{-- 계좌별칭 --}}
            <div>
                <label for="account_name" class="block text-sm font-medium text-gray-700 mb-1">
                    계좌별칭
                </label>
                <input type="text" name="account_name" id="account_name"
                       value="{{ $account->account_name }}"
                       placeholder="예: 주거래 계좌, 급여계좌"
                       class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500">
            </div>

            {{-- 예금종류 --}}
            <div>
                <label for="account_type" class="block text-sm font-medium text-gray-700 mb-1">
                    예금종류
                </label>
                <select name="account_type" id="account_type"
                        class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500">
                    @foreach(['보통예금', '정기예금', '적금', '법인카드 출금', '외화예금', '기타'] as $type)
                    <option value="{{ $type }}" {{ $account->account_type === $type ? 'selected' : '' }}>{{ $type }}</option>
                    @endforeach
                </select>
            </div>

            {{-- 현재 잔액 --}}
            <div>
                <label for="balance" class="block text-sm font-medium text-gray-700 mb-1">
                    현재 잔액
                </label>
                <input type="number" name="balance" id="balance"
                       value="{{ $account->balance }}" min="0" step="1"
                       class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500">
            </div>

            {{-- 개설일자 --}}
            <div>
                <label for="opened_at" class="block text-sm font-medium text-gray-700 mb-1">
                    개설일자
                </label>
                <input type="date" name="opened_at" id="opened_at"
                       value="{{ $account->opened_at?->format('Y-m-d') }}"
                       class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500">
            </div>

            {{-- 지점명 --}}
            <div>
                <label for="branch_name" class="block text-sm font-medium text-gray-700 mb-1">
                    지점명
                </label>
                <input type="text" name="branch_name" id="branch_name"
                       value="{{ $account->branch_name }}"
                       class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500">
            </div>

            {{-- 메모 --}}
            <div>
                <label for="memo" class="block text-sm font-medium text-gray-700 mb-1">
                    메모
                </label>
                <textarea name="memo" id="memo" rows="3"
                          class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500">{{ $account->memo }}</textarea>
            </div>

            {{-- 상태 --}}
            <div>
                <label for="status" class="block text-sm font-medium text-gray-700 mb-1">
                    상태
                </label>
                <select name="status" id="status"
                        class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500">
                    <option value="active" {{ $account->status === 'active' ? 'selected' : '' }}>활성</option>
                    <option value="inactive" {{ $account->status === 'inactive' ? 'selected' : '' }}>비활성</option>
                </select>
            </div>

            {{-- 버튼 --}}
            <div class="flex justify-end gap-3 pt-4 border-t">
                <a href="{{ route('finance.accounts.index') }}"
                   class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition-colors">
                    취소
                </a>
                <button type="submit"
                        class="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-colors">
                    저장
                </button>
            </div>
        </form>
    </div>
</div>
@endsection

@push('scripts')
<script>
    document.body.addEventListener('htmx:afterRequest', function(event) {
        if (event.detail.successful) {
            try {
                const response = JSON.parse(event.detail.xhr.responseText);
                if (response.success) {
                    window.location.href = '{{ route('finance.accounts.index') }}';
                }
            } catch (e) {}
        }
    });
</script>
@endpush
