import { getSimpTreeList, getTwoClass } from '@/api/Gantt'

const subEventRules = {
  name: { required: true, message: '请输入名称', trigger: ['blur', 'input'] },
  timeRange: {
    required: true,
    message: '请选择时间',
    trigger: ['blur', 'input'],
  },
  twoType: {
    required: true,
    message: '请选择二级分类',
    trigger: ['blur', 'change'],
  },
}
// const mainEventRules = {
//   name: { required: true, message: '请输入名称', trigger: ['blur', 'input'] },

//   timeRange: {
//     required: true,
//     message: '请选择时间',
//     trigger: ['blur', 'input'],
//   },
// }
const mainEventRules = {
  name: { required: true, message: '请输入名称', trigger: ['blur', 'input'] },

  timeRange: {
    required: true,
    validator(rule, value) {
      if (!value) {
        return new Error('请选择时间范围')
      }
      return true
    },
    trigger: ['change'],
  },
}
const showMainEvent = ref(false)

watch(showMainEvent, show => {
  if (!show) {
    resetMainEventData()
  }
})

const mainEventData = ref({
  name: '',
  timeRange: null,

  // type: 'mainEvent',
  describe: '',
  fileUrl: '',
  eventType: true,
  category: '123333',
})

function resetMainEventData() {
  mainEventData.value = {
    name: '',
    timeRange: null,
    describe: '',
    eventType: true,
    fileUrl: '',
  }
}

const targetId = ref(null)
const range = ref(null)

const showNewEvent = ref(false)

watch(showNewEvent, show => {
  if (!show) {
    oneClassData.value = null
    resetEventData()
  }
})

const eventData = ref({
  name: '',
  timeRange: null,
  fileUrl: '',
  describe: '',
  eventType: true,
  equipModel: '',
  reportSite: '',
  twoType: '',
  // type: 'subEvent',
})

function resetEventData() {
  eventData.value = {
    name: '',
    timeRange: null,
    fileUrl: '',
    describe: '',
    eventType: true,
    equipModel: '',
    reportSite: '',
    twoType: '',
  }
}

const tableData = ref([])

const oneClassData = ref(null)
async function searchTreeList() {
  tableData.value = []

  const res = await getSimpTreeList({
    targetId: targetId.value,
    startTime: range.value ? range.value[0] : null,
    endTime: range.value ? range.value[1] : null,
  })
  tableData.value = res.data.list
  // console.log('searchTreeList', tableData)
}

const sonOptions = ref([])
async function getTwoClassList(oneTypeId) {
  const res = await getTwoClass({ oneType: oneTypeId })
  sonOptions.value = res.data.list
}
// const getSonList = async()
export const useEvent = () => {
  return {
    subEventRules,
    mainEventRules,
    showMainEvent,
    mainEventData,
    showNewEvent,
    eventData,
    targetId,
    searchTreeList,
    tableData,
    range,
    oneClassData,
    sonOptions,
    getTwoClassList,
  }
}
