// pages/invoiceList/components/notyet/index.js
import { getInvoiceList } from '../../../../service/invoice';
import { getTenementBackInfo } from '../../../../service/tenement';
import { alertInfo, loadingFunc } from '../../../../utils/index';
import request from '../../../../utils/request';
const { OK } = request;

Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },
  lifetimes: {
    attached() {
      loadingFunc(async () => await this.init())
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    chooseList: [],
    selectList: [],
    list: [],
    selectCount: 0,
    selectMoney: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    async init() {
      const { code, message, data } = await getInvoiceList();
      if (code !== OK) {
        alertInfo(message)
        return;
      }
      
      this.setData({ list: data, selectList: new Array(data?.length).map(() => false), allChecked: false, })
    },
    onRefresh() {
      loadingFunc(async () => await this.init())
    },
    onChange(e) {
      const { id, index } = e.currentTarget.dataset;
      const { list } = this.data;
      const newSelectList = this.data.selectList;
      if (e.detail) {
        newSelectList[index] = true;
      } else {
        newSelectList[index] = false;
      }
      let selectCount = 0;
      let selectMoney = 0;
      for(let i = 0; i < newSelectList.length; i ++) {
        const item = newSelectList[i];
        if (item) {
          selectCount++;
          selectMoney += list[i]?.money;
        }
      }
      this.setData({
        selectList: newSelectList,
        allChecked: selectCount === list.length,
        selectCount,
        selectMoney: Number(selectMoney.toFixed(2))
      })
    },
    async next() {
      const park = wx.getStorageSync("park")
      const tenement = wx.getStorageSync("tenement")
      const { code: detailCode, message: detailMessage, tenement: Detail } = await getTenementBackInfo(park?.id, tenement?.id)
      if (detailCode !== OK) {
        alertInfo(detailMessage)
        return;
      }
      if (Detail?.stop_invoice) {
        alertInfo("您暂不可使用此功能，可联系客服处理。")
        return
      }
      const { selectList, list, selectMoney, selectCount } = this.data;
      let tenementID = "";
      let tenementName = "";
      let ids = [];
      for(let i = 0; i < selectList.length; i ++) {
        if (selectList[i]) {
          const currentTenementID = list[i]?.tenement?.id;
          ids.push(list[i]?.id);
          if (!tenementID) {
            tenementID = currentTenementID;
            tenementName = list[i]?.tenement?.name;
          } else if(tenementID !== currentTenementID) {
            alertInfo("只能对一个公司开票")
            return;
          }
        }
      }
      wx.navigateTo({
        url: `/pages/invoicing/index?money=${selectMoney}&count=${selectCount}&tenement=${tenementID}&ids=${ids}&tenementName=${tenementName}`,
      })
    },
    onAllChecked(e) {
      const { list } = this.data;
      if (e.detail) {
        const newSelectList = Array.from({ length: list.length }, () => true)

        this.setData({ selectList: newSelectList, selectCount: list.length, selectMoney: list.map(item => item.money).reduce((prev, next) => prev + next).toFixed(2), allChecked: true  })
      } else {
        this.setData({ allChecked: false, selectMoney: 0, selectList: [], selectCount: 0 })
      }
    }
  }
})