@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.deposits.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.deposits.store') }}" method="POST">
                        @csrf
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="treasury_id">الخزينة <span class="text-danger">*</span></label>
                                    <select name="treasury_id" id="treasury_id" class="select2 form-control @error('treasury_id') is-invalid @enderror" required>
                                        <option value="">-- اختر الخزينة --</option>
                                        @foreach($treasuries as $treasury)
                                            <option value="{{ $treasury->id }}" {{ old('treasury_id') == $treasury->id ? 'selected' : '' }}>
                                                {{ $treasury->name }} ({{ $treasury->code }})
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('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="branch_id">الفرع <span class="text-danger">*</span></label>
                                    <select name="branch_id" id="branch_id" class="select2 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="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="deposit_date">تاريخ الإيداع <span class="text-danger">*</span></label>
                                    <input type="date" name="deposit_date" id="deposit_date" class="form-control @error('deposit_date') is-invalid @enderror" value="{{ old('deposit_date', date('Y-m-d')) }}" required>
                                    @error('deposit_date')
                                        <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="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 class="col-md-6">
                                <div class="form-group">
                                    <label for="deposit_type">نوع الإيداع <span class="text-danger">*</span></label>
                                    <select name="deposit_type" id="deposit_type" class="select2 form-control @error('deposit_type') is-invalid @enderror" required>
                                        <option value="">-- اختر نوع الإيداع --</option>
                                        <option value="cash" {{ old('deposit_type') == 'cash' ? 'selected' : '' }}>نقداً</option>
                                        <option value="check" {{ old('deposit_type') == 'check' ? 'selected' : '' }}>شيك</option>
                                        <option value="bank_transfer" {{ old('deposit_type') == 'bank_transfer' ? 'selected' : '' }}>تحويل بنكي</option>
                                        <option value="other" {{ old('deposit_type') == 'other' ? 'selected' : '' }}>أخرى</option>
                                    </select>
                                    @error('deposit_type')
                                        <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="account_id">الحساب <span class="text-danger">*</span></label>
                                    <select name="account_id" id="account_id" class="select2 form-control @error('account_id') is-invalid @enderror" required>
                                        <option value="">-- اختر الحساب --</option>
                                        @foreach($accounts as $account)
                                            <option value="{{ $account->id }}" {{ old('account_id') == $account->id ? 'selected' : '' }}>
                                                {{ $account->name }} ({{ $account->code }})
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('account_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="source_type">مصدر الإيداع</label>
                                    <input type="text" name="source_type" id="source_type" class="form-control @error('source_type') is-invalid @enderror" value="{{ old('source_type') }}" placeholder="مثال: عميل، مشروع">
                                    @error('source_type')
                                        <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="card mt-3">
                            <div class="card-header bg-light">
                                <h5 class="mb-0">مراكز التكلفة (اختياري)</h5>
                            </div>
                            <div class="card-body">
                                <div id="cost-centers-container">
                                    <div class="cost-center-row mb-3">
                                        <div class="row">
                                            <div class="col-md-5">
                                                <label>مركز التكلفة</label>
                                                <select name="cost_centers[]" class="select2 form-control">
                                                    <option value="">-- اختر مركز التكلفة --</option>
                                                    @foreach($costCenters as $costCenter)
                                                        <option value="{{ $costCenter->id }}">{{ $costCenter->code }} - {{ $costCenter->name }}</option>
                                                    @endforeach
                                                </select>
                                            </div>
                                            <div class="col-md-3">
                                                <label>المبلغ</label>
                                                <input type="number" step="0.01" name="cost_center_amounts[]" class="form-control" placeholder="0.00">
                                            </div>
                                            <div class="col-md-3">
                                                <label>النسبة %</label>
                                                <input type="number" step="0.01" name="cost_center_percentages[]" class="form-control" placeholder="0.00">
                                            </div>
                                            <div class="col-md-1">
                                                <label>&nbsp;</label>
                                                <button type="button" class="btn btn-danger btn-block remove-cost-center" style="display:none;">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <button type="button" class="btn btn-success btn-sm" id="add-cost-center">
                                    <i class="fas fa-plus"></i> إضافة مركز تكلفة
                                </button>
                            </div>
                        </div>
                        
                        <div class="form-group mt-3">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i> حفظ
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
$(document).ready(function() {
    let costCenterRowCount = 1;

    $('#add-cost-center').on('click', function() {
        const costCenterOptions = `@foreach($costCenters as $costCenter)<option value="{{ $costCenter->id }}">{{ $costCenter->code }} - {{ $costCenter->name }}</option>@endforeach`;
        
        const newRow = `
            <div class="cost-center-row mb-3">
                <div class="row">
                    <div class="col-md-5">
                        <label>مركز التكلفة</label>
                        <select name="cost_centers[]" class="select2 form-control">
                            <option value="">-- اختر مركز التكلفة --</option>
                            ${costCenterOptions}
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label>المبلغ</label>
                        <input type="number" step="0.01" name="cost_center_amounts[]" class="form-control" placeholder="0.00">
                    </div>
                    <div class="col-md-3">
                        <label>النسبة %</label>
                        <input type="number" step="0.01" name="cost_center_percentages[]" class="form-control" placeholder="0.00">
                    </div>
                    <div class="col-md-1">
                        <label>&nbsp;</label>
                        <button type="button" class="btn btn-danger btn-block remove-cost-center">
                            <i class="fas fa-trash"></i>
                        </button>
                    </div>
                </div>
            </div>
        `;
        
        $('#cost-centers-container').append(newRow);
        costCenterRowCount++;
        updateRemoveButtons();
    });

    $(document).on('click', '.remove-cost-center', function() {
        $(this).closest('.cost-center-row').remove();
        costCenterRowCount--;
        updateRemoveButtons();
    });

    function updateRemoveButtons() {
        const rows = $('.cost-center-row');
        if (rows.length === 1) {
            rows.find('.remove-cost-center').hide();
        } else {
            rows.find('.remove-cost-center').show();
        }
    }

    updateRemoveButtons();
});
</script>
@endpush
@endsection
