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.Renovation.RenovationWorkers.List, useModel?.formListData?.value).then(res => {
    useModel.listData.value = [...useModel.listData.value, ...res.data]
    useModel.metaData.value = res.meta
    console.log(res)
  })
}

const getShow = (res: { id: number }) => {
  getApiLoading(Apis.Renovation.RenovationApplies.Show, { id: res?.id }).then(res => {
    useModel.showData.value = res?.data
    useModel.formListData.value.renovation_applies_id = res?.data?.id
    handleLoadList()
    console.log(res)
  })
}

const handleLoadList = () => {
  useModel.formListData.value.page = 1
  useModel.listData.value = []
  getList()
}

export default {
  loadMore(page: number) {
    useModel.formListData.value.page = page
    getList()
  },
  getShow,
  handleToPage(i: { url?: string }) {
    uni.navigateTo({
      url: `${i?.url || ''}?id=${useModel?.showData?.value?.id}`
    })
  },
  handleAcceptanceSubmit(data?: any) {
    //申请验收
    getApiLoading(Apis.Renovation.RenovationApplies.ApplyAccept, data).then(res => {
      useModel.showPopupAcceptance.value = false
      getShow(useModel?.showData?.value)
      console.log(res)
    })
  },
  handleExtensionSubmit(data?: any) {
    //延期
    getApiLoading(Apis.Renovation.RenovationApplies.ApplyExtension, data).then(res => {
      useModel.showPopupExtension.value = false
      getShow(useModel?.showData?.value)
      console.log(res)
    })
  },
  handleAcceptance(res: any) {
    useModel.showPopupAcceptance.value = true
  },
  handleExtension(res: any) {
    useModel.showPopupExtension.value = true
    console.log(res)
  },
  handleStopVisitor(data: { id: string }, type: number) {
    uni.showModal({
      title: '提示',
      content: type ? '确定启用通行证？' : '确定要停用通行证？',
      success: function (res) {
        if (res.confirm) {
          if (type) {
            getApiLoading(Apis.Renovation.RenovationWorkers.EnableVisitor, data).then(res => {
              handleLoadList()
              console.log(res)
            })
          } else {
            getApiLoading(Apis.Renovation.RenovationWorkers.StopVisitor, data).then(res => {
              handleLoadList()
              console.log(res)
            })
          }

          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  handleOpenDelayedEntryExit(i: any) {
    useModel.showPopupDelayedEntryExit.value = true
    useModel.dataPopupDelayedEntryExit.value = i
  },
  handleDelayedEntryExitSubmit(data: any) {
    getApiLoading(Apis.Renovation.RenovationWorkers.UpdateVisitorTime, data).then(res => {
      showToast('提交成功！', () => {
        useModel.showPopupDelayedEntryExit.value = false
        handleLoadList()
      })
      console.log(res)
    })
  }
}
