// const app = getApp()
import uni from '@/utils/uniHooks';
import LoginDom from '@/components/Login/Login.vue';
import { wxToLoginCallback } from '@/utils';
import { orders } from '@/api/parking';
import orderMockData from '@/api/mockData/orders.json'
import { ORDER_STATUS } from '@/common/js/BaseDictionary'

let canloading = true;
let pageNum = 0;
// import moment from 'moment';
// import authorize from '@/components/authorize/authorize.vue';
import { mapState } from 'vuex';
// import {data as mockData} from './list'

export default {
  // components: {
  //   // authorize,
  // },
  data() {
    return {
      list: [],
      numberOfElements: 1,
      pageNum: 0,
    };
  },
  computed: {
    ...mapState({
      mallId: ( state ) => state.mallId,
      openId: ( state ) => state.openid,
      member: ( state ) => state.member,
    }),
  },
  components: {LoginDom},
  mounted() {
    setTimeout(() => {
      uni.setNavigationBarTitle({
        title: '缴费记录',
      });
    },301);
    setTimeout(() => {
      window?.toWXSendMsg({
        type: 'uni_func',
        funcName: 'setNavigationBarColor',
        options: {
          frontColor: '#000000',
          backgroundColor: '#FBFCFF',
        },
      });
    }, 500)
    this.$store.dispatch('clearUnlicensed');
    this.list = [];
    this.numberOfElements = 0;
    // 重新获取数据
    canloading = true;
    pageNum = 0;
    const member = uni.getStorageSync('member');
    this.loadData();
    if ( !uni.getStorageSync('loadData') && !member ) {
      uni.setStorageSync('loadData','loadData')
      wxToLoginCallback('parkingFeeList',( options ) => {
        this.loadData();
      });
    }
  },
  filters: {
    parkingTime( val ) {
      const days = parseInt(val / 60 / 24);
      const hours = parseInt(( val / 60 ) % 24);
      const minutes = parseInt(val % 60);
      if ( days > 0 ) {
        return `${ days }天 ${ hours }小时 ${ minutes }分钟`;
      }
      if ( hours > 0 ) {
        return `${ hours }小时 ${ minutes }分钟`;
      }
      return `${ minutes }分钟`;
    },
    orderStatus( val ) {
      return ORDER_STATUS[val]
    }
  },
  methods: {
    bindscrolltolower: function () {
      if ( canloading ) {
        pageNum++;
        this.loadData();
      }
    },
    // 获取停车记录
    loadData: async function () {
      canloading = false;
      const params = {
        mallid: this.mallId,
        vipcode: this.member?.vipcode,
        openId: this.openId,
        page: pageNum || 0,
        pageSize: 4,
      };
      console.log(120,orderMockData);
      try {
        const res = await orders({
          page: params.page,
          size: params.pageSize,
        })
        console.log('res2',res);
        // const res = orderMockData
        const records = res.content || [];
        this.numberOfElements = records.numberOfElements
        if ( records.length ) {
          const list = this.list;
          this.list = [...list,...records];
          this.numberOfElements = 1
        } else if ( records.length < params.pageSize ) {
          this.numberOfElements = 0
        } else {
          this.numberOfElements = 0
          this.initPage();
        }
        canloading = true;
      } catch ( err ) {
        uni.hideLoading();
        canloading = true;
        this.initPage();
        uni.showToast({
          title: err.langMessage,
          duration: 2000,
          icon: 'none',
        });
      }
      return
      // const res = await orders({
      //   page: params.page,
      //   pageSize: params.pageSize,
      // })
      console.log(res);
      uni.showLoading({
        title: '加载中',
      });
      this.$md(params);
      uni.request({
        url: this.$baseURL + 'api/1.0/park/parkOrderList',
        data: params,
        method: 'POST',
        header: JSON.parse(uni.getStorageSync('handleUser')),
        success: ( res ) => {
          uni.hideLoading();
          if ( res.data.code !== 0 ) {
            uni.showToast({
              title: res.data.msg,
              duration: 2000,
              icon: 'none',
            });
            this.initPage();
          } else {
            // 合并数据
            const records = res.data.data || [];
            if ( records.length ) {
              const list = this.list;
              this.list = [...list,...records];
            } else {
              this.initPage();
            }
            // this.list = mockData.data
          }
          canloading = true;
        },
        fail: ( err ) => {
          uni.hideLoading();
          canloading = true;
          this.initPage();
          uni.showToast({
            title: res.data.msg,
            duration: 2000,
            icon: 'none',
          });
        },
      });
    },
    // 页码复原
    initPage: function () {
      pageNum--;
      if ( pageNum <= 0 ) {
        pageNum = 0;
      }
    },
    // 查看详情
    toDetail( orderno ) {
      this.$router.push({
        path: 'parkingFeeDetailSuccess?orderNo=' + orderno,
      });
    },
  },
};
