import { getApiLoading } from '@/common/libraries/apiLoading'
import { isDatePassed } from '@/common/libraries/day'
import { showToast } from '@/common/libraries/naviHelper'
import { Apis } from '@/gen/Apis'
import useModel from './model'
const getFitmentShow = (data: { id: string }) => {
  getApiLoading(Apis.Visitor.VisitorApplies.Show, data).then(res => {
    useModel.showData.value = res?.data
    if (res?.data?.renovation_workers?.length) {
      useModel.userInfo.value = res?.data?.renovation_workers?.[0]
    }
    if (res?.data?.code_expired_at) {
      useModel.isExpired.value = isDatePassed(res?.data?.code_expired_at)
    }
    console.log(res)
  })
}

export default {
  init(e:any){
    useModel.formData.value = e
    console.log(e,useModel.formData.value,'sncd')
    getFitmentShow(e)
  },
  handleSubmit(val?: string) {
    getApiLoading(Apis.Visitor.VisitorApplies.Scan, {
      code: useModel?.showData?.value?.visitor_code,
      scan_type: val,
      ...useModel?.formData?.value
    }).then(res => {
      showToast('操作成功！', () => {
        getFitmentShow(useModel.formData.value)
      })
      console.log(res)
    })
  }
}
