import useModel from './model'
import { showToast } from '@/common/libraries/naviHelper'
import { Apis } from '@/gen/Apis'
import { getApiLoading } from '@/common/libraries/apiLoading'
import getUserLogin from '@/common/libraries/userUserLogin'
import { sendChatMessage } from '@/common/libraries/sendWxWork'

const getHouseBill = (id: string, name: string) => {
  useModel.currentHouses.value = { id, name }
  getApiLoading(Apis.Bill.HouseBills.List, { asset_houses_id: id, type: 1 }).then(res => {
    res?.data?.map((i: any) => {
      i.isSelect = true
      i?.bills?.map((b: any) => {
        b.isSelect = true
      })
    })
    useModel.listData.value = res?.data || []
    console.log(res)
  })
}

export default {
  handleChange(e: any) {
    let data = useModel?.showData?.value?.houseList[e?.detail?.value]
    getHouseBill(data?.asset_houses_id, data?.asset_house?.full_name)
    console.log(e, 'e')
  },
  getList() {
    getApiLoading(Apis.Customer.CustomerHouses.List, useModel?.formData?.value).then(res => {
      if (res?.data?.houseList?.length) {
        let data = res?.data?.houseList[0]
        res?.data?.houseList?.map(
          (i: { full_name: string; asset_house: { full_name: string } }) => {
            i.full_name = i?.asset_house?.full_name
          }
        )
        getHouseBill(data?.asset_houses_id, data?.asset_house?.full_name)
      }
      useModel.showData.value = res?.data
      console.log(res)
    })
  },
  getHouseBill,
  handleSelect(idx: number) {
    let is = useModel.listData.value[idx].isSelect
    useModel.listData.value[idx].isSelect = !is
    useModel.listData.value[idx]?.bills?.map((b: any) => {
      b.isSelect = !is
    })
  },
  handleFindSelect(res: any, index: number, idx: number) {
    let is = res?.isSelect
    useModel.listData.value[index].bills[idx].isSelect = !is
    let allChecked = useModel.listData.value[index].bills?.every(
      (item: { isSelect: boolean }) => item?.isSelect
    )
    useModel.listData.value[index].isSelect = allChecked
  },
  handleParmaryClick() {
    let bill_ids: number[] = []
    useModel?.listData?.value?.map((res: any) => {
      if (res?.bills?.length) {
        res?.bills?.map((i: { id: number; isSelect: boolean }) => {
          if (i?.isSelect) {
            bill_ids?.push(i?.id)
          }
        })
      }
    })
    if (!bill_ids?.length) {
      return showToast('请至少选择一个月账单！')
    }
    sendChatMessage({
      page: `/INDEX/bill_order/index.html?bill_ids=${JSON.stringify(bill_ids)}&house_id=${useModel?.currentHouses?.value?.id}`,
      title: `分享给您${useModel?.currentHouses?.value?.name}物业账单`,
      imgUrl:
        'https://pay-prod-1369486729.cos.ap-guangzhou.myqcloud.com/uploads/cs-test/01K5B5RY2TQQAJ1GT2RZSK426V.jpg'
    })
  },
  handleQRCode() {
    let bill_ids: number[] = []
    let total_amount = 0
    useModel?.listData?.value?.map((res: any) => {
      if (res?.bills?.length) {
        res?.bills?.map((i: { id: number; isSelect: boolean; amount: string }) => {
          if (i?.isSelect) {
            bill_ids?.push(i?.id)
            total_amount += parseFloat(i?.amount)
          }
        })
      }
    })
    if (!bill_ids?.length) {
      return showToast('请至少选择一个月账单！')
    }
    getApiLoading(Apis.HouseOrder.HouseOrders.Store, {
      bill_ids: bill_ids || [],
      total_payable_amount: total_amount,
      amount: total_amount
    }).then(res => {
      uni.navigateTo({
        url: `/BILL/poster/index?id=${useModel.currentHouses?.value?.id}&order_id=${res?.data?.id}&time=${res?.data?.created_at}&name=${useModel?.currentHouses?.value?.name}&amount=${total_amount}`
      })
    })
  }
}
