<template>
  <div>
    <div v-show="!inAddScene" class="container">
      <sticky-bar>
        <template #left>
          <div class="title-bar" :class="{ active: scene == 0 }" @click="changeScene(0)">
            <van-badge dot>警事圈</van-badge>
          </div>
          <div
            class="title-bar"
            style="margin-left: 20px"
            :class="{ active: scene == 1 }"
            @click="changeScene(1)"
          >
            热榜
          </div>
        </template>
        <template #right>
          <img
            :src="getIconUrl('btn_search_black_24')"
            alt=""
            class="top-icon"
            style="margin-right: 15px"
          />
          <img :src="getIconUrl('btn_phone_black_24')" alt="" class="top-icon" @click="openAdd" />
        </template>
      </sticky-bar>

      <van-pull-refresh
        v-model="refreshLoading"
        :head-height="80"
        class="content"
        @refresh="onRefresh"
      >
        <!-- 下拉提示 -->
        <template #pulling="props">
          <div class="custom-refresh">
            <div class="refresh-progress-bar">
              <div
                class="progress"
                :style="{ width: `${Math.min((props.distance / 80) * 100, 100)}%` }"
              ></div>
            </div>
            <div class="refresh-text">
              <van-icon
                name="arrow-down"
                :style="{ transform: `rotate(${Math.min((props.distance / 80) * 180, 180)}deg)` }"
              />
              <span>{{ props.distance > 70 ? '松开刷新' : '下拉刷新' }}</span>
            </div>
          </div>
        </template>

        <!-- 释放提示 -->
        <template #loosing>
          <div class="custom-refresh">
            <div class="refresh-progress-bar">
              <div class="progress complete"></div>
            </div>
            <div class="refresh-text">
              <van-icon name="arrow-up" />
              <span>松开刷新</span>
            </div>
          </div>
        </template>

        <!-- 加载提示 -->
        <template #loading>
          <div class="custom-refresh">
            <div class="refresh-loading">
              <div class="loading-circle"></div>
            </div>
            <div class="refresh-text">
              <span>正在刷新...</span>
            </div>
          </div>
        </template>

        <van-list v-model:loading="loading" :finished="finished" finished-text="我是有底线的~">
          <PoliceAffairsItem
            v-for="item in 10"
            :key="item"
            @comment="handleComment"
          ></PoliceAffairsItem>
        </van-list>
        <van-back-top right="2vw" bottom="10vh" />
      </van-pull-refresh>
    </div>

    <Transition
      enter-active-class="animate__animated animate__fadeInRight"
      leact-active-class="animate__animated animate__fadeOutRight"
    >
      <AddCircleVue v-if="inAddScene" @push-jsq="pushJsq"></AddCircleVue>
    </Transition>

    <!-- 添加共享的评论弹出层 -->
    <van-popup
      v-model:show="showCommentPopup"
      position="bottom"
      :style="{ padding: '10px' }"
      @close="commentText = ''"
    >
      <div class="comment-input-container">
        <input
          ref="commentInputRef"
          v-model="commentText"
          placeholder="说点什么..."
          class="comment-input"
        />
        <van-button type="primary" size="small" @click="submitComment"> 发送 </van-button>
      </div>
    </van-popup>
  </div>
</template>

<script setup>
  import { getIconUrl } from '@/utils/utils'
  import PoliceAffairsItem from '@/views/WarningCircle/PoliceAffairsItem.vue'
  import AddCircleVue from './WarningCircle/AddCircle.vue'
  import useShowFooterStore from '@/store/modules/showFooter'
  import { nextTick, getCurrentInstance } from 'vue'

  const { proxy } = getCurrentInstance()
  // 初始化底部导航栏状态管理
  const showFooterStore = useShowFooterStore()
  import { ref } from 'vue'
  const scene = ref(0)
  const inAddScene = ref(false)

  // 评论相关状态
  const showCommentPopup = ref(false)
  const commentText = ref('')
  const commentInputRef = ref(null)
  const currentCommentItem = ref(null)
  // const listData = ref([]) // 这里应该是你的列表数据

  // 处理评论点击
  const handleComment = item => {
    currentCommentItem.value = item
    showCommentPopup.value = true

    // 等待弹窗显示后再聚焦输入框
    nextTick(() => {
      if (commentInputRef.value) {
        commentInputRef.value.focus()
      }
    })
  }

  // 提交评论
  const submitComment = () => {
    if (commentText.value.trim()) {
      proxy.$showSuccessToast('评论成功')
      // 这里可以添加提交评论的逻辑
      // 例如：发送API请求，更新当前评论的项等
    } else {
      proxy.$showFailToast('评论内容不能为空')
      return
    }
    showCommentPopup.value = false
  }

  const changeScene = value => {
    scene.value = value
  }
  const loading = ref(false)
  const finished = ref(true)

  const pushJsq = () => {
    inAddScene.value = false
    showFooterStore.setShowFooter(true)
  }

  const openAdd = () => {
    inAddScene.value = true
    showFooterStore.setShowFooter(false)
  }

  const refreshLoading = ref(false)
  const onRefresh = () => {
    setTimeout(() => {
      proxy.$showSuccessToast('刷新成功')
      refreshLoading.value = false
    }, 1000)
  }
