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 getList = () => {
  getApiLoading(Apis.Renovation.RenovationApplies.List, useModel?.formData?.value).then(res => {
    useModel.listData.value = [...useModel.listData.value, ...res.data]
    useModel.metaData.value = res.meta
    useModel.loadMore.value = true
    console.log(res)
  })
}

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

export default {
  getList,
  loadMore(page: number) {
    useModel.formData.value.page = page
    this.getList()
  },
  headleToGoShow(e: { id: string; status: string }) {
    if (e?.status === 'Approved') {
      uni.navigateTo({
        url: `/ME/decoration/show/index?id=${e?.id}`
      })
    }
  },
  toPageAdd() {
    uni.navigateTo({
      url: '/ME/decoration/add/index'
    })
  },
  handleToPage(url: string) {
    uni.navigateTo({
      url: url
    })
  },
  handleAcceptanceSubmit(data?: any) {
    getApiLoading(Apis.Renovation.RenovationApplies.ApplyAccept, data).then(res => {
      showToast('提交成功！', () => {
        useModel.showPopupAcceptance.value = false
        handleLoadList()
      })
      console.log(res)
    })
  },
  handleExtensionSubmit(data?: any) {
    getApiLoading(Apis.Renovation.RenovationApplies.ApplyExtension, data).then(res => {
      showToast('提交成功！', () => {
        useModel.showPopupExtension.value = false
        handleLoadList()
      })
      console.log(res)
    })
  },
  handleAcceptance(res: any) {
    useModel.popupFormData.value = { id: res?.id }
    useModel.showPopupAcceptance.value = true
  },
  handleExtension(res: any) {
    useModel.popupFormData.value = res
    useModel.showPopupExtension.value = true
    console.log(res)
  },
  handleDelete(data: { id: string }) {
    uni.showModal({
      title: '提示',
      content: '确定要删除？不可恢复！',
      success: function (res) {
        if (res.confirm) {
          getApiLoading(Apis.Renovation.RenovationApplies.SoftDelete, data).then(res => {
            showToast('删除成功！', () => {
              handleLoadList()
            })
            console.log(res)
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
}
