<template>
  <ContentWrap style="padding: 10px">
    <section class="flex flex-col gap-20px">
      <el-form :model="queryParams" ref="queryFormRef" inline>
        <el-form-item label="" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入任务标题"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="" prop="taskType">
          <el-select v-model="queryParams.taskType" placeholder="请选择任务类型" clearable>
            <el-option
              v-for="dict in taskTypeList"
              :key="dict.id"
              :label="dict.tagName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="taskType">
          <el-select v-model="queryParams.status" placeholder="请选择任务状态" clearable>
            <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.deptId" placeholder="请选择发布部门" clearable>
            <el-option
              v-for="dict in createDeptList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="startDate">
          <el-select v-model="queryParams.selectWeek" placeholder="请选择日期范围" clearable>
            <el-option
              v-for="dict in getIntDictOptions(DICT_TYPE.SELECT_WEEK)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery" type="primary" plain
            ><Icon icon="ep:search" class="mr-5px" /> 查询</el-button
          >
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
        <el-form-item class="last-children">
          <el-button
            type="primary"
            v-hasPermi="['system:task-info:create']"
            @click="push({ path: '/task/create' })"
          >
            <Icon icon="ep:circle-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>
      <el-table
        v-loading="loading"
        :data="list"
        :show-overflow-tooltip="true"
      
      >
        <el-table-column label="任务名称" align="left" prop="title" width="200" />
        <el-table-column label="任务类型" align="left" prop="taskType">
          <template #default="scope">
            {{ scope.row.taskTypeName }}
          </template>
        </el-table-column>
        <el-table-column label="发布部门" align="left" prop="deptName" />
        <el-table-column label="任务状态" align="left" 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="left" min-width="100px">
          <template #default="scope">
            {{ new Set(scope.row.enterpriseIdes?.map((e) => e.type)).size }}个区域 ,
            {{ scope.row.enterpriseIdes.length }}家企业
          </template>
        </el-table-column>
        <el-table-column label="执行时间" align="left" prop="startDate">
          <template #default="scope">
            {{ scope.row.startDate }}
            {{ getDictLabel(DICT_TYPE.TASK_NOTICE_TIME, scope.row.execCycle) }}
          </template>
        </el-table-column>

        <el-table-column label="操作选项" align="center" width="100px">
          <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="primary" @click="openForm(scope.row.id)"> 详情 </el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        :total="total"
        :show-page-size="false"
        layout="total, prev, pager, next"
        v-model:current-page="queryParams.pageNo"
        v-model:page-size="queryParams.pageSize"
        @change="getList"
        class="ml-auto"
      />
    </section>
  </ContentWrap>

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

<script setup lang="ts">
import { getDictLabel, DICT_TYPE, getIntDictOptions } from '@/utils/dict'

import download from '@/utils/download'
import { TaskInfoApi, TaskInfoVO } from '@/api/system/taskinfo'
import TaskInfoForm from './TaskInfoForm.vue'
import { TagLibraryApi } from '@/api/system/taglibrary'
import { deptByMenu } from '@/api/system/user'

/** 任务表，用于存储所有的任务信息，任务可由不同用户创建并管理。 列表 */
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,
  selectWeek: undefined,
  deptId: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const taskTypeList: any = ref([])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TaskInfoApi.getTaskInfoPage(queryParams)
    list.value = data.list
    total.value = data.total

    //任务标签
    const params = {
      tagType: 2,
      pageSize: -1
    }

    const tag = await TagLibraryApi.getTagLibraryPage(params)
    taskTypeList.value = tag.list
  } finally {
    loading.value = false
  }
}

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

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

/** 添加/修改操作 */
const formRef = ref()

/** 删除按钮操作 */
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
  }
}

const createDeptList = ref()
const getCreateDept = async () => {
  const data = await deptByMenu()
  createDeptList.value = data
}

/** 初始化 **/
onMounted(() => {
  getList()
  getCreateDept()
})
</script>
<style scoped lang="scss">
::v-deep(.el-form-item) {
  width: calc(25% - 15px);
  &:last-child {
    margin-left: auto;
    width: fit-content;
  }
}
</style>
