<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="作业名称" prop="ticketName">
        <el-input
          v-model="queryParams.ticketName"
          placeholder="请输入作业名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="作业类型" prop="ticketType">
        <el-select
          v-model="queryParams.ticketType"
          placeholder="请选择作业类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.TICKET_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="作业状态" prop="ticketStatus">
        <el-select
          v-model="queryParams.ticketStatus"
          placeholder="请选择作业状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.TICKET_STATUS)"
            :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-button
          type="primary"
          plain
          @click="openForm('create', 0, 'noSop')"
          v-hasPermi="['iscs:job:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          新增普通作业
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create', 0, 'Sop')"
          v-hasPermi="['iscs:job:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          新增SOP作业
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="multiple"
          @click="handleDelete()"
          v-hasPermi="['iscs:job:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" />
          批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="jobList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" align="center" />
      <el-table-column label="作业名称" prop="ticketName" />
      <el-table-column label="作业类型" prop="ticketType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TICKET_TYPE" :value="scope.row.ticketType" />
        </template>
      </el-table-column>
      <el-table-column label="作业状态" prop="ticketStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TICKET_STATUS" :value="scope.row.ticketStatus" />
        </template>
      </el-table-column>
      <el-table-column label="所属区域" prop="workstationName" :show-overflow-tooltip="true" />
      <el-table-column label="设备/工艺" prop="machineryName" align="center" />
      <el-table-column label="作业执行监控" align="center">
        <template #default="{ row }">
          <el-button
            link
            type="primary"
            @click="lookMonitor(row.id)"
            v-hasPermi="['iscs:job:update']"
          >
            查看
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button
            link
            type="primary"
            @click="openForm('update', row.id, '')"
            v-hasPermi="['iscs:job:update']"
          >
            修改
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(row.id)"
            v-hasPermi="['iscs:job:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script lang="ts" setup>
import * as JobApi from '@/api/job/index'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

const router = useRouter()

defineOptions({ name: 'SopManagement' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const jobList = ref([]) // 列表的数据
const total = ref(0) // 总条数
const ids = ref<number[]>([]) // 选中的数据
const multiple = ref(true) // 非多个禁用
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  ticketName: undefined,
  ticketType: undefined,
  ticketStatus: undefined
})
const queryFormRef = ref() // 搜索的表单

/** 多选框选中数据 */

const handleSelectionChange = (selection: any[]) => {
  ids.value = selection.map((item) => item.id)

  multiple.value = !selection.length
}

/** 查询模式列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await JobApi.getJobTicketPage(queryParams)
    jobList.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

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

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

/** 添加/修改/查看操作 */
const openForm = (type: string, id?: number, info?: string) => {
  if (type == 'create' && info == 'noSop') {
    router.push({
      name: 'CreateJob',
      query: { id: id, type: 'create', info: info }
    })
  } else if (type == 'create'&& info == 'Sop') {
    router.push({
      name: 'CreateSopJob',
      query: { id: id, type: 'create',info: info }
    })
  } else if (type == 'update') {
    router.push({
      name: 'UpdateJob',
      query: { id: id, type: 'update' }
    })
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  const jobId = id || ids.value

  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await JobApi.deleteJobTicketList(jobId)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(async () => {
  await getList()
})

// 查看作业执行监控
const lookMonitor = (id:number) => {
router.push({
  name:'JobMonitor',
  query: { id: id },
})
}
</script>
<style scoped></style>
