import { reactive, ref } from 'vue'
import { airportList, airportLine } from '@/api/dashboard/index.js'
import { dataToSelect } from '@/utils/handleData.js'
import { TASK_STATUS, TASK_TYPE } from '@/utils/dictionary.js'

const airOptions = ref([])
const lineOptions = ref([])
export const search = reactive({
  search: [
    {
      label: '任务编号',
      key: 'code',
      props: {
        placeholder: '请输入任务编号'
      }
    },
    {
      label: '任务名称',
      key: 'name',
      props: {
        placeholder: '请输入任务名称'
      }
    },
    {
      label: '任务状态',
      key: 'status',
      type: 'select',
      value: 'all',
      props: {
        placeholder: '请选择任务状态',
        options: [
          { label: '全部', value: 'all' },
          ...TASK_STATUS
        ]
      }
    },
    {
      label: '巡检机场',
      key: 'airportId',
      type: 'select',
      props: {
        placeholder: '请选择任务状态',
        options: airOptions
      }
    },
    {
      label: '巡检路线',
      key: 'inspectionLine',
      type: 'select',
      props: {
        placeholder: '请选择任务状态',
        options: lineOptions
      }
    },
    {
      label: '任务类型',
      key: 'type',
      type: 'select',
      props: {
        placeholder: '请选择任务状态',
        options: TASK_TYPE
      }
    }
  ]
})

export const getAirOptions = async function() {
  const res = await airportList()
  airOptions.value = dataToSelect(res.data, { label: 'name', value: 'id' })
}

// 获取角色列表
export const getLineOptions = async function(id) {
  const item = airOptions.value.find((item) => { return item.id === id })
  const res = await airportLine(item.droneId)
  lineOptions.value = dataToSelect(res.data, { label: 'name', value: 'id' })
}

// 获取角色列表
export const clearLineOptions = async function() {
  lineOptions.value = []
}
