<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="模式名称" prop="modeName">
        <el-input
          v-model="queryParams.modeName"
          placeholder="请输入模式名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="标题" prop="modeTitle">
        <el-input
          v-model="queryParams.modeTitle"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支持共锁" prop="isColockSupport">
        <el-select
          v-model="queryParams.isColockSupport"
          placeholder="请选择支持共锁"
          clearable
          class="!w-240px"
        >
          <el-option label="是" value="true" />
          <el-option label="否" value="false" />
        </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')"
          v-hasPermi="['iscs:mode:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="modeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" width="80" align="center" >
        <template #default="{ row }">
          <span :style="{ color: row.isPreset ? 'red' : 'black' }">
            {{ row.id }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="模式名称" prop="modeName">
        <template #default="{ row }">
          <span :style="{ color: row.isPreset ? 'red' : 'black' }">
            {{ row.modeName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="标题" prop="modeTitle" />
      <el-table-column label="描述" prop="modeDescription" :show-overflow-tooltip="true" />
      <el-table-column label="支持共锁" prop="isColockSupport" width="100" align="center">
        <template #default="{ row }">
          <el-checkbox
            :model-value="Boolean(row.isColockSupport)"
            :disabled="true"
            class="black-checkbox"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createBy" width="120" />
      <el-table-column label="创建时间" prop="createTime" width="180" align="center">
        <template #default="{ row }">
          {{ formatDate(row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="更新人" prop="updateBy" width="120" />
      <el-table-column label="更新时间" prop="updateTime" width="180" align="center">
        <template #default="{ row }">
          {{ formatDate(row.updateTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template #default="{ row }">
          <el-button
            link
            type="primary"
            @click="openForm('view', row.id)"
            v-hasPermi="['iscs:mode:view']"
          >
            查看
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', row.id)"
            v-hasPermi="['iscs:mode:update']"
          >
            修改
          </el-button>
          <el-button

            link
            type="danger"
            @click="handleDelete(row.id)"
            v-hasPermi="['iscs:mode: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 ModeApi from '@/api/custonWorkflow/index'
import { formatDate } from '@/utils/formatTime'

const router = useRouter()

defineOptions({ name: 'ModeManagement' })

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

const loading = ref(true) // 列表的加载中
const modeList = ref([]) // 列表的数据
const total = ref(0) // 总条数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  modeName: undefined,
  modeTitle: undefined,
  isColockSupport: undefined
})
const queryFormRef = ref() // 搜索的表单
const ids = ref() // 选中的ID数组

/** 多选框选中数据 */
const handleSelectionChange = (selection: any[]) => {
  ids.value = selection.map((item) => item.id)
}

/** 查询模式列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ModeApi.getWorkflowModePage(queryParams)
    modeList.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) => {
  if (type === 'create') {
    router.push({
      name: 'CreateView',
      query: { id: id, type: 'create' }
    })
  } else if (type === 'update') {
    router.push({
      name: 'UpdateView',
      query: { id: id, type: 'update', }
    })
  } else if (type === 'view') {
    router.push({
      name: 'CheckView',
      query: { id: id, type: 'view' }
    })
  }
}

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

/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>
<style scoped>
/* 未选中状态：黑色边框，白色背景，无对勾 */
.black-checkbox :deep(.el-checkbox__input.is-disabled .el-checkbox__inner) {
  background-color: #ffffff !important;
  border-color: #000000 !important;
  opacity: 1 !important;
}

/* 选中状态：黑色边框，白色背景，黑色对勾 */
.black-checkbox :deep(.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner) {
  background-color: #ffffff !important;
  border-color: #000000 !important;
  opacity: 1 !important;
}

/* 选中状态的对勾图标：黑色 */
.black-checkbox :deep(.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner::after) {
  border-color: #000000 !important;
  opacity: 1 !important;
}

/* 未选中状态的对勾图标：隐藏 */
.black-checkbox :deep(.el-checkbox__input.is-disabled .el-checkbox__inner::after) {
  border-color: transparent !important;
  opacity: 0 !important;
}
</style>
