﻿@extends('layouts.app')

@section('title', 'كشوف الرواتب')

@section('content')
<div class="container m-auto p-4 sm:p-6 lg:p-8 py-8">
    <div class="flex justify-between items-center mb-6">
        <h1 class="text-2l font-bold text-gray-900">كشوف الرواتب</h1>
        
        @can('create', App\Models\HR\Payroll::class)
        <a href="{{ route('payrolls.create') }}" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 p-4 rounded">
            <i class="fas fa-plus ml-1"></i> إنشاء كشف راتب جديد
        </a>
        @endcan
    </div>

    @if(session('success'))
    <div class="bg-green-100 border border-green-400 text-green-700 p-4 py-3 rounded relative mb-4" role="alert">
        <span class="block sm:inline">{{ session('success') }}</span>
    </div>
    @endif

    @if(session('error'))
    <div class="bg-red-100 border border-red-400 text-red-700 p-4 py-3 rounded relative mb-4" role="alert">
        <span class="block sm:inline">{{ session('error') }}</span>
    </div>
    @endif

    <div class="bg-white shadow-md rounded-lg overflow-hidden">
        @if($payrolls->count() > 0)
        <table class="min-w-full divide-y divide-gray-200">
            <thead class="bg-gray-50">
                <tr>
                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                        رقم الكشف
                    </th>
                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                        الشهر/السنة
                    </th>
                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                        تاريخ الدفع
                    </th>
                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                        إجمالي الرواتب
                    </th>
                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                        الحالة
                    </th>
                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                        الإجراءات
                    </th>
                </tr>
            </thead>
            <tbody class="bg-white divide-y divide-gray-200">
                @foreach($payrolls as $payroll)
                <tr>
                    <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">
                        {{ $payroll->payroll_number }}
                    </td>
                    <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">
                        {{ $payroll->month }}/{{ $payroll->year }}
                    </td>
                    <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">
                        {{ $payroll->payment_date->format('Y-m-d') }}
                    </td>
                    <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">
                        {{ number_format($payroll->total_net_salary, 2) }} ريال
                    </td>
                    <td class="p-6 py-4 whitespace-nowrap">
                        @if($payroll->status == 'draft')
                        <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-yellow-100 text-yellow-800">
                            مسودة
                        </span>
                        @elseif($payroll->status == 'approved')
                        <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-blue-100 text-blue-800">
                            معتمد
                        </span>
                        @elseif($payroll->status == 'paid')
                        <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                            مدفوع
                        </span>
                        @elseif($payroll->status == 'cancelled')
                        <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-red-100 text-red-800">
                            ملغي
                        </span>
                        @endif
                    </td>
                    <td class="p-6 py-4 whitespace-nowrap text-sm font-medium">
                        <div class="flex space-y-2 space-y-reverse">
                            @can('view', $payroll)
                            <a href="{{ route('payrolls.show', $payroll) }}" class="text-blue-600 hover:text-blue-900">
                                <i class="fas fa-eye"></i>
                            </a>
                            @endcan
                            
                            @if($payroll->status == 'draft')
                                @can('update', $payroll)
                                <a href="{{ route('payrolls.edit', $payroll) }}" class="text-indigo-600 hover:text-indigo-900 mr-2">
                                    <i class="fas fa-edit"></i>
                                </a>
                                @endcan
                                
                                @can('approve', $payroll)
                                <form action="{{ route('payrolls.approve', $payroll) }}" method="POST" class="inline">
                                    @csrf
                                    <button type="submit" class="text-green-600 hover:text-green-900 mr-2" onclick="return confirm('هل أنت متأكد من اعتماد كشف الرواتب هذا؟')">
                                        <i class="fas fa-check"></i>
                                    </button>
                                </form>
                                @endcan
                                
                                @can('delete', $payroll)
                                <form action="{{ route('payrolls.destroy', $payroll) }}" method="POST" class="inline">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="text-red-600 hover:text-red-900 mr-2" onclick="return confirm('هل أنت متأكد من حذف كشف الرواتب هذا؟')">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </form>
                                @endcan
                            @endif
                            
                            @if($payroll->status == 'approved')
                                @can('markAsPaid', $payroll)
                                <form action="{{ route('payrolls.markAsPaid', $payroll) }}" method="POST" class="inline">
                                    @csrf
                                    <button type="submit" class="text-green-600 hover:text-green-900 mr-2" onclick="return confirm('هل أنت متأكد من تحديث حالة كشف الرواتب إلى مدفوع؟')">
                                        <i class="fas fa-money-bill-wave"></i>
                                    </button>
                                </form>
                                @endcan
                            @endif
                        </div>
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
        <div class="p-6 py-4">
            {{ $payrolls->links() }}
        </div>
        @else
        <div class="p-6 py-4 text-center text-gray-500">
            لا توجد كشوف رواتب حتى الآن.
        </div>
        @endif
    </div>
</div>
@endsection