<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="108px"
    >
      <el-form-item label="任务标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入任务标题"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />


      </el-form-item>


      <el-form-item label="任务状态" prop="taskType">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择任务状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.TASK_STATE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="任务类型" prop="taskType">
        <el-select
          v-model="queryParams.taskType"
          placeholder="请选择任务类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.TASK_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务时间" prop="startDate">
        <el-date-picker
          v-model="queryParams.startDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          v-hasPermi="['system:task-info:create']"
          @click="push({ path: '/task/create' })"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 创建任务
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:task-info:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="任务编号" align="center" width="150px" prop="taskNumber" />
      <el-table-column label="任务状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TASK_STATE" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="任务名称" align="center" prop="title" />
      <!-- <el-table-column label="执行周期" align="center" prop="execCycle">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TASK_EXEC_TIME" :value="scope.row.execCycle" />
        </template>
      </el-table-column> -->
      <el-table-column label="任务类型" align="center" prop="taskType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TASK_TYPE" :value="scope.row.taskType" />
        </template>
      </el-table-column>
      <el-table-column label="任务优先级" align="center" prop="priority">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TASK_PRIORITY" :value="scope.row.priority" />
        </template>
      </el-table-column>

      <el-table-column label="发布部门" align="center" prop="publishDep" />

      <el-table-column
        label="计划开始时间"
        align="center"
        prop="startDate"
        :formatter="dateFormatter3"
      />
      <el-table-column
        label="计划结束时间"
        align="center"
        prop="endDate"
        :formatter="dateFormatter3"
      />
      <!-- <el-table-column
        label="任务创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      /> -->
      <el-table-column label="创建人" align="center" prop="createName" />
      <!-- <el-table-column label="父任务id" align="center" prop="parentId" />
      <el-table-column label="父子任务类型" align="center" prop="parentType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TASK_TYPE_PARENT" :value="scope.row.parentType" />
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="执行到第几" align="center" prop="taskStep" /> -->

      <el-table-column label="执法对象" align="center" min-width="120px">
        <template #default="scope">

          <el-button link type="primary"   @click="push({
                path: 'create',
                query: {
                  id: scope.row.id
                }
              })
            ">
             <el-Tag type="success">{{scope.row.enterpriseIdes.length}}</el-Tag>&nbsp;家企业 
          </el-button>&nbsp;

        </template>

      </el-table-column>

      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="
              push({
                path: 'create',
                query: {
                  id: scope.row.id
                }
              })
            "
            v-hasPermi="['system:task-info:update']"
           
          >
            详情
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:task-info:delete']"
            v-if="scope.row.status == 1"
          >
            删除
          </el-button>
          <!-- <el-button link type="primary" @click="openForm(scope.row.id)"> 详情 </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <TaskInfoForm ref="formRef" @success="getList" />



</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter3 } from '@/utils/formatTime'
import download from '@/utils/download'
import { TaskInfoApi, TaskInfoVO } from '@/api/system/taskinfo'
import TaskInfoForm from './TaskInfoForm.vue'

/** 任务表，用于存储所有的任务信息，任务可由不同用户创建并管理。 列表 */
defineOptions({ name: 'TaskList' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { push } = useRouter()
const loading = ref(true) // 列表的加载中
const list = ref<TaskInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  title: undefined,
  description: undefined,
  execCycle: undefined,
  taskType: undefined,
  priority: undefined,
  status: undefined,
  startDate: [],
  endDate: [],
  createTime: [],
  createBy: undefined,
  updateBy: undefined,
  parentId: undefined,
  parentType: undefined,
  taskStep: undefined,
  taskTotal: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TaskInfoApi.getTaskInfoPage(queryParams)
    console.log("data==>", data)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

//选择任务中企业范围
const selectEnterprise = async (id: number) => {}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await TaskInfoApi.deleteTaskInfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await TaskInfoApi.exportTaskInfo(queryParams)
    download.excel(data, '任务表，用于存储所有的任务信息，任务可由不同用户创建并管理。.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
