﻿@extends('layouts.app')

@section('title', \App\Helpers\TranslationHelper::transFromDatabase('Incoming Checks', __('Incoming Checks')))

@section('content')
<div class="container-fluid p-4">
    <h1 class="mt-4">{{ \App\Helpers\TranslationHelper::transFromDatabase('Incoming Checks', __('Incoming Checks')) }}</h1>
    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">{{ \App\Helpers\TranslationHelper::transFromDatabase('Dashboard', __('Dashboard')) }}</a></li>
        <li class="breadcrumb-item"><a href="{{ route('banking.checks.index') }}">{{ \App\Helpers\TranslationHelper::transFromDatabase('Checks', __('Checks')) }}</a></li>
        <li class="breadcrumb-item active">{{ \App\Helpers\TranslationHelper::transFromDatabase('Incoming Checks', __('Incoming Checks')) }}</li>
    </ol>

    <div class="card mb-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <div>
                <i class="fas fa-money-check-alt me-1"></i>
                {{ \App\Helpers\TranslationHelper::transFromDatabase('Incoming Checks List', __('Incoming Checks List')) }}
            </div>
            <div>
                @permission('banking.checks.create')
                <a href="{{ route('banking.checks.create') }}" class="btn btn-primary btn-sm">
                    <i class="fas fa-plus"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Add New Check', __('Add New Check')) }}
                </a>
                @endpermission
            </div>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-striped table-hover" id="checksTable">
                    <thead>
                        <tr>
                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Check Number', __('Check Number')) }}</th>
                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Bank', __('Bank')) }}</th>
                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Account Number', __('Account Number')) }}</th>
                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Amount', __('Amount')) }}</th>
                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Check Date', __('Check Date')) }}</th>
                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Due Date', __('Due Date')) }}</th>
                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Issuer Name', __('Issuer Name')) }}</th>
                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Status', __('Status')) }}</th>
                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Actions', __('Actions')) }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($checks as $check)
                        <tr>
                            <td>{{ $check->check_number }}</td>
                            <td>{{ $check->bankAccount->bank->name }} - {{ $check->bankAccount->account_name }}</td>
                            <td>{{ $check->bankAccount->account_number }}</td>
                            <td>{{ number_format($check->amount, 2) }} {{ $check->bankAccount->currency }}</td>
                            <td>{{ $check->check_date }}</td>
                            <td>{{ $check->due_date }}</td>
                            <td>{{ $check->issuer_name }}</td>
                            <td>
                                @if($check->status == 'pending')
                                    <span class="badge bg-warning">{{ \App\Helpers\TranslationHelper::transFromDatabase('Pending', __('Pending')) }}</span>
                                @elseif($check->status == 'deposited')
                                    <span class="badge bg-success">{{ \App\Helpers\TranslationHelper::transFromDatabase('Deposited', __('Deposited')) }}</span>
                                @elseif($check->status == 'cashed')
                                    <span class="badge bg-primary">{{ \App\Helpers\TranslationHelper::transFromDatabase('Cashed', __('Cashed')) }}</span>
                                @elseif($check->status == 'returned')
                                    <span class="badge bg-danger">{{ \App\Helpers\TranslationHelper::transFromDatabase('Returned', __('Returned')) }}</span>
                                @elseif($check->status == 'cancelled')
                                    <span class="badge bg-secondary">{{ \App\Helpers\TranslationHelper::transFromDatabase('Cancelled', __('Cancelled')) }}</span>
                                @endif
                            </td>
                            <td>
                                @permission('banking.checks.view')
                                <a href="{{ route('banking.checks.show', $check) }}" class="btn btn-info btn-sm">
                                    <i class="fas fa-eye"></i>
                                </a>
                                @endpermission
                                
                                @if($check->status == 'pending')
                                    @permission('banking.checks.edit')
                                    <a href="{{ route('banking.checks.edit', $check) }}" class="btn btn-primary btn-sm">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    @endpermission
                                    
                                    @permission('banking.checks.status')
                                    <a href="{{ route('banking.checks.deposit.form', $check) }}" class="btn btn-success btn-sm" title="إيداع">
                                        <i class="fas fa-money-bill-wave"></i>
                                    </a>
                                    @endpermission
                                    
                                    @permission('banking.checks.status')
                                    <a href="{{ route('banking.checks.cash.form', $check) }}" class="btn btn-primary btn-sm" title="صرف">
                                        <i class="fas fa-cash-register"></i>
                                    </a>
                                    @endpermission
                                    
                                    @permission('banking.checks.status')
                                    <a href="{{ route('banking.checks.return.form', $check) }}" class="btn btn-warning btn-sm" title="إرجاع">
                                        <i class="fas fa-undo"></i>
                                    </a>
                                    @endpermission
                                    
                                    @permission('banking.checks.status')
                                    <a href="{{ route('banking.checks.cancel.form', $check) }}" class="btn btn-danger btn-sm" title="إلغاء">
                                        <i class="fas fa-ban"></i>
                                    </a>
                                    @endpermission
                                    
                                    @permission('banking.checks.delete')
                                    <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('{{ \App\Helpers\TranslationHelper::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>
                                        </button>
                                    </form>
                                    @endpermission
                                @endif

                                @if($check->status == 'deposited')
                                    @permission('banking.checks.status')
                                    <a href="{{ route('banking.checks.collect.form', $check) }}" class="btn btn-info btn-sm" title="تحصيل">
                                        <i class="fas fa-check-double text-white"></i>
                                    </a>
                                    @endpermission
                                @endif
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="9" class="text-center">{{ \App\Helpers\TranslationHelper::transFromDatabase('No incoming checks', __('No incoming checks')) }}</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    $(document).ready(function() {
        $('#checksTable').DataTable({
            language: {
                url: "//cdn.datatables.net/plug-ins/1.10.25/i18n/Arabic.json"
            },
            "order": [[ 5, "asc" ]] // ترتيب حسب تاريخ الاستحقاق تصاعدياً
        });
    });
</script>
@endsection
