<template>
  <van-sticky>
    <van-nav-bar title="即查即改" />
  </van-sticky>
  <!-- <div class="listViewContain"> -->
  <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      class="listDiv deep"
    >
      <van-swipe-cell v-for="item in list" :key="item.id" >
        <van-cell
          :title="item.code"
          @click="To(item)"
        >
          <template #label>
            <div>隐患管控等级: {{ item.troublecontrolLevel || "未知" }}</div>
            <div>发现时间: {{ item.findDate }}</div>
            <div>处理时间: {{ item.handleDate }}</div>
          </template>
          <template #value>
            <!-- 当前状态 -->
            <div>{{ item.status }}</div>
          </template>
        </van-cell>
      </van-swipe-cell>
    </van-list>
  </van-pull-refresh>
  <!-- </div> -->
  <van-back-top bottom="12vh" />
</template>

<script setup>
import { onMounted, ref, reactive } from "vue";
import { useRouter, useRoute } from "vue-router";
import { CheckList, missionFinish, getDictionary } from "@/api/check";
import "@/assets/custom.less";
const route = useRoute();
const router = useRouter();
const isfinish = ref(false);
const pageNo = ref(1);
const pageSize = ref(10);
const loading = ref(false);
const finished = ref(false);
const refreshing = ref(false);

const list = reactive([]);
const status = reactive([]);
const statusDict = () => {
  try {
    getDictionary("check_task_status").then((res) => {
      // console.log("check_task_status 检查项状态字典", res);
      if (res) {
        status.push(...res);
      }
    });
  } catch (error) {
    console.error("获取字典失败:", error);
  }
};
const formatStatus = (id) => {
  let findObj = status.find((item) => item.dictValue === id);
  if (findObj) {
    return status.find((item) => item.dictValue === id).dictLabel;
  } else {
    return "未知";
  }
};
const onLoad = async () => {
  if (refreshing.value) {
    list.length = 0;
    pageNo.value = 1;
    refreshing.value = false;
  }
  const params = {
    organizationName: "",
    checkReason: "",
    checkPerson: "",
    pageNo: pageNo.value,
    pageSize: pageSize.value,
  };
  try {
    const res = await CheckList(params);
    console.log("检查任务列表", res);
    // console.log("页码：", pageNo.value);
    const data = res.list;
    if (pageSize.value * pageNo.value < res.count) {
      list.push(...data);
      pageNo.value++;
    } else {
      list.push(...data);
      finished.value = true;
    }
    loading.value = false;
  } catch (error) {
    showFailToast("加载失败，请重试");
    console.log(error);
    finished.value = true;
  } finally {
    loading.value = false;
  }
};
async function To(item) {
  let taskid = item.id;
  if(formatStatus(item.status)==="已检查"){
    isfinish.value = true;
  }
  router.push({
    path: "/jcrw-jcx",
    query: { 
      id: taskid,
      band:isfinish.value,
      templateName:item.templateName
     },
  });
}
const onRefresh = async () => {
  finished.value = false;
  // 重新加载数据
  // 将 loading 设置为 true，表示处于加载状态
  loading.value = true;
  onLoad();
};
async function finish(id){
  // 确认要更新检查结果为完成吗？完成后不可修改检查内容
  showConfirmDialog({
  title: '完成任务',
  message: '确认要更新检查结果为完成吗？完成后不可修改检查内容。'
})
  .then(async() => {
    // console.log('用户确认操作');
    let missionid = id;
    const res = await missionFinish(missionid);
    if(res.code===200){
      // console.log("任务完成", res.message)
    }else{
      showFailToast(res.message)
    }
  })
  .catch(() => {
    // console.log('用户取消操作');
  });
}
onMounted(() => {
  statusDict();
});
</script>
<style lang="less" scoped>
.deep ::v-deep .van-cell__value {
  flex: none;
}
.van-swipe-cell__left {
  height: 100%;
  display: flex;
}
.van-swipe-cell__left .van-button {
  flex: 1;
  // width: 100%;
  width: 60px;
  font-size: 16px;
  height: 100%;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
