// childPackage/pages/electricQuery/components/reading/index.js
import { exportElectricityList, getAccountingList, getElectricityList, getMeterReadingList } from "../../../../../service/accounting";
import { getTenementMeterList } from "../../../../../service/meter";
import dayjs from "../../../../../utils/dayjs";
import { getParkInfoByTime } from "../../../../../service/park";
import request from '../../../../../utils/request';
import { alertInfo, getPixelRatio, loadingFunc } from "../../../../../utils/index";
const { OK } = request;
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    meter: String,
    parkInfo: Object,
  },
  observers: {
    'meter': function() {
      loadingFunc(async () => {
        await this.getReadingList();
      })
    },
    "parkInfo": function(newValue) {
      this.setData({ park: newValue })
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    readingDetailShow: false,
    readingDetail: {},
    park: {},
    meterReadingHeader: [
      { key: 'address', title: '电表地址', renderBody: (item) => item.meter?.address },
      { title: '倍率', key: 'ratio' },
      { key: 'number', title: '当前表字' },
    ],
    meterReadingList: [],
    yearMonthDayReading: dayjs().format("YYYY-MM-DD"),
    yearMonthDayReadingStamp: new Date().getTime(),
    readingPage: 1,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    clickReadingTime() {
      this.setData({
        readingVisible: true
      })
    },
    async getReadingList() {
      const { meter, yearMonthDayReading, readingPage } = this.data;
      const { code, message, data, total } = await getMeterReadingList({
        id: meter,
        time: yearMonthDayReading,
        page: readingPage
      })
      if (code !== OK) {
        alertInfo(message)
        return;
      }
      this.setData({ meterReadingList: data, totalPage: Math.ceil(total / 20) })
    },
    onReadingTimeClose() {
      this.setData({ readingVisible: false })
    },
    onReadingTimeCancel() {
      this.setData({ readingVisible: false })
    },
    onReadingTimeConfirm(e) {
      const { time } = e.detail;
      
      this.setData({ 
        yearMonthDayReading: time, 
        yearMonthDayReadingStamp: new Date(time).getTime(),
        readingVisible: false,
        readingPage: 1,
      }, () => {
        loadingFunc(async () => {
          await this.getParkInfo(time)
          await this.getReadingList();
        })
      })
         
    },
    async getParkInfo(time) {
      const park = wx.getStorageSync('park');
      const { park:parkInfo, code, message } = await getParkInfoByTime(park?.id,  `${time}`, `${0}`,)
      if (code !== OK) {
        alertInfo(message)
        return;
      }
      const that = this;
      return new Promise((resolve) => {
       
        that.setData({
          parkInfo: parkInfo
        }, () => {
          resolve()
        })
      })
  
      
    },
    showDetail(e) {
      const { index, data = {} } = e.detail;
      this.setData({
        readingDetailShow: true,
        readingDetail: data || {}
      })
    },
    async onChangePage(e) {
      const page = e.detail.currentIndex;
      const that = this;
      this.setData({
        readingPage: page
      }, () => {
        loadingFunc(async () => {
          await that.getReadingList();
        })
      })
    },
  }
})