<template>
  <div class="home">
    <van-nav-bar title="设备确认" fixed left-arrow @click-left="onClickLeft" />

    <van-cell-group>
      <van-cell title="当前站点" :value="yhzInfo.mc" />
    </van-cell-group>

    <div class="content" v-if="pendingConfirmList">
      <van-cell-group>
        <van-cell
          v-for="(item, index) in pendingConfirmList"
          :key="index"
          :title="item.sbmc"
          is-link
          :label="`设备类型: ` + item.sblx"
          :to="{
            name: 'EquipDetail',
            params: {
              data: encodeURIComponent(
                JSON.stringify({
                  equipmentInfo: item,
                  yhzInfo: yhzInfo,
                  isConfirm: true,
                })
              ),
            },
          }"
        >
          <template #value>
            <span
              :class="[
                'status-tag',
                `status-` +
                  (item.sbzt === '完好'
                    ? 'good'
                    : item.sbzt === '损坏'
                    ? 'warning'
                    : 'danger'),
              ]"
              >{{ item.sbzt }}</span
            >
          </template>
        </van-cell>
      </van-cell-group>
    </div>
  </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 yhzInfo = ref({}); // 养护站详情数据
const pendingConfirmList = ref([]); // 待确认列表数据

// 获取养护站详情数据
onMounted(() => {
  yhzInfo.value = JSON.parse(decodeURIComponent(route.params.data));
  console.log("yhzInfo", toRaw(yhzInfo.value));
  getPendingConfirmList();
});

const getPendingConfirmList = async (sbmc) => {
  try {
    const data = {
      yhzid: yhzInfo.value.id,
      sbmc,
      pageNum: 1,
      pageSize: 9999,
    };
    const res = await request({
      url: "/snow-ops-platform/yjsb/pendingConfirmList",
      method: "get",
      params: data,
    });
    if (res.code === "00000") {
      pendingConfirmList.value = res.data.records;
    } else {
      throw new Error(res.message);
    }
  } catch (error) {
    showToast({
      type: "fail",
      message: error.message,
    });
    console.log("error", error);
  }
};

const onClickLeft = () => {
  router.push({
    name: "EquipManage",
    params: { data: encodeURIComponent(JSON.stringify(yhzInfo.value)) },
  });
};
</script>

<style>
.home {
  padding-top: var(--van-nav-bar-height); /* 自动匹配导航栏高度 */
}
.content {
  padding: 16px 16px 80px 16px;
}
.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;
}
</style>