<template>
  <van-nav-bar
    fixed
    title="信教群众异常"
    right-text="上报"
    left-arrow
    @click-left="onClickLeft"
    @click-right="onClickRight"
  />
  <div class="main">
    <van-search
      v-model="searchVal"
      clearable
      show-action
      shape="round"
      placeholder="请输入搜索关键词"
      label="姓名"
      input-align="center"
      @search="onSearch"
      @cancel="onCancel"
    />
    <van-tabs
      v-show="!searchShow"
      v-model:active="active"
      title-inactive-color="#bdbdbd"
      title-active-color="#36a7f3"
    >
      <van-tab title="未审核" name="0">
        <van-list
          v-model:loading="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <van-swipe-cell
            v-for="item in list"
            :key="item"
            :before-close="beforeClose"
          >
            <div class="list_item" @click="goInfo(item.id)">
              <div class="item-left">
                <p style="color: #c4c4c4">{{ item.partyTime }}</p>
                <p style="color: red">待审核</p>
              </div>
              <van-cell is-link>
                <template #title>
                  <p>异常人员：{{ item.abnormalName }}</p>
                  <p>身份证号：{{ item.abnormalIdcar }}</p>
                  <p>异常行为：{{ item.abnormalBehavior }}</p>
                </template>
              </van-cell>
            </div>
            <template #right>
              <van-button
                square
                type="danger"
                text="删除"
                class="button"
                @click="beforeClose(item.id)"
              />
              <van-button
                square
                type="primary"
                text="修改"
                class="button"
                @click="updateItem(item)"
              />
            </template>
          </van-swipe-cell>
        </van-list>
      </van-tab>
      <van-tab title="已审核" name="1">
        <van-list
          v-model:loading="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <van-swipe-cell v-for="item in readList" :key="item">
            <div class="list_item" @click="goInfo(item.id)">
              <div class="item-left">
                <p style="color: #c4c4c4">{{ item.partyTime }}</p>
                <p style="color: red">已审核</p>
              </div>
              <van-cell is-link>
                <template #title>
                  <p>异常人员：{{ item.abnormalName }}</p>
                  <p>身份证号：{{ item.abnormalIdcar }}</p>
                  <p>异常行为：{{ item.abnormalBehavior }}</p>
                </template>
              </van-cell>
            </div>
          </van-swipe-cell>
        </van-list>
      </van-tab>
    </van-tabs>
    <!-- 搜索结果 -->
    <van-list
      v-show="searchShow"
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-swipe-cell v-for="item in searchList" :key="item">
        <div class="list_item" @click="goInfo(item.id)">
          <div class="item-left">
            <p style="color: #c4c4c4">{{ item.partyTime }}</p>
            <p style="color: red">已审核</p>
          </div>
          <van-cell is-link>
            <template #title>
              <p>异常人员：{{ item.abnormalName }}</p>
              <p>身份证号：{{ item.abnormalIdcar }}</p>
              <p>异常行为：{{ item.abnormalBehavior }}</p>
            </template>
          </van-cell>
        </div>
      </van-swipe-cell>
    </van-list>
    <van-dialog
      v-model:show="showDel"
      show-cancel-button
      confirmButtonText="删除"
      message="请确认删除该记录"
      @confirm="Itemdel"
    >
    </van-dialog>
  </div>
</template>
    
<script>
import { ref } from "vue";
import ReligiousPeopleReportService from "@/api/differentbelievers/ReligiousPeopleReportService";
import { useRouter } from "vue-router";
export default {
  name: "ReligiousPeopleReportList",
  setup() {
    const onClickLeft = () => {
      history.back();
    };
    // 上报
    const onClickRight = () => {
      router.push("/differentbelievers");
    };
    // 切换
    let active = ref(0);
    // 未审核列表
    let list = ref([]);
    // 已审核列表
    let readList = ref([]);
    const loading = ref(false);
    const finished = ref(false);
    let index = 0;
    const onLoad = () => {
      loading.value = true;
      finished.value = false;
      // 异步更新数据
      new ReligiousPeopleReportService()
        .list({
          current: index + 1,
          size: 10,
        })
        .then(({ records, total }) => {
          records.forEach((item) => {
            if (item.assessment == 0) {
              list.value.push(item);
            } else {
              readList.value.push(item);
            }
          });
          // 加载状态结束
          loading.value = false;
          let page = parseInt(total / 10);

          // 数据全部加载完成
          if (index >= page || total < 10) {
            finished.value = true;
          } else {
            index++;
          }
        });
    };
    // 搜索
    let searchShow = ref(false);
    let searchList = ref([]);
    const searchVal = ref("");
    const onSearch = () => {
      searchShow.value = true;
      searchList.value = [];
      new ReligiousPeopleReportService()
        .list({
          current: 1,
          size: 10000,
          abnormalName: searchVal.value,
        })
        .then(({ records }) => {
          searchList.value.push(...records);
          // 加载状态结束
          loading.value = false;
        });
    };
    const onCancel = () => {
      searchVal.value = "";
      searchShow.value = false;
    };
    // 删除确认
    let showDel = ref(false);
    let ids = ref("");
    const beforeClose = (val) => {
      showDel.value = true;
      ids.value = val;
    };
    // 删除
    const Itemdel = () => {
      new ReligiousPeopleReportService().delete(ids.value).then((res) => {
        console.log("删除", res);
        index = 0;
        loading.value = true;
        list.value = [];
        readList.value = [];
        onLoad();
      });
    };
    let router = useRouter();
    // 修改跳转
    const updateItem = (val) => {
      router.push({
        path: "/differentbelievers",
        query: { id: val.id },
      });
    };
    // 详情跳转
    const goInfo = (val) => {
      router.push({
        path: "/differentbelieversInfo",
        query: { id: val },
      });
    };
    return {
      active,
      onClickLeft,
      list,
      readList,
      onLoad,
      loading,
      finished,
      searchShow,
      searchVal,
      searchList,
      onSearch,
      onCancel,
      onClickRight,
      updateItem,
      goInfo,
      showDel,
      beforeClose,
      Itemdel,
    };
  },
};
</script >
    
<style scope>
.main {
  background: #fff;
  position: relative;
  top: 40px;
}
.van-list {
  height: 80%;
  margin-top: 5px;
}
.list_item {
  display: flex;
  background: #fff;
}
.item-left {
  text-align: center;
  width: 30%;
  font-size: 12px;
  border-right: 1px solid #eee;
}
.button {
  height: 100%;
}
</style>