window.onload = function () {
  // 首页开屏加载等待
  function indexSuccess() {
    const loading = document.querySelector('.loading')
    if (!loading) return
    loading.classList.add('success')
    setTimeout(function () {
      loading.style.display = 'none'
    }, 500)
  }
  // indexSuccess()
}

document.addEventListener('DOMContentLoaded', function () {
  // let vConsole = new VConsole()
  // alert(window.innerWidth)
  !(function (n, e) {
    // 判断是否为 safari 浏览器 配合css 兼容100vh
    let isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
    function setViewHeight() {
      let windowVH = e.innerHeight / 100
      n.documentElement.style.setProperty('--vh', windowVH + 'px')
    }
    let i = 'orientationchange' in window ? 'orientationchange' : 'resize'
    if (isSafari) {
      n.addEventListener('DOMContentLoaded', setViewHeight)
      e.addEventListener(i, setViewHeight)
    }
  })(document, window)
  /**
   * 判断是否为移动端
   */
  function isMobileDevice() {
    return typeof window.orientation !== 'undefined' || navigator.userAgent.indexOf('IEMobile') !== -1
  }
  /**
   * 导航搜索
   */
  let search = document.querySelector('.search')
  let searchBtn = document.querySelector('.show_search')
  const searchFrom = document.querySelector('header .search form')
  searchBtn.addEventListener('click', () => {
    search.classList.toggle('active')
    searchBtn.classList.toggle('off')
  })
  searchFrom.addEventListener('submit', e => {
    e.preventDefault()
    const keyword = e.target.querySelector('input')
    window.location.href = 'news.html?keywords=' + keyword.value
  })
  /**
   * 首页视频按钮滚动
   */
  let videoBottom = document.querySelector('.video_bottom')
  let headElement = document.querySelector('header')
  let targetElement = document.querySelector('.products_box')
  if (videoBottom && headElement && targetElement) {
    videoBottom.addEventListener('click', function () {
      let targetPosition = targetElement.getBoundingClientRect().top + window.scrollY
      window.scrollTo({
        top: targetPosition,
        behavior: 'smooth',
      })
    })
  }
  /**
   * 移动端首页视频弹窗
   */
  function showVideoDialog() {
    const showVideoBtn = document.querySelector('.show_video')
    // if (!isMobileDevice()) return
    if (showVideoBtn == null) return
    const videoDialog = document.querySelector('.video_dialog')
    const offVideoBtn = document.querySelector('.video_dialog .off')
    showVideoBtn.addEventListener('click', () => {
      videoDialog.classList.add('active')
    })
    offVideoBtn.addEventListener('click', () => {
      videoDialog.classList.remove('active')
    })
    // 点击空白处 关闭 视频弹窗
    videoDialog.addEventListener('click', event => {
      const target = event.target
      if (!target.classList.contains('video_dialog_content')) {
        videoDialog.classList.remove('active')
      }
    })
  }
  showVideoDialog()
  /**
   * 节流函数
   */
  function throttle(func, wait) {
    let timeout
    return function () {
      let context = this
      let args = arguments
      if (!timeout) {
        timeout = setTimeout(function () {
          timeout = null
          func.apply(context, args)
        }, wait)
      }
    }
  }
  /**
   * 首页滚动导航
   */
  let productBox = document.querySelector('.products_box')
  let aboutIcon = document.querySelector('.about_vision_mission .icon')
  let scrollBox = document.querySelector('.scroll_top_box') // 质量页面浮动栏
  let content = document.querySelector('.video_box') || document.querySelector('.placard') || document.querySelector('.facilities_swiper') || document.querySelector('.operations_content')

  if (content) {
    let prevScrollPos = document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset
    window.addEventListener(
      'scroll',
      throttle(function () {
        // if (isMobileDevice()) return
        let rect = content.getBoundingClientRect()
        let currentScrollPos = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop

        headElement.classList.toggle('is_bg', rect.top < 0)
        if (productBox) {
          productBox.classList.toggle('show_before', rect.top < 0)
        }
        if (scrollBox) {
          scrollBox.classList.toggle('show', prevScrollPos > currentScrollPos)
        }
        if (aboutIcon) {
          aboutIcon.classList.toggle('rotate', prevScrollPos > currentScrollPos)
        }
        prevScrollPos = currentScrollPos
      }, 300)
    )
  }
  /**
   * 首页导航上下滑动 移除添加对应类名
   */
  function handleScroll() {
    let lastScrollTop = 0
    let delta = 5 // 偏移量
    let navbarHeight = headElement.offsetHeight
    window.addEventListener('scroll', function () {
      let scrollTop = window.pageYOffset || document.documentElement.scrollTop
      if (Math.abs(lastScrollTop - scrollTop) <= delta) {
        return
      }
      if (scrollTop > lastScrollTop && scrollTop > navbarHeight && !isMobileDevice()) {
        // 向下滚动
        headElement.classList.add('hide')
        headElement.classList.remove('show')
      } else {
        // 向上滚动
        headElement.classList.remove('hide')
        headElement.classList.add('show')
      }
      lastScrollTop = scrollTop
    })
  }
  handleScroll()
  /**
   * 移动端导航点击效果
   */
  function oneSelects(tit) {
    for (let i = 0; i < tit.length; i++) {
      tit[i].index = i
      tit[i].onclick = () => {
        if (tit[i].classList.contains('active')) {
          tit[i].classList.remove('active')
        } else {
          for (let n = 0; n < tit.length; n++) {
            tit[n].classList.remove('active')
          }
          tit[i].classList.add('active')
        }
      }
    }
  }
  function wapShowNav() {
    if (isMobileDevice()) {
      const moreNav = document.querySelector('.show_wap_nav')
      const nav = document.querySelectorAll('nav > ul > li')
      moreNav.addEventListener('click', () => {
        for (let n = 0; n < nav.length; n++) {
          nav[n].classList.remove('active')
        }
        headElement.classList.toggle('show-nav')
      })
      oneSelects(nav)
    }
  }
  wapShowNav()
  /**
   * 网站滚动元素--出现在视图就变化
   */
  /**
   * 网站滚动元素--出现在视图就变化一次
   */
  function webNumber() {
    if (!window.counterUp) return
    const counterUp = window.counterUp.default
    const callback = entries => {
      entries.forEach(entry => {
        const el = entry.target
        if (entry.isIntersecting) {
          counterUp(el, {
            duration: 1500, // 2秒内完成
            delay: 16, //16 毫秒更新一次
          })
          IO.unobserve(el) // 解除对该元素的观察
        }
      })
    }
    const IO = new IntersectionObserver(callback, { threshold: 1 })
    const el = document.querySelectorAll('.counter')
    if (el) {
      el.forEach(item => {
        IO.observe(item)
      })
    }
  }
  webNumber()
  /**
   * 首页轮播图
   */
  function shippingSwiper() {
    if (!window.Swiper) return
    let mySwiper = new Swiper('.shipping_swiper .swiper', {
      // autoplay: true, //可选选项，自动滑动
      loop: true, // 循环
      grabCursor: true,
      pagination: {
        el: '.shipping-swiper-pagination',
        clickable: true,
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
    })
  }
  shippingSwiper()
  /**
   * 首页新闻切换
   */
  let newImgs = document.querySelectorAll('.news_box .news_imgs li')
  let newIis = document.querySelectorAll('.news_box .news_list li')
  const select = (tit, item) => {
    for (let i = 0; i < tit.length; i++) {
      tit[i].index = i
      tit[i].onmousemove = () => {
        for (let n = 0; n < tit.length; n++) {
          tit[n].classList.remove('active')
        }
        tit[i].classList.add('active')
        for (let n = 0; n < item.length; n++) {
          item[n].classList.remove('active')
        }
        item[i].classList.add('active')
      }
    }
  }
  select(newIis, newImgs)
  /**
   * 关于我们轮播图
   */
  function aboutSwiper() {
    if (!window.Swiper) return
    let aboutSwiper = new Swiper('.about_three_swiper .swiper', {
      // autoplay: true, //可选选项，自动滑动
      grabCursor: true,
      // loop: true,
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
    })
  }
  aboutSwiper()
  /**
   * 关于我们-成长与发展了解更多
   */
  function showGrowthInfoMore() {
    const mores = document.querySelectorAll('.join_growth_info .more')
    const growthContent = document.querySelectorAll('.join_growth_info .growth_info_more')
    const offMore = document.querySelectorAll('.join_growth_info .growth_info_more .off')
    if (!mores) return
    // 当点击空白区域时关闭 growth_info_more
    document.body.addEventListener('click', event => {
      const target = event.target
      if (!target.classList.contains('more') && !target.classList.contains('growth_info_more')) {
        growthContent.forEach(content => content.classList.remove('active'))
      }
    })
    mores.forEach((item, index) => {
      const i = item.querySelector('i')
      item.addEventListener('click', () => {
        growthContent.forEach(content => content.classList.remove('active'))
        item.nextElementSibling.classList.toggle('active')
      })
      i.addEventListener('click', event => {
        event.stopPropagation()
        growthContent.forEach(content => content.classList.remove('active'))
        growthContent[index].classList.toggle('active')
      })
      offMore[index].addEventListener('click', () => {
        growthContent[index].classList.remove('active')
      })
    })
  }
  showGrowthInfoMore()

  /**
   * 我们的历程轮播图
   */
  function milestoneListSwiper_old() {
    if (!window.Swiper) return
    let milestone = document.querySelector('.milestone_time')
    let milestoneTimes = document.querySelectorAll('.milestone_time li')
    milestoneTimes.forEach((item, index) => {
      item.addEventListener('click', function () {
        milestoneTimes.forEach(el => {
          el.classList.remove('active')
        })
        item.classList.add('active')
        milestoneListSwiper.slideTo(index)
      })
    })
    let milestoneListSwiper = new Swiper('.milestone_list .swiper', {
      grabCursor: true,
      mousewheel: true, //鼠标滚轮控制
      direction: 'vertical',
      on: {
        slideChangeTransitionStart_old: function () {
          milestoneTimes.forEach((item, index) => {
            item.classList.remove('active')
            if (index === this.activeIndex) {
              item.classList.add('active')
              let scrollHeight = milestone.scrollHeight
              let clientHeight = milestone.clientHeight
              let scrollTop = (scrollHeight - clientHeight) * (index / (milestoneTimes.length - 1))
              milestone.scrollTo({ top: scrollTop, behavior: 'smooth' })
            }
          })
        },
        slideChangeTransitionStart: function () {
          milestoneTimes.forEach((item, index) => {
            item.classList.remove('active')
            if (index === this.activeIndex) {
              item.classList.add('active')
              let scrollSize = isMobileDevice() ? milestone.offsetWidth : milestone.offsetHeight
              let scrollLength = milestoneTimes.length - 1
              let scrollTop = (scrollSize / scrollLength) * index
              let pad = parseFloat(getComputedStyle(milestone.parentNode).getPropertyValue('padding-left'))
              let scrollLeft = item.offsetLeft - pad
              if (isMobileDevice()) {
                // if (scrollLeft > window.innerWidth / 2) {
                //   scrollLeft = scrollLeft - window.innerWidth / 2
                //   if (scrollLeft < pad * 2) {
                //     scrollLeft = 0
                //   }
                // }
                milestone.scrollTo({ left: scrollLeft, behavior: 'smooth' })
              } else {
                milestone.scrollTo({ top: scrollTop, behavior: 'smooth' })
              }
            }
          })
        },
      },
    })
  }
  function milestoneListSwiper() {
    const milestone_item_tit = document.querySelectorAll('.milestone_main >ul>li .title')
    const swiper_lis = document.querySelectorAll('.milestone_main >ul>li .content .swiper')
    for (let i = 0; i < swiper_lis.length; i++) {
      new Swiper(swiper_lis[i], {
        grabCursor: true, // 在轮播图上变成鼠标变成小手
        effect: 'fade',
        fadeEffect: {
          crossFade: true, //开启淡入淡出效果
        },
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
        },
      })
    }
    window.addEventListener('scroll', function () {
      for (let i = 1; i < milestone_item_tit.length; i++) {
        const element = milestone_item_tit[i]
        const elementTop = element.getBoundingClientRect().top
        const halfViewportHeight = window.innerHeight / 2
        if (elementTop < halfViewportHeight) {
          element.classList.add('active')
        } else {
          element.classList.remove('active')
        }
      }
    })
  }
  milestoneListSwiper()
  /**
   * 产品轮播图
   */
  function productSwiper() {
    if (!window.Swiper) return
    let productSwiper = new Swiper('.product_swiper .swiper', {
      // autoplay: true, //可选选项，自动滑动
      // grabCursor: true, // 在轮播图上变成鼠标变成小手
      // loop: true,
      effect: 'fade',
      fadeEffect: {
        crossFade: true, //开启淡入淡出效果
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
    })
    const productSwiperItem = document.querySelectorAll('.product_swiper .swiper-slide')
    productSwiperItem.forEach(item => {
      const productSwiperTitle = item.querySelectorAll('li')
      // const productSwiperImages = item.querySelectorAll('img')
      oneSelect(productSwiperTitle)
    })
  }
  productSwiper()
  /**
   * 产品页
   * 合作伙伴
   */
  function productBuddySwiper() {
    if (!window.Swiper) return
    new Swiper('.product_buddy_swiper .swiper', {
      // autoplay: true, //可选选项，自动滑动
      grabCursor: true, // 在轮播图上变成鼠标变成小手
      loop: true,
      slidesPerView: isMobileDevice() ? (window.innerWidth > 576 ? 5 : 2) : 6, // 设置每页显示的slide数量为6
      grid: {
        fill: 'column',
        rows: isMobileDevice() ? 2 : 1,
      },
      fadeEffect: {
        crossFade: true,
      },
      navigation: {
        nextEl: '.swiper_box .swiper-button-next',
        prevEl: '.swiper_box .swiper-button-prev',
      },
    })
  }
  productBuddySwiper()

  /**
   * 基地码头轮播图
   */
  function facilitiesSwiper() {
    if (!window.Swiper) return
    let facilitiesSwiper = new Swiper('.facilities_swiper', {
      // autoplay: true, //可选选项，自动滑动
      // loop: true,
      grabCursor: true, // 在轮播图上变成鼠标变成小手
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
    })
  }
  facilitiesSwiper()
  /**
   * 首页新闻切换
   */
  let AdvantageLis = document.querySelectorAll('.Advantage_lis li')
  function oneSelect(tit) {
    for (let i = 0; i < tit.length; i++) {
      tit[i].index = i
      tit[i].onmousemove = () => {
        for (let n = 0; n < tit.length; n++) {
          tit[n].classList.remove('active')
        }
        tit[i].classList.add('active')
      }
    }
  }
  // oneSelect(AdvantageLis)
  /**
   * 加入我们轮播图
   */
  function joinLifeSwiper() {
    if (!window.Swiper) return
    let joinLifeSwiper = new Swiper('.join_life_swiper', {
      watchSlidesProgress: true,
      slidesPerView: 'auto',
      centeredSlides: true,
      slideToClickedSlide: true,
      loop: true,
      // loopedSlides: 3,
      // autoplay: {
      //   delay: 4000,
      // },
      // navigation: {
      //   nextEl: '.swiper-button-next',
      //   prevEl: '.swiper-button-prev',
      // },
      navigation: {
        nextEl: '.swiper-button-prev',
        prevEl: '.swiper-button-next',
      },
      pagination: {
        el: '.swiper-pagination',
        type: 'fraction',
      },
      on: {
        slideChange: function () {
          let activeIndex = this.realIndex
          let liList = document.querySelectorAll('.join_life_swiper .swiper_info ul li')
          liList.forEach(function (li, index) {
            if (index === activeIndex) {
              li.classList.add('active')
            } else {
              li.classList.remove('active')
            }
          })
        },
      },
    })
  }
  joinLifeSwiper()
  /**
   * 加入我们最新动态
   */
  const joinSelectTit = document.querySelectorAll('.join_news_select_tit li')
  const joinSelectContent = document.querySelectorAll('.join_news_select_content>ul> li')
  select(joinSelectTit, joinSelectContent)
  /**
   * 投资者关系底部报告轮播图
   */
  function reportSwiper() {
    if (!window.Swiper) return
    let reportSwiper = new Swiper('.report_swiper .swiper', {
      // autoplay: true, //可选选项，自动滑动
      grabCursor: true,
      // loop: true,
      effect: 'fade',
      noSwiping: true, // 禁止鼠标切换
      noSwipingClass: 'swiper-slide', // 禁止鼠标切换
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
    })
  }
  reportSwiper()
  // 基地码头页初始化滚动
  function facilitiesScroll() {
    const facilitiesSwiper = document.querySelector('.facilities_swiper')
    if (!facilitiesSwiper) return
    if (location.hash === '#1') {
      facilitiesSwiper.scrollIntoView({ block: 'center' })
      return
    }
    const anchor = document.querySelectorAll('.facilities_lis>ul>li')
    anchor.forEach((item, index) => {
      if (location.hash === '#' + (index + 2)) {
        item.scrollIntoView({ block: 'center' })
      }
    })
  }
  facilitiesScroll()
  // 产品页初始化滚动
  function productScroll() {
    const anchor = document.querySelectorAll('.product_lis li')
    if (!anchor) return
    anchor.forEach((item, index) => {
      if (location.hash === '#' + (index + 1)) {
        item.scrollIntoView({ block: 'center' })
      }
    })
  }
  productScroll()
  // 加入我们
  function joinscroll() {
    // 监听滚动事件
    window.addEventListener('scroll', () => {
      const navItems = document.querySelectorAll('.scroll_box ul li')
      const scrollPosition = window.scrollY
      navItems.forEach(item => {
        const sectionId = item.getAttribute('scroll')
        const section = document.querySelector(sectionId)
        const offset = -120 // 偏移量
        item.addEventListener('click', () => {
          section.scrollIntoView({ behavior: 'smooth', block: 'center' })
        })
        if (section.offsetTop + offset <= scrollPosition && section.offsetTop + section.offsetHeight - offset > scrollPosition) {
          navItems.forEach(otherItem => {
            if (otherItem !== item) {
              otherItem.classList.remove('active')
            }
          })
          item.classList.add('active')
        } else {
          item.classList.remove('active')
        }
      })
    })
  }
  joinscroll()
  /**
   * 技术创新底部报告轮播图
   */
  function innovationDevelopmentSwiper() {
    if (!window.Swiper || window.innerWidth < 576) return
    new Swiper('.innovation_development_swiper.swiper', {
      // autoplay: true, //可选选项，自动滑动
      // grabCursor: true,
      slidesPerView: 2,
      spaceBetween: '2.734375%',
      // loop: true,
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
    })
  }
  innovationDevelopmentSwiper()
  const selectThree = (tit, item, bgi) => {
    for (let i = 0; i < tit.length; i++) {
      tit[i].index = i
      tit[i].onmousemove = () => {
        for (let n = 0; n < tit.length; n++) {
          tit[n].classList.remove('active')
        }
        tit[i].classList.add('active')
        for (let n = 0; n < item.length; n++) {
          item[n].classList.remove('active')
        }
        item[i].classList.add('active')
        for (let n = 0; n < bgi.length; n++) {
          bgi[n].classList.remove('active')
        }
        bgi[i].classList.add('active')
      }
    }
  }
  //技术创新 切换
  const productSelect = document.querySelectorAll('.innovation_product_select li')
  const productTitle = document.querySelectorAll('.innovation_product_title_select .innovation_product_title')
  const innovationProductBG = document.querySelectorAll('.innovation_product .bgs img')
  selectThree(productSelect, productTitle, innovationProductBG)
  // 获取元素距离信息
  function GetRect(element) {
    let rect = element.getBoundingClientRect() // 距离视窗的距离
    let top = document.documentElement.clientTop ? document.documentElement.clientTop : 0 // html元素对象的上边框的宽度
    let left = document.documentElement.clientLeft ? document.documentElement.clientLeft : 0
    return {
      top: rect.top - top,
      bottom: rect.bottom - top,
      left: rect.left - left,
      right: rect.right - left,
    }
  }
  // 关于我们展开简介
  function showAboutContent() {
    const aboutViewMore = document.querySelector('.about_view_more')
    const aboutIntroduceContent = document.querySelector('.about_introduce_content')
    if (!aboutViewMore) return
    aboutViewMore.addEventListener('click', function () {
      aboutIntroduceContent.classList.toggle('active')
    })
    // 进入视口添加类名
    // let divH = aboutViewMore.offsetHeight // div高度
    // let windowH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight // 浏览器高度兼容写法
    // window.onscroll = function () {
    //   let obj = GetRect(aboutViewMore)
    //   if (obj.top > 0 && obj.top < windowH && obj.bottom > 0 && obj.bottom < windowH) {
    //     // 正在视口中
    //     aboutViewMore.classList.add('active')
    //   }
    //   if (obj.bottom <= 0 || obj.top >= windowH) {
    //     // 已经离开视口
    //     aboutViewMore.classList.remove('active')
    //   }
    // }
  }
  showAboutContent()
  // 分享工作链接

  function jobShare(share) {
    if (!share) return
    share.forEach(item => {
      let code = item.querySelector('.code')
      let share_btn = item.querySelector('.share')
      let share_box = item.querySelector('.share_box')
      let text = item.querySelector('input')
      let copy = item.querySelector('.copy')
      let success = item.querySelector('.success')
      let timer = ''
      let qrcode = new QRCode(code, {
        text: text.value,
        width: 160,
        height: 160,
        colorDark: '#000000',
        colorLight: '#ffffff',
        correctLevel: QRCode.CorrectLevel.H,
      })
      share_btn.addEventListener('mouseover', function () {
        const allShareBox = document.querySelectorAll('.join_news_select_content .share_box')
        allShareBox.forEach(item => item.classList.remove('active'))
        share_box.classList.add('active')
      })
      share_btn.addEventListener('mouseleave', function () {
        setTimeout(function () {
          if (!share_box.matches(':hover')) {
            share_box.classList.remove('active')
          }
        }, 200)
      })
      share_box.addEventListener('mouseleave', function () {
        timer = setTimeout(() => {
          share_box.classList.remove('active')
        }, 200)
      })
      share_box.addEventListener('mouseenter', function () {
        clearTimeout(timer)
      })
      copy.addEventListener('click', () => {
        /* 选择input的内容 */
        text.select()
        text.setSelectionRange(0, 99999) /* 兼容手机端 */
        /* 复制内容到剪贴板 */
        document.execCommand('copy')
        text.blur()
        success.classList.add('active')
        setTimeout(() => {
          success.classList.remove('active')
        }, 1000)
      })
    })
  }
  const share = document.querySelectorAll('.join_news_select_content>ul>li ul li')
  const shareDetails = document.querySelectorAll('.job_btns')
  const shareDetailsList = document.querySelectorAll('.job_details_list ul li')
  jobShare(share)
  jobShare(shareDetails)
  jobShare(shareDetailsList)
  /**
   * 处理时间
   */
  function formatDate(timestamp) {
    const date = new Date(timestamp * 1000)
    const year = date.getFullYear()
    const month = (date.getMonth() + 1).toString().padStart(2, '0')
    const day = date.getDate().toString().padStart(2, '0')
    return `${year}-${month}-${day}`
  }
  /**
   * 新闻业邮箱订阅
   * 新闻关键词搜索
   */
  function newsEmailSubscription() {
    const form = document.querySelector('.add_mailbox form')
    const newsList = document.querySelector('.news_list ul')
    const Pagination = document.querySelector('.news_content .Pagination')
    let searchParams = new URLSearchParams(window.location.search)
    let keywords = searchParams.get('keywords')
    const baseURL = ''
    if (!form) return
    const script = document.createElement('script')
    script.src = 'https://www.dajin.cn/cn/statics/js/axios.min.js'
    script.onload = function () {
      if (keywords && keywords.length >= 1) {
        // 分页
        Pagination.innerHTML = ''
        axios.get(baseURL + '/api.php?op=news&siteid=1&act=ajax_getlist&page=1&keywords=' + keywords).then(res => {
          if (res.data.code === 0) {
            let datas = res.data.data
            newsList.innerHTML = ''
            let newsListHtml = ''
            for (let i = 0; i < datas.length; i++) {
              newsListHtml += `<li>
              <a href="${datas[i].url}">
                <div class="tit">
                  <i>${formatDate(datas[i].updatetime)}</i>
                  <span>${datas[i].title}</span>
                </div>
                <div class="content">${datas[i].description}</div>
              </a>
            </li>`
            }
            newsList.innerHTML = newsListHtml
          } else if (res.data.code === 1) {
            // 没有相关新闻
            newsList.innerHTML = '<div class="no-data">未找到结果</div>'
          }
        })
      }
      form.addEventListener('submit', function (event) {
        event.preventDefault() // 阻止表单的默认提交行为
        // 示例：获取email输入框的值
        let email = event.target.querySelector('input[type="email"]').value // 邮箱
        let dongtai = event.target.querySelector('#dongtai').checked ? '1' : ''
        let qushi = event.target.querySelector('#qushi').checked ? '2' : ''
        let huodong = event.target.querySelector('#huodong').checked ? '3' : ''
        let text = event.target.querySelector('#text').checked // 必选项
        let typeid = (dongtai + qushi + huodong).split('').join(',')
        if (!text && email.length > 1) {
          event.target.querySelector('.error').classList.add('active')
        } else if (typeid.length > 1 && email.length > 1) {
          axios.get(baseURL + '/api.php?op=dingyue&siteid=1&act=ajax_dingyue&email=' + email + '&typeid=' + typeid).then(res => {
            if (res.data.code === 0) {
              // 显示成功消息
              event.target.querySelector('.success').classList.add('active')
            }
          })
        }
      })
    }
    document.head.appendChild(script)
  }
  newsEmailSubscription()
  /**
   * 底部logo 滑动到顶部
   */
  function scrollToTop() {
    const scrollToTop = document.querySelector('.footer_logo')
    scrollToTop.addEventListener('click', () => {
      window.scrollTo({
        top: 0,
        behavior: 'smooth', // 平滑滚动效果
      })
    })
  }
  scrollToTop()
  /**
   * 加入我们搜索
   */
  function joinSearch() {
    const searchBtn = document.querySelector('.join_search .search_box button')
    if (!searchBtn) return
    const searchValue = document.querySelector('.join_search .search_box input')
    searchBtn.addEventListener('click', () => {
      if (searchValue.value.length > 0) {
        window.location.href = '/cn/index?m=content&c=index&a=lists&catid=15?value=' + searchValue.value
      }
    })
  }
  joinSearch()
})
