<template>
  <div class="home">
    <van-nav-bar title="冰雪灾害" fixed left-arrow @click-left="onClickLeft">
    </van-nav-bar>
    <van-search
      shape="round"
      v-model="searchValue"
      :show-action="false"
      placeholder="请输入地点关键词"
    />
    <van-cell-group>
      <van-cell title="当前站点" :value="yhzDetail.mc" />
    </van-cell-group>

    <div class="content">
      <van-cell-group>
        <van-cell
          v-for="(item, index) in eventList"
          center
          :key="index"
          :title="item.occurLocation"
          is-link
          :label="`填报时间：${item.reportTime}`"
          :value="`填报人：${item.reporterName}`"
          :to="{
            name: 'IceEventDetail',
            params: {
              data: encodeURIComponent(
                JSON.stringify({
                  yhzDetail: yhzDetail,
                  event: item,
                })
              ),
            },
          }"
        >
        </van-cell>
      </van-cell-group>
    </div>

    <van-button type="primary" class="add-btn" icon="plus" @click="handleAdd">
      冰雪填报
    </van-button>
  </div>
</template>
    
    <script setup>
import { ref, onMounted, reactive, toRaw, watch } from "vue";
import { useRouter, useRoute } from "vue-router";
import { showToast, showLoadingToast } from "vant";
import { request } from "../../../../shared/utils/request";

const router = useRouter();
const route = useRoute();
const searchValue = ref(""); // 搜索框输入值
const yhzDetail = ref({}); // 养护站详情数据
const eventList = ref([]); // 冰雪灾害列表


// 根据养护站rid获取冰雪事件列表
const getIceEventList = async (occurLocation) => {
  try {
    const yhzid = yhzDetail.value.id;
    if (!yhzid) {
      return;
    }
    const data = {
      yhzid,
      occurLocation,
      paageNum: 1,
      paageSize: 9999,
    };
    const res = await request({
      url: "/snow-ops-platform/event/list",
      method: "GET",
      params: data,
    });
    if (res.code && res.code === "00000") {
      eventList.value = res.data.records;
    } else {
      throw new Error(res.message);
    }
  } catch (error) {
    showToast({
      type: "error",
      message: error.message || "获取物资列表失败",
    });
  }
};

// 组件挂载时获取数据
onMounted(() => {
  yhzDetail.value = JSON.parse(decodeURIComponent(route.params.data));
  console.log("yhzDetail", toRaw(yhzDetail.value));
  getIceEventList();
});

watch(
  () => searchValue.value,
  (newVal, oldVal) => {
    if (newVal !== oldVal) {
      getIceEventList(newVal);
    }
  }
);

const onClickLeft = () => {
  router.push("/");
};

const handleAdd = () => {
  router.push({
    name: "IceEventAdd",
    params: { data: encodeURIComponent(JSON.stringify(yhzDetail.value)) },
  })
};

</script> 
    
    <style scoped>
.home {
  padding-top: var(--van-nav-bar-height); /* 自动匹配导航栏高度 */
}

.content {
  padding: 16px 16px 80px 16px;
}

.content .van-cell-group .van-cell {
  margin-bottom: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.add-btn {
  position: fixed;
  bottom: 20px;
  left: 16px;
  right: 16px;
  width: calc(100% - 32px);
  margin: 0 auto;
  border-radius: 24px;
  font-size: 16px;
  height: 44px;
  z-index: 999;
}

.grid {
  margin-top: 16px;
}

.btn {
  margin-top: 24px;
}

.status-tag {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 4px;
  color: white;
  font-size: 12px;
}
.status-good {
  background-color: #07c160;
}
.status-warning {
  background-color: #ff976a;
}
.status-danger {
  background-color: #ee0a24;
}

.materialAddForm {
  padding: 16px 16px 80px 16px;
}
</style>
    