﻿@extends('layouts.app')

@section('title', 'تقارير الخزينة')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">تقارير الخزينة</h3>
                </div>
                <div class="card-body">
                    <form action="{{ route('treasury.reports.index') }}" method="GET">
                        <div class="row">
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label for="treasury_id">الخزينة</label>
                                    <select name="treasury_id" id="treasury_id" class="form-control">
                                        <option value="">-- جميع الخزائن --</option>
                                        @foreach($treasuries as $treasury)
                                            <option value="{{ $treasury->id }}" {{ request('treasury_id') == $treasury->id ? 'selected' : '' }}>
                                                {{ $treasury->name }}
                                            </option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label for="transaction_type">نوع العملية</label>
                                    <select name="transaction_type" id="transaction_type" class="form-control">
                                        <option value="">-- جميع العمليات --</option>
                                        <option value="deposit" {{ request('transaction_type') == 'deposit' ? 'selected' : '' }}>إيداع</option>
                                        <option value="payment" {{ request('transaction_type') == 'payment' ? 'selected' : '' }}>صرف</option>
                                        <option value="transfer_in" {{ request('transaction_type') == 'transfer_in' ? 'selected' : '' }}>تحويل وارد</option>
                                        <option value="transfer_out" {{ request('transaction_type') == 'transfer_out' ? 'selected' : '' }}>تحويل صادر</option>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label for="from_date">من تاريخ</label>
                                    <input type="date" name="from_date" id="from_date" class="form-control" value="{{ request('from_date') }}">
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label for="to_date">إلى تاريخ</label>
                                    <input type="date" name="to_date" id="to_date" class="form-control" value="{{ request('to_date') }}">
                                </div>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-search"></i> بحث
                            </button>
                            
                            <a href="{{ route('treasury.reports.index') }}" class="btn btn-secondary">
                                <i class="fas fa-redo"></i> إعادة تعيين
                            </a>
                            
                            @if(count($transactions) > 0)
                            <a href="javascript:void(0)" onclick="window.print()" class="btn btn-info">
                                <i class="fas fa-print"></i> طباعة
                            </a>
                            <a href="{{ route('treasury.reports.export', request()->all()) }}" class="btn btn-success">
                                <i class="fas fa-file-excel"></i> تصدير إلى Excel
                            </a>
                            @endif
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
    @if(count($transactions) > 0)
    <div class="row mt-4">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">نتائج البحث</h3>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>التاريخ</th>
                                    <th>الخزينة</th>
                                    <th>نوع العملية</th>
                                    <th>المرجع</th>
                                    <th>المبلغ</th>
                                    <th>الرصيد قبل</th>
                                    <th>الرصيد بعد</th>
                                    <th>الوصف</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($transactions as $transaction)
                                <tr>
                                    <td>{{ $loop->iteration }}</td>
                                    <td>{{ $transaction->created_at->format('Y-m-d H:i') }}</td>
                                    <td>{{ $transaction->treasury->name }}</td>
                                    <td>
                                        @if($transaction->transaction_type == 'deposit')
                                            <span class="badge badge-success">إيداع</span>
                                        @elseif($transaction->transaction_type == 'payment')
                                            <span class="badge badge-danger">صرف</span>
                                        @elseif($transaction->transaction_type == 'transfer_in')
                                            <span class="badge badge-info">تحويل وارد</span>
                                        @elseif($transaction->transaction_type == 'transfer_out')
                                            <span class="badge badge-warning">تحويل صادر</span>
                                        @endif
                                    </td>
                                    <td>
                                        @if($transaction->transaction_type == 'deposit')
                                            <a href="{{ route('treasury.deposits.show', $transaction->transaction_id) }}">
                                                {{ $transaction->deposit ? $transaction->deposit->reference_number : 'غير متاح' }}
                                            </a>
                                        @elseif($transaction->transaction_type == 'payment')
                                            <a href="{{ route('treasury.payments.show', $transaction->transaction_id) }}">
                                                {{ $transaction->payment ? $transaction->payment->reference_number : 'غير متاح' }}
                                            </a>
                                        @elseif($transaction->transaction_type == 'transfer_in' || $transaction->transaction_type == 'transfer_out')
                                            <a href="{{ route('treasury.transfers.show', $transaction->transaction_id) }}">
                                                {{ $transaction->transfer ? $transaction->transfer->reference_number : 'غير متاح' }}
                                            </a>
                                        @endif
                                    </td>
                                    <td class="text-right">
                                        @if($transaction->transaction_type == 'deposit' || $transaction->transaction_type == 'transfer_in')
                                            <span class="text-success">+ {{ number_format($transaction->amount, 2) }}</span>
                                        @else
                                            <span class="text-danger">- {{ number_format($transaction->amount, 2) }}</span>
                                        @endif
                                    </td>
                                    <td class="text-right">{{ number_format($transaction->balance_before, 2) }}</td>
                                    <td class="text-right">{{ number_format($transaction->balance_after, 2) }}</td>
                                    <td>{{ $transaction->description }}</td>
                                </tr>
                                @endforeach
                            </tbody>
                            <tfoot>
                                <tr class="bg-light">
                                    <th colspan="5" class="text-right">الإجمالي</th>
                                    <th class="text-right">
                                        <span class="text-success">+ {{ number_format($totalDeposits, 2) }}</span><br>
                                        <span class="text-danger">- {{ number_format($totalPayments, 2) }}</span><br>
                                        <hr style="margin: 5p 0;">
                                        <span>{{ number_format($totalDeposits - $totalPayments, 2) }}</span>
                                    </th>
                                    <th colspan="3"></th>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                    
                    <div class="mt-4">
                        {{ $transactions->appends(request()->all())->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
    @elseif(request()->has('treasury_id') || request()->has('transaction_type') || request()->has('from_date') || request()->has('to_date'))
    <div class="row mt-4">
        <div class="col-md-12">
            <div class="alert alert-info">
                لا توجد معاملات تطابق معايير البحث.
            </div>
        </div>
    </div>
    @endif
    
    <div class="row mt-4">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">ملخص أرصدة الخزائن</h3>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>الخزينة</th>
                                    <th>الفرع</th>
                                    <th>الرصيد الافتتاحي</th>
                                    <th>الرصيد الحالي</th>
                                    <th>الإجراءات</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($treasurySummary as $treasury)
                                <tr>
                                    <td>{{ $loop->iteration }}</td>
                                    <td>{{ $treasury->name }}</td>
                                    <td>{{ $treasury->branch->name }}</td>
                                    <td class="text-right">{{ number_format($treasury->initial_balance, 2) }}</td>
                                    <td class="text-right">{{ number_format($treasury->current_balance, 2) }}</td>
                                    <td>
                                        <a href="{{ route('treasury.reports.index', ['treasury_id' => $treasury->id]) }}" class="btn btn-info btn-sm">
                                            <i class="fas fa-search"></i> عرض المعاملات
                                        </a>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                            <tfoot>
                                <tr class="bg-light">
                                    <th colspan="3" class="text-right">الإجمالي</th>
                                    <th class="text-right">{{ number_format($treasurySummary->sum('initial_balance'), 2) }}</th>
                                    <th class="text-right">{{ number_format($treasurySummary->sum('current_balance'), 2) }}</th>
                                    <th></th>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
