import request from "@/util/oaRequest"

// 用印申请
export function listSealReq(query) {
  return request({
    url: '/hrm/seal/list',
    method: 'get',
    params: query
  })
}

export function getSealReq(bizId) {
  return request({
    url: `/hrm/seal/${bizId}`,
    method: 'get'
  })
}

export function addSealReq(data) {
  return request({
    url: '/hrm/seal',
    method: 'post',
    data
  })
}

export function editSealReq(data) {
  return request({
    url: '/hrm/seal',
    method: 'put',
    data
  })
}

export function delSealReq(bizIds) {
  return request({
    url: `/hrm/seal/${bizIds}`,
    method: 'delete'
  })
}

export function approveSealReq(bizId) {
  return request({
    url: `/hrm/seal/${bizId}/approve`,
    method: 'post'
  })
}

export function rejectSealReq(bizId) {
  return request({
    url: `/hrm/seal/${bizId}/reject`,
    method: 'post'
  })
}

export function cancelSealReq(bizId) {
  return request({
    url: `/hrm/seal/${bizId}/cancel`,
    method: 'post'
  })
}

export function stampSealJava(bizId, data) {
  // 确保数据正确序列化，特别是 0 值
  const payload = {
    targetFileUrl: String(data.targetFileUrl || ''),
    stampImageUrl: String(data.stampImageUrl || ''),
    pageNo: Number(data.pageNo) || 1,
    xPx: Number(data.xPx) || 0,
    yPx: Number(data.yPx) || 0,
    viewportWidth: data.viewportWidth !== undefined && data.viewportWidth !== null ? Number(data.viewportWidth) : undefined,
    viewportHeight: data.viewportHeight !== undefined && data.viewportHeight !== null ? Number(data.viewportHeight) : undefined
  }
  // 可选字段
  if (data.widthPx !== undefined && data.widthPx !== null) {
    payload.widthPx = Number(data.widthPx)
  }
  if (data.heightPx !== undefined && data.heightPx !== null) {
    payload.heightPx = Number(data.heightPx)
  }
  // viewportWidth/Height：如果是 undefined 则不传（兼容后端校验）
  if (payload.viewportWidth === undefined) delete payload.viewportWidth
  if (payload.viewportHeight === undefined) delete payload.viewportHeight
  console.log('stampSealJava API call, payload:', JSON.stringify(payload, null, 2))
  console.log('yPx in API:', typeof payload.yPx, payload.yPx)
  return request({
    url: `/hrm/seal/${bizId}/stamp/java`,
    method: 'post',
    data: payload
  })
}

export function stampSealPython(bizId, data) {
  return request({
    url: `/hrm/seal/${bizId}/stamp/python`,
    method: 'post',
    data
  })
}
