/**
 * @Description : 公共请求方法 - 工具方法
 * @Author      : jinxin
 * @Date        : 2023-02-13 16:29:05
 */
import Request from '@/utils/request'

// 公共 - 人员变更数据
export async function getComUser(val = 1) {
  let userlist = []
  await new Request().get('project/data/user', {
    page: val,
    size: 1000
  }).then(res => {
    userlist = res.data
  })
  return userlist
}

// 公共 - 分类列表数据
export async function getComCate(val = 1) {
  let catelist = []
  await new Request().get('project/data/cate', {
    page: val
  }).then(res => {
    catelist = res.data
  })
  return catelist
}

// 公共 - 品牌列表数据
export async function getComBrand(val = 1) {
  let brandlist = []
  await new Request().get('project/data/brand', {
    page: val
  }).then(res => {
    brandlist = res.data
  })
  return brandlist
}

// 公共 - 项目任务基础数据
export async function getComTaskData(val = 1) {
  let tasklist = []
  await new Request().get('project/data/base_task_data', {
    page: val
  }).then(res => {
    tasklist = res.data
  })
  return tasklist
}

// 公共 - 数据转变 object key nickname --> name
export function changeObjKey(val) {
  let obj = []
  val.forEach(it => {
    obj.push({
      id: it.id,
      name: it.nickname
    })
  });
  return obj
}

// 私有 - 获取 认证信息列表
export async function getAuthListData(val) {
  let arr = []
  await new Request().get('project/task/certAuth/authList'), then(res => {
    arr = res.data
  })
  return arr
}

// 公共 - 顶部按钮组 - 保存
export async function topBtnSave(url, query) {
  let arr = {}
  await new Request().post(url, query).then(res => {
    arr = res
  })
  return arr
}

// 公共 - 请求数据拼接 - 用,号隔开 - 去除最后一个,
export function jointStrQuery(arr) {
  let delidlist = []
  let joinId = ''
  arr.forEach(el => {
    delidlist.push(el.id)
  });
  delidlist.forEach(el => {
    joinId = joinId + el + ','
  });
  if (joinId.charAt(joinId.length - 1) == ',') {
    joinId = joinId.slice(0, joinId.length - 1)
  }
  return joinId
}

// 公共 - 传递值 获取人员 list - 值
export function requestPersonListTransfer(ori, rlist) {
  let newList = changeObjKey(rlist)
  let arr = JSON.parse(JSON.stringify(ori))
  arr.forEach(el => {
    if (el.list) {
      el.list = newList
    }
  });
  return arr
}

// 项目资料
export async function getProjectInfo(id) {
  let arr = []
  await new Request().get('project/project/file', {
    project_id: id
  }).then(res => {
    arr = res
  })
  return arr
}

// 公共 - 数据值的转换
export function queryDataConversion(list) {
  let newlist = []
  list.forEach(el => {
    el.examine_status = el.examine_status == -1 ? '全部' : el.examine_status == 0 ? '未提交' : el.examine_status == 10 ? '未审核' : el.examine_status == 20 ? '已结束' : ''
    el.task_status = el.task_status == -1 ? '全部' : el.task_status == 0 ? '排期等待' : el.task_status == 10 ? '进行中' : el.task_status == 20 ? '暂停' : el.task_status == 30 ? '已结束' : ''
    newlist.push(el)
  });
  return newlist
}

// 公共 - 路由跳转 - 链接获取
export function routeLinksConversion(id) {
  let url = ''
  url = id == 1 ? 'scfx' : id == 2 ? 'ggdy' : id == 3 ? 'zscqxq' : id == 4 ? 'rzxq' : id == 5 ? 'bzyx' : id == 6 ? 'bzljxz' :
    id == 7 ? 'gysj' : id == 8 ? 'cpplfb' : id == 9 ? 'jgsj' : id == 10 ? 'dzsj' : id == 11 ? 'zscqsq' : id == 14 ? 'wlbj' : id == 18 ? 'cpzl' : id == 19 ? 'spddxq' : id == 20 ? 'dj' :
    id == 21 ? 'km' : id == 22 ? 'bzsj' : id == 23 ? 'bl' : id == 24 ? 'skuinfo' : id == 25 ? 'sczl' ? id == 26 ? 'xxlr' : id == 29 : 'zlsh' : id == 30 ? 'spxd' : id == 27 ? 'rz' : ''
  if (id == 29) {
    url = 'zlsh'
  }
  if (id == 26) {
    url = 'xxlr'
  }
  if (id == 25) {
    url = 'sczl'
  }
  return url
}

// 公共 - 所有数据
export async function getAllInfo(queryId, url) {
  let obj = []
  await new Request().get(url, {
    id: queryId
  }).then(res => {
    obj = res.data
  })
  return obj
}

// 公共 - 基础信息 - 输出文件
export function getBaseInfo(ori, rlist) {
  let newlist = JSON.parse(JSON.stringify(ori))
  newlist.forEach(el => {
    for (const key in el) {
      if (key == 'field' && rlist[el[key]]) {
        el.value = rlist[el[key]]
      }
    }
  });
  return newlist
}

// 公共 - 输出文件
export function getBaseInfoFile(list, tag) {
  let newlist = JSON.parse(JSON.stringify(list))
  newlist.forEach(el => {
    if (el.url_type == 'file' || (tag && tag == 'file')) {
      el.downLoadUrl = el.url
      el.isDownLoadShow = 'yes'
    }
  });
  return newlist
}

// 公共 - 获取当前时间 年-月-日 时-分-秒
export function getComCurrentDate() {
  const date = new Date();
  const Y = date.getFullYear() + '/';
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';
  const D = date.getDate() + ' ';
  const h = date.getHours() + ':';
  const m = date.getMinutes() + ':';
  const s = date.getSeconds();
  const dateString = Y + M + D + h + m + s;
  return dateString;
}

// 公共 - 添加序号
export function addOrderNumber(iArr) {
  if (iArr && iArr.length > 0) {
    let newArr = JSON.parse(JSON.stringify(iArr))
    let num = 1
    newArr.forEach(el => {
      el.index = num++
    });
    return newArr
  }
}

// 公共 - 规格定义 - 添加 结构BOM - 包装BOM - 查看
export function addBomFieldCK(iArr) {
  if (iArr && iArr.length > 0) {
    let newArr = JSON.parse(JSON.stringify(iArr))
    newArr.forEach(el => {
      el.links_qd_bom_text = '查看'
      el.links_bz_bom_text = '查看'
    });
    return newArr
  }
}

// 公共 - 0|1 - 转换 boolean
export function zeroOneChangeValue(list) {
  if (list && list.length > 0) {
    let newArr = JSON.parse(JSON.stringify(list))
    newArr.forEach(item => {
      item.status = item.status ? true : false
    });
    return newArr
  }
}

// 公共 - 0|1 - 转换 boolean
export function zeroOneChangeValueKeys(list) {
  let newArr = JSON.parse(JSON.stringify(list))
  newArr.forEach(item => {
    item.status = item.status ? true : false
    item.name = item.country
    item.key = item.code
  });
  return newArr
}

// 公共 checkbox 集合 获取需要的key 值提交
export function checkboxGetKeys(list) {
  let newArr = []
  let newList = JSON.parse(JSON.stringify(list))
  newList.forEach(item => {
    if (item.status) {
      newArr.push(item.key)
    }
  });
  return newArr
}

// 公共 - 包装原型
export function getImageShowUrl(list) {
  let newlist = JSON.parse(JSON.stringify(list))
  newlist.forEach(el => {
    el.cover = el.image
  });
  return newlist
}