<!-- 参试任务 -->
<script lang="ts" setup name="DeviceModelList">
import { reactive, ref, watch } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import { getTaskList } from '@/api/eqpt/device/task'
import { getDictByCode } from '@/api/system/dict'
const emits = defineEmits(['add'])
const { proxy } = getCurrentInstance() as any
const dialogFormVisible = ref(false)
const listQuery = reactive({
  endTime: '',
  startTime: '',
  taskName: '',
  offset: 1,
  limit: 20,
})
const columns = ref([
  {
    text: '任务名称',
    value: 'taskName',
    align: 'center',
  },
  {
    text: '开始时间',
    value: 'startTime',
    align: 'center',
  },
  {
    text: '结束时间',
    value: 'endTime',
    align: 'center',
  },
  {
    text: '备注',
    value: 'remark',
    align: 'center',
  },
])
const list = ref([])
const total = ref(0)
const listLoading = ref(true)

// 获取数据
const fetchData = (isNowPage = true) => {
  listLoading.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.offset = 1
  }
  getTaskList(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
fetchData()
// 查询数据
const search = () => {
  fetchData(false)
}
// 开始结束时间
const datetimerange = ref()
watch(() => datetimerange.value, (newVal) => {
  listQuery.startTime = ''
  listQuery.endTime = ''
  if (Array.isArray(newVal)) {
    if (newVal.length) {
      listQuery.startTime = `${newVal[0]} 00:00:00`
      listQuery.endTime = `${newVal[1]} 23:59:59`
    }
  }
})
// 重置
const reset = () => {
  datetimerange.value = []
  listQuery.taskName = ''
  listQuery.startTime = ''
  listQuery.endTime = ''
  listQuery.offset = 1
  listQuery.limit = 20
  search()
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}

// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}

// 点击保存
const submitForm = () => {
  // 多选
  if (selectList.value.length) {
    emits('add', selectList.value)
    dialogFormVisible.value = false
    reset()
    selectList.value = []
  }
  else {
    ElMessage.warning('请先选择设备')
  }
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  reset()
}

// 初始化
const initDialog = () => {
  // reset()
  dialogFormVisible.value = true
}
fetchData()
defineExpose({ initDialog, list })
</script>

<template>
  <el-dialog v-model="dialogFormVisible" title="" width="65%" style="z-index: 9999999999999;">
    <!-- 筛选条件 -->
    <add-dialog ref="addRef" @reset-data="search" />
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.taskName" placeholder="参试任务名称" clearable />
      </search-item>
      <search-item>
        <el-date-picker
          v-model="datetimerange" type="daterange" value-format="YYYY-MM-DD"
          format="YYYY-MM-DD" range-separator="至" start-placeholder="参试开始时间" end-placeholder="参试结束时间"
        />
      </search-item>
    </search-area>
    <table-container>
      <!-- 普通表格 -->
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="listLoading"
        :is-showmulti-select="true" :is-multi="true" @change="changePage" @multi-select="multiSelect"
      />
    </table-container>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="submitForm">确认</el-button>
        <el-button @click="resetForm">
          取消
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

