import { clearEmtryData } from '@/utils'
import request from '@/utils/request/index'

/**
 * 获取素材列表
 */
export const fetchMaterialList = (data: object) => {
  return request({
    url: '/cbs-app/material/page',
    method: 'post',
    data
  } as any)
}

/**
 * 素材分类分页
 */
export const fetchCategoryList: any = (data: any) => {
  return new Promise((resolve) => {
    request({
      url: '/cbs-app/materialCategory/page',
      method: 'post',
      data:{
        ...data,
        rows: 1000
      }
    }).then((res) => {
      if (res?.data){
        res.data.rows = clearEmtryData(res.data.rows, 'subMaterialCategoryList')
        resolve(res)
      }
    })
    .catch(() => {
      resolve({})
    })
  })
}
/**
 * 添加素材
 */
export const materialSave = (data: any) => {
  return request({
    url: '/cbs-app/material/save',
    method: 'post',
    data
  })
}

/**
 * 删除素材
 */
export const deleteMaterial = (data: any) => {
  return request({
    url: '/cbs-app/material/remove?id=' + data,
    method: 'post'
  })
}

/**
 * 素材详情
 */
export const fetchMaterailDetail = (data: any) => {
  return request({
    url: `/cbs-app/material/detail/${data}`,
    method: 'get'
  })
}

/**
 * 修改素材
 */
export const updateMaterailData = (data: any) => {
  return request({
    url: `/cbs-app/material/update`,
    method: 'post',
    data
  })
}

/**
 * 教学素材分类分页
 */
export const materialCategoryPage = (data: any) => {
  return request({
    url: `/cbs-app/materialCategory/page`,
    method: 'post',
    data
  })
}

/**
 * 教学素材分类详情
 */
export const materialCategoryDetail = (data: any) => {
  return request({
    url: `/cbs-app/materialCategory/detail/${data}`,
    method: 'get',
  })
}

/**
 * 教学素材分类新增
 */
 export const materialCategorySave = (data: any) => {
  return request({
    url: `/cbs-app/materialCategory/save`,
    method: 'post',
    data
  })
}

/**
 * 教学素材分类删除
 */
 export const materialCategoryRemove = (data: any) => {
  return request({
    url: `/cbs-app/materialCategory/remove?id=` + data,
    method: 'post'
  })
}

/**
 * 教学素材分类修改
 */
 export const materialCategoryUpdate = (data: any) => {
  return request({
    url: `/cbs-app/materialCategory/update`,
    method: 'post',
    data
  })
}