﻿@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">تعديل فئة أصول: {{ $category->name }}</h1>
        
        <a href="{{ route('assets.categories.index') }}" class="bg-gray-500 hover:bg-gray-600 text-white font-bold py-2 p-4 rounded">
            <i class="fas fa-arrow-right ml-1"></i> العودة للقائمة
        </a>
    </div>

    @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 p-6">
        <form action="{{ route('assets.categories.update', $category) }}" method="POST">
            @csrf
            @method('PUT')
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div>
                    <label for="name" class="block text-sm font-medium text-gray-700 mb-1">اسم الفئة <span class="text-red-600">*</span></label>
                    <input type="text" name="name" id="name" value="{{ old('name', $category->name) }}" required
                        class="w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 @error('name') border-red-500 @enderror">
                    @error('name')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>
                
                <div>
                    <label for="code" class="block text-sm font-medium text-gray-700 mb-1">الرمز</label>
                    <input type="text" name="code" id="code" value="{{ old('code', $category->code) }}"
                        class="w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 @error('code') border-red-500 @enderror">
                    @error('code')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>
                
                <div>
                    <label for="depreciation_rate" class="block text-sm font-medium text-gray-700 mb-1">معدل الإهلاك (%) <span class="text-red-600">*</span></label>
                    <input type="number" name="depreciation_rate" id="depreciation_rate" value="{{ old('depreciation_rate', $category->depreciation_rate) }}" min="0" ma="100" required
                        class="w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 @error('depreciation_rate') border-red-500 @enderror">
                    @error('depreciation_rate')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>
                
                <div>
                    <label for="useful_life" class="block text-sm font-medium text-gray-700 mb-1">العمر الإنتاجي (سنوات) <span class="text-red-600">*</span></label>
                    <input type="number" name="useful_life" id="useful_life" value="{{ old('useful_life', $category->useful_life) }}" min="0" required
                        class="w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 @error('useful_life') border-red-500 @enderror">
                    @error('useful_life')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>
                
                <div>
                    <label for="accumulated_depreciation_account_id" class="block text-sm font-medium text-gray-700 mb-1">حساب مجمع الإهلاك <span class="text-red-600">*</span></label>
                    <select name="accumulated_depreciation_account_id" id="accumulated_depreciation_account_id" required
                        class="w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 @error('accumulated_depreciation_account_id') border-red-500 @enderror">
                        <option value="">-- اختر الحساب --</option>
                        @foreach($accounts as $account)
                        <option value="{{ $account->id }}" {{ old('accumulated_depreciation_account_id', $category->accumulated_depreciation_account_id) == $account->id ? 'selected' : '' }}>
                            {{ $account->name }} ({{ $account->code }})
                        </option>
                        @endforeach
                    </select>
                    @error('accumulated_depreciation_account_id')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>
                
                <div>
                    <label for="asset_account_id" class="block text-sm font-medium text-gray-700 mb-1">حساب الأصل <span class="text-red-600">*</span></label>
                    <select name="asset_account_id" id="asset_account_id" required
                        class="w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 @error('asset_account_id') border-red-500 @enderror">
                        <option value="">-- اختر الحساب --</option>
                        @foreach($accounts as $account)
                        <option value="{{ $account->id }}" {{ old('asset_account_id', $category->asset_account_id) == $account->id ? 'selected' : '' }}>
                            {{ $account->name }} ({{ $account->code }})
                        </option>
                        @endforeach
                    </select>
                    @error('asset_account_id')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>
                
                <div>
                    <label for="expense_account_id" class="block text-sm font-medium text-gray-700 mb-1">حساب مصروف الإهلاك <span class="text-red-600">*</span></label>
                    <select name="expense_account_id" id="expense_account_id" required
                        class="w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 @error('expense_account_id') border-red-500 @enderror">
                        <option value="">-- اختر الحساب --</option>
                        @foreach($accounts as $account)
                        <option value="{{ $account->id }}" {{ old('expense_account_id', $category->expense_account_id) == $account->id ? 'selected' : '' }}>
                            {{ $account->name }} ({{ $account->code }})
                        </option>
                        @endforeach
                    </select>
                    @error('expense_account_id')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>
            </div>
            
            <div class="mt-6">
                <label for="description" class="block text-sm font-medium text-gray-700 mb-1">الوصف</label>
                <textarea name="description" id="description" rows="3"
                    class="w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 @error('description') border-red-500 @enderror">{{ old('description', $category->description) }}</textarea>
                @error('description')
                <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                @enderror
            </div>
            
            <div class="mt-6 flex justify-end">
                <button type="submit" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 p-4 rounded">
                    <i class="fas fa-save ml-1"></i> حفظ التغييرات
                </button>
            </div>
        </form>
    </div>
</div>
@endsection
