jQuery(document).ready(function () {
  saveMetaData();
  closeMeta();
  openMeta();
  changeUser();
  openUserList();
  openPostList();
  changePostType();
  changePostCat();
  aprovePost();
});

//Newxt Lvl
function saveMetaData() {
  jQuery(".save-meta-data").click(() => {
    console.log("saved");
    jQuery(".edit-meta-data").css("display", "none");
  });
}

function closeMeta() {
  jQuery(".close-meta-data").click(() => {
    console.log("hello");
    //   jQuery(e.currentTarget)
    //     .parent()
    //     .parent()
    //     .parent(".editMetaTags")
    //     .css("display", "none");
  });
}

function openUserList() {
  jQuery(".open-user-list").click((e) => {
    jQuery(e.currentTarget)
      .siblings(".user-pannel")
      .toggleClass("user-pannel-open");
    jQuery(e.currentTarget)
      .children(".bx-chevron-up")
      .toggleClass("bx-chevron-down");
  });
}

function changeUser() {
  jQuery(".edit-select-user").click((e) => {
    jQuery(".edit-current-user").html(jQuery(e.currentTarget).html());
    let id = jQuery(e.currentTarget).children("li").val();
    let postId = jQuery(e.currentTarget).children("input").val();
    console.log(postId);
    jQuery(".post-autor" + postId).val(id);
    jQuery(".save-post-data").css("background-color", "#ff4900");
    jQuery(".save-post-data").css("color", "white");
  });
}

function openPostList() {
  jQuery(".open-user-list").click((e) => {
    let id = jQuery(e.currentTarget).siblings(".post-pannel").attr("id");
    jQuery(".channel" + id).css("display", "none");
    jQuery(e.currentTarget)
      .siblings(".post-pannel")
      .toggleClass("post-pannel-open");
  });
}

function changePostType() {
  jQuery(".edit-select-type").click((e) => {
    let id = jQuery(e.currentTarget).attr("id");

    if (jQuery(e.currentTarget).text().includes("post") == true)
      jQuery(".channel" + id).css("display", "block");
    else jQuery(".channel" + id).css("display", "none");
    jQuery(".edit-current-post-type" + id).html(jQuery(e.currentTarget).html());
    jQuery(".save-post-data").css("background-color", "#ff4900");
    jQuery(".save-post-data").css("color", "white");
  });
}
function changePostCat() {
  jQuery(".edit-select-cat").click((e) => {
    let id = jQuery(e.currentTarget).attr("id");
    jQuery(".category" + id).html(jQuery(e.currentTarget).html());
    jQuery(".save-post-data").css("background-color", "#ff4900");
    jQuery(".save-post-data").css("color", "white");
  });
}

function editMeta() {
  jQuery(".save-post-data").click((e) => {
    let id = jQuery(e.currentTarget).attr("id");
    let title = jQuery(".title" + id).val();
    let meta_title = jQuery(".meta-title" + id).val();
    let description = jQuery(".description" + id).val();
    let keywords = jQuery(".keywords" + id).val();

    let authorId = jQuery(".post-autor" + id).val();
    let postType = jQuery(".edit-current-post-type" + id).text();
    let postCat = jQuery(".category" + id).text();

    return jQuery
      .post(sqQuery.ajaxurl, {
        strb_nonce: sqQuery.nonce,
        id: id,
        title: title,
        meta_title: meta_title,
        description: description,
        keywords: keywords,
        authorId: authorId,
        postType: postType,
        postCat: postCat,
        action: "nxt_update_post_meta",
      })
      .done(function (data) {
        jQuery(".save-post-data").css("background-color", "#50E0FE");
        jQuery(".save-post-data").css("color", "black");
      });
  });
}

function aprovePost() {
  jQuery(".aprove-post").click((e) => {
    let id = jQuery(e.currentTarget).siblings(".get-id").attr("id");
    jQuery(e.currentTarget).css("display", "none");
    jQuery(e.currentTarget).parent(".button-holder").css("width", "20vw");
    return jQuery
      .post(sqQuery.ajaxurl, {
        id: id,
        strb_nonce: sqQuery.nonce,
        action: "nxt_aprove_post",
      })
      .done(function (data) {
        console.log(data + "id");
      });
  });
}

