// JavaScript Document

jQuery(document).ready(function ($) {
  $("body").on("click", "#wpstv-trigger-import", function (e) {
    e.preventDefault();
    var urlImport = $("#rovidx_smart_tv_roku_dp_json").val();
    var importType = $("#rovidx_smart_tv_import_type").val();
    var importData = wpstv_import_data(urlImport, importType);
  });

  function wpstv_import_data(url, importType) {
    var dataPack = {
      action: "rovidx_start_json_import",
      in_url: url,
      in_type: importType,
      nonce: typeof wpstvdata !== "undefined" ? wpstvdata.nonce : "",
    };

    $.ajax({
      url: wpstvdata.ajax_url,
      type: "post",
      data: dataPack,
      beforeSend: function () {
        $("#r-import-console").html(
          '<i class="fas fa-spinner fa-pulse"></i> <strong>Import:</strong> This may take a minute if you have a lot of content!'
        );
      },
      cache: false,
      success: function (res) {
        if (res && res.success) {
          var out = "";
          $.each(res.data, function (indx, val) {
            out += "Imported " + val + " in to " + indx + "<br>";
          });
          $("#r-import-console").html(out);
        } else if (res && res.data && res.data.message) {
          $("#r-import-console").html('<span class="error">' + res.data.message + "</span>");
        } else {
          $("#r-import-console").html('<span class="error">Unexpected response.</span>');
        }
      },
      error: function (xhr) {
        var msg = "Request failed.";
        try {
          var json = JSON.parse(xhr.responseText);
          if (json && json.data && json.data.message) {
            msg = json.data.message;
          }
        } catch (e) {}
        $("#r-import-console").html('<span class="error">' + msg + "</span>");
      },
    });
  }
});
