<script>
  function filterCategoriesByShop(shopId) {
    var $cat = $('#category_id');
    $cat.find('option').each(function() {
      var s = $(this).data('shop');
      if (!s) return;
      $(this).toggle(String(s) === String(shopId));
    });
    if ($cat.find('option:selected').length && !$cat.find('option:selected').is(':visible')) {
      $cat.val('');
    }
    $cat.trigger('change.select2');
  }
  $(document).ready(function() {
    $('.form-validate').validate();
    $('.select2').select2();
    filterCategoriesByShop($('#shop_id').val());
    $('#shop_id').on('change', function() {
      filterCategoriesByShop($(this).val());
    });
  });
</script>