function checkKeyword(id) {
  let keyword = jQuery("#keyword" + id).val();
  let newMetaTitle = jQuery("#title" + id).val();
  let newMetaDescription = jQuery("#description" + id).val();
  if (newMetaTitle.includes(keyword) == false || keyword == "")
    jQuery("#isOptimized" + id).css("display", "flex");
  if (newMetaDescription.includes(keyword) == false || keyword == "")
    jQuery("#isDescripitonOptimized" + id).css("display", "flex");
}

function metaChars(id) {
  jQuery("#meta-title-chars" + id).text(jQuery("#title" + id).val().length);
  jQuery("#meta-description-chars" + id).text(
    jQuery("#description" + id).val().length
  );
  jQuery("#title-chars" + id).text(jQuery("#title" + id).val().length);
}

function metaCharsDinamic(id) {
  jQuery("#title" + id).on("keydown", () => {
    jQuery("#meta-title-chars" + id).text(
      jQuery("#title" + id).val().length + 1
    );
  });

  jQuery("#description" + id).on("keydown", () => {
    jQuery("#meta-description-chars" + id).text(
      jQuery("#description" + id).val().length + 1
    );
  });

  jQuery("#main-title" + id).on("keydown", () => {
    jQuery("#title-chars" + id).text(
      jQuery("#main-title" + id).val().length + 1
    );
  });

  jQuery("#main-content" + id).on("keydown", () => {
    jQuery("#content-chars" + id).text(
      jQuery("#main-content" + id).val().length + 1
    );
  });
}

function openMeta(meta) {
  jQuery(meta).css("display", "flex");
}
//EDIT THEME

