<script>


$(document).ready(function() {
    var oldPrimaryCity = @json(old('primary_school_city'));
    var oldPrimaryCity2 = @json(old('primary_school2_city'));
    var oldPrimaryCity3 = @json(old('primary_school3_city'));
    var oldSecondaryCity = @json(old('secondary_school_city'));
    var oldSecondaryCity2 = @json(old('secondary_school2_city'));
    var oldSecondaryCity3 = @json(old('secondary_school3_city'));
    var oldPrimarySchool = @json(old('primary_school_school'));
    var oldPrimarySchool2 = @json(old('primary_school2_school'));
    var oldPrimarySchool3 = @json(old('primary_school3_school'));
    var oldSecondarySchool = @json(old('secondary_school_school'));
    var oldSecondarySchool2 = @json(old('secondary_school2_school'));
    var oldSecondarySchool3 = @json(old('secondary_school3_school'));
    var oldAddress2 = @json(old('address2'));
    var oldAddress3 = @json(old('address3'));
        if(oldAddress2 != null) {

            
            var oldPrimary = @json(old('address3'));
            
            var oldSecondary = @json(old('address3'));

            var cityId = oldAddress2;
            var obj = $('[name=address2]');
            var prefId = $('[name^=address1]').val();
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                type: 'POST',
                datatype: 'json',
                url: '/admin/city',
                data: {
                    prefId: prefId,
                    cityId: cityId
                }
            })
            .done(function(data){ //ajaxの通信に成功した場合
                // alert("success!");
                $(obj).next('[name^=address3]').children('option').remove();
                $(obj).next('[name^=address3]').append($('<option>').text('町村を選択').attr('value', ''));

                $('[name=primary_school_city] option').remove();
                $('[name=primary_school_city]').append($('<option>').text('町村を選択').attr('value', ''));
                $('[name=primary_school2_city] option').remove();
                $('[name=primary_school2_city]').append($('<option>').text('町村を選択').attr('value', ''));
                $('[name=primary_school3_city] option').remove();
                $('[name=primary_school3_city]').append($('<option>').text('町村を選択').attr('value', ''));
    
                $('[name=secondary_school_city] option').remove();
                $('[name=secondary_school_city]').append($('<option>').text('町村を選択').attr('value', ''));
                $('[name=secondary_school2_city] option').remove();
                $('[name=secondary_school2_city]').append($('<option>').text('町村を選択').attr('value', ''));
                $('[name=secondary_school3_city] option').remove();
                $('[name=secondary_school3_city]').append($('<option>').text('町村を選択').attr('value', ''));
                $.each(data[0], function(row, value){
                    $(obj).next('[name^=address3]').append($('<option>').text(value['name']).attr('value', value['code']));
                });
                $(obj).next('[name^=address3]').val(oldAddress3);

                $.each(data[1], function(row, value){
                    $('[name=primary_school_city]').append($('<option>').text(value['name']).attr('value', value['name']));
                    $('[name=primary_school2_city]').append($('<option>').text(value['name']).attr('value', value['name']));
                    $('[name=primary_school3_city]').append($('<option>').text(value['name']).attr('value', value['name']));
                    $('[name=secondary_school_city]').append($('<option>').text(value['name']).attr('value', value['name']))
                    $('[name=secondary_school2_city]').append($('<option>').text(value['name']).attr('value', value['name']));
                    $('[name=secondary_school3_city]').append($('<option>').text(value['name']).attr('value', value['name']));
                });

                if(oldPrimaryCity != null) {
                    $('[name=primary_school_city]').val(oldPrimaryCity);
                }
                if(oldPrimaryCity2 != null) {
                    $('[name=primary_school2_city]').val(oldPrimaryCity2);
                }
                if(oldPrimaryCity3 != null) {
                    $('[name=primary_school2_city]').val(oldPrimaryCity3);
                }

                if(oldSecondaryCity != null) {
                    $('[name=secondary_school_city]').val(oldSecondaryCity);
                }
                if(oldSecondaryCity2 != null) {
                    $('[name=secondary_school2_city]').val(oldSecondaryCity2);
                }
                if(oldSecondaryCity3 != null) {
                    $('[name=secondary_school2_city]').val(oldSecondaryCity3);
                }
                
    
            })
            .fail(function(data){ //ajaxの通信に失敗した場合
                alert("error!");
            });
        }
        if(oldPrimaryCity != null) {
            var obj = $('[name=primary_school_city]');
            var town = oldPrimaryCity;
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                type: 'POST',
                datatype: 'json',
                url: '/admin/search_e_school',
                data: {
                    town: town
                }
            })
            .done(function(data){ //ajaxの通信に成功した場合
                var obj_school = $('[name=primary_school_school]');
                $(obj_school).find('option').remove();
                $(obj_school).append($('<option>').text('小学校を選択').attr('value', ''));
                $.each(data, function(row, value){
                    $(obj_school).append($('<option>').text(value['name']).attr('value', value['code']));
                });
                $(obj_school).val(oldPrimarySchool);
            })
            .fail(function(data){ //ajaxの通信に失敗した場合
                alert("error!");
            });
        }
        if(oldPrimaryCity2 != null) {
            var obj = $('[name=primary_school2_city]');
            var town = oldPrimaryCity2;
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                type: 'POST',
                datatype: 'json',
                url: '/admin/search_e_school',
                data: {
                    town: town
                }
            })
            .done(function(data){ //ajaxの通信に成功した場合
                var obj_school = $('[name=primary_school2_school]');
                $(obj_school).find('option').remove();
                $(obj_school).append($('<option>').text('小学校を選択').attr('value', ''));
                $.each(data, function(row, value){
                    $(obj_school).append($('<option>').text(value['name']).attr('value', value['code']));
                });
                $(obj_school).val(oldPrimarySchool2);
            })
            .fail(function(data){ //ajaxの通信に失敗した場合
                alert("error!");
            });
        }
        if(oldPrimaryCity3 != null) {
            var obj = $('[name=primary_school3_city]');
            var town = oldPrimaryCity3;
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                type: 'POST',
                datatype: 'json',
                url: '/admin/search_e_school',
                data: {
                    town: town
                }
            })
            .done(function(data){ //ajaxの通信に成功した場合
                var obj_school = $('[name=primary_school3_school]');
                $(obj_school).find('option').remove();
                $(obj_school).append($('<option>').text('小学校を選択').attr('value', ''));
                $.each(data, function(row, value){
                    $(obj_school).append($('<option>').text(value['name']).attr('value', value['code']));
                });
                $(obj_school).val(oldPrimarySchool3);
            })
            .fail(function(data){ //ajaxの通信に失敗した場合
                alert("error!");
            });
        }

        if(oldSecondaryCity != null) {
            var obj = $('[name=secondary_school_city]');
            var town = oldSecondaryCity;
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                type: 'POST',
                datatype: 'json',
                url: '/admin/search_j_school',
                data: {
                    town: town
                }
            })
            .done(function(data){ //ajaxの通信に成功した場合
                var obj_school = $('[name=secondary_school_school]');
                $(obj_school).find('option').remove();
                $(obj_school).append($('<option>').text('中学校を選択').attr('value', ''));
                var selected = $('[name=secondary_school_hidden]').val();
                $.each(data, function(row, value){
                    $(obj_school).append($('<option>').text(value['name']).attr('value', value['code']));
                });
                $(obj_school).val(oldSecondarySchool);
            })
            .fail(function(data){ //ajaxの通信に失敗した場合
                alert("error!");
            });
        }
        if(oldSecondaryCity2 != null) {
            var obj = $('[name=secondary_school2_city]');
            var town = oldSecondaryCity2;
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                type: 'POST',
                datatype: 'json',
                url: '/admin/search_j_school',
                data: {
                    town: town
                }
            })
            .done(function(data){ //ajaxの通信に成功した場合
                var obj_school = $('[name=secondary_school2_school]');
                $(obj_school).find('option').remove();
                $(obj_school).append($('<option>').text('中学校を選択').attr('value', ''));
                var selected = $('[name=secondary_school2_hidden]').val();
                $.each(data, function(row, value){
                    $(obj_school).append($('<option>').text(value['name']).attr('value', value['code']));
                });
                $(obj_school).val(oldSecondarySchool2);
            })
            .fail(function(data){ //ajaxの通信に失敗した場合
                alert("error!");
            });
        }
        if(oldSecondaryCity3 != null) {
            var obj = $('[name=secondary_school3_city]');
            var town = oldSecondaryCity3;
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                type: 'POST',
                datatype: 'json',
                url: '/admin/search_j_school',
                data: {
                    town: town
                }
            })
            .done(function(data){ //ajaxの通信に成功した場合
                var obj_school = $('[name=secondary_school3_school]');
                $(obj_school).find('option').remove();
                $(obj_school).append($('<option>').text('中学校を選択').attr('value', ''));
                var selected = $('[name=secondary_school3_hidden]').val();
                $.each(data, function(row, value){
                    $(obj_school).append($('<option>').text(value['name']).attr('value', value['code']));
                });
                $(obj_school).val(oldSecondarySchool2);
            })
            .fail(function(data){ //ajaxの通信に失敗した場合
                alert("error!");
            });
        }
    });
@php
    foreach($errors->getMessages() as $key => $message) {
@endphp
        $('[name=@php echo($key) @endphp]').parents('tr').addClass('require_error');
@php
    }
@endphp
    </script>