@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">تفاصيل الخزينة: {{ $treasury->name }}</h3>
                    <div class="card-tools">
                        <a href="{{ route('treasury.treasuries.index') }}" class="btn btn-default btn-sm">
                            <i class="fas fa-arrow-right"></i> العودة للقائمة
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <table class="table table-bordered">
                                <tr>
                                    <th style="width: 30%">اسم الخزينة</th>
                                    <td>{{ $treasury->name }}</td>
                                </tr>
                                <tr>
                                    <th>الكود</th>
                                    <td>{{ $treasury->code }}</td>
                                </tr>
                                <tr>
                                    <th>الفرع</th>
                                    <td>{{ $treasury->branch->name }}</td>
                                </tr>
                                <tr>
                                    <th>الموقع</th>
                                    <td>{{ $treasury->location ?: 'غير محدد' }}</td>
                                </tr>
                                <tr>
                                    <th>المسؤول</th>
                                    <td>{{ $treasury->responsibleUser ? $treasury->responsibleUser->name : 'غير محدد' }}</td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table class="table table-bordered">
                                <tr>
                                    <th style="width: 30%">الرصيد الافتتاحي</th>
                                    <td>{{ number_format($treasury->initial_balance, 2) }}</td>
                                </tr>
                                <tr>
                                    <th>الرصيد الحالي</th>
                                    <td>{{ number_format($treasury->current_balance, 2) }}</td>
                                </tr>
                                <tr>
                                    <th>الحالة</th>
                                    <td>
                                        @if($treasury->is_active)
                                            <span class="badge badge-success">نشط</span>
                                        @else
                                            <span class="badge badge-danger">غير نشط</span>
                                        @endif
                                    </td>
                                </tr>
                                <tr>
                                    <th>تاريخ الإنشاء</th>
                                    <td>{{ $treasury->created_at->format('Y-m-d') }}</td>
                                </tr>
                                <tr>
                                    <th>آخر تحديث</th>
                                    <td>{{ $treasury->updated_at->format('Y-m-d') }}</td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    @if($treasury->description)
                    <div class="row mt-4">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title">الوصف</h4>
                                </div>
                                <div class="card-body">
                                    {{ $treasury->description }}
                                </div>
                            </div>
                        </div>
                    </div>
                    @endif

                    <div class="row mt-4">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title">آخر المعاملات</h4>
                                    <div class="card-tools">
                                        <a href="{{ route('treasury.reports.index', ['treasury_id' => $treasury->id]) }}" class="btn btn-info btn-sm">
                                            <i class="fas fa-search"></i> عرض جميع المعاملات
                                        </a>
                                    </div>
                                </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>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @forelse($transactions as $transaction)
                                                <tr>
                                                    <td>{{ $loop->iteration }}</td>
                                                    <td>{{ $transaction->created_at->format('Y-m-d H:i') }}</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) }}">
                                                                عرض الإيداع
                                                            </a>
                                                        @elseif($transaction->transaction_type == 'payment')
                                                            <a href="{{ route('treasury.payments.show', $transaction->transaction_id) }}">
                                                                عرض سند الصرف
                                                            </a>
                                                        @elseif($transaction->transaction_type == 'transfer_in' || $transaction->transaction_type == 'transfer_out')
                                                            <a href="{{ route('treasury.transfers.show', $transaction->transaction_id) }}">
                                                                عرض التحويل
                                                            </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>
                                                @empty
                                                <tr>
                                                    <td colspan="8" class="text-center">لا توجد معاملات</td>
                                                </tr>
                                                @endforelse
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row mt-4">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title">الإجراءات</h4>
                                </div>
                                <div class="card-body">
                                    <div class="btn-group">
                                        @can('treasury.edit')
                                        <a href="{{ route('treasury.treasuries.edit', $treasury->id) }}" class="btn btn-primary">
                                            <i class="fas fa-edit"></i> تعديل
                                        </a>
                                        @endcan
                                        
                                        @can('treasury.deposit.create')
                                        <a href="{{ route('treasury.deposits.create', ['treasury_id' => $treasury->id]) }}" class="btn btn-success">
                                            <i class="fas fa-plus"></i> إضافة إيداع
                                        </a>
                                        @endcan
                                        
                                        @can('treasury.payment.create')
                                        <a href="{{ route('treasury.payments.create', ['treasury_id' => $treasury->id]) }}" class="btn btn-danger">
                                            <i class="fas fa-minus"></i> إضافة سند صرف
                                        </a>
                                        @endcan
                                        
                                        @can('treasury.transfer.create')
                                        <a href="{{ route('treasury.transfers.create', ['from_treasury_id' => $treasury->id]) }}" class="btn btn-info">
                                            <i class="fas fa-echange-alt"></i> تحويل
                                        </a>
                                        @endcan
                                        
                                        @can('treasury.delete')
                                        <form action="{{ route('treasury.treasuries.destroy', $treasury->id) }}" method="POST" style="display: inline;">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-danger" onclick="return confirm('هل أنت متأكد من حذف هذه الخزينة؟')">
                                                <i class="fas fa-trash"></i> حذف
                                            </button>
                                        </form>
                                        @endcan
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
