﻿@extends('layouts.app')

@section('title', 'تعديل الخزينة')

@section('content')
<div class="container m-auto p-4 py-6">
    <div class="flex justify-between items-center mb-6">
        <h1 class="text-2l font-bold text-gray-800">تعديل الخزينة</h1>
        <a href="{{ route('treasury.treasuries.index') }}" class="bg-gray-200 hover:bg-gray-300 text-gray-700 font-bold py-2 p-4 rounded inline-flex items-center">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 ml-1" viewBox="0 0 20 20" fill="currentColor">
                <path fill-rule="evenodd" d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd" />
            </svg>
            <span>العودة إلى قائمة الخزائن</span>
        </a>
    </div>

    <div class="bg-white rounded-lg shadow-md p-6">
        <form action="{{ route('treasury.treasuries.update', $treasury->id) }}" method="POST">
            @csrf
            @method('PUT')

            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <!-- اسم الخزينة -->
                <div>
                    <label for="name" class="block text-gray-700 font-bold mb-2">اسم الخزينة <span class="text-red-600">*</span></label>
                    <input type="text" name="name" id="name" value="{{ old('name', $treasury->name) }}" required
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary-600 focus:border-transparent">
                    @error('name')
                        <p class="text-red-600 text-sm mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <!-- كود الخزينة -->
                <div>
                    <label for="code" class="block text-gray-700 font-bold mb-2">كود الخزينة <span class="text-red-600">*</span></label>
                    <input type="text" name="code" id="code" value="{{ old('code', $treasury->code) }}" required
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary-600 focus:border-transparent">
                    @error('code')
                        <p class="text-red-600 text-sm mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <!-- الفرع -->
                <div>
                    <label for="branch_id" class="block text-gray-700 font-bold mb-2">الفرع <span class="text-red-600">*</span></label>
                    <select name="branch_id" id="branch_id" required
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary-600 focus:border-transparent">
                        <option value="">اختر الفرع</option>
                        @foreach($branches as $branch)
                            <option value="{{ $branch->id }}" {{ old('branch_id', $treasury->branch_id) == $branch->id ? 'selected' : '' }}>
                                {{ $branch->name }}
                            </option>
                        @endforeach
                    </select>
                    @error('branch_id')
                        <p class="text-red-600 text-sm mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <!-- الموقع -->
                <div>
                    <label for="location" class="block text-gray-700 font-bold mb-2">الموقع</label>
                    <input type="text" name="location" id="location" value="{{ old('location', $treasury->location) }}"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary-600 focus:border-transparent">
                    @error('location')
                        <p class="text-red-600 text-sm mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <!-- المسؤول -->
                <div>
                    <label for="responsible_user_id" class="block text-gray-700 font-bold mb-2">المسؤول</label>
                    <select name="responsible_user_id" id="responsible_user_id"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary-600 focus:border-transparent">
                        <option value="">اختر المسؤول</option>
                        @foreach($users as $user)
                            <option value="{{ $user->id }}" {{ old('responsible_user_id', $treasury->responsible_user_id) == $user->id ? 'selected' : '' }}>
                                {{ $user->name }}
                            </option>
                        @endforeach
                    </select>
                    @error('responsible_user_id')
                        <p class="text-red-600 text-sm mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <!-- الرصيد الافتتاحي -->
                <div>
                    <label for="initial_balance" class="block text-gray-700 font-bold mb-2">الرصيد الافتتاحي</label>
                    <input type="number" name="initial_balance" id="initial_balance" value="{{ old('initial_balance', $treasury->initial_balance) }}" step="0.01"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary-600 focus:border-transparent">
                    @error('initial_balance')
                        <p class="text-red-600 text-sm mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <!-- الحالة -->
                <div>
                    <label for="is_active" class="block text-gray-700 font-bold mb-2">الحالة</label>
                    <select name="is_active" id="is_active"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary-600 focus:border-transparent">
                        <option value="1" {{ old('is_active', $treasury->is_active) == 1 ? 'selected' : '' }}>نشط</option>
                        <option value="0" {{ old('is_active', $treasury->is_active) == 0 ? 'selected' : '' }}>غير نشط</option>
                    </select>
                    @error('is_active')
                        <p class="text-red-600 text-sm mt-1">{{ $message }}</p>
                    @enderror
                </div>
            </div>

            <!-- الوصف -->
            <div class="mt-6">
                <label for="description" class="block text-gray-700 font-bold mb-2">الوصف</label>
                <textarea name="description" id="description" rows="3"
                    class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary-600 focus:border-transparent">{{ old('description', $treasury->description) }}</textarea>
                @error('description')
                    <p class="text-red-600 text-sm mt-1">{{ $message }}</p>
                @enderror
            </div>

            <div class="mt-6 flex justify-end">
                <button type="submit" class="bg-primary-600 hover:bg-primary-700 text-white font-bold py-2 p-4 rounded focus:outline-none focus:shadow-outline">
                    حفظ التغييرات
                </button>
            </div>
        </form>
    </div>
</div>
@endsection
