@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">إضافة تحويل جديد بين الخزائن</h3>
                    <div class="card-tools">
                        <a href="{{ route('treasury.transfers.index') }}" class="btn btn-default btn-sm">
                            <i class="fas fa-arrow-right"></i> العودة للقائمة
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <form action="{{ route('treasury.transfers.store') }}" method="POST">
                        @csrf
                        
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="branch_id">الفرع <span class="text-danger">*</span></label>
                                    <select name="branch_id" id="branch_id" class="form-control @error('branch_id') is-invalid @enderror" required>
                                        <option value="">-- اختر الفرع --</option>
                                        @foreach($branches as $branch)
                                            <option value="{{ $branch->id }}" {{ old('branch_id') == $branch->id ? 'selected' : '' }}>
                                                {{ $branch->name }}
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('branch_id')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="from_treasury_id">من خزينة <span class="text-danger">*</span></label>
                                    <select name="from_treasury_id" id="from_treasury_id" class="form-control @error('from_treasury_id') is-invalid @enderror" required>
                                        <option value="">-- اختر الخزينة --</option>
                                        @foreach($treasuries as $treasury)
                                            <option value="{{ $treasury->id }}" {{ old('from_treasury_id') == $treasury->id ? 'selected' : '' }}>
                                                {{ $treasury->name }} ({{ $treasury->code }}) - الرصيد: {{ number_format($treasury->current_balance, 2) }}
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('from_treasury_id')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                    @enderror
                                </div>
                            </div>
                            
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="to_treasury_id">إلى خزينة <span class="text-danger">*</span></label>
                                    <select name="to_treasury_id" id="to_treasury_id" class="form-control @error('to_treasury_id') is-invalid @enderror" required>
                                        <option value="">-- اختر الخزينة --</option>
                                        @foreach($treasuries as $treasury)
                                            <option value="{{ $treasury->id }}" {{ old('to_treasury_id') == $treasury->id ? 'selected' : '' }}>
                                                {{ $treasury->name }} ({{ $treasury->code }})
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('to_treasury_id')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="reference_number">رقم التحويل <span class="text-danger">*</span></label>
                                    <input type="text" name="reference_number" id="reference_number" class="form-control @error('reference_number') is-invalid @enderror" value="{{ old('reference_number', $referenceNumber) }}" required>
                                    @error('reference_number')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                    @enderror
                                </div>
                            </div>
                            
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="amount">المبلغ <span class="text-danger">*</span></label>
                                    <input type="number" step="0.01" min="0.01" name="amount" id="amount" class="form-control @error('amount') is-invalid @enderror" value="{{ old('amount') }}" required>
                                    @error('amount')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="transfer_date">تاريخ التحويل <span class="text-danger">*</span></label>
                                    <input type="date" name="transfer_date" id="transfer_date" class="form-control @error('transfer_date') is-invalid @enderror" value="{{ old('transfer_date', date('Y-m-d')) }}" required>
                                    @error('transfer_date')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                    @enderror
                                </div>
                            </div>
                            
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="notes">المرجع</label>
                                    <input type="text" name="notes" id="notes" class="form-control @error('notes') is-invalid @enderror" value="{{ old('notes') }}">
                                    @error('notes')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label for="description">الوصف</label>
                            <textarea name="description" id="description" rows="3" class="form-control @error('description') is-invalid @enderror">{{ old('description') }}</textarea>
                            @error('description')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                        
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i> حفظ
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    $(document).ready(function() {
        // منع اختيار نفس الخزينة في الحقلين
        $('#from_treasury_id, #to_treasury_id').change(function() {
            var fromTreasuryId = $('#from_treasury_id').val();
            var toTreasuryId = $('#to_treasury_id').val();
            
            if (fromTreasuryId && toTreasuryId && fromTreasuryId === toTreasuryId) {
                alert('لا يمكن اختيار نفس الخزينة للتحويل منها وإليها');
                $(this).val('');
            }
        });
    });
</script>
@endsection