</script>

<style lang="scss" scoped>
  .container {
    width: 100%;
    height: 100vh;
    background: linear-gradient(to left bottom, #cfeafe, #ffffff);
    .title-bar {
      color: rgba(102, 102, 102, 1);
      font-size: 18px;
      position: relative;
      transition: all 0.2s;
      .red-dot {
        position: absolute;
        right: -6px;
        top: -2px;
        width: 10px;
        height: 10px;
        border-radius: 50%;
        background-color: red;
      }
      &.active {
        color: #111518;
        font-weight: bold;
      }
    }
    .content {
      background-color: #fff;
      border-radius: 20px 20px 0 0;
      width: 100%;
      height: calc(100% - 45px - 50px);
      overflow-y: auto;
      padding: 10px;
      box-sizing: border-box;
      scrollbar-width: none; /* Firefox */
      -ms-overflow-style: none; /* IE/Edge */
      &::-webkit-scrollbar {
        display: none; /* Chrome/Safari/Opera */
      }
      // 自定义下拉刷新样式
      .custom-refresh {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        height: 80px;
        padding: 0 20px;

        .refresh-progress-bar {
          width: 100%;
          height: 4px;
          background-color: #f5f5f5;
          border-radius: 2px;
          overflow: hidden;
          margin-bottom: 15px;

          .progress {
            height: 100%;
            background: linear-gradient(to right, #4facfe, #00f2fe);
            border-radius: 2px;
            transition: width 0.2s ease;

            &.complete {
              width: 100%;
              animation: progress-flash 1s infinite;
            }
          }
        }

        .refresh-loading {
          width: 30px;
          height: 30px;
          position: relative;
          margin-bottom: 15px;

          .loading-circle {
            width: 100%;
            height: 100%;
            border: 3px solid transparent;
            border-top-color: #4facfe;
            border-radius: 50%;
            animation: spin 1s linear infinite;

            &:before,
            &:after {
              content: '';
              position: absolute;
              border: 3px solid transparent;
              border-radius: 50%;
            }

            &:before {
              top: 2px;
              left: 2px;
              right: 2px;
              bottom: 2px;
              border-top-color: #00f2fe;
              animation: spin 1.5s linear infinite;
            }
          }
        }

        .refresh-text {
          display: flex;
          align-items: center;
          font-size: 14px;
          color: #666;

          .van-icon {
            margin-right: 6px;
            transition: transform 0.3s;
          }
        }
      }

      :deep(.van-list__finished-text) {
        padding-bottom: 20px;
      }

      @keyframes progress-flash {
        0%,
        100% {
          opacity: 1;
        }
        50% {
          opacity: 0.6;
        }
      }

      @keyframes spin {
        0% {
          transform: rotate(0deg);
        }
        100% {
          transform: rotate(360deg);
        }
      }
    }
  }

  .comment-input-container {
    display: flex;
    align-items: center;
    width: 100%;
    padding: 10px 0;

    .comment-input {
      flex: 1;
      height: 36px;
      border: 1px solid #eee;
      border-radius: 18px;
      padding: 0 15px;
      margin-right: 10px;
      font-size: 14px;
      outline: none;

      &:focus {
        border-color: #4e75c6;
      }
    }
  }

  .right-in-enter-active {
    animation: right-in 0.3s;
  }
  .right-in-leave-active {
    animation: right-in 0.3s reverse;
  }

  @keyframes right-in {
    from {
      transform: translateX(100%);
      opacity: 0;
    }
    to {
      transform: translateX(0);
      opacity: 1;
    }
  }
</style>
