<template>
  <!-- <headers title="设置考勤定位" /> -->
  <div class="set-position-container">
    <u-tabs
      :list="tabsState.list"
      :is-scroll="false"
      v-model="tabsState.current"
      bg-color="#f5f5f5"
      @change="tabsChange"
    />
    <!-- 搜索 -->
    <uni-search-bar
      placeholder="课程名称/班级名称"
      bgColor="#ffffff"
      clearButton="auto"
      cancelButton="none"
      @confirm="search"
      @clear="clear"
    />
    <!-- 内容 -->
    <view v-if="positionList.length">
      <text style="margin-bottom: 20rpx; display: block"
        >为你找到{{ listQuery.total }} 个的课程</text
      >

      <block v-for="(item, index) in positionList" :key="index">
        <setPositionItem :positionItem="item" :current="tabsState.current" />
      </block>
      <view style="padding: 20rpx">
        <u-loadmore :status="status" icon-type="flower" />
      </view>
    </view>
    <view v-else class="empty">
      <u-empty text="暂无数据" mode="list" />
    </view>
  </div>
</template>

<script setup lang="ts">
// import headers from "@/components/header/index.vue"
import setPositionItem from "./components/setPositionItem.vue"
import { getLocationList } from "@/api/position"
import { useGlobalStore } from "@/store/useStore"

const globalStore = useGlobalStore()

const tabsState = reactive({
  list: [
    {
      name: "未设置",
    },
    {
      name: "已设置",
    },
  ],
  current: 0,
})

const positionList: any = ref([] as any)
const listQuery = reactive({
  Phone: globalStore.Phone,
  pageIndex: 1,
  pageSize: 10,
  total: 0,
  type: 0,
  Name: "",
})
const status = ref("loadmore")
const tabsChange = (index: number) => {
  positionList.value = []
  if (index) {
    listQuery.type = 1
  } else {
    listQuery.type = 0
  }
  init()
}

const search = (val: any) => {
  listQuery.Name = val.value
  init()
}

const clear = () => {
  listQuery.Name = ""
  init()
}

// 上拉加载数据
onReachBottom(() => {
  // 判断是否还有下一页数据
  if (listQuery.pageIndex * listQuery.pageSize >= listQuery.total)
    return (status.value = "nomore")
  // // 判断是否正在请求其它数据，如果是，则不发起额外的请求
  if (status.value === "loading") return
  listQuery.pageIndex += 1
  init()
})

const init = async () => {
  status.value = "loading"
  const { data: res } = await getLocationList(listQuery)
  if (res.code == 200) {
    if (res.total > 10) status.value = "loadmore"
    else status.value = "nomore"
    positionList.value = [...positionList.value, ...res.data]
    listQuery.total = res.total
    // console.log(positionList.value)
  }
}

onShow(() => {
  tabsChange(tabsState.current)
})
</script>

<style lang="scss" scoped>
.set-position-container {
  padding: 0 30rpx 30rpx;
  padding-top: 20rpx;

  .tag {
    margin-bottom: 30rpx;

    .u-tag {
      margin-right: 20rpx;
    }
  }
}
</style>
