<!DOCTYPE html>
<html>
<head>
  <title>CMS - {{ config('app.name') }} </title>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  <!-- CSRF Token -->
  <meta name="_token" content="{{ csrf_token() }}">
  <meta name="csrf-token" content="{{ csrf_token() }}">
  
  <link rel="shortcut icon" href="{{ asset('/favicon.ico') }}">
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">

  <!-- plugin css -->
  {!! Html::style('assets/plugins/@mdi/font/css/materialdesignicons.min.css') !!}
  {!! Html::style('assets/plugins/perfect-scrollbar/perfect-scrollbar.css') !!}
  <!-- end plugin css -->

  @stack('plugin-styles')
  <!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> -->
  
  <!-- common css -->
  {!! Html::style('css/app.css') !!}
  {!! Html::style('css/admin.css') !!}
  
  <!-- end common css -->
  

  @stack('style')

</head>
<body data-base-url="{{url('/')}}">

  <div class="container-scroller" id="app">
    @include('layout.header')
    <div class="container-fluid page-body-wrapper">
      @include('layout.sidebar')
      <div class="main-panel">
        <div class="content-wrapper">
          @yield('content')
        </div>
        @include('layout.footer')
      </div>
    </div>
  </div>
  <script>        var host = "{{URL::to('/')}}";    </script>
  
  <!-- base js -->
  {!! Html::script('js/app.js') !!}
  

  {!! Html::script('assets/plugins/perfect-scrollbar/perfect-scrollbar.min.js') !!}
  <!-- end base js -->

  <!-- plugin js -->
  @stack('plugin-scripts')
  <!-- end plugin js -->

  <!-- common js -->
  {!! Html::script('assets/js/off-canvas.js') !!}
  {!! Html::script('assets/js/hoverable-collapse.js') !!}
  {!! Html::script('assets/js/misc.js') !!}
  {!! Html::script('assets/js/settings.js') !!}
  {!! Html::script('assets/js/todolist.js') !!}
  <!-- end common js -->

  @stack('custom-scripts')
  {!! Html::script('js/admin.js') !!}

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
  <script src="{{ asset('node_modules/tinymce/tinymce.js') }}"></script>
  <script>
      tinymce.init({
          selector:'textarea.description',
          width: 900,
          height: 300,
          setup: function (editor) {
                editor.on('init change', function () {
                    editor.save();
                });
            },
            plugins: [
                "advlist autolink lists link image charmap print preview anchor",
                "searchreplace visualblocks code fullscreen",
                "insertdatetime media table contextmenu paste imagetools"
            ],
            menubar:'',
            toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | media",
            content_css: [
                '//fonts.googleapis.com/css?family=Lato:300,300i,400,400i',
                '//www.tinymce.com/css/codepen.min.css'
            ],
            image_title: true,
            automatic_uploads: true,
            relative_urls : false,
            remove_script_host : true,
            document_base_url : "/",
            convert_urls : true,
            file_picker_types: 'image',
            images_upload_handler: function (blobInfo, success, failure) {
                var xhr, formData;
                xhr = new XMLHttpRequest();
                
                var token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
                
                xhr.open('POST', '/admin/upload');
                xhr.setRequestHeader("X-CSRF-TOKEN", token);
                xhr.setRequestHeader("Accept", "application/json");

                xhr.onload = function() {
                    var json;

                    let status = xhr.status;
                    let response = xhr.responseText;
                     // Se a resposta começar com "<", significa que é HTML e não JSON
                    if (response.trim().startsWith('<')) {
                        failure("Tipo inválido de imagem ");
                        return;
                    }

                    if (xhr.status != 200) {
                        if(xhr.status === 422 )
                          failure("Tipo inválido de imagem ");
                        else
                            failure('HTTP Error: ' + xhr.status);
                        return;
                    }
                    json = JSON.parse(xhr.responseText);

                    if (!json || typeof json.location != 'string') {
                        failure('Invalid JSON: ' + xhr.responseText);
                        return;
                    }
                    success(json.location);
                };
                formData = new FormData();
                formData.append('_token', token);
                formData.append('file', blobInfo.blob(), blobInfo.filename());
                xhr.send(formData);
            }
            
      });
  </script>
  <script>
      $(function() {
        $( ".datepicker" ).datepicker();
      });
  </script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $(function() {
    
    $("#sortable tbody").sortable({ 
        cursor: "move",
        update : function () { 
          var values = [];
          $("input[name='ids[]']").each(function() {
              values.push($(this).val());
          });
          var token = $(this).data("token");
          var model = $("input[name='model']").val();
          var parent_id = $("input[name='parent_id']").val();
          $.ajax({
            type: "PUT",
            url: host+'/admin/orders',
            headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')},
            data: {ids:values, model:model, parent_id:parent_id},
            dataType: "json",
            success: function( data ) {
                
            }
        });
        } 
    }); 
    
    $( "#sortable tbody" ).disableSelection({

    });
 
      $(".sortable tbody").sortable({ 
          cursor: "move",
          update : function () { 
            var values = [];
            $("input[name='ids_items[]']").each(function() {
                values.push($(this).val());
            });
            var token = $(this).data("token");
            var model = $("input[name='model_items']").val();
            var parent_id = $("input[name='parent_id_items']").val();
            $.ajax({
              type: "PUT",
              url: host+'/admin/orders',
              headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')},
              data: {ids:values, model:model, parent_id:parent_id},
              dataType: "json",
              success: function( data ) {
                  
              }
            });
          } 
      }); 
      
    
    $( ".sortable tbody" ).disableSelection({

    });

    
  });
  </script>

<script>
  $(function() {
      $('.bt-enviar').on('click', function(e){
          var form = document.getElementById('form-busca');
          form.action = '';
          form.method = 'get';
      });
      $('.bt-exportar').on('click', function(e){
          var form = document.getElementById('form-busca');
          form.action = host+'/admin/posts/export';
          form.method = 'post';
      });
  })
</script>
</body>
</html>