<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="接收人" prop="recipient">
              <el-input v-model="queryParams.recipient" placeholder="请输入接收人" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="邮件地址" prop="email">
              <el-input v-model="queryParams.email" placeholder="请输入邮件地址" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="发送时间" prop="sendTime">
              <el-date-picker v-model="queryParams.sendTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择发送时间" />
            </el-form-item>
<!--            <el-form-item label="请求报文ID" prop="requestId">-->
<!--              <el-input v-model="queryParams.requestId" placeholder="请输入请求报文ID" clearable @keyup.enter="handleQuery" />-->
<!--            </el-form-item>-->
            <el-row>
              <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-row>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <el-table v-loading="loading" :data="emailsendList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="true" label="发送记录唯一标识" align="center" prop="sendId" />
        <el-table-column label="模板唯一标识" align="center" prop="templateId" />
        <el-table-column label="接收人" align="center" prop="recipient" />
        <el-table-column label="邮件地址" align="center" prop="email" />
        <el-table-column label="邮件内容" align="center" prop="emailContent" />
        <el-table-column label="发送时间" align="center" prop="sendTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="发送状态" align="center" prop="sendStatus" />
        <!--        <el-table-column label="请求报文ID" align="center" prop="requestId" />-->
      </el-table>

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

<script setup name="Emailsend" lang="ts">
import { listEmailsend, getEmailsend, delEmailsend, addEmailsend, updateEmailsend } from '@/api/msg/emailsend';
import { EmailsendVO, EmailsendQuery, EmailsendForm } from '@/api/msg/emailsend/types';
import { parseTime } from '../../../utils/ruoyi';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const emailsendList = ref<EmailsendVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const emailsendFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: EmailsendForm = {
  sendId: undefined,
  templateId: undefined,
  recipient: undefined,
  email: undefined,
  emailContent: undefined,
  sendTime: undefined,
  sendStatus: undefined,
  requestId: undefined
};
const data = reactive<PageData<EmailsendForm, EmailsendQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    templateId: undefined,
    recipient: undefined,
    email: undefined,
    emailContent: undefined,
    sendTime: undefined,
    sendStatus: undefined,
    requestId: undefined,
    params: {}
  },
  rules: {
    sendId: [{ required: true, message: '发送记录唯一标识不能为空', trigger: 'blur' }],
    templateId: [{ required: true, message: '模板唯一标识不能为空', trigger: 'blur' }],
    recipient: [{ required: true, message: '接收人不能为空', trigger: 'blur' }],
    email: [{ required: true, message: '邮件地址不能为空', trigger: 'blur' }],
    emailContent: [{ required: true, message: '邮件内容不能为空', trigger: 'blur' }],
    sendTime: [{ required: true, message: '发送时间不能为空', trigger: 'blur' }],
    sendStatus: [{ required: true, message: '发送状态不能为空', trigger: 'change' }],
    requestId: [{ required: true, message: '请求报文ID不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询邮件发送记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listEmailsend(queryParams.value);
  emailsendList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  emailsendFormRef.value?.resetFields();
};

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

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

/** 多选框选中数据 */
const handleSelectionChange = (selection: EmailsendVO[]) => {
  ids.value = selection.map((item) => item.sendId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加邮件发送记录';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: EmailsendVO) => {
  reset();
  const _sendId = row?.sendId || ids.value[0];
  const res = await getEmailsend(_sendId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改邮件发送记录';
};

/** 提交按钮 */
const submitForm = () => {
  emailsendFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.sendId) {
        await updateEmailsend(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addEmailsend(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: EmailsendVO) => {
  const _sendIds = row?.sendId || ids.value;
  await proxy?.$modal.confirm('是否确认删除邮件发送记录编号为"' + _sendIds + '"的数据项？').finally(() => (loading.value = false));
  await delEmailsend(_sendIds);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'msg/emailsend/export',
    {
      ...queryParams.value
    },
    `emailsend_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
