<template>
  <view>
    <view class="searcher">
      <view>
        <u-search
          v-model="searchForm.keyword"
          placeholder="请输入关键字"
          shape="round"
          bg-color="#f7f7f8"
          action-text="搜索"
          @search="fnResetPaging"
          @custom="fnResetPaging"
        />
      </view>
    </view>

    <view class="info_mainer">
      <view v-if="list.length > 0">
        <u-list @scrolltolower="scrolltolower">
          <u-list-item
            v-for="item in list"
            :key="item.DISASTER_PREVENTION_INSPECT_CONTENT_ID"
            class="wrap"
          >
            <view class="tit">
              <view class="u-line-2">{{ item.ACTIVITY_TASK_NAME }}</view>
            </view>
            <view class="text">
              <text>上报单位:</text>
              {{ item.DEPARTMENT_NAME }}
            </view>
            <view class="text">
              <text>上报时间:</text>
              {{ item.CREATTIME }}
            </view>
            <view class="text">
              <text>上报人:</text>
              {{ item.CREATOR }}
            </view>
            <view class="bottem">
              <u-button
                text="查看"
                color="linear-gradient(to right, #27a0ff, #2a56f7)"
                custom-style="width:150upx; height:60upx; margin-left: 20upx;margin-right: 0;"
                @click="
                  $u.route({
                    url: '/pages/disaster_prevention/inspect_record_view',
                    params: {
                      DISASTER_PREVENTION_INSPECT_ID:
                        item.DISASTER_PREVENTION_INSPECT_ID,
                      DISASTER_PREVENTION_ACTIVITY_TASK_ID:
                        DISASTER_PREVENTION_ACTIVITY_TASK_ID,
                    },
                  })
                "
              />
              <u-button
                v-if="item.ISREPORT"
                text="删除"
                type="error"
                custom-style="width:150upx; height:60upx; margin-left: 20upx;margin-right: 0;"
                @click="fnDelete(item.DISASTER_PREVENTION_INSPECT_ID)"
              />
              <!--              <u-button-->
              <!--                text="上报"-->
              <!--                color="linear-gradient(to right, #27a0ff, #2a56f7)"-->
              <!--                custom-style="width:150upx; height:60upx; margin-left: 20upx;margin-right: 0;"-->
              <!--                @click="-->
              <!--                  $u.route({-->
              <!--                    url: '/pages/disaster_prevention/add_inspect',-->
              <!--                    params: {-->
              <!--                      DISASTER_PREVENTION_INSPECT_ID:-->
              <!--                        item.DISASTER_PREVENTION_INSPECT_ID,-->
              <!--                      DISASTER_PREVENTION_ACTIVITY_TASK_ID:-->
              <!--                        DISASTER_PREVENTION_ACTIVITY_TASK_ID,-->
              <!--                    },-->
              <!--                  })-->
              <!--                "-->
              <!--              />-->
            </view>
          </u-list-item>
        </u-list>
      </view>
      <empty v-else />
    </view>
  </view>
</template>

<script>
import { getDisasterPreventionInspectionEnterpriseList } from "@/api";
import { setDisasterPreventionInspectionRecordDelete } from "../../api";

export default {
  data() {
    return {
      DISASTER_PREVENTION_ACTIVITY_ID: "",
      DISASTER_PREVENTION_ACTIVITY_TASK_ID: "",
      list: [],
      pageSize: 10,
      currentPage: 1,
      totalPage: 0,
      searchForm: {
        keyword: "",
      },
    };
  },
  onLoad(query) {
    this.DISASTER_PREVENTION_ACTIVITY_ID =
      query.DISASTER_PREVENTION_ACTIVITY_ID;
    this.DISASTER_PREVENTION_ACTIVITY_TASK_ID =
      query.DISASTER_PREVENTION_ACTIVITY_TASK_ID;
  },
  // created() {
  //   this.fnGetData();
  // },
  onShow() {
    this.list = [];
    this.pageSize = 10;
    this.currentPage = 1;
    this.totalPage = 0;
    this.fnGetData();
  },
  computed: {
    userInfo() {
      return this.$store.getters.getUserInfo;
    },
  },
  methods: {
    async fnGetData() {
      const resData = await getDisasterPreventionInspectionEnterpriseList({
        showCount: this.pageSize,
        currentPage: this.currentPage,
        DISASTER_PREVENTION_ACTIVITY_TASK_ID:
          this.DISASTER_PREVENTION_ACTIVITY_TASK_ID,
        KEYWORDS: this.searchForm.keyword,
      });
      // 当前时间
      const currentDateStr = new Date().toISOString().slice(0, 10);
      resData.varList.forEach((item) => {
        const time1 = new Date(item.ACTIVITY_TIME_BEGIN);
        const time2 = new Date(item.ACTIVITY_TIME_END);
        if (
          time1.getTime() <= new Date(currentDateStr).getTime() &&
          new Date(currentDateStr).getTime() <= time2.getTime()
        ) {
          item.ISREPORT = true;
        } else {
          item.ISREPORT = false;
        }
      });
      this.list = [...this.list, ...resData.varList];
      this.totalPage = resData.page.totalPage;
    },
    scrolltolower() {
      this.currentPage++;
      if (this.totalPage >= this.currentPage) this.fnGetData();
    },
    fnResetPaging() {
      this.list = [];
      this.currentPage = 1;
      this.fnGetData();
    },
    fnDelete(DISASTER_PREVENTION_INSPECT_ID) {
      uni.showModal({
        title: "提示",
        content: "确认要删除吗？",
        success: async (res) => {
          if (res.confirm) {
            await setDisasterPreventionInspectionRecordDelete({
              OPERATOR: this.userInfo.NAME,
              DISASTER_PREVENTION_INSPECT_ID,
            });
            setTimeout(() => {
              uni.$u.toast("删除成功");
            });
            this.fnResetPaging();
          }
        },
      });
    },
  },
};
</script>

<style scoped lang="scss">
.searcher {
  width: 100%;
  background: #ffffff;
  padding: 20upx;
  box-sizing: border-box;
  margin-top: 20upx;
  border-bottom: 1px solid #eeeeee;

  .mainer {
    width: 100%;
    display: flex;
    justify-content: space-between;
    margin-top: 20upx;
    align-items: center;
    font-size: 28upx;
  }
}

.info_mainer {
  width: 100%;
  margin-top: 20upx;

  .wrap {
    width: 100%;
    background: #ffffff;
    margin-bottom: 20upx;
    padding: 20upx;
    line-height: 2;
    box-sizing: border-box;

    .tit {
      font-weight: bold;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .text text {
      color: #999999;
      margin-right: 10upx;
    }

    .bottem {
      width: 100%;
      border-top: 1px solid #eeeeee;
      margin-top: 20upx;
      display: flex;
      justify-content: flex-end;
      padding: 20upx 0 0;
    }
  }
}
</style>
