@extends('admin.layouts.container')

@section('content')
    <div class="side-title">
        <h3>{{ $head ?? ''}}</h3>
    </div>

    <div class="border-item">
        <form class="form-row" method="post" onsubmit="saveForm(this)" action="{{ $action }}" enctype="multipart/form-data">
            @if($unit ?? ''  && $unit->id)
                @method('PUT')
            @endif
            @csrf

            @foreach($languages as $language)

                <div class="form-group col-lg-6">
                    <label>{{ meta('title', $language->local) ?? __('units.title') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
                    <input type="text" class="form-control {{ $errors->has('title_'.$language->local) ? 'is-invalid' : '' }}" name="title_{{ $language->local }}" placeholder="{{ meta('title', $language->local) ?? __('units.title') }}" value='{{ old("title_{$language->local}", $unit[$language->local]->title ?? "") }}'/>
                </div>
            @endforeach
            @foreach($languages as $language)
                <div class="form-group col-lg-12">
                    <label>{{ meta('description', $language->local) ?? __('units.description') }} {{ count($languages) > 1 ? "({$language->name})" : '' }} </label>
                    <textarea  class="form-control {{ $errors->has('description_'.$language->local) ? 'is-invalid' : '' }}" name="description_{{ $language->local }}" placeholder="{{ meta('description', $language->local) ?? __('units.description') }}">{{ old("description_{$language->local}", $unit[$language->local]->description ?? "") }}</textarea>
                </div>
            @endforeach

            <div class="form-group col-lg-6">
                <label>{{ __('units.user_id') }} </label>
                <select name="user_code" id="user_code" class="form-control {{ $errors->has('user_code') ? 'is-invalid' : '' }}">
                    <option value="">{{__('dashboard.select')}}</option>
                    @foreach($users as $row)
                        <option value="{{$row->code}}" {{ old('user_code', $unit->user->code ?? '') == $row->code ? 'selected' : ''}}>{{$row->name}}</option>
                    @endforeach
                </select>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ __('units.type_id') }} </label>
                <select name="type_id" id="type_id" class="form-control {{ $errors->has('type_id') ? 'is-invalid' : '' }}">
                    <option value="">{{__('dashboard.select')}}</option>
                    @foreach($types as $row)
                        <option value="{{$row->id}}" {{ old('type_id', $unit->type_id ?? '') == $row->id ? 'selected' : ''}}>{{$row->title}}</option>
                    @endforeach
                </select>
            </div>

            <div class="form-group col-lg-6">
                <label>{{ __('units.city_id') }} </label>
                <select name="city_id" id="city_id" class="form-control {{ $errors->has('city_id') ? 'is-invalid' : '' }}">
                    <option value="">{{__('dashboard.select')}}</option>
                    @foreach($cities as $row)
                        <option value="{{$row->id}}" {{ old('city_id', $unit->area->city_id ?? '') == $row->id ? 'selected' : ''}}>{{$row->title}}</option>
                    @endforeach
                </select>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ __('units.area_id') }} </label>
                <select name="area_id" id="area_id" class="form-control {{ $errors->has('area_id') ? 'is-invalid' : '' }}">
                    <option value="">{{__('dashboard.select')}}</option>
                </select>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ __('units.floor') }} </label>
                <select name="floor" id="floor" class="form-control {{ $errors->has('floor') ? 'is-invalid' : '' }}">
                    <option value="">{{__('dashboard.select')}}</option>
                    @for ($i = 1; $i <= 20; $i++)
                        <option value="{{ $i }}" {{ old('floor', $unit->floor ?? '') == $i ? 'selected' : '' }}> الدور  {{ $i }}</option>
                    @endfor
                </select>
            </div>

            <div class="form-group col-lg-6">
                <label>{{ __('units.price') }} </label>
                <input type="number" step="0.1" name="price" value="{{ old('price', $unit->price ?? '') }}" class="form-control {{ $errors->has('price') ? 'is-invalid' : '' }}" placeholder="{{ __('units.price') }}">
            </div>

            <div class="form-group col-lg-6">
                <label>{{ __('units.status') }} </label>
                <select name="status" id="status" class="form-control">
                    <option value="">{{__('dashboard.select')}}</option>
                    @foreach (config('globals.unit_statuses') as $status)
                        <option value="{{ $status }}" {{ ($unit->status ?? '') == $status ? 'selected' : '' }}>{{ __('units.statuses.'.$status) }}</option>
                    @endforeach
                </select>
            </div>

            <div class="form-group col-lg-6">
                <label>{{ __('units.sold_status') }} </label>
                <select name="sold_status" id="sold_status" class="form-control">
                    <option value="">{{__('dashboard.select')}}</option>
                    @foreach (config('globals.sold_statuses') as $sold_status)
                        <option value="{{ $sold_status }}" {{ ($unit->sold_status ?? '') == $sold_status ? 'selected' : '' }}>{{ __('units.sold_statuses.'.$sold_status) }}</option>
                    @endforeach
                </select>
            </div>

            <div class="form-group col-lg-6">
                <label>{{ __('units.baths') }} </label>
                <input type="number" name="baths" value="{{ old('baths', $unit->baths ?? '') }}" class="form-control {{ $errors->has('baths') ? 'is-invalid' : '' }}" placeholder="{{ __('units.baths') }}">
            </div>

            <div class="form-group col-lg-6">
                <label>{{ __('units.space') }} </label>
                <input type="number" step="0.1" name="space" value="{{ old('space', $unit->space ?? '') }}" class="form-control {{ $errors->has('space') ? 'is-invalid' : '' }}" placeholder="{{ __('units.space') }}">
            </div>

            <div class="form-group col-lg-6">
                <label>{{ __('units.net_space') }} </label>
                <input type="number" step="0.1" name="net_space" value="{{ old('net_space', $unit->net_space ?? '') }}" class="form-control {{ $errors->has('net_space') ? 'is-invalid' : '' }}" placeholder="{{ __('units.net_space') }}">
            </div>

            <div class="form-group col-lg-6">
                <label>{{ __('units.image') }} </label>
                <input id="old_image" class="old_image" type="hidden" name="old_image" value="{{ $unit->image ?? ""}}"/>
                <input type="file" class="form-control {{ $errors->has('image') ? 'is-invalid' : '' }}" name="image" placeholder="{{ __('units.image') }}" value='{{ old('image', '') }}' accept="image/*"/>
                @if (($unit->image ?? null) && ($unit->image_url ?? null))
                    <a href="{{ $unit->image_url }}" class="btn btn-primary" target="_new"><i class="fas fa-download"></i></a>
                @endif
            </div>
            
            <div class="border-item photos col-md-12 other_image_container">
                <div class="row" id="imageContainer">
                    @foreach ($unit['photos'] ?? [] as $photo)
                        <div class="col-md-4">
                            <div class="items">
                                <div class="img">
                                    <a href="{{ $photo->image_url}}" data-fancybox="gallery"><img src="{{ $photo->image_url}}" alt=""></a>
                                </div>
                                <input type="hidden" name="old_images[]" value="{{ $photo->image ?? "" }}">
                                <a href="#" onclick="DeleteImg(this)" class="close"><i class="fas fa-times"></i></a>
                            </div>
                        </div>
                    @endforeach
                </div>
                <div class="text-center">
                    <div class="clearfix"></div>
                    <span id="add">
                        <a class="add" id="addImage"> {{__('units.add_photos')}} </a>
                    </span>
                </div>
            </div>

            <div class="text-center mt-5 col-12">
                <button class="butn"><span>{{ __('dashboard.save') }}</span></button>
            </div>
        </form>
        {{-- must be oustide the form --}}
        <input type="file" id="fileTemplate" name="images[]" accept="image/*" style="display: none;" />
    </div>

@endsection

@section('inner_js')
<script>
    const area_route = '{{ route("dashboard.areas") }}';
    const area_id = '{{ old("area_id", $unit->area_id ?? '') }}';
</script>
<script src="{{ asset('assets/admin/js/custom/admin/units.js') }}"></script>
<script src="{{ asset('assets/admin/js/custom/admin/images_grid.js') }}"></script>
@endsection



