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

@section("title")
    Vaskemaskiner - Opret
@endsection

@section("path")
    <a href="{{ route('washing-machines.create') }}" class="text-white">Opret Vaskemaskine</a> /
@endsection

@section("content")
    <h1 id="errormesseages" >Opret Vaskemaskine:</h1>
    <form method="post" action="{{ route("washing-machines.store") }}">
        @csrf
        <label for="name_first">Vaskemaskine Navn:</label>
        <label hidden id="error" for="errormesseages">Vaskemaskinen med den lokation findes allerede</label>
        <input type="text" name="name" id="name" max="60" placeholder="Vaskemaskine nr. 1" required>
        <label for="location_id">Lokation:</label>
        <select name="location_id" id="location_id" class="mb-2" required>
            <option disabled selected value> -- Vælg Lokation -- </option>
            @if(count($locations) > 0)
                @foreach($locations as $location)
                <option value="{{ $location->id }}">{{ $location->name }}</option>
                @endforeach
            @else
                <option disabled> Der er ingen lokationer oprettet!</option>
            @endif
        </select>
        <input type="submit" id="disable"  class="btn btn-dark text-white" value="Opret">
    </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.'
                }
            );
        });

        //Dont delete me, if the user reloads the pages with text in the text-box this code runs.
        $(document).ready(function () {
            $value = $('#name').val();
            $.ajax({
                type: 'get',
                url: '{{route('washing-machines.nameCheck')}}',
                data: {'nameCheck':$value, 'location': $('#location_id').val()},
                success:function (data) {
                    console.log(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('washing-machines.nameCheck')}}',
                data: {'nameCheck':$value, 'location': $('#location_id').val()},
                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);
                }
            });
        })

        $('#location_id').on('change', function () {
            $value = $('#name').val();
            $.ajax({
                type: 'get',
                url: '{{route('washing-machines.nameCheck')}}',
                data: {'nameCheck':$value, 'location': $('#location_id').val()},
                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
