@extends('dashboard.layout.main')
@section('breadcrumbs')
    <h2 class="content-header-title float-left mb-0">@lang('sidebar.users')</h2>
    <div class="breadcrumb-wrapper col-12">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ route('dashboard.home') }}">@lang('sidebar.dashboard')</a>
            </li>
            <li class="breadcrumb-item"><a href="#">@lang('sidebar.users_management')</a>
            </li>
            <li class="breadcrumb-item"><a href="{{ route('users.index') }}">@lang('sidebar.users')</a>
            </li>
            <li class="breadcrumb-item active">@lang('global.edit')
            </li>
        </ol>
    </div>
@endsection
@section('content')
    <section id="column-selectors">
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-content">
                        <div class="card-body">
                            <form class="form" action="{{ route('users.update', $user->id) }}" method="post" enctype="multipart/form-data">
                                @csrf
                                @method('PUT')
                                <div class="form-body">
                                    <div class="col-12 text-center mb-1 imageDiv">
                                        <input type="file" style="display: none" name="shop_image" id="defaultImageInput">
                                        <img src="{{ $user->default_image }}" class="img-fluid mb-1 rounded-sm defaultImage" style="width: 100px" alt="user_image">
                                        <br>
                                        <button type="button" class="btn btn-primary btn-sm setDefaultImage" data-input="defaultImageInput">@lang('global.change_user_image')</button>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-4 col-6">
                                            <div class="form-group">
                                                <div class="controls">
                                                    <label>@lang('global.name')</label>
                                                    <input type="text" class="form-control" name="name" placeholder="@lang('global.name')" value="{{ old('name', $user->name) }}" required>
                                                    <div class="help-block"></div>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="col-md-4 col-6">
                                            <div class="form-group">
                                                <div class="controls">
                                                    <label>@lang('global.country')</label>
                                                    <select class="form-control citySelect" name="country_id" required="" data-validation-required-message="@lang('global.this_filed_required')">
                                                        @foreach($countries as $country)
                                                            <option value="{{ $country->id }}" {{ old('country_id', $user->country_id) == $country->id ? "selected" : "" }}>
                                                                {{ $country->name }}</option>
                                                        @endforeach
                                                    </select>
                                                    <div class="help-block"></div>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="col-md-4 col-6">
                                            <fieldset class="form-group position-relative input-divider-right">
                                                <label>@lang('global.phone')</label>
                                                <input type="text"
                                                       class="form-control"
                                                       name="phone"
                                                       placeholder="@lang('global.phone_pattern')"
                                                       value="{{ old('phone', $user->phone) }}"
                                                       autocomplete="off"
                                                       required="">
                                            </fieldset>
                                        </div>

                                        <div class="col-md-4 col-6">
                                            <fieldset class="form-group position-relative input-divider-right">
                                                <label>@lang('global.email')</label>
                                                <input type="email"
                                                       class="form-control"
                                                       name="email"
                                                       placeholder="@lang('global.email')"
                                                       value="{{ old('email', $user->email) }}"
                                                       autocomplete="off">
                                            </fieldset>
                                        </div>

                                        <div class="col-md-4 col-6">
                                            <div class="form-group">
                                                <div class="controls">
                                                    <label>@lang('global.active')</label>
                                                    <select class="form-control" name="is_active" required="" data-validation-required-message="@lang('global.this_filed_required')">
                                                        <option value="1" {{ old('is_active', $user->is_active) == 1 ? "selected" : "" }}>@lang('global.active')</option>
                                                        <option value="0" {{ !is_null(old('is_active', $user->is_active)) && old('is_active', $user->is_active) == 0 ? "selected" : "" }}>@lang('global.not_active')</option>
                                                    </select>
                                                    <div class="help-block"></div>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="col-md-4 col-6">
                                            <div class="form-group">
                                                <div class="controls">
                                                    <label>@lang('global.language')</label>
                                                    <select class="form-control" name="lang" required="" data-validation-required-message="@lang('global.this_filed_required')">
                                                        <option value="ar" {{ old('lang', $user->lang) == 'ar' ? "selected" : "" }}>@lang('global.arabic')</option>
                                                        <option value="en" {{ !is_null(old('lang', $user->lang)) && old('lang', $user->lang) == 'en' ? "selected" : "" }}>@lang('global.english')</option>
                                                    </select>
                                                    <div class="help-block"></div>
                                                </div>
                                            </div>
                                        </div>

                                    </div>
                                </div>

                                <div class="col-12 text-center">
                                    <button type="submit" class="btn btn-primary mr-1 mb-1 waves-effect waves-light saveBtn">حفظ</button>
                                    <a href="{{ route('users.index') }}" class="btn btn-outline-warning mr-1 mb-1 waves-effect waves-light">الغاء</a>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

@endsection
@push('style')
    <style>
        .custom-hr{
            width: 100%;
            height: 20px;
            border-bottom: 1px solid #5BA72C;
            text-align: center;
            margin: 30px 0;
        }
        .custom-hr > span{
            font-size: 25px;
            background-color: #ffffff;
            padding: 0 20px;
        }
    </style>
@endpush
@push('scripts')
    <script>
        $(function (){

            function readURL(input) {
                if (input.files && input.files[0]) {
                    var reader = new FileReader();

                    reader.onload = function(e) {
                        $(input).closest('.imageDiv').find('img').attr('src', e.target.result);
                    };

                    reader.readAsDataURL(input.files[0]);
                }
            }

            $("#defaultImageInput, #defaultImageShopInput").change(function() {
                readURL(this);
            });

            $('.setDefaultImage').on('click', function (evt) {
                evt.preventDefault();
                let input = $(this).data('input')
                $("#"+input).click();
            })
        })
    </script>
@endpush
