@extends("admin.layout.base")
@extends("admin.layout.header")

@section("title")
    Rolle - Rediger
@endsection

@section("path")
    <a href="{{ route('roles.edit', ['role' => $role]) }}" class="text-white">Rediger Rolle</a> /
@endsection

@section("content")
    <h1>Rediger Rolle:</h1>
    <form method="post" action="{{ route("roles.update", ['role' => $role]) }}">
        @csrf
        @method("put")
        <label for="name">Navn:</label>
        <label hidden id="error" for="errormesseages">Rolle navnet findes allerede</label>
            <input type="text" name="name" id="name" placeholder="Admin" value="{{ $role->name }}" required>
        <label for="name">Beskrivelse:</label>
            <input type="text" name="description" id="description" placeholder="Admin rollen bruges til administratorene" value="{{ $role->description }}" required>
        <input type="submit" id="disable" class="btn btn-dark text-white" value="Rediger">
    </form>
@endsection
@section("scripts")
    <script>
        $(function() {
            $('form').areYouSure(
                {
                    message: 'It looks like you have been editing something. '
                        + 'If you leave before saving, your changes will be lost.'
                }
            );
        });

        $(document).ready(function () {
            $value = $('#name').val();
            $.ajax({
                type: 'get',
                url: '{{route('roles.nameCheckUpdate')}}',
                data: {'nameCheck':$value, 'id': {{$role->id}}},
                success:function (data) {
                    if(data){
                        $("#error").show(100);
                        $("#error").css('color', 'red');
                        $("#disable").prop('disabled', true);
                        $('#disable').css('cursor','not-allowed');
                    }else{
                        $("#error").hide();
                        $("#disable").prop('disabled', false);
                        $('#disable').css('cursor','pointer');
                    }

                },
                error:function (data) {
                    console.log(data);
                }
            });
        });

        $('#name').on('keyup', function () {
            $value = $(this).val();
            $.ajax({
                type: 'get',
                url: '{{route('roles.nameCheckUpdate')}}',
                data: {'nameCheck':$value, 'id': {{$role->id}}},
                success:function (data) {
                    if(data){
                        $("#error").show(100);
                        $("#error").css('color', 'red');
                        $("#disable").prop('disabled', true);
                        $('#disable').css('cursor','not-allowed');
                    }else{
                        $("#error").hide();
                        $("#disable").prop('disabled', false);
                        $('#disable').css('cursor','pointer');
                    }

                },
                error:function (data) {
                    console.log(data);
                }
            });
        })
    </script>
@endsection
