<!--
Copyright (C) 2022 Echedey López Romero <elr@disroot.org>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row mb-3">
        <h1 class="col-12 mb-0 text-center">User edition</h1>
    </div>
    @if ($errors->any())
    <div class = "row mb-3">
        <div class="col-xl-6 col-lg-8 col-md-10 col-12 mx-auto alert alert-danger">
            <div class="container">
                <div class="row mb-2">
                    <span class="col-12 text-center"><strong>Whoops!</strong> There were some problems with your input.</span>
                </div>
                <div class="row">
                    <ul class="col-12 mb-0">
                        @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            </div>
        </div>
    </div>
    @endif
    <div class="row">
        <form class="col-xl-6 col-lg-8 col-md-10 col-12 mx-auto" action="{{ route('users.update', $user->id) }}" method="POST" enctype="multipart/form-data">
            @csrf
            @method('PUT')
            <div class="container">
                <div class="row mb-3">
                    <div class="form-group col-12">
                        <label class="form-label" for="name">Name</label>
                        <input class="form-control" type="text" id="name" name="name" value="{{ old('name') ? old('name') : $user->name }}" required=""/>
                    </div>
                </div>
                <div class="row mb-3">
                    <div class="form-group col-12">
                        <label class="form-label" for="email">E-Mail</label>
                        <input class="form-control" type="email" id="email" name="email" value="{{ old('email') ? old('email') : $user->email }}" required=""/>
                    </div>
                </div>
                <div class="row mb-3">
                    <div class="form-group col-12">
                        <label class="form-label" for="password">Password</label>
                        <input class="form-control" type="password" id="password" name="password" value="{{ old('password') }}"/>
                    </div>
                </div>
                <div class="row mb-3">
                    <div class="form-group col-12">
                        <label class="form-label" for="Image">Image</label>
                        <input class="form-control" type="file" id="image" name="image"/>
                    </div>
                </div>
                <div class="row mb-3">
                    <div class="form-group col-12 px-0">
                        <div class="container">
                            <div class="row mb-2">
                                <div class="col-12 px-0">
                                    <span class="fst-normal">Previous image</span>
                                </div>
                            </div>
                            <div class="row">
                                <img src="{{ $user->image }}" class="col-xl-8 col-lg-10 col-12 img-thumbnail" alt="{{ $user->name }}"/>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row mb-3">
                    <div class="col-12">
                        <label class="form-label" for="role">Role</label>
                        <select class="form-control" id="role" name="role_id" required="">
                            <option value="">-- Choose --</option>
                            @foreach ($roles as $role)
                            @if (old('role_id') ? old('role_id') : $user->HasRole($role->name))
                            <option value="{{ $role->id }}" selected="">{{ $role->name }}</option>
                            @else
                            <option value="{{ $role->id }}">{{ $role->name }}</option>
                            @endif
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-6 col-12 mb-lg-0 mb-2">
                        <div class="d-grid gap-2">
                            <button type="submit" class="btn btn-primary">Modify</button>
                        </div>
                    </div>
                    <div class="col-lg-6 col-12">
                        <div class="d-grid gap-2">
                            <button type="reset" class="btn btn-danger">Reset</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
@endsection
