<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="提醒事项" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入提醒事项"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="邮件模板" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入邮件模板"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </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:notify:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="templatesNotifyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="提醒事项" prop="name" />
      <el-table-column label="是否激活" prop="status">
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="'1'"
            :inactive-value="'0'"
            @change="handleFrozenChange(row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="templateCode" label="邮件模板编码" />
      <el-table-column prop="reminderTime" label="提醒时长">
        <template #default="{ row }">
          {{ formattedTime(row.reminderTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150">
        <template #default="{ row }">
          <el-button
            link
            type="primary"
            @click="openForm('update', row.id)"
            v-hasPermi="['iscs:notify:update']"
          >
            修改
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(row.id)"
            v-hasPermi="['iscs:notify:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>

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

<script lang="ts" setup>
import * as EmailNotifyApi from '@/api/email/notify/index'
import EmailNotifyForm from './EmailNotifyForm.vue'

defineOptions({ name: 'EmailNotify' })

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

const loading = ref(true) // 列表的加载中
const templatesNotifyList = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  templateName: undefined
})
const queryFormRef = ref() // 搜索的表单
const refreshTable = ref(true) // 重新渲染表格状态
const ids=ref()
/** 多选框选中数据 */
const handleSelectionChange = (selection: any[]) => {
  ids.value = selection.map(item => item.id)
}
/** 查询邮件提醒列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await EmailNotifyApi.listIsMailNotifyConfigPage(queryParams)
    templatesNotifyList.value = data.list
  } finally {
    loading.value = false
  }
}

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.pageNo = 1
  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 EmailNotifyApi.deleteIsMailNotifyConfig(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 是否激活状态修改 */
const handleFrozenChange = async (row: any) => {
  try {
    const data={
      ...row,
      status:'1'
    }
    await EmailNotifyApi.updateIsMailNotifyConfig(data)
    message.success(row.status === '1' ? '激活成功' : '取消激活')
  } catch {}
}

/** 格式化时间 */
const formattedTime = (totalSeconds: number) => {
  const days = Math.floor(totalSeconds / (24 * 60 * 60))
  const hours = Math.floor((totalSeconds % (24 * 60 * 60)) / (60 * 60))
  const minutes = Math.floor((totalSeconds % (60 * 60)) / 60)
  const seconds = totalSeconds % 60

  const parts = []
  if (days > 0) parts.push(`${days}天`)
  if (hours > 0) parts.push(`${hours}小时`)
  if (minutes > 0) parts.push(`${minutes}分钟`)
  if (seconds > 0) parts.push(`${seconds}秒`)

  return parts.join(' ')
}

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