(function ($) {
  "use strict";
  $(document).ready(function () {
    var ajax_url = civi_template_vars.ajax_url,
      apply_saved = civi_template_vars.apply_saved,
      not_file = civi_template_vars.not_file,
      $form_popup = $(".form-popup-apply");

    $form_popup.each(function () {
      var $btn_submit = $("#" + $(".btn-submit-apply-jobs").attr("id"));
      var $btn_popup = $(".civi-button-apply." + $(this).attr("id"));
      var apply_form = $("#" + $(this).attr("id"));
      $btn_submit.on("click", function (e) {
        e.preventDefault();
        var $this = $(this),
          emaill = apply_form.find('input[name="apply_emaill"]').val(),
          message = apply_form.find('textarea[name="apply_message"]').val(),
          phone = apply_form.find('input[name="apply_phone"]').val(),
          candidate_id = $btn_popup.data("candidate_id"),
          jobs_id = $btn_popup.data("jobs_id"),
          cv_url = apply_form.find('input[name="jobs_cv_url"]').val(),
          type_apply = apply_form.find('input[name="type_apply"]').val(),
          candidate_categories = apply_form
            .find('select[name="candidate_categories"]')
            .val(),
          candidate_dob = apply_form.find('input[name="candidate_dob"]').val(),
          candidate_current_position = apply_form
            .find('input[name="candidate_current_position"]')
            .val(),
          candidate_age = apply_form.find('select[name="candidate_age"]').val(),
          candidate_gender = apply_form
            .find('select[name="candidate_gender"]')
            .val(),
          candidate_languages = apply_form
            .find('select[name="candidate_languages"]')
            .val(),
          candidate_qualification = apply_form
            .find('select[name="candidate_qualification"]')
            .val(),
          candidate_yoe = apply_form.find('select[name="candidate_yoe"]').val();

        $.ajax({
          type: "POST",
          url: ajax_url,
          dataType: "json",
          data: {
            action: "jobs_add_to_apply",
            jobs_id: jobs_id,
            candidate_id: candidate_id,
            emaill: emaill,
            phone: phone,
            message: message,
            cv_url: cv_url,
            type_apply: type_apply,
            security: theme_vars.job_apply_nonce,

            candidate_current_position: candidate_current_position,
            candidate_categories: candidate_categories,
            candidate_dob: candidate_dob,
            candidate_age: candidate_age,
            candidate_gender: candidate_gender,
            candidate_languages: candidate_languages,
            candidate_qualification: candidate_qualification,
            candidate_yoe: candidate_yoe,
          },
          beforeSend: function () {
            $this.find(".btn-loading").fadeIn();
          },
          success: function (data) {
            if (data.success == true) {
              apply_form.find(".message_error").addClass("true");
              apply_form.find(".message_error").text(data.message);
              $(".civi-button-apply[data-jobs_id =" + jobs_id + "]").html(
                apply_saved
              );
              if (data.redirect) {
                window.location.href = data.redirect;
              } else {
                location.reload();
              }
            } else {
              $(".message_error").text(data.message);
            }
            $this.find(".btn-loading").fadeOut();
          },
        });
      });
    });
  });
})(jQuery);
