<template>
  <div class="table-box">
    <ProTable ref="proTable" :is-show-search="false" :columns="columns" row-key="id" :data="subTaskList">
      <!-- 表格 header 按钮 -->
      <!--      <template #tableHeader="scope">-->
      <!-- <el-button type="primary" v-auth="['identification:identificationTask:add']" icon="CirclePlus" @click="openDialog(1, '算法子任务新增')"> 新增 </el-button>
        <el-button type="primary" v-auth="['identification:identificationTask:import']" icon="Upload" plain @click="batchAdd"> 导入 </el-button> -->
      <!--        <el-button type="primary" v-auth="['identification:identificationTask:export']" icon="Download" plain @click="downloadFile"> 导出 </el-button>-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          v-auth="['identification:identificationTask:remove']"-->
      <!--          icon="Delete"-->
      <!--          plain-->
      <!--          :disabled="!scope.isSelected"-->
      <!--          @click="batchDelete(scope.selectedListIds)"-->
      <!--        >-->
      <!--          批量删除-->
      <!--        </el-button>-->
      <!--      </template>-->
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link icon="View" v-auth="['identification:identificationTask:query']" @click="viewDetails(scope.row)">
          查看详情
        </el-button>
        <el-button
          type="primary"
          link
          icon="EditPen"
          v-auth="['identification:identificationTask:edit']"
          @click="openDialog(2, '算法子任务编辑', scope.row)"
        >
          编辑
        </el-button>
        <el-button type="primary" link icon="Delete" v-auth="['identification:identificationTask:remove']" @click="deleteSubtask(scope.row)">
          删除
        </el-button>
      </template>
    </ProTable>
    <FormDialog ref="formDialogRef" />
    <ImportExcel ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="Subtask">
import { ref, reactive, onUnmounted, onMounted } from 'vue'
import { useHandleData } from '@/hooks/useHandleData'
import { useDownload } from '@/hooks/useDownload'
import { ElMessageBox } from 'element-plus'
import ProTable from '@/components/ProTable/index.vue'
import ImportExcel from '@/components/ImportExcel/index.vue'
import FormDialog from '@/components/FormDialog/index.vue'
import { useRoute, useRouter } from 'vue-router'
import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import { listSubtaskApi, delSubtaskApi, addSubtaskApi, updateSubtaskApi, exportSubtaskApi, getSubtaskApi } from '@/api/modules/task/subtaskNew'
import { getDictsApi } from '@/api/modules/system/dictData'
const router = useRouter()
const route = useRoute()
const taskId = route.query.id as string
// ProTable 实例
const proTable = ref<ProTableInstance>()
let subTaskList = ref()

// 每隔10秒请求一下列表
const timer = ref() // 定时器
const refreshList = () => {
  setTimeout(() => {
    listSubtaskApi({
      pageNum: 1,
      pageSize: 100,
      taskId: taskId
    }).then(res => {
      subTaskList.value = res.data['list']
    })
  }, 0)
}
onMounted(() => {
  // timer
  refreshList()
  timer.value = setInterval(() => {
    refreshList()
  }, 10000)
})
// 查看详情
const viewDetails = row => {
  if (row.name === '数据扩增') {
    router.push({ path: `/task/amplify/`, query: { id: row.id } })
  } else {
    router.push({ path: `/task/bizProcess/`, query: { id: row.id } })
  }
}
// 删除算法子任务信息
const deleteSubtask = async (params: any) => {
  await useHandleData(delSubtaskApi, params.id, '删除【' + params.id + '】算法子任务')
  proTable.value?.getTableList()
}

// 批量删除算法子任务信息
const batchDelete = async (ids: string[]) => {
  await useHandleData(delSubtaskApi, ids, '删除所选算法子任务信息')
  proTable.value?.clearSelection()
  proTable.value?.getTableList()
}

// 导出算法子任务列表
const downloadFile = async () => {
  ElMessageBox.confirm('确认导出算法子任务数据?', '温馨提示', { type: 'warning' }).then(() =>
    useDownload(exportSubtaskApi, '算法子任务列表', proTable.value?.searchParam)
  )
}

// 批量添加算法子任务
const dialogRef = ref<InstanceType<typeof ImportExcel> | null>(null)
// const batchAdd = () => {
//   const params = {
//     title: '算法子任务',
//     tempApi: importTemplateApi,
//     importApi: importSubtaskDataApi,
//     getTableList: proTable.value?.getTableList
//   }
//   dialogRef.value?.acceptParams(params)
// }

