<template>
<a-card>
  <a-row>
    <a-space>
     <a-col>
        <a-range-picker v-model:value="time" format="YYYY/MM/DD"  />
      </a-col>
      <a-col>
        <a-button type="primary" @click="getTrackTaskList">搜索</a-button>
     </a-col>
    </a-space>
  </a-row>

  <a-table
    style="margin-top: 20px;"
    :columns="columns"
    :data-source="state.dataSource"
    :loading="state.loading"
    :pagination="queryParamsState"
    @change="changePage"
  >
    <template #bodyCell="{column, record}">
      <template v-if="column.key === 'ts'" >
          {{dayjs(record.ts).format('YYYY/MM/DD HH:MM:ss')}}
      </template>
          <template v-if="column.key === 'action'" >
            <a-space>
              <a @click="openModal('preview', record.id)" >详情</a>
            </a-space>
          </template>
    </template>
  </a-table>
</a-card>

<modal-pro
    style="width: 700px;overflow: hidden;"
    label="最终详情"
    :open="state.visable"
    @cancel="state.visable = false"
    @ok="state.visable = false"
  >

</modal-pro>

</template>

<script lang='ts' setup >
import { TaskController } from '@/controller/iot/task'
import { onMounted, reactive, ref, watch } from 'vue'
import dayjs from 'dayjs'

const columns = [
  {
    title: '任务id',
    dataIndex: 'taskId'
  },
  {
    title: '执行时间',
    dataIndex: 'ts',
    key: 'ts'
  },
  {
    title: '任务参数',
    dataIndex: 'params',
    key: 'params'
  },
  {
    title: '任务结果',
    dataIndex: 'ret',
    key: 'ret'
  },
  {
    title: '操作',
    dataIndex: 'action',
    key: 'action'
  }
]

const time = ref([])

const queryParamsState = reactive({
  page: 1,
  pageSize: 10,
  start: '',
  end: '',
  taskId: ''
})

const state = reactive({
  taskList: [],
  dataSource: [],
  loading: false,
  visable: false,
  opraState: '',
  id: ''
})

watch(
  () => time.value,
  () => {
    if (time.value && time.value.length === 2) {
      queryParamsState.start = new Date(dayjs(time.value[0])).getTime()
      queryParamsState.end = new Date(dayjs(time.value[1])).getTime()
    } else {
      queryParamsState.start = ''
      queryParamsState.end = ''
    }
  },
  {
    deep: true
  }
)

const openModal = (opraState: 'preview', id: string) => {
  state.opraState = opraState
  state.id = id
}

const changePage = ({ current }) => {
  queryParamsState.page = current
  getTrackTaskList()
}

const getTrackTaskList = async () => {
  state.loading = true
  const { data } = await TaskController.trackTaskPage(queryParamsState)
  state.loading = false
  state.dataSource = data
}

onMounted(() => {
  getTrackTaskList()
})

</script>

<style lang='less' scoped >
</style>
