@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">تفاصيل سند الصرف #{{ $payment->id }}</h3>
                    <div class="card-tools">
                        <a href="{{ route('treasury.payments.print', $payment->id) }}" target="_blank" class="btn btn-info btn-sm">
                            <i class="fas fa-print"></i> طباعة السند
                        </a>
                        <a href="{{ route('treasury.payments.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>{{ $payment->id }}</td>
                                </tr>
                                <tr>
                                    <th style="width: 30%">رقم المرجع</th>
                                    <td>{{ $payment->reference_number }}</td>
                                </tr>
                                <tr>
                                    <th>الخزينة</th>
                                    <td>{{ $payment->treasury->name }}</td>
                                </tr>
                                <tr>
                                    <th>المبلغ</th>
                                    <td>{{ number_format($payment->amount, 2) }}</td>
                                </tr>
                                <tr>
                                    <th>تاريخ الصرف</th>
                                    <td>{{ $payment->payment_date->format('Y-m-d') }}</td>
                                </tr>
                                <tr>
                                    <th>نوع المستفيد</th>
                                    <td>{{ $payment->beneficiary_type ?? 'N/A' }}</td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table class="table table-bordered">
                                <tr>
                                    <th style="width: 30%">الحساب</th>
                                    <td>{{ $payment->account ? $payment->account->name : 'غير محدد' }}</td>
                                </tr>
                                <tr>
                                    <th>نوع الصرف</th>
                                    <td>
                                        @if($payment->payment_type == 'cash')
                                            نقدي
                                        @elseif($payment->payment_type == 'cheque')
                                            شيك
                                        @elseif($payment->payment_type == 'transfer')
                                            تحويل بنكي
                                        @else
                                            {{ $payment->payment_type }}
                                        @endif
                                    </td>
                                </tr>
                                <tr>
                                    <th>الفرع</th>
                                    <td>{{ $payment->branch ? $payment->branch->name : 'غير محدد' }}</td>
                                </tr>
                                <tr>
                                    <th>الحالة</th>
                                    <td>
                                        @if($payment->status == 'pending')
                                            <span class="badge badge-warning">قيد الانتظار</span>
                                        @elseif($payment->status == 'approved')
                                            <span class="badge badge-success">معتمد</span>
                                        @elseif($payment->status == 'rejected')
                                            <span class="badge badge-danger">مرفوض</span>
                                        @endif
                                    </td>
                                </tr>
                                <tr>
                                    <th>تاريخ الإنشاء</th>
                                    <td>{{ $payment->created_at->format('Y-m-d H:i') }}</td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    @if($payment->expense_category || $payment->expense_type || $payment->cost_center_id || $payment->cash_account_id)
                    <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="row">
                                        <div class="col-md-6">
                                            <table class="table table-bordered">
                                                <tr>
                                                    <th style="width: 40%">فئة المصروف</th>
                                                    <td>
                                                        @if($payment->expense_category == 'operating_expense')
                                                            <span class="badge badge-primary">مصروف تشغيلي</span>
                                                        @elseif($payment->expense_category == 'advance')
                                                            <span class="badge badge-info">سلفة</span>
                                                        @elseif($payment->expense_category == 'salary')
                                                            <span class="badge badge-warning">راتب</span>
                                                        @elseif($payment->expense_category)
                                                            <span class="badge badge-secondary">{{ $payment->expense_category }}</span>
                                                        @else
                                                            <span class="text-muted">غير محدد</span>
                                                        @endif
                                                    </td>
                                                </tr>
                                                @if($payment->expense_type)
                                                <tr>
                                                    <th>نوع المصروف</th>
                                                    <td>
                                                        @if($payment->expense_type == 'salary')
                                                            أجور (510102)
                                                        @elseif($payment->expense_type == 'contractor')
                                                            مقاولين (510103)
                                                        @elseif($payment->expense_type == 'equipment')
                                                            معدات (510104)
                                                        @elseif($payment->expense_type == 'direct_operating')
                                                            مصروفات تشغيلية مباشرة (510105)
                                                        @else
                                                            {{ $payment->expense_type }}
                                                        @endif
                                                    </td>
                                                </tr>
                                                @endif
                                            </table>
                                        </div>
                                        <div class="col-md-6">
                                            <table class="table table-bordered">
                                                @if($payment->costCenter)
                                                <tr>
                                                    <th style="width: 40%">مركز التكلفة</th>
                                                    <td>
                                                        <a href="{{ route('accounting.cost-centers.show', $payment->costCenter->id) }}">
                                                            {{ $payment->costCenter->code }} - {{ $payment->costCenter->name }}
                                                        </a>
                                                    </td>
                                                </tr>
                                                @endif
                                                @if($payment->cashAccount)
                                                <tr>
                                                    <th>حساب الصندوق/البنك</th>
                                                    <td>
                                                        <a href="{{ route('accounting.chart-of-accounts.show', $payment->cashAccount->id) }}">
                                                            {{ $payment->cashAccount->code }} - {{ $payment->cashAccount->name }}
                                                        </a>
                                                    </td>
                                                </tr>
                                                @endif
                                            </table>
                                        </div>
                                    </div>
                                </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>
                                <div class="card-body">
                                    {{ $payment->description ?: 'لا يوجد وصف' }}
                                </div>
                            </div>
                        </div>
                    </div>

                    @if($payment->costCenters && $payment->costCenters->count() > 0)
                    <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">
                                    <table class="table table-bordered">
                                        <thead>
                                            <tr>
                                                <th>الكود</th>
                                                <th>الاسم</th>
                                                <th>المبلغ</th>
                                                <th>النسبة %</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach($payment->costCenters as $costCenter)
                                            <tr>
                                                <td>{{ $costCenter->code }}</td>
                                                <td>{{ $costCenter->name }}</td>
                                                <td>{{ $costCenter->pivot->amount ? number_format($costCenter->pivot->amount, 2) : '-' }}</td>
                                                <td>{{ $costCenter->pivot->percentage ? number_format($costCenter->pivot->percentage, 2) . '%' : '-' }}</td>
                                            </tr>
                                            @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    @endif

                    @if($payment->status == 'pending')
                    <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.payment.edit')
                                        <a href="{{ route('treasury.payments.edit', $payment->id) }}" class="btn btn-primary">
                                            <i class="fas fa-edit"></i> تعديل
                                        </a>
                                        @endcan
                                        
                                        @can('treasury.payment.approve')
                                        <form action="{{ route('treasury.payments.approve', $payment->id) }}" method="POST" style="display: inline;">
                                            @csrf
                                            <button type="submit" class="btn btn-success" onclick="return confirm('هل أنت متأكد من اعتماد هذا السند؟')">
                                                <i class="fas fa-check"></i> اعتماد
                                            </button>
                                        </form>
                                        
                                        <form action="{{ route('treasury.payments.reject', $payment->id) }}" method="POST" style="display: inline;">
                                            @csrf
                                            <button type="submit" class="btn btn-danger" onclick="return confirm('هل أنت متأكد من رفض هذا السند؟')">
                                                <i class="fas fa-times"></i> رفض
                                            </button>
                                        </form>
                                        @endcan
                                        
                                        @can('treasury.payment.delete')
                                        <form action="{{ route('treasury.payments.destroy', $payment->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>
                    @endif

                    @if($payment->status == 'approved' && $payment->journal_entry_id)
                    <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">
                                    <a href="{{ route('accounting.journal-entries.show', $payment->journal_entry_id) }}" class="btn btn-info">
                                        <i class="fas fa-eye"></i> عرض القيد المحاسبي
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
