import request from "@/utils/request"

/** 分页列表 */
export function listYakHerdInventory(query) {
  return request({
    url: "/dataModel/yakHerdInventory/list",
    method: "get",
    params: query
  })
}

/** 详情 */
export function getYakHerdInventory(id) {
  return request({
    url: "/dataModel/yakHerdInventory/" + id,
    method: "get"
  })
}

/** 新增 */
export function addYakHerdInventory(data) {
  return request({
    url: "/dataModel/yakHerdInventory",
    method: "post",
    data
  })
}

/** 修改 */
export function updateYakHerdInventory(data) {
  return request({
    url: "/dataModel/yakHerdInventory",
    method: "put",
    data
  })
}

/** 删除 */
export function delYakHerdInventory(ids) {
  return request({
    url: "/dataModel/yakHerdInventory/" + ids,
    method: "delete"
  })
}

/** 可选年份（当前年起向前 5 年） */
export function listYakHerdInventoryYearOptions() {
  return request({
    url: "/dataModel/yakHerdInventory/yearOptions",
    method: "get"
  })
}

/** 可选月份（1～12） */
export function listYakHerdInventoryMonthOptions() {
  return request({
    url: "/dataModel/yakHerdInventory/monthOptions",
    method: "get"
  })
}

/** 乡镇下拉 */
export function listYakHerdInventoryTownOptions() {
  return request({
    url: "/dataModel/yakHerdInventory/townOptions",
    method: "get"
  })
}

/** 县→乡镇→村树（表单/筛选选村） */
export function listYakHerdInventoryVillageTree() {
  return request({
    url: "/dataModel/yakHerdInventory/villageTree",
    method: "get"
  })
}

/** Excel 导入（multipart/form-data，file 为 el-upload 的 raw File） */
export function importYakHerdInventory(file, statYear, statMonth) {
  const formData = new FormData()
  formData.append("file", file, file.name)
  formData.append("statYear", String(statYear))
  formData.append("statMonth", String(statMonth))
  return request({
    url: "/dataModel/yakHerdInventory/importData",
    method: "post",
    headers: {
      repeatSubmit: false
    },
    data: formData
  })
}

/** 下载导入模板（GET，后端 @GetMapping("/importTemplate")） */
export function downloadYakHerdInventoryTemplate() {
  return request({
    url: "/dataModel/yakHerdInventory/importTemplate",
    method: "get",
    responseType: "blob"
  })
}

/**
 * 按年月导出（GET，后端 @GetMapping("/export")）
 * @param {number} statYear
 * @param {number} statMonth
 * @param {number} [townDeptId] 可选乡镇
 */
export function exportYakHerdInventory(statYear, statMonth, townDeptId) {
  const params = { statYear, statMonth }
  if (townDeptId != null && townDeptId !== "") {
    params.townDeptId = townDeptId
  }
  return request({
    url: "/dataModel/yakHerdInventory/export",
    method: "get",
    params,
    responseType: "blob"
  })
}
