import user from '../state/user'
import * as config from './config'

const NOT_ADDS = [
  config.exporrtSecondHouseAuditEstate,
  config.getSecondHouseList1,
  config.getSecondHouseTotal
]
const normalizeData = (data, url) => {
  
  if (data.pageNum) {
    data.currentPage = data.pageNum
    // delete data.pageNum
    // test
    data.current = data.pageNum
    data.size = data.pageSize
  }
  

  if ((user.value.role !== 'admin' && user.value.role !== 'group') || !NOT_ADDS.includes(url)) {
    data.companyId || (data.companyId = user.value.info.departmentId)
    data.userId = user.value.info.id
    data.companyName || (data.companyName = '')
  }
  

  data.maintenanceman || (data.maintenanceman = '')
  data.maintenancemanId || (data.maintenancemanId = '')
  data.updatorId || (data.updatorId = '')
  data.updatorName || (data.updatorName = '')
  data.roleKey || (data.roleKey = user.value.role)
  

}

export const normalizeRequest = config => {
  if (!config.data) {
    config.data = {}
  }

  if (config.params) {
    normalizeData(config.params, config.url)
  }
  if (config.data) {
    normalizeData(config.data, config.url)
  }

  if (!config.params && !config.data) {
    config.data = {}
  }

  config.data = {
    ...config.params,
    ...config.data
  }
  return config
}


export const normalizeResponse = response => {
  if (response.status !== 200) {
    return response
  }

  let data = response.data
  if (data.resultType !== 1) {
    data = { code: -1, msg: data.resultMsg }
  } else if (data.resultData.state) {
    if (data.resultData.state === 'FAIL'){
      data = { code: -1, msg: data.resultData.msg }
    } else {
      data = {
        ...data.resultData,
        code: 0,
        msg: '接口调用成功',
        data: data.resultData.data
      }
    }
  } else if (data.resultData.code) {
    if (data.resultData.code !== '00000'){
      data = { code: -1, msg: data.resultData.message }
    } else {
      data = {
        ...data.resultData,
        code: 0,
        msg: '接口调用成功',
        data: data.resultData.data
      }
    }
  }

  return {
    ...response,
    data
  }
}


export const normalizePagesContent = response => {
  let data = response.data

  if (data.code === 0) {
    const params = response.config.params
    let content 

    if (Array.isArray(data.data)) {
      const serviceContent = data  
      content = {
        list: serviceContent.data.map(item => ({
          ...item,
          auditStatus: Number(item.auditStatus) || 0
        })),
        curPage: params.current,
        totalNum: serviceContent.total, 
        totalPageNum: Math.ceil(serviceContent.total / params.pageSize)
      }

    } else {
      const serviceContent =  data.data
      content = {
        list: serviceContent.records.map(item => ({
          ...item,
          auditStatus: Number(item.auditStatus) || 0
        })),
        curPage: params.currentPage,
        totalNum: serviceContent.recordCount, 
        totalPageNum: Math.ceil(serviceContent.recordCount / params.pageSize)
      }
    }

    data = {
      ...data,
      data: content
    }
  }
  
  return {
    ...response,
    data
  }
}