<template>
  <van-nav-bar
    fixed
    title="场所活动"
    left-text=""
    left-arrow
    @click-left="onClickLeft"
    @click-right="onClickRight"
  >
    <template #right>
      <van-icon name="plus" size="18" />
    </template>
  </van-nav-bar>
  <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">
            <div class="list_item" @click="goInfo(item.id)">
              <div class="item-left">
                <p v-show="item.activityTime != ''" style="color: #c4c4c4">
                  {{ item.activityTime }}
                </p>
                <p style="color: red">待审核</p>
              </div>
              <van-cell>
                <template #title>
                  <van-text-ellipsis
                    rows="2"
                    :content="`${item.reporter.name}申请于${item.placeDel}举办${item.activityName}活动`"
                  />
                </template>
                <template #label>
                  {{ item.place }}
                </template>
              </van-cell>
            </div>
            <template #right>
              <van-button
                square
                type="danger"
                text="删除"
                class="button"
                @click="deleteItem(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 v-show="item.activityTime != ''" style="color: #c4c4c4">
                  {{ item.activityTime }}
                </p>
                <p style="color: red">已审核</p>
              </div>
              <van-cell>
                <template #title>
                  <van-text-ellipsis
                    rows="2"
                    :content="`${item.reporter.name}申请于${item.placeDel}举办${item.activityName}活动`"
                  />
                </template>
                <template #label>
                  {{ item.place }}
                </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 v-show="item.activityTime != ''" style="color: #c4c4c4">
              {{ item.activityTime }}
            </p>
            <p style="color: red">
              <span v-if="item.assessment == 1">已审核</span>
              <span v-else>待审核</span>
            </p>
          </div>
          <van-cell>
            <template #title>
              <van-text-ellipsis
                rows="2"
                :content="` ${item.reporter.name}申请于${item.placeDel}举办${item.activityName}活动`"
              />
            </template>
            <template #label>
              {{ item.place }}
            </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 placeActivityServer from "@/api/placeActivity/placeActivityServer";
import { useRouter } from "vue-router";
export default {
  name: "placeActivityList",
  setup() {
    // 返回
    const onClickLeft = () => {
      history.back();
    };
    let router = useRouter();
    // 上报
    const onClickRight = () => {
      router.push("placeActivity");
    };
    // 切换
    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 placeActivityServer()
        .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 placeActivityServer()
        .list({
          current: 1,
          size: 10000,
          activityName: 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 deleteItem = (val) => {
      showDel.value = true;
      ids.value = val;
    };
    // 删除
    const Itemdel = () => {
      new placeActivityServer().delete(ids.value).then((res) => {
        console.log("删除", res);
        index = 0;
        loading.value = true;
        list.value = [];
        readList.value = [];
        onLoad();
      });
    };
    // 修改跳转
    const updateItem = (val) => {
      router.push({
        path: "/placeActivity",
        query: { id: val.id },
      });
    };
    // 详情跳转
    const goInfo = (val) => {
      router.push({
        path: "/placeActivityInfo",
        query: { id: val },
      });
    };
    return {
      active,
      onClickLeft,
      list,
      readList,
      onLoad,
      loading,
      finished,
      searchShow,
      searchVal,
      searchList,
      onSearch,
      onCancel,
      onClickRight,
      deleteItem,
      updateItem,
      goInfo,
      showDel,
      Itemdel,
    };
  },
};
</script>

<style scoped>
</style>