//AXAJ
(function ($) {
  "use strict";
  $(document).ready(function () {
    //Open and preview theme

    $(".nxt-check-token").click(() => {
      console.log("test");
      // let token = $(".nxt-input-token").val();
      // return $.post(sqQuery.ajaxurl, {
      //   token: token,
      //   strb_nonce: sqQuery.nonce,
      //   action: "nxt_ajax_check_token",
      // }).done(function (data) {
      //   console.log(data);
      //   if (data == true) location.reload();
      //   else {
      //     $(".token-alert").css("display", "block");
      //     $(".nxt-input-token").css("border", "1px solid orange");
      //   }
      // });
    });

    $(".test-jq").click(() => {
      console.log(".....");
    });

    $(".save-display-options").click((e) => {
      e.preventDefault();

      return $.post(sqQuery.ajaxurl, {
        strb_nonce: sqQuery.nonce,
        action: "nxt_ajax_display_all_posts",
        formChecked: $(".display-options-form").serialize(),
      }).done(function (data) {
        console.log(data);
        // jQuery(".add-author").css("display", "none");
        // jQuery(".authors").css("display", "block");
        // jQuery(".display-all-themes").css("display", "block");

        location.reload();
      });
    });

    //Display Options
    //All
    $(".all-post-types").click(() => {
      $(".specific-pos").css("display", "none");
      $(".add-specific-post-types").prop("checked", false);
      if ($(".all-post-types").is(":checked")) {
        var val = "true";
      } else val = "false";

      return $.post(sqQuery.ajaxurl, {
        checked: val,
        strb_nonce: sqQuery.nonce,
        action: "nxt_ajax_display_all_posts",
      }).done(function (data) {});
    });
    //Specific Post types

    $(".add-specific-post-types").click(() => {
      $(".all-post-types").prop("checked", false);

      if ($(".add-specific-post-types").is(":checked")) {
        $(".specific-pos").css("display", "flex");
        var val = "true";
      } else {
        val = "false";
        $(".specific-pos").css("display", "none");
      }

      return $.post(sqQuery.ajaxurl, {
        checked: val,
        strb_nonce: sqQuery.nonce,
        action: "nxt_ajax_display_specific_posts",
      }).done(function (data) {});
    });
    //
    $(".add-specific-post").click(() => {
      if ($(".add-specific-post").is(":checked")) {
        var val = "true";
      } else val = "false";

      return $.post(sqQuery.ajaxurl, {
        checked: val,
        strb_nonce: sqQuery.nonce,
        action: "nxt_ajax_display_posts",
      }).done(function (data) {});
    });
    //
    $(".add-specific-pages").click(() => {
      if ($(".add-specific-pages").is(":checked")) {
        var val = "true";
      } else val = "false";

      return $.post(sqQuery.ajaxurl, {
        checked: val,
        strb_nonce: sqQuery.nonce,
        action: "nxt_ajax_display_pages",
      }).done(function (data) {});
    });

    //NExt level ajax
    let text;
    let postId;
    $(".publish-option").click((e) => {
      text = $(e.currentTarget).text();
      postId = $(e.currentTarget).siblings(".post-id").val();

      $(e.currentTarget)
        .parent()
        .siblings(".dropdown-displays")
        .children(".dropdown-btn-text")
        .text($(e.currentTarget).text());
      $(".pannel-displays").removeClass("open");
      $(".dropdown-arrow").removeClass("bx-chevron-down");
      $(".dropdown-arrow").addClass("bx-chevron-up");
      $(e.currentTarget).parent().siblings(".publish").css("display", "flex");
    });
    $(".publish").click((e) => {
      $(e.currentTarget).siblings(".checkmark").css("display", "block");
      $(e.currentTarget).css("display", "none");
      setTimeout(() => {
        $(e.currentTarget).siblings(".checkmark").css("display", "none");
      }, 200);

      $(".toggler-knob" + postId).css("background-color", "lime");
      return $.post(sqQuery.ajaxurl, {
        postType: text,
        post_id: postId,
        strb_nonce: sqQuery.nonce,
        action: "nxt_change_post_type",
      }).done(function (data) {});
    });
    $(".makeNewPost").click(() => {
      $(".checkmark-install").css("display", "block");
      setTimeout(() => {
        $(".install-sq").css("display", "none");
      }, 400);

      return $.post(sqQuery.ajaxurl, {
        strb_nonce: sqQuery.nonce,
        action: "sq_advanced_install",
      }).done(function () {});
    });

    $(".meta-checkbox").click((e) => {
      let currentMeta = $(e.currentTarget)
        .parent()
        .parent()
        .siblings(".editMetaTags");
      let id = $(e.currentTarget).siblings(".post_id").val();

      return $.post(sqQuery.ajaxurl, {
        strb_nonce: sqQuery.nonce,
        post_id: id,
        action: "nxt_open_meta_form",
      }).done(function (data) {
        openMeta(currentMeta);
        currentMeta.html(data);
        closeMeta();
        editMeta();
        checkKeyword(id);
        metaChars(id);
        metaCharsDinamic(id);
        openUserList();
        changeUser();
        openPostList();
        changePostType();
        changePostCat();
        aprovePost();
      });
    });

    $(".nxt-bin").click((e) => {
      let binID = $(e.currentTarget).attr("id");

      $(".all-posts" + binID).css("background-color", "rgb(255, 43, 35)");
      setTimeout(() => {
        $(".all-posts" + binID).css("display", "none");
      }, 200);

      return $.post(sqQuery.ajaxurl, {
        strb_nonce: sqQuery.nonce,
        bin_id: binID,
        action: "nxt_bin_meta_post",
      }).done(function () {});
    });

    $(".edit_theme_info_customize").click(() => {
      $("#theme_edit_container").css("display", "block");
      $(".general-author").css("display", "none");
      $("#close_theme").attr("id", "close-custom");
    });

    $(document).on("click", "#close-custom", function (e) {
      $(".general-author").css("display", "block");
    });

    //next lvl

    $(".close-meta-data").click(() => {
      $(".edit-meta-data").css("display", "none");
    });
    $(".dropdown-displays").click((e) => {
      $(".pannel-displays").removeClass("open");
      $(".dropdown-arrow").removeClass("bx-chevron-down");
      $(".dropdown-arrow").addClass("bx-chevron-up");
      $(e.currentTarget)
        .children(".dropdown-arrow")
        .removeClass("bx-chevron-up");
      $(e.currentTarget)
        .children(".dropdown-arrow")
        .toggleClass("bx-chevron-down", "bx-chevron-up");

      $(e.currentTarget)
        .siblings(".pannel-displays")
        .toggleClass("closed open");
    });

    $(".time-week").click((e) => {
      $(".save-schedule").css("background-color", "#ff4900");
      $(".save-schedule").css("color", "white");
      $(e.currentTarget).toggleClass("time-week-selected");
    });

    $(".remove-time").click((e) => {
      let id = $(e.currentTarget).attr("id");
      $(".time-" + id).text("");
      $(".time-" + id).css("display", "none");
      $(".save-schedule").css("background-color", "#ff4900");
      $(".save-schedule").css("color", "white");
      // return $.post(sqQuery.ajaxurl, {
      //   strb_nonce: sqQuery.nonce,
      //   time_id: id,
      //   action: "nxt_remove_post_dates",
      // }).done(function (data) {
      //   console.log(data);
      // });
    });

    $(".save-schedule").click((e) => {
      let week_day;
      let hour;
      let minute;
      let time_format;
      if ($(".time-week").hasClass("time-week-selected")) {
        week_day = $(".time-week-selected").text();
      }
      hour = $(".hour-input").find(":selected").text();
      minute = $(".minute-input").find(":selected").text();
      time_format = $(".time-format").find(":selected").text();

      return $.post(sqQuery.ajaxurl, {
        strb_nonce: sqQuery.nonce,
        day: week_day,
        hours: hour,
        minutes: minute,
        time_format: time_format,
        action: "nxt_set_post_dates",
      }).done(function (data) {
        $(".save-schedule").css("background-color", "#50E0FE");
        $(".save-schedule").css("color", "black");
      });
    });

    $(".open-schedule").click(() => {
      $(".schedule-pannel").toggleClass("schedule-pannel-open");
    });

    $(".remove-post").click((e) => {
      let post_id = $(e.currentTarget).siblings("div").attr("class");
      $(".all-posts" + post_id).css("display", "none");
      return $.post(sqQuery.ajaxurl, {
        strb_nonce: sqQuery.nonce,
        id: post_id,
        action: "nxt_remove_post",
      }).done(function (data) {
        console.log(data);
      });
    });

    $(".add-time").click(() => {
      $(".save-schedule").css("background-color", "#ff4900");
      $(".save-schedule").css("color", "white");
      let format = ["AM", "PM"];
      let hour = [
        " 1 ",
        " 2 ",
        " 3 ",
        " 4 ",
        " 5 ",
        " 6 ",
        " 7 ",
        " 8 ",
        " 9 ",
        " 10 ",
        " 11 ",
        " 12 ",
      ];
      let minutes = [
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "11",
        "12",
        "13",
        "14",
        "15",
        "16",
        "17",
        "18",
        "19",
        "20",
        "21",
        "22",
        "23",
        "24",
        "25",
        "26",
        "27",
        " 28",
        "29",
        " 30",
        "31,",
        "32",
        "33",
        " 34",
        " 35",
        " 36",
        " 37",
        "38",
        " 39",
        " 40",
        " 41",
        "42",
        "43",
        "44",
        "45",
        "46",
        "47",
        "48",
        "49",
        "50",
        "51",
        "52",
        "53",
        " 54",
        "55",
        " 56",
        "57",
        "58",
        "59",
      ];
      let data_format = "";
      let data_hour = "";
      let data_minutes = "";
      for (let i = 0; i < format.length; i++) {
        data_format +=
          "<option lass='select-channel' style='height:5px;font-weight:500;'>" +
          format[i] +
          "</option>";
      }
      for (let i = 0; i < hour.length; i++) {
        data_hour +=
          "<option lass='select-channel' style='height:5px;font-weight:500;'>" +
          hour[i] +
          "</option>";
      }
      for (let i = 0; i < minutes.length; i++) {
        data_minutes +=
          "<option lass='select-channel' style='height:5px;font-weight:500;'>" +
          minutes[i] +
          "</option>";
      }

      let tmpHTML = `<div class="new-time-slot" style="">
                        <select class="hour-input" style="border-radius:15px;">
                            ${data_hour}
                        </select>    
                        :
                        <select class="minute-input" style="border-radius:15px;">
                            ${data_minutes}
                        </select>
                        
                        <select class="time-format" style="border-radius:15px;">
                            ${data_format}
                        </select>
                    </div>`;

      $(".schedule-time-pannel").append(tmpHTML);
    });
  });
})(jQuery);
