import { Apis } from '@/gen/Apis'
import { getApiLoading } from '@/common/libraries/apiLoading'
import userLoginStatus from '@/common/libraries/userUserLogin'
import { showToastBack, showToast } from '@/common/libraries/naviHelper'
import { useWeAppAuthStore, useWorkStore } from '@/common'
import useModel from './model'
const work = useWorkStore()
const auth = useWeAppAuthStore()
const getList = () => {
  getApiLoading(Apis.Activity.ActivityEnrolls.List, useModel?.formData?.value).then(res => {
    useModel.listData.value = [...useModel.listData.value, ...res.data]
    useModel.metaData.value = res.meta
    console.log(res)
  })
}

export default {
  getList,
  init() {
    if (work?.selectWorkHouse?.asset_house) {
      useModel.formData.value = {
        ...useModel.formData.value,
        asset_houses_id: work?.selectWorkHouse?.asset_house?.id,
        full_name: work?.selectWorkHouse?.asset_house?.full_name,
        apply_phone: useModel.formData.value.apply_phone || auth?.data?.user?.phone || '',
        apply_name: useModel.formData.value.apply_name || auth?.data?.user?.name || ''
      }
    } else {
      useModel.formData.value = {
        ...useModel.formData.value,
        asset_houses_id: auth?.data?.selected_house?.id,
        full_name: auth?.data?.selected_house?.full_name,
        apply_phone: useModel.formData.value.apply_phone || auth?.data?.user?.phone || '',
        apply_name: useModel.formData.value.apply_name || auth?.data?.user?.name || ''
      }
    }
  },
  loadMore(page: number) {
    useModel.formData.value.page = page
    this.getList()
  },
  handleTabsChange(idx: number, res: any) {
    useModel.currentTabs.value = idx
    useModel.formData.value = {
      page: 1,
      status: res?.value
    }
    useModel.listData.value = []
    getList()
    console.log(idx, 'e')
  },
  handleToSelectHouse() {
    uni.navigateTo({
      url: '/INDEX/asset_houses/index?type=work_add'
    })
  },
  handleSubmit() {
    let data = useModel?.formData?.value
    if (!data?.asset_houses_id) {
      return showToast('请选择房屋!')
    }
    if (!data?.apply_name) {
      return showToast('请设置申请人姓名!')
    }
    if (!data?.apply_phone) {
      return showToast('请设置申请人手机号!')
    }
    for (let i = 0; i < data?.goods?.length; i++) {
      if (!data?.goods[i]?.name) {
        return showToast('请设置物品名称!')
      }
    }
    if (!data?.passed_time) {
      return showToast('请选择放行日期!')
    }
    getApiLoading(Apis.GoodsReleases.GoodsReleases.Store, useModel?.formData?.value).then(res => {
      showToastBack('提交成功！', 1, true)
      console.log(res)
    })
  },
  handleAddGoods() {
    useModel.formData.value.goods.push({
      name: '',
      number: 1,
      remark: ''
    })
  },
  handleDeleteGoods(index: number) {
    useModel.formData.value.goods.splice(index, 1)
  }
}
