import { listDrafts } from '@/subPackages/sub_salesperson/common/inputDraftStorage.js'

export function resolveDoctorFormPath(bizCtx) {
  const root = (bizCtx && bizCtx.packageRoot) || '/subPackages/sub_salesperson'
  return `${root}/doctor-input/form`
}

export function goDoctorInputFromStore(storeInputId, bizCtx) {
  const id = Number(storeInputId || 0)
  if (!id) return
  const path = resolveDoctorFormPath(bizCtx)
  uni.navigateTo({ url: `${path}?storeInputId=${id}` })
}

export function navigateToInputForm(formPath, inputType, callbacks = {}) {
  const drafts = listDrafts(inputType)
  if (!drafts.length) {
    uni.navigateTo({ url: formPath })
    return
  }
  uni.showActionSheet({
    itemList: ['空白新建', '从草稿继续…'],
    success: (res) => {
      if (res.tapIndex === 0) {
        uni.navigateTo({ url: formPath })
      } else if (res.tapIndex === 1) {
        if (typeof callbacks.onPickDraft === 'function') {
          callbacks.onPickDraft()
        } else {
          const listPath = formPath.replace('/form', '/index')
          uni.navigateTo({ url: `${listPath}?openDraft=1` })
        }
      }
    },
  })
}

export function openDraftForm(formPath, draftId) {
  if (!draftId) return
  const sep = formPath.indexOf('?') >= 0 ? '&' : '?'
  uni.navigateTo({ url: `${formPath}${sep}draftId=${encodeURIComponent(draftId)}` })
}
