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(e: any) {
    console.log(e, 'e')
    if (e?.page_type === 'share' || useModel?.formData?.value?.page_type === 'share') {
      useModel.formData.value = {
        ...useModel.formData.value,
        asset_houses_id: e?.id || useModel?.formData?.value?.asset_houses_id,
        full_name: e?.full_name || useModel?.formData?.value?.full_name,
        page_type: e?.page_type || useModel?.formData?.value?.page_type,
        visitor_phone: useModel.formData.value.visitor_phone || auth?.data?.user?.phone || '',
        visitor_name: useModel.formData.value.visitor_name || auth?.data?.user?.name || '',
        companions_json: [
          {
            name: ''
          }
        ]
      }
      return false
    }
    if (useModel?.formData?.value?.page_type === 'share') {
      return
    }
    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,
        visitor_phone: useModel.formData.value.visitor_phone || '',
        visitor_name: useModel.formData.value.visitor_name || ''
      }
    } else {
      useModel.formData.value = {
        ...useModel.formData.value,
        asset_houses_id: auth?.data?.selected_house?.id,
        full_name: auth?.data?.selected_house?.full_name,
        visitor_phone: useModel.formData.value.visitor_phone || '',
        visitor_name: useModel.formData.value.visitor_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 (!userLoginStatus?.onBindPhone()) {
      return false
    }
    if (!data?.asset_houses_id) {
      return showToast('请选择房屋!')
    }
    if (!data?.visitor_name) {
      return showToast('请设置访客姓名!')
    }
    if (!data?.visitor_phone) {
      return showToast('请设置访客手机号!')
    }
    if (auth?.data?.user?.phone === data?.visitor_phone) {
      return showToast('当前访客手机号已是住户!')
    }
    if (!data?.visit_reason) {
      return showToast('请添加来访事由!')
    }
    if (!data?.visit_start_time || !data?.visit_end_time) {
      return showToast('请选择到访时间-离开时间!')
    }
    getApiLoading(
      useModel?.formData?.value?.page_type === 'share'
        ? Apis.Visitor.VisitorApplies.Register
        : Apis.Visitor.VisitorApplies.Invite,
      useModel?.formData?.value
    ).then(res => {
      showToastBack('提交成功！', 1, true)
      console.log(res)
    })
  },
  handleAddGoods() {
    useModel.formData.value.companions_json.push({
      name: ''
    })
  },
  handleDeleteGoods(index: number | string) {
    useModel.formData.value.companions_json.splice(index, 1)
  },
  handleToApplyRecords() {
    if (!userLoginStatus?.onBindPhone()) {
      return false
    }
    uni.navigateTo({
      url: '/INDEX/visitor_applies/list/index'
    })
  }
}
