import { useApiFetch } from '/@src/composables/fetch.ts'
import type { iEstimate, iCode, iPrcsPageable, UserData, iEstimatePageable, iEstimateList } from '/@src/utils/types'

const $fetch = useApiFetch()
type params = Record<string, any>

const api = {
  login: (bizNo: string, pwd: string) => {
    return new Promise<UserData>((resolve, reject) => {
      const router = useRouter()
      $fetch('/api/login', {
        method: 'POST',
        body: {
          bizNo: bizNo,
          pwd: pwd,
        },
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  register: (register: params) => {
    return new Promise((resolve, reject) => {
      const router = useRouter()
      $fetch('/api/business', {
        method: 'POST',
        body: register,
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  modify: (register: params) => {
    return new Promise((resolve, reject) => {
      const router = useRouter()
      $fetch('/api/business', {
        method: 'PUT',
        body: register,
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  rePassword: (register: params) => {
    return new Promise((resolve, reject) => {
      const router = useRouter()
      $fetch('/api/business/password', {
        method: 'PUT',
        body: register,
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  getCode: (id: number) => {
    const router = useRouter()
    return new Promise<iCode[]>((resolve, reject) => {
      $fetch(`/api/code/${id}`, {
        method: 'GET',
      }).then(res => resolve(res)).catch((e: any) => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  tokenValid: (bizNo: string) => {
    return new Promise((resolve, reject) => {
      const router = useRouter()
      $fetch(`/api/token/valid?bizNo=${bizNo}`, {
        method: 'GET',
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  getUser: () => {
    return new Promise((resolve, reject) => {
      const router = useRouter()
      $fetch('/api/business/me', {
        method: 'GET',
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  getPrcs: (params: params) => {
    return new Promise<iPrcsPageable>((resolve, reject) => {
      const router = useRouter()
      $fetch(`/api/prcs/external/page?${new URLSearchParams(params)}`, {
        method: 'GET',
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  getPrc: (prcsNo: string) => {
    return new Promise<iEstimate>((resolve, reject) => {
      const router = useRouter()
      $fetch(`/api/prcs/external/${prcsNo}`, {
        method: 'GET',
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  getEstimate: (params: params) => {
    return new Promise<iEstimatePageable>((resolve, reject) => {
      const router = useRouter()
      $fetch(`/api/estimate/external/page?${new URLSearchParams(params)}`, {
        method: 'GET',
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  deletePbAtt: (prcsNo: string, bizNo: string, fileOrd: number) => {
    console.log(prcsNo, bizNo, fileOrd)
    return new Promise<string>((resolve, reject) => {
      const router = useRouter()
      $fetch(`/api/pbAtt/${prcsNo}/${bizNo}/${fileOrd}`, {
        method: 'DELETE',
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  downloadPbAtt: (prcsNo: string, bizNo: string, fileOrd: number) => {
    return new Promise<any>((resolve, reject) => {
      const router = useRouter()
      $fetch(`/api/pbAtt/${prcsNo}/${bizNo}/${fileOrd}`, {
        method: 'GET',
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  downloadPrcsFile: (prcsNo: string, fileOrd: number) => {
    return new Promise<any>((resolve, reject) => {
      const router = useRouter()
      $fetch(`/api/prcs/${prcsNo}/${fileOrd}`, {
        method: 'GET',
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  getPrcsReqCount: () => {
    return new Promise((resolve, reject) => {
      const router = useRouter()
      $fetch(`/api/prcs/external/count`, {
        method: 'GET',
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  saveEstimate: (params: iEstimateList) => {
    return new Promise((resolve, reject) => {
      const router = useRouter()
      $fetch(`/api/estimate`, {
        method: 'POST',
        body: params,
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  updateEstimate: (params: iEstimateList) => {
    return new Promise<iEstimateList>((resolve, reject) => {
      const router = useRouter()
      $fetch(`/api/estimate`, {
        method: 'PUT',
        body: params,
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
  findUser: (name: string) => {
    return new Promise<any>((resolve, reject) => {
      const router = useRouter()
      $fetch(`/api/user?name=${name}`, {
        method: 'GET',
      }).then(res => resolve(res)).catch(e => {
        if ( e.response._data.code === '30000' || e.response._data.code === '44012') {
          router.replace('/auth/login')
        }
        else {
          reject(e)
        }
      })
    })
  },
}

export default api
