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

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

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

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

export const form = reactive({
  userForm: {
    inspectionType: null,
    taskName: '',
    patrolLocation: '',
    endValue: '',
    taskStartTime: null,
    flightHandId: null,
    equipmentId: null,
    equipmentMountId: null,
    photographyWay: null,
    remark: ''
  },
  userRules: {
    inspectionType: [{ required: true, message: '请选择巡检类型', type: 'number', trigger: ['blur', 'change'] }],
    taskName: [{ required: true, message: '请输入任务名称', trigger: 'blur' }],
    patrolLocation: [{ required: true, message: '请输入任务起点', trigger: 'blur' }],
    endValue: [{ required: true, message: '请输入任务终点', trigger: 'blur' }],
    taskStartTime: [{ required: true, message: '请输入飞行时间', trigger: 'blur' }],
    flightHandId: [{ required: true, message: '请选择飞手', trigger: 'blur' }]
  },
  formItem: [
    { type: 'select', key: 'inspectionType', label: '巡检类型', props: { options: [{ label: '自营计划', value: 1 }], maxlength: '20', placeholder: '请输入巡检类型', clearable: true }},
    { type: 'input', key: 'taskName', label: '任务名称', props: { maxlength: '20', placeholder: '请输入任务名称', clearable: true }},

    { type: 'map' },

    { type: 'inputStart', key: 'patrolLocation', label: '任务起点', props: { maxlength: '20', placeholder: '请选择任务起点', clearable: true }},
    { type: 'inputEnd', key: 'endValue', label: '任务终点', props: { maxlength: '20', placeholder: '请选择任务终点', clearable: true }},

    { type: 'datetime', key: 'taskStartTime', label: '飞行时间', props: { maxlength: '20', placeholder: '请选择飞行时间', clearable: true }},
    { type: 'select', key: 'flightHandId', label: '飞手', props: { options: pilotList, placeholder: '请选择飞手', clearable: true }},
    { type: 'select', key: 'equipmentId', label: '执飞无人机', props: { options: equipmentList, maxlength: '20', placeholder: '请选择执飞无人机', clearable: true }},
    { type: 'select', key: 'equipmentMountId', label: '挂载设备', props: { options: equipmentMountList, maxlength: '20', placeholder: '请选择挂载设备', clearable: true }},
    { type: 'select', key: 'photographyWay', label: '拍摄方式', props: { options: PHOTOGRAPHY_WAY, maxlength: '20', placeholder: '请选择拍摄方式', clearable: true }},

    { type: 'input', key: 'remark', label: '备注', props: { type: 'textarea', autosize: { maxlength: '200', minRows: 3, maxRows: 3 }}}

  ]
})

// 获取飞手列表
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
  }))
}

