import { Query } from '@/api/dataIntegration'
import { translateDataToTreeAll } from '@/utils/validate'
import { ref } from 'vue'
import * as _ from 'lodash'
const dataObj = ref<any>({
  auth_id: -1,
  auth_name: '所有权限',
  auth_status: true,
  up_auth_id: -1,
  auth_type: 0,
  disabled: true,
  children: [] as Array<any>,
})
const data = ref<any>([])
const newData = ref<any>([])
const setTree = (arr: Array<any>, upkey = 'up_auth_id', key = 'auth_id') => {
  if (arr.length) {
    const datas = translateDataToTreeAll(arr, upkey, key)
    dataObj.value.children = datas
  }
  return dataObj
}

const getTree = async () => {
  try {
    const { returnData, code } = await Query({
      id: DATACONTENT_ID.authTreeNewId,
      dataContent: [sessionStorage.getItem('User_Id')],
    })
    if (code == 0 && returnData.listValues) {
      newData.value = _.cloneDeep(returnData.listValues)
      data.value = [setTree(returnData.listValues)]
      return data
    } else {
      data.value = [dataObj]
      return data
    }
  } catch (error) {
    data.value = [dataObj]
    return data
  }
}
export default {
  setTree,
  getTree,
  newData,
}
