$(function () {
    // Sidebar links
    $('.sidebar .sidebar-menu li a').on('click', function () {
      const $this = $(this);
  
      if ($this.parent().hasClass('open')) {
        $this
          .parent()
          .children('.dropdown-menu')
          .slideUp(200, () => {
            $this.parent().removeClass('open');
          });
      } else {
        $this
          .parent()
          .parent()
          .children('li.open')
          .children('.dropdown-menu')
          .slideUp(200);
  
        $this
          .parent()
          .parent()
          .children('li.open')
          .children('a')
          .removeClass('open');
  
        $this
          .parent()
          .parent()
          .children('li.open')
          .removeClass('open');
  
        $this
          .parent()
          .children('.dropdown-menu')
          .slideDown(200, () => {
            $this.parent().addClass('open');
          });
      }
    });

    $('.offcanvas-toggle').click(function(e) {
        e.preventDefault();
        $('.offcanvas-menu').toggleClass('open');
    });

    // Sidebar Activity Class
    const sidebarLinks = $('.sidebar').find('.sidebar-link');
  
    sidebarLinks
      .each((index, el) => {
        $(el).removeClass('active');
      })
      .filter(function () {
        const href = $(this).attr('href');
        const pattern = href[0] === '/' ? href.substr(1) : href;
        return pattern === (window.location.pathname).substr(1);
      })
      .addClass('active');
  
    // ٍSidebar Toggle
    $('.sidebar-toggle').on('click', e => {
      var body_class = $.cookie('body_class');
      console.log(body_class);
      if(body_class == 'app is-opened')
      {
        document.body.classList.remove('is-opened');
        document.body.classList.add('is-collapsed');
        $.cookie('body_class','app is-collapsed', {expires:7,path:'/'});
      }
      else if(body_class == 'app is-collapsed')
      {
        document.body.classList.remove('is-collapsed');
        document.body.classList.add('is-opened');
        $.cookie('body_class', 'app is-opened', {expires:7, path:'/'});
      }

      // $('.app').toggleClass('is-collapsed');
      e.preventDefault();
    });
}());

var body_class = $.cookie('body_class');
if(body_class)
{
  if(body_class == 'app' || body_class == 'app is-opened' )
  {
    document.body.classList.add('app');
    document.body.classList.add('is-opened');
  }
  else if(body_class == 'app is-collapsed')
  {
     document.body.classList.add('app');
     document.body.classList.add('is-collapsed');
  }
}
else
{
  $.cookie('body_class', 'app is-collapsed', {expires:7, path: '/'});
  document.body.classList.add('app');
  document.body.classList.add('is-collapsed');
}

// function windowSize() {
//   windowWidth = window.innerWidth ? window.innerWidth : $(window).width();
//   if(windowWidth < 768) {
//     if($('.app').hasClass('is-collapsed')) {
//       $('.app').removeClass('is-collapse');
//     }
//   }
//   $('.app').addClass('is-collapsed')
// }

// windowSize();

// // Add Element to dom when resize screen
// $(window).resize(function() {
//   windowSize();
// });

// ------------------------------------------------------
// @Scrollbar
// ------------------------------------------------------

$(function () {
  const scrollables = $('.scrollable');
  if (scrollables.length > 0) {
    scrollables.each((index, el) => {
      new PerfectScrollbar(el);
    });
  }
}());

// ------------------------------------------------------
// @Navbar search
// ------------------------------------------------------
  
$(function () {
  $('.search-toggle').on('click', e => {
    $('.search-box, .search-input').toggleClass('active');
    $('.search-input input').focus();
    e.preventDefault();
  });
}());