import type { AxiosResponse } from 'axios'
import type { Result } from '@cacp/ui'
import type { DeptSn, FormType, WorkflowRelation } from '@/views/form/types'
import request from '@/utils/request'

const contextPath = '/form'

export async function getDeptSnList(): Promise<Result<DeptSn[]>> {
  const res: AxiosResponse<Result<DeptSn[]>> = await request.get(
    `${contextPath}/dept-sn/get-list`
  )
  return res.data
}
export async function insertDeptSn(info: DeptSn): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(
    `${contextPath}/dept-sn/insert`,
    info
  )
  return res.data
}
export async function deleteDeptSn(deptId: string): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(
    `${contextPath}/dept-sn/delete?deptId=${deptId}`
  )
  return res.data
}

export async function getFormTypeList(): Promise<Result<FormType[]>> {
  const res: AxiosResponse<Result<FormType[]>> = await request.get(
    `${contextPath}/form-type/get-list`
  )
  return res.data
}
export async function insertFormType(info: FormType): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(
    `${contextPath}/form-type/insert`,
    info
  )
  return res.data
}
export async function updateFormType(info: FormType): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(
    `${contextPath}/form-type/update`,
    info
  )
  return res.data
}
export async function deleteFormType(deptId: string): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(
    `${contextPath}/form-type/delete?deptId=${deptId}`
  )
  return res.data
}

export async function getWorkflowRelationList(formCode: string): Promise<Result<WorkflowRelation[]>> {
  const res: AxiosResponse<Result<WorkflowRelation[]>> = await request.get(
    `${contextPath}/workflow-relation/get-list`,
    { params: { formCode: formCode }}
  )
  return res.data
}
export async function insertWorkflowRelation(info: WorkflowRelation): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(`${contextPath}/workflow-relation/insert`, info)
  return res.data
}
export async function deleteWorkflowRelation(formCode: string, deptId: string, processRange: number): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(
    `${contextPath}/workflow-relation/delete?formCode=${formCode}&deptId=${deptId}&processRange=${processRange}`
  )
  return res.data
}
