<template>
  <!-- apk管理 -->
  <div class="page flex-col">
    <s-header :name="$t('apkManage.apkManagement')" :noback="false"></s-header>
    <img src="../../assets/advertManage/pageBg.png" />
    <nav-bar></nav-bar>
  </div>
</template>

<script>
import { getApkListPage } from '@/service/apkManage';
import { onMounted, reactive, toRefs, ref } from 'vue';
import navBar from "@/components/NavBar";
import sHeader from "@/components/SimpleHeader";
import { Toast } from 'vant';
import { getLoginUser } from '@/common/js/utils';
// 
export default {
  setup() {
    const loading = ref(false);
    const error = ref(false);
    const finished = ref(false);
    const list = ref([]);
    const deviceTotal = ref(0);
    let searchParams = reactive({
      id: '', // 用户账户id
      current: 1, // 页数
      size: 10 // 页大小
    });
    // 初始化页面获取列表
    onMounted(async () => {
      list.value = [];
      searchParams.current = 1;
      const user = getLoginUser();
      if (user) {
        searchParams.id = user.id;
        if (user.isAdmined) {
          searchParams.adminName = user.username;
        }
        getList();
      }
    });
    // 滚动加载
    const onLoad = () => {
      if (!finished.value) { searchParams.current = searchParams.current + 1; getList(); }
    };
    // 获取设备列表数据
    const getList = async () => {
      const { data } = await getApkListPage(Object.assign({}, searchParams));
      if (data.code === '00000') {
        if (searchParams.current === 0) {list.value = []; }
        // 列表值叠加
        list.value = list.value.concat(data.data.records.map((item) => {
          return {
            ...item,
            checkType: false,
          }
        }));
        deviceTotal.value = data.data.total;
        if (list.value.length === data.data.total) { finished.value = true; }
        loading.value = false;
      } else { Toast.fail(data.message); }
    }
    // 搜索点击
    const searchClick = () => { console.log('searchClick'); };
    return {
      ...toRefs(searchParams),
      loading,
      error,
      finished,
      deviceTotal,
      onLoad,
      searchClick
    };
  },
  components: { sHeader, navBar },
};
</script>

<style lang="less" scoped>
@import "../../common/style/common.less";
.page{ width: 375px; position: relative; }
</style>
