<!-- 参试任务 -->
<script lang="ts" setup name="DeviceModelList">
import { reactive, ref, watch } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import addDialog from './addDialog.vue'
import { addTask, delTask, getTaskList, updateTask } from '@/api/eqpt/device/task'
const emits = defineEmits(['confirm'])
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('confirm', selectList.value.map((item: any) => item.id))
    dialogFormVisible.value = false
    selectList.value = []
    reset()
  }
  else {
    ElMessage.warning('请先选择设备')
  }
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  reset()
}

// 初始化
const initDialog = () => {
  // reset()
  dialogFormVisible.value = true
  fetchData()
}

defineExpose({ initDialog })
const addRef = ref()
// 新建
const add = () => {
  addRef.value.initDialog('create')
}
// 编辑
const handler = (row: any) => {
  addRef.value.initDialog('update', row)
}

// 批量删除
const batchDel = () => {
  if (selectList.value.length) {
    delTask({ ids: selectList.value.map(item => item.id) }).then((res) => {
      ElMessage.success('操作成功')
      search()
    })
  }
  else {
    ElMessage.warning('请选择需要删除的数据')
  }
}
</script>

<template>
  <el-dialog v-model="dialogFormVisible" title="" width="65%">
    <!-- 筛选条件 -->
    <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>
      <template #btns-right>
        <icon-button icon="icon-add" title="新增" @click="add" />
        <icon-button icon="icon-delete" title="删除" @click="batchDel" />
      </template>
      <!-- 普通表格 -->
      <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"
      >
        <template #columns>
          <el-table-column label="操作" width="100" align="center">
            <template #default=" scope ">
              <el-button
                link type="primary" size="small"
                @click="handler(scope.row)"
              >
                编辑
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </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>

