@extends('layouts.app')

@section('title', transFromDatabase('Check Details', __('Check Details')))

@section('content')
<div class="container-fluid p-4">
    <h1 class="mt-4">{{ transFromDatabase('Check Details', __('Check Details')) }}</h1>
    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">{{ transFromDatabase('Dashboard', __('Dashboard')) }}</a></li>
        <li class="breadcrumb-item"><a href="{{ route('banking.checks.index') }}">{{ transFromDatabase('Checks', __('Checks')) }}</a></li>
        <li class="breadcrumb-item active">{{ transFromDatabase('Check Details', __('Check Details')) }} #{{ $check->check_number }}</li>
    </ol>

    <div class="row mb-3">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <div>
                        <i class="fas fa-money-check me-1"></i>
                        {{ transFromDatabase('Check Details', __('Check Details')) }} #{{ $check->check_number }}
                    </div>
                    <div>
                        @if($check->status == 'pending')
                            @if($check->check_type == 'incoming')
                                @permission('banking.checks.status')
                                <form action="{{ route('banking.checks.deposit', $check) }}" method="POST" class="d-inline">
                                    @csrf
                                    <button type="submit" class="btn btn-success btn-sm" onclick="return confirm('{{ transFromDatabase('Are you sure you want to deposit this check?', __('Are you sure you want to deposit this check?')) }}')">
                                        <i class="fas fa-money-bill-wave"></i> {{ transFromDatabase('Deposit Check', __('Deposit Check')) }}
                                    </button>
                                </form>
                                @endpermission
                                @permission('banking.checks.status')
                                <form action="{{ route('banking.checks.cash', $check) }}" method="POST" class="d-inline">
                                    @csrf
                                    <button type="submit" class="btn btn-primary btn-sm" onclick="return confirm('{{ transFromDatabase('Are you sure you want to cash this check?', __('Are you sure you want to cash this check?')) }}')">
                                        <i class="fas fa-cash-register"></i> {{ transFromDatabase('Cash Check', __('Cash Check')) }}
                                    </button>
                                </form>
                                @endpermission
                            @endif
                            @permission('banking.checks.status')
                            <form action="{{ route('banking.checks.return', $check) }}" method="POST" class="d-inline">
                                @csrf
                                <button type="submit" class="btn btn-warning btn-sm" onclick="return confirm('{{ transFromDatabase('Are you sure you want to return this check?', __('Are you sure you want to return this check?')) }}')">
                                    <i class="fas fa-undo"></i> {{ transFromDatabase('Return Check', __('Return Check')) }}
                                </button>
                            </form>
                            @endpermission
                            @permission('banking.checks.status')
                            <form action="{{ route('banking.checks.cancel', $check) }}" method="POST" class="d-inline">
                                @csrf
                                <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('{{ transFromDatabase('Are you sure you want to cancel this check?', __('Are you sure you want to cancel this check?')) }}')">
                                    <i class="fas fa-ban"></i> {{ transFromDatabase('Cancel Check', __('Cancel Check')) }}
                                </button>
                            </form>
                            @endpermission
                        @endif
                        @permission('banking.checks.edit')
                        @if(!in_array($check->status, ['cashed', 'deposited']))
                        <a href="{{ route('banking.checks.edit', $check) }}" class="btn btn-primary btn-sm">
                            <i class="fas fa-edit"></i> {{ transFromDatabase('Edit', __('Edit')) }}
                        </a>
                        @endif
                        @endpermission
                        @permission('banking.checks.delete')
                        @if(!in_array($check->status, ['cashed', 'deposited']))
                        <form action="{{ route('banking.checks.destroy', $check) }}" method="POST" class="d-inline">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('{{ transFromDatabase('Are you sure you want to delete this check?', __('Are you sure you want to delete this check?')) }}')">
                                <i class="fas fa-trash"></i> {{ transFromDatabase('Delete', __('Delete')) }}
                            </button>
                        </form>
                        @endif
                        @endpermission
                    </div>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <table class="table table-bordered">
                                <tr>
                                    <th style="width: 30%">{{ transFromDatabase('Check Number', __('Check Number')) }}</th>
                                    <td>{{ $check->check_number }}</td>
                                </tr>
                                <tr>
                                    <th>{{ transFromDatabase('Check Type', __('Check Type')) }}</th>
                                    <td>
                                        @if($check->check_type == 'incoming')
                                            <span class="badge bg-success">{{ transFromDatabase('Incoming Check', __('Incoming Check')) }}</span>
                                        @else
                                            <span class="badge bg-primary">{{ transFromDatabase('Outgoing Check', __('Outgoing Check')) }}</span>
                                        @endif
                                    </td>
                                </tr>
                                <tr>
                                    <th>{{ transFromDatabase('Check Status', __('Check Status')) }}</th>
                                    <td>
                                        @if($check->status == 'pending')
                                            <span class="badge bg-warning">{{ transFromDatabase('Pending', __('Pending')) }}</span>
                                        @elseif($check->status == 'deposited')
                                            <span class="badge bg-success">{{ transFromDatabase('Deposited', __('Deposited')) }}</span>
                                        @elseif($check->status == 'cashed')
                                            <span class="badge bg-primary">{{ transFromDatabase('Cashed', __('Cashed')) }}</span>
                                        @elseif($check->status == 'returned')
                                            <span class="badge bg-danger">{{ transFromDatabase('Returned', __('Returned')) }}</span>
                                        @elseif($check->status == 'cancelled')
                                            <span class="badge bg-secondary">{{ transFromDatabase('Cancelled', __('Cancelled')) }}</span>
                                        @endif
                                    </td>
                                </tr>
                                <tr>
                                    <th>{{ transFromDatabase('Amount', __('Amount')) }}</th>
                                    <td>{{ number_format($check->amount, 2) }} {{ $check->bankAccount->currency }}</td>
                                </tr>
                                <tr>
                                    <th>{{ transFromDatabase('Check Date', __('Check Date')) }}</th>
                                    <td>{{ $check->check_date }}</td>
                                </tr>
                                <tr>
                                    <th>{{ transFromDatabase('Due Date', __('Due Date')) }}</th>
                                    <td>{{ $check->due_date }}</td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table class="table table-bordered">
                                <tr>
                                    <th style="width: 30%">{{ transFromDatabase('Bank', __('Bank')) }}</th>
                                    <td>{{ $check->bankAccount->bank->name }}</td>
                                </tr>
                                <tr>
                                    <th>{{ transFromDatabase('Bank Account', __('Bank Account')) }}</th>
                                    <td>{{ $check->bankAccount->account_number }}</td>
                                </tr>
                                <tr>
                                    <th>{{ transFromDatabase('Beneficiary Name', __('Beneficiary Name')) }}</th>
                                    <td>{{ $check->beneficiary_name }}</td>
                                </tr>
                                @if($check->check_type == 'incoming')
                                <tr>
                                    <th>{{ transFromDatabase('Issuer Name', __('Issuer Name')) }}</th>
                                    <td>{{ $check->issuer_name }}</td>
                                </tr>
                                <tr>
                                    <th>{{ transFromDatabase('Issuer Bank', __('Issuer Bank')) }}</th>
                                    <td>{{ $check->issuer_bank_name }}</td>
                                </tr>
                                @endif
                                <tr>
                                    <th>{{ transFromDatabase('Counterpart Account', __('Counterpart Account')) }}</th>
                                    <td>{{ $check->chartOfAccount->account_number }} - {{ $check->chartOfAccount->name }}</td>
                                </tr>
                                <tr>
                                    <th>{{ transFromDatabase('Description', __('Description')) }}</th>
                                    <td>{{ $check->description ?: transFromDatabase('None', __('None')) }}</td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    @if($check->attachment)
                    <div class="row mt-4">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header">
                                    <i class="fas fa-paperclip me-1"></i>
                                    {{ transFromDatabase('Attachments', __('Attachments')) }}
                                </div>
                                <div class="card-body">
                                    <div class="mb-3">
                                        <a href="{{ asset($check->attachment) }}" target="_blank" class="btn btn-sm btn-info">
                                            <i class="fas fa-eye"></i> {{ transFromDatabase('View Attachment', __('View Attachment')) }}
                                        </a>
                                        <a href="{{ asset($check->attachment) }}" download class="btn btn-sm btn-secondary">
                                            <i class="fas fa-download"></i> {{ transFromDatabase('Download Attachment', __('Download Attachment')) }}
                                        </a>
                                    </div>
                                    @php
                                        $extension = pathinfo($check->attachment, PATHINFO_EXTENSION);
                                    @endphp
                                    
                                    @if(in_array(strtolower($extension), ['jpg', 'jpeg', 'png', 'gif']))
                                        <img src="{{ asset($check->attachment) }}" alt="{{ transFromDatabase('Attachment', __('Attachment')) }}" class="img-fluid" style="max-height: 300px;">
                                    @elseif(strtolower($extension) == 'pdf')
                                        <div class="alert alert-info">
                                            <i class="fas fa-file-pdf"></i> {{ transFromDatabase('PDF File', __('PDF File')) }} - {{ transFromDatabase('Click on View Attachment to open the file', __('Click on View Attachment to open the file')) }}
                                        </div>
                                    @else
                                        <div class="alert alert-info">
                                            <i class="fas fa-file"></i> {{ transFromDatabase('Attachment', __('Attachment')) }} - {{ transFromDatabase('Click on Download Attachment to download the file', __('Click on Download Attachment to download the file')) }}
                                        </div>
                                    @endif
                                </div>
                            </div>
                        </div>
                    </div>
                    @endif

                    @if($check->status != 'pending')
                    <div class="row mt-4">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header">
                                    <i class="fas fa-history me-1"></i>
                                    {{ transFromDatabase('Operation History', __('Operation History')) }}
                                </div>
                                <div class="card-body">
                                    <table class="table table-bordered">
                                        <thead>
                                            <tr>
                                                <th>{{ transFromDatabase('Operation', __('Operation')) }}</th>
                                                <th>{{ transFromDatabase('Date', __('Date')) }}</th>
                                                <th>{{ transFromDatabase('By', __('By')) }}</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @if($check->status == 'deposited' && $check->deposited_at)
                                            <tr>
                                                <td>{{ transFromDatabase('Deposit Check', __('Deposit Check')) }}</td>
                                                <td>{{ $check->deposited_at }}</td>
                                                <td>{{ $check->depositedBy->name ?? transFromDatabase('Unknown', __('Unknown')) }}</td>
                                            </tr>
                                            @endif
                                            @if($check->status == 'cashed' && $check->cashed_at)
                                            <tr>
                                                <td>{{ transFromDatabase('Cash Check', __('Cash Check')) }}</td>
                                                <td>{{ $check->cashed_at }}</td>
                                                <td>{{ $check->cashedBy->name ?? transFromDatabase('Unknown', __('Unknown')) }}</td>
                                            </tr>
                                            @endif
                                            @if($check->status == 'returned' && $check->returned_at)
                                            <tr>
                                                <td>{{ transFromDatabase('Return Check', __('Return Check')) }}</td>
                                                <td>{{ $check->returned_at }}</td>
                                                <td>{{ $check->returnedBy->name ?? transFromDatabase('Unknown', __('Unknown')) }}</td>
                                            </tr>
                                            @endif
                                            @if($check->status == 'cancelled' && $check->cancelled_at)
                                            <tr>
                                                <td>{{ transFromDatabase('Cancel Check', __('Cancel Check')) }}</td>
                                                <td>{{ $check->cancelled_at }}</td>
                                                <td>{{ $check->cancelledBy->name ?? transFromDatabase('Unknown', __('Unknown')) }}</td>
                                            </tr>
                                            @endif
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
