$(function () {
  $(".header_root").load("./comm/header.html");
  $(".footer_root").load("./comm/footer.html");
  // nav传值显示下划线
  $(".header_root").data("nav_info", 5);
  // 新闻资讯搜索信息
  let newsInfo = {
    page: Number($.getQueryVariable("page") || 1),
    size: 9,
    newstitle: $.getQueryVariable("newstitle") || "",
    total: 0,
  };
  $(".news_info #search_help").val(newsInfo.newstitle);
  // 加载新闻资讯列表
  function loadNewsList(params) {
    let newsItemHtml = "";
    $.each(params.list, function (newsIndex, newsItem) {
      newsItemHtml =
        '<div class="news_list_item" data-newsid="' +
        newsItem.newsid +
        '"><img class="news_item_img" src="' +
        $.showImageUrl +
        newsItem.newspic +
        '" /><div class="news_item_title">' +
        newsItem.newstitle +
        '</div><div class="news_item_stitle">' +
        $.msToDateString(newsItem.createtime * 1000) +
        "</div></div>";
    });
    $(".news_list_box").html(newsItemHtml);
    $(".news_list_box").on("click", ".news_list_item", function () {
      window.location.href =
        "/sections/news_detail.html?newsid=" + $(this).data("newsid");
    });
  }
  // 加载新闻资讯页码
  function loadNewsPages(params) {
    newsInfo.total = params.total || 0;
    if (newsInfo.total === 0) {
      $(".news_page_box").html("");
      return;
    }
    let newsPageHtml =
      '<div class="news_page_prev"><img src="../img/home/scheme/jiantou_right.png" /></div>';
    for (let _page = 0; _page < Math.ceil(newsInfo.total / 9); _page++) {
      newsPageHtml +=
        '<div data-page="' +
        (_page + 1) +
        '" class="news_page_num ' +
        (_page + 1 === newsInfo.page ? "selected" : "") +
        '">' +
        (_page + 1) +
        "</div>";
    }
    newsPageHtml +=
      '<div class="news_page_next"><img src="../img/home/scheme/jiantou_right.png" /></div>';
    $(".news_page_box").html(newsPageHtml);
    // 上下页禁止点击
    function setNotAllowed() {
      if (newsInfo.page === 1) {
        $(".news_page_box .news_page_prev").addClass("disabled");
      } else {
        $(".news_page_box .news_page_prev.disabled").removeClass("disabled");
      }
      if (newsInfo.page === Math.ceil(newsInfo.total / 9)) {
        $(".news_page_box .news_page_next").addClass("disabled");
      } else {
        $(".news_page_box .news_page_next.disabled").removeClass("disabled");
      }
    }
    setNotAllowed();
    $(".news_page_box").on("click", ".news_page_prev", function () {
      if (newsInfo.page > 1) {
        newsInfo.page--;
        $(".news_page_box .news_page_num.selected").removeClass("selected");
        $(
          ".news_page_box .news_page_num[data-page='" + newsInfo.page + "']"
        ).addClass("selected");
        setNotAllowed();
        // searchList();
        window.location.href = "/sections/news.html?page=" + newsInfo.page + "&newstitle=" + newsInfo.newstitle;
      }
    });
    $(".news_page_box").on("click", ".news_page_num", function () {
      newsInfo.page = $(this).data("page");
      $(".news_page_box .news_page_num.selected").removeClass("selected");
      $(
        ".news_page_box .news_page_num[data-page='" + newsInfo.page + "']"
      ).addClass("selected");
      setNotAllowed();
      // searchList();
      window.location.href = "/sections/news.html?page=" + newsInfo.page + "&newstitle=" + newsInfo.newstitle;
    });
    $(".news_page_box").on("click", ".news_page_next", function () {
      if (newsInfo.page < Math.ceil(newsInfo.total / 9)) {
        newsInfo.page++;
        $(".news_page_box .news_page_num.selected").removeClass("selected");
        $(
          ".news_page_box .news_page_num[data-page='" + newsInfo.page + "']"
        ).addClass("selected");
        setNotAllowed();
        // searchList();
        window.location.href = "/sections/news.html?page=" + newsInfo.page + "&newstitle=" + newsInfo.newstitle;
      }
    });
  }
  // 搜索新闻列表
  function searchList() {
    let _url = $.baseUrl + "n/listc";
    $.ajax({
      type: "POST",
      url: _url,
      dataType: "json",
      contentType: "application/json; charset=utf-8",
      data: JSON.stringify({
        page: newsInfo.page,
        size: newsInfo.size,
        newstitle: newsInfo.newstitle,
      }),
      success: function (data) {
        if (data.code === 0) {
          if (data.obj.list.length === 0 && newsInfo.page > 1) {
            newsInfo.page--;
            searchList();
          } else {
            loadNewsList(data.obj);
            loadNewsPages(data.obj);
          }
        } else {
          alert(data.msg);
        }
      },
    });
  }
  // 回车搜索
  function enterSearch(event) {
    // 兼容FF和IE和Opera
    let theEvent = event || window.event;
    let code = theEvent.keyCode || theEvent.which || theEvent.charCode;
    let content = theEvent.target.value;
    newsInfo.newstitle = content;
    if (code == 13) {
      // searchList();
      window.location.href = "/sections/news.html?page=" + newsInfo.page + "&newstitle=" + newsInfo.newstitle;
    }
  }
  // 输入内容改变
  function searchInputChange(event) {
    let theEvent = event || window.event;
    newsInfo.newstitle = theEvent.target.value;
  }
  $(".news_info #search_help").on("change", searchInputChange);
  $(".news_info #search_help").on("keydown", enterSearch);
  $(".news_info .search_box").click(function () {
    // searchList();
    window.location.href = "/sections/news.html?page=" + newsInfo.page + "&newstitle=" + newsInfo.newstitle;
  });
  searchList();
});