const formDialogRef = ref<InstanceType<typeof FormDialog> | null>(null)
// 打开弹框的功能
const openDialog = async (type: number, title: string, row?: any) => {
  let res = { data: {} }
  if (row?.id) {
    res = await getSubtaskApi(row?.id || null)
  }
  // 重置表单
  setItemsOptions()
  const params = {
    title,
    width: 580,
    isEdit: type !== 3,
    itemsOptions: itemsOptions,
    model: type == 1 ? {} : res.data,
    api: type == 1 ? addSubtaskApi : updateSubtaskApi,
    getTableList: proTable.value?.getTableList
  }
  formDialogRef.value?.openDialog(params)
}
onUnmounted(() => {
  clearInterval(timer.value)
  timer.value = null
})
// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  { prop: 'id', label: '主键ID' },
  {
    prop: 'name',
    label: '任务类型',
    search: {
      el: 'input'
    }
  },
  {
    prop: 'createTime',
    label: '创建时间',
    search: {
      el: 'input'
    }
  },
  // {
  //   prop: 'status',
  //   label: '任务状态',
  //   tag: true,
  //   enum: () => getDictsApi('biz_task_status'),
  //   search: {
  //     el: 'tree-select'
  //   },
  //   fieldNames: { label: 'dictLabel', value: 'dictValue' }
  // },
  // {
  //   prop: 'type',
  //   label: '任务类型',
  //   tag: true,
  //   enum: () => getDictsApi('biz_ag_type'),
  //   fieldNames: { label: 'dictLabel', value: 'dictValue' }
  // },
  // {
  //   prop: 'parameters',
  //   label: '调用算法时所用的参数'
  // },
  // {
  //   prop: 'startTime',
  //   label: '开始时间',
  //   search: {
  //     el: 'date-picker',
  //     props: { type: 'datetimerange', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
  //   },
  //   width: 120
  // },
  // {
  //   prop: 'endTime',
  //   label: '结束时间',
  //   search: {
  //     el: 'date-picker',
  //     props: { type: 'datetimerange', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
  //   },
  //   width: 120
  // },
  // {
  //   prop: 'costSecond',
  //   label: '耗时'
  // },
  { prop: 'operation', label: '操作', width: 270, fixed: 'right' }
])
// 表单配置项
let itemsOptions: ProForm.ItemsOptions[] = []
const setItemsOptions = () => {
  itemsOptions = [
    {
      label: '任务ID',
      prop: 'taskId',
      compOptions: {
        placeholder: '请输入任务ID'
      }
    },
    {
      label: '任务名称',
      prop: 'name',
      rules: [{ required: true, message: '任务名称不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入任务名称'
      }
    },
    {
      label: '任务状态',
      prop: 'status',
      rules: [{ required: true, message: '任务状态不能为空', trigger: 'change' }],
      compOptions: {
        elTagName: 'select',
        labelKey: 'dictLabel',
        valueKey: 'dictValue',
        enum: () => getDictsApi('biz_task_status'),
        placeholder: '请选择任务状态'
      }
    },
    {
      label: '任务类型',
      prop: 'type',
      rules: [{ required: true, message: '任务类型不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入任务类型'
      }
    },
    {
      label: '调用算法时所用的参数',
      prop: 'parameters',
      compOptions: {
        type: 'textarea',
        clearable: true,
        placeholder: '请输入内容'
      }
    },
    {
      label: '开始时间',
      prop: 'startTime',
      compOptions: {
        elTagName: 'date-picker',
        type: 'date',
        placeholder: '请选择开始时间'
      }
    },
    {
      label: '结束时间',
      prop: 'endTime',
      compOptions: {
        elTagName: 'date-picker',
        type: 'date',
        placeholder: '请选择结束时间'
      }
    },
    {
      label: '耗时',
      prop: 'costSecond',
      compOptions: {
        placeholder: '请输入耗时'
      }
    },
    {
      label: '日志',
      prop: 'log',
      compOptions: {
        type: 'textarea',
        clearable: true,
        placeholder: '请输入内容'
      }
    },
    {
      label: '序号',
      prop: 'index',
      compOptions: {
        placeholder: '请输入序号'
      }
    }
  ]
}
</script>
