import { Apis } from '@/gen/Apis'
import useModel from './model'
import { useWeAppAuthStore } from '@/common'
import { getApiLoading } from '@/common/libraries/apiLoading'
import userLoginStatus from '@/common/libraries/userUserLogin'
import { showToastBack, showToast } from '@/common/libraries/naviHelper'
const auth = useWeAppAuthStore()

const get_customer_enroll = (id: number) => {
  getApiLoading(Apis.Activity.ActivityEnrolls.GetCustomerEnroll, {
    activities_id: id
  }).then(res => {
    useModel.showData.value = { ...useModel.showData.value, ...res?.data }
    console.log(res)
  })
}

export default {
  getShow(id: number) {
    getApiLoading(Apis.Activity.Activities.Show, {
      id
    }).then(res => {
      useModel.showData.value = res?.data
      get_customer_enroll(id)
      console.log(res)
    })
  },
  handleSignUp() {
    if (userLoginStatus?.onBindPhone()) {
      if (!auth?.data?.house_occupant) {
        uni.showModal({
          title: '提示',
          content: '未绑定房屋，请先绑定房屋！',
          confirmText: '去绑定',
          confirmColor: '#0082FA',
          success: function (res) {
            if (res.confirm) {
              uni.navigateTo({
                url: '/INDEX/binding/index'
              })
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        return false
      }
      getApiLoading(Apis.Activity.ActivityEnrolls.Store, {
        activities_id: useModel.showData.value?.id,
        customer_name: auth?.data?.user?.name || '',
        customer_phone: auth?.data?.user?.phone
      }).then(res => {
        showToastBack('报名成功', 1, true)
        console.log(res)
      })
    }
  },
  handleCancelSignUp(status: string) {
    uni.showModal({
      title: '提示',
      content:
        status === 'Cancelled'
          ? `您将取消 [${useModel?.showData?.value?.title}]活动，取消后释放名额?`
          : `您将重新报名 [${useModel?.showData?.value?.title}]，活动?`,
      success(res) {
        if (res.confirm) {
          getApiLoading(Apis.Activity.ActivityEnrolls.Cancelled, {
            id: useModel?.showData?.value?.result?.id,
            status
          }).then(res => {
            showToastBack(status === 'Cancelled' ? '取消报名成功' : '重新报名成功', 1, true)
            console.log(res)
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
}
