<script src="/admin/app-assets/vendors/js/forms/select/select2.full.min.js"></script>
<script src="/admin/app-assets/js/scripts/forms/select/form-select2.js"></script>
<script>
  $(document).ready(function () {
    $(".select2").select2({
      closeOnSelect: true,
    });
  });

  const translations = {
    add: "<%= i18n.__('common.add') %>",
    edit: "<%= i18n.__('common.edit') %>",
  };

  function shared(e, action, formElement) {
    e.preventDefault();

    const confirm = translations[action];

    const cityAr = String($('input[name="ar"]').val());

    const cityEn = String($('input[name="en"]').val());

    if (!cityAr.trim().length || !cityEn.trim().length) {
      $(formElement).removeClass("was-validated");
    } else {
      $(formElement).addClass("was-validated");
    }

    if (cityAr.trim().length && cityEn.trim().length) {
      ajaxUpload($(formElement), {}, "uploadWithoutImage", confirm);
    }
  }

  $(document).ready(function () {
    $(document).on("submit", "#addCity", function (e) {
      shared(e, "add", this);
    });

    $(document).on("submit", "#editCity", function (e) {
      shared(e, "edit", this);
    });
  });
</script>
