import { ref, reactive } from 'vue'
import { getTaskPilot } from '@/api/task/index.js'
import { getEquipment as equipment, getEquipmentMount as equipmentMount, getCloud } from '@/api/equipment'
import { PHOTOGRAPHY_WAY } from '@/utils/dictionary'

// 无人机列表
const equipmentList = ref([])

// 挂载设备列表
const equipmentMountList = ref([])

// 飞手列表
const pilotList = ref([])

// 挂载盒子
const cloudList = ref([])

export const form = reactive({
  userForm: {
    flightHandId: null,
    equipmentId: null,
    equipmentMountId: null,
    cloudBoxId: null,
    photographyWay: null
  },
  userRules: {
    flightHandId: [{ required: true, message: '请选择飞手', trigger: 'blur' }]
  },
  formItem: [
    { type: 'select', key: 'flightHandId', label: '飞手', props: { options: pilotList, placeholder: '请选择飞手', clearable: true }},
    { type: 'select', key: 'equipmentId', label: '执飞无人机', props: { options: equipmentList, placeholder: '请选择执飞无人机', clearable: true }},
    { type: 'select', key: 'equipmentMountId', label: '挂载设备', props: { options: equipmentMountList, placeholder: '请选择挂载设备', clearable: true }},
    { type: 'select', key: 'cloudBoxId', label: '挂载盒子', isLive: 2, props: { options: cloudList, placeholder: '请选择盒子', clearable: true }},
    { type: 'select', key: 'photographyWay', label: '拍摄方式', props: { options: PHOTOGRAPHY_WAY, placeholder: '请选择拍摄方式', clearable: true }}
  ],
  // 飞手接单 - 校验
  userRulesOrder: {
    equipmentId: [{ required: true, message: '请选择执飞无人机', trigger: 'blur' }],
    photographyWay: [{ required: true, message: '请选择摄影方式', type: 'number', trigger: 'blur' }],
    equipmentMountId: [{ required: true, message: '请选择挂载设备', trigger: 'blur' }]
  },
  // 飞手接单 - 表单
  formItemOrder: [
    { type: 'select', key: 'equipmentId', label: '执飞无人机', props: { options: equipmentList, placeholder: '请选择执飞无人机', clearable: true }},
    { type: 'select', key: 'photographyWay', label: '摄影方式', props: { options: PHOTOGRAPHY_WAY, placeholder: '请选择拍摄方式', clearable: true }},
    { type: 'select', key: 'equipmentMountId', label: '挂载设备', props: { options: equipmentMountList, placeholder: '请选择挂载设备', clearable: true }},
    { type: 'select', key: 'cloudBoxId', label: '盒子名称', isLive: 2, props: { options: cloudList, placeholder: '请选择盒子', clearable: true }}
  ]
})

// 获取飞手列表
export const getPilotList = async function() {
  const { data } = await getTaskPilot()
  pilotList.value = data.map(it => ({
    ...it,
    label: it.realname,
    value: it.id
  }))
}

// 无人机列表
export const getEquipment = async function() {
  const { data } = await equipment()
  equipmentList.value = data.map(it => ({
    ...it,
    label: it.name,
    value: it.id
  }))
}

// 挂载设备列表
export const getEquipmentMount = async function() {
  const { data } = await equipmentMount()
  equipmentMountList.value = data.map(it => ({
    ...it,
    label: it.name,
    value: it.id
  }))
}

// 挂载盒子列表
export const getCloudMount = async function() {
  const { data } = await getCloud()
  cloudList.value = data.map(it => ({
    ...it,
    label: it.boxName,
    value: it.id
  }))
}
