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

const airOptions = ref([])
const lineOptions = ref([])

function disableTime(ts) {
  /* 当前日期 */
  const DATE = new Date().getDate()
  /* 当前小时 */
  const HOURS = new Date().getHours()
  /* 当前分钟 */
  const MINUTES = new Date().getMinutes()
  /* 当前秒 */
  const SECONDS = new Date().getSeconds()
  /* 选择日期 */
  const tDate = new Date(ts).getDate()
  /* 选择小时 */
  const tHours = new Date(ts).getHours()
  /* 选择分钟 */
  const tMinutes = new Date(ts).getMinutes()
  return {
    /* 小时禁用 */
    isHourDisabled: (hour) => {
      /* 选择的日期与当前日期相同，且小于当前时间 */
      return tDate === DATE && hour < HOURS
    },
    /* 分钟禁用 */
    isMinuteDisabled: (minute) => {
      /* 日期相同 */
      if (tDate === DATE) {
        /* 选择小时大于当前小时，则可以全选 */
        if (tHours > HOURS) {
          return false
        } else {
          return minute < MINUTES
        }
      } else {
        return false
      }
    },
    isSecondDisabled: (second) => {
      /* 日期相同 */
      if (tDate === DATE) {
        /* 选择小时大于当前小时，则可以全选 */
        if (tHours > HOURS) {
          return false
          /* 选择分钟大于当前分钟，则可以全选 */
        } else if (tMinutes > MINUTES) {
          return false
        } else {
          return second < SECONDS
        }
      } else {
        return false
      }
      // return tDate === DATE && tHours <= HOURS && tMinutes <= MINUTES && second < SECONDS
    }
  }
}

export const form = reactive({
  taskForm: {
    name: null,
    inspectionType: null,
    airportId: null,
    inspectionLine: null,
    type: null,
    executionStartTime: null,
    note: null
  },
  taskRules: {
    name: [{ required: true, message: '请输入任务名称', trigger: 'blur' }],
    inspectionType: [{ required: true, type: 'number', message: '请选择巡检方式', trigger: 'blur' }],
    airportId: [{ required: true, type: 'number', message: '请选择巡检机场', trigger: 'blur' }],
    inspectionLine: [{ required: true, type: 'number', message: '请选择巡检路线', trigger: 'blur' }],
    type: [{ required: true, type: 'number', message: '请选择任务类型', trigger: 'blur' }],
    executionStartTime: [{ required: true, type: 'date', message: '请选择巡检时间', trigger: ['blur', 'change'] }]
  },
  formItem: [
    { type: 'input', key: 'name', label: '任务名称', props: { maxlength: 100 }},
    { type: 'select', key: 'inspectionType', label: '巡检方式', props: { options: TASK_MODE }},
    { type: 'select', key: 'airportId', label: '巡检机场', props: { options: airOptions }},
    { type: 'select', key: 'inspectionLine', label: '巡检路线', props: { options: lineOptions }},
    { type: 'select', key: 'type', label: '任务类型', props: { options: TASK_TYPE }},
    { type: 'date', key: 'executionStartTime', label: '巡检时间', props: {
      type: 'datetime',
      valueFormat: 'yyyy-MM-dd HH:mm:ss', format: 'yyyy-MM-dd HH:mm:ss',
      isDateDisabled: (ts) => { return ts < (Date.now() - 24 * 60 * 60 * 1000) },
      isTimeDisabled: disableTime,
      actions: ['clear', 'confirm'],
      timePickerProps: { actions: ['confirm'] }
    }},
    { type: 'input', key: 'note', label: '备注', props: { type: 'textarea', autosize: { minRows: 3, maxRows: 3 }}}
  ]
})

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 res = await airportLine(id)
  lineOptions.value = dataToSelect(res.data, { label: 'name', value: 'id' })
}

export const getOptions = function() {
  return {
    airOptions,
    lineOptions
  }
}
