<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="收件人" prop="receiver">
        <el-input v-model="queryParams.receiver" placeholder="请输入收件人" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="邮件主题" prop="subject">
        <el-input v-model="queryParams.subject" placeholder="请输入邮件主题" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="发送状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 200px">
          <el-option label="成功" value="0" />
          <el-option label="失败" value="1" />
          <el-option label="发送中" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="发送时间" style="width: 308px">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:mail:log:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" @click="handleClean" v-hasPermi="['system:mail:log:remove']">清空</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:mail:log:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日志ID" align="center" prop="logId" width="80" />
      <el-table-column label="配置名称" align="center" prop="configName" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="发件人" align="center" prop="sender" :show-overflow-tooltip="true" />
      <el-table-column label="收件人" align="center" prop="receiver" :show-overflow-tooltip="true" />
      <el-table-column label="邮件主题" align="center" prop="subject" :show-overflow-tooltip="true" />
      <el-table-column label="发送状态" align="center" prop="status" width="100">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ getStatusName(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发送时间" align="center" prop="sendTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['system:mail:log:query']">详情</el-button>
          <el-button link type="primary" icon="RefreshRight" @click="handleResend(scope.row)" v-if="scope.row.status === '1'" v-hasPermi="['system:mail:log:resend']">重发</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:mail:log:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 邮件详情对话框 -->
    <el-dialog title="邮件详情" v-model="detailOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="日志ID">{{ detail.logId }}</el-descriptions-item>
        <el-descriptions-item label="配置名称">{{ detail.configName }}</el-descriptions-item>
        <el-descriptions-item label="发件人">{{ detail.sender }}</el-descriptions-item>
        <el-descriptions-item label="收件人">{{ detail.receiver }}</el-descriptions-item>
        <el-descriptions-item label="抄送" v-if="detail.cc">{{ detail.cc }}</el-descriptions-item>
        <el-descriptions-item label="密送" v-if="detail.bcc">{{ detail.bcc }}</el-descriptions-item>
        <el-descriptions-item label="邮件主题" :span="2">{{ detail.subject }}</el-descriptions-item>
        <el-descriptions-item label="内容类型">{{ detail.contentType === 'html' ? 'HTML' : '纯文本' }}</el-descriptions-item>
        <el-descriptions-item label="发送状态">
          <el-tag :type="getStatusType(detail.status)">{{ getStatusName(detail.status) }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="发送时间">{{ detail.sendTime }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detail.createTime }}</el-descriptions-item>
        <el-descriptions-item label="错误信息" :span="2" v-if="detail.errorMsg">
          <span style="color: #f56c6c">{{ detail.errorMsg }}</span>
        </el-descriptions-item>
      </el-descriptions>
      <el-divider content-position="left">邮件内容</el-divider>
      <div class="content-container" v-if="detail.contentType === 'html'" v-html="detail.content"></div>
      <div class="content-container" v-else>{{ detail.content }}</div>
    </el-dialog>
  </div>
</template>

<script setup name="MailLog">
import { listMailLog, getMailLog, delMailLog, cleanMailLog, resendMail } from "@/api/system/mail"

const { proxy } = getCurrentInstance()

const logList = ref([])
const detailOpen = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const multiple = ref(true)
const total = ref(0)
const dateRange = ref([])
const detail = ref({})

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    receiver: null,
    subject: null,
    status: null
  }
})

const { queryParams } = toRefs(data)

/** 查询邮件发送记录列表 */
function getList() {
  loading.value = true
  listMailLog(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    logList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 获取状态名称 */
function getStatusName(status) {
  const map = { '0': '成功', '1': '失败', '2': '发送中' }
  return map[status] || status
}

/** 获取状态标签类型 */
function getStatusType(status) {
  const map = { '0': 'success', '1': 'danger', '2': 'warning' }
  return map[status] || 'info'
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.logId)
  multiple.value = !selection.length
}

/** 查看详情 */
function handleView(row) {
  getMailLog(row.logId).then(response => {
    detail.value = response.data
    detailOpen.value = true
  })
}

/** 重新发送 */
function handleResend(row) {
  proxy.$modal.confirm('是否确认重新发送该邮件？').then(function() {
    return resendMail(row.logId)
  }).then(response => {
    proxy.$modal.msgSuccess(response.msg || "重发成功")
    getList()
  }).catch(() => {})
}

/** 删除按钮操作 */
function handleDelete(row) {
  const logIds = row.logId || ids.value
  proxy.$modal.confirm('是否确认删除邮件记录编号为"' + logIds + '"的数据项？').then(function() {
    return delMailLog(logIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 清空按钮操作 */
function handleClean() {
  proxy.$modal.confirm('是否确认清空所有邮件发送记录？').then(function() {
    return cleanMailLog()
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("清空成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/mail/log/export', {
    ...queryParams.value
  }, `mail_log_${new Date().getTime()}.xlsx`)
}

getList()
</script>

<style scoped>
.content-container {
  padding: 15px;
  background-color: #f5f5f5;
  border-radius: 4px;
  min-height: 200px;
  max-height: 400px;
  overflow: auto;
}
</style>
