<template>
  <ContentWrap style="padding: 10px;">
    <!-- 搜索工作栏 -->
    <el-form
      class="formClass"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      size="large"
    >
    <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.taskType"
          placeholder="请选择任务类型"
          clearable
          class="!w-240px"
        >
          <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
          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 taskTypeList"
            :key="dict.id"
            :label="dict.tagName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="startDate" style="width: 100%;">
        <el-select
          v-model="queryParams.selectWeek"
          placeholder="请选择日期范围"
          clearable
          class="!w-240px"
        >
          <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"><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="mergeClass" >
        <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" :cell-style="{'text-align': 'left'}">
      <el-table-column label="任务名称" align="left" prop="title" width="200"/>
      <el-table-column label="任务类型" align="left" prop="taskType">
        <template #default="scope">
        <el-tag>{{ scope.row.taskTypeName }}</el-tag>
        </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="left" min-width="50px">
        <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>
    <!-- 分页 -->
    <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 { 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'


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

const  taskTypeList:any = ref([])

const mydateFormatter3 = ()=> {

}

/** 查询列表 */
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 taskExec = async (data) => {

  loading.value = true

  try {
    await TaskInfoApi.taskExec(data)
    message.success(t('common.createSuccess'))
    getList()
  } 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>
<style scoped lang="scss">
  .formClass {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr;
    grid-template-rows: 2fr;
    align-items: center;
    row-gap: 20px;
    column-gap: 40px;

    .mergeClass {
      grid-column: 2 span;
      
      :deep(.el-form-item__content) {
        display: flex !important;
        justify-content:flex-end !important;
      }
    }

    

    :deep(.el-form-item) {
      margin: 0 !important;
    }
  }
</style>
