<template>
  <div>
    <TopNav navTitle="警情列表" :showRight="false" :showLeft="true" />
    <van-sticky>
      <div class="sticky_box">
        <Tabs :status="status" @TabsItem="onSelect" :key="tabsIndex"></Tabs>
        <Search
          placeholder="请输入关键字"
          v-model="kwd"
          :isSx="true"
          @update:sx="showPopup = !showPopup"
          @update:modelValue="onSearch"
        ></Search>
      </div>
    </van-sticky>
    <SxPopup
      :showPopup="showPopup"
      :list="yjxx.sxList"
      :p_top="145"
      @update:close="showPopup = false"
      @update:onConfirm="onConfirm"
    />
    <van-pull-refresh v-model="loadingPull" @refresh="onRefresh">
      <van-list
        v-model:loading="loading"
        :finished="finished"
        finished-text=" "
        @load="onLoad"
        offset="3"
        :immediate-check="false"
      >
        <List
          v-for="(item, index) in yjxx.list"
          :key="index"
          :item="item"
          path="/newTwoHome/jq/xzjqDetail"
        />
        <van-empty
          description="没有警情信息"
          image="default"
          v-if="yjxx.list.length <= 0 && showEmpty"
        />
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
export default {
  name: "newTwoHome/jq/xzjqlist",
};
</script>
<script setup>
import axios from "axios";
import { baseUrl2 } from "../../../utils/request";
import TopNav from "../../../components/topNav.vue";
import Search from "../../../components/search.vue";
import Tabs from "../../../components/tabs/tabs.vue";
import SxPopup from "../../../components/SxPopup.vue";
import List from "../../../components/xzjqList.vue";
import { setTimeQuantum, dateFormat } from "../../../utils/tools.js";
import { getDictList, setDict } from "../../../utils/dict";
import { ref, reactive, onMounted, watch, onActivated } from "vue";
import { getJqList, getStatistics } from "../../../api/jqxx";
import { useRoute, onBeforeRouteLeave } from "vue-router";
import router from "../../../router";
const status = ref(1);
const showEmpty = ref(false);
const loading = ref(false);
const total = ref(0);
const tabsIndex = ref(1);
const params = ref({
  pageNum: 1,
  pageSize: 20,
  bjnr: "",
  endTime: dateFormat(),
  startTime: dateFormat("z"),
});
const yjxx = reactive({
  sxList: [setTimeQuantum()], //筛选条件数据
  list: [], //预警列表数据,
});
const kwd = ref("");
const finished = ref(false);
const loadingPull = ref(false);
const showPopup = ref(false); //筛选弹窗
onMounted(() => {
  if (useRoute().query.status) {
    status.value = useRoute().query.status;
    onSelect();
  }
});
//下拉刷新
function onRefresh() {
  yjxx.list = [];
  tabsIndex.value++;
  params.value.pageNum = 1;
  params.value.bjnr = "";
  params.value.endTime = dateFormat();
  params.value.startTime = dateFormat("z");
  onSelect();
}
//触底加载
function onLoad() {
  if (params.value.pageNum >= total.value) {
    finished.value = true;
  } else {
    params.value.pageNum++;
    onSelect();
  }
}

/**
 * tab选择
 * @param {Object} val
 */
function onSelect(val) {
  if (val || val == 0) {
    status.value = val + 1;
    yjxx.list = [];
    params.value.pageNum = 1;
    params.value.bjnr = "";
    finished.value = false;
  }
  loading.value = true;
  let data = {
    orgId: JSON.parse(window.localStorage.getItem("userInfo")).deptList[0]
      .deptCode,
    startTime: `${params.value.startTime} 16:00:00`,
    endTime: `${params.value.endTime} 16:00:00`,
    type: status.value,
    keyword: params.value.bjnr,
    currPage: params.value.pageNum,
    pageSize: params.value.pageSize,
  };
  axios
    .post(`${baseUrl2}/xz1Api/api/alarm/list `, data)
    .then((res) => {
      loading.value = false;
      loadingPull.value = false;
      if (res.data && res.data.data.list.length > 0) {
        yjxx.list = [...res.data.data.list, ...yjxx.list];
        total.value = res.data.data.totalPage;
      } else {
        showEmpty.value = true;
      }
    })
    .catch((err) => {
      loading.value = false;
      showEmpty.value = true;
    });
}
/**
 * 关键字搜索
 * @param {Object} val
 */
function onSearch(val) {
  yjxx.list = [];
  params.value.endTime = dateFormat();
  params.value.startTime = dateFormat("z");
  params.value.pageNum = 1;
  params.value.bjnr = val;
  finished.value = false;
  onSelect();
}
function onConfirm(val) {
  yjxx.list = [];
  params.value.startTime = `${val.startTime}`;
  params.value.endTime = `${val.endTime}`;
  showPopup.value = false;
  params.value.pageNum = 1;
  params.value.bjnr = "";
  finished.value = false;
  onSelect();
}
</script>

<style>
</style>
