<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
        >新增</el-button
      >
    </el-row>
    <el-table ref="tables" v-loading="loading" :data="list">
      <el-table-column
        label="邮箱名称"
        align="center"
        width="200"
        prop="recipientEmail"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建时间"
        align="center"
        width="200"
        prop="createTime"
        :show-overflow-tooltip="true"
      />
    <el-table-column
        label="备注"
        align="center"
        width="200"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="showEdit(scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加邮箱" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="邮箱" required>
              <el-input
                v-model="form.recipientEmail"
                type="input"
                placeholder="请输入邮箱"
              ></el-input>
            </el-form-item>
            <el-form-item label="备注" required>
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入备注"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onAdd">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑邮箱" :visible.sync="editOpen" width="600px" append-to-body>
      <el-form ref="form" :model="editForm" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="邮箱" required>
              <el-input
                v-model="editForm.recipientEmail"
                type="input"
                placeholder="请输入邮箱"
              ></el-input>
            </el-form-item>
            <el-form-item label="备注" required>
              <el-input
                v-model="editForm.remark"
                type="textarea"
                placeholder="请输入备注"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleEdit">确定修改</el-button>
        <el-button @click="cancelEdit">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { emailList, add, deleteList, updateList } from "@/api/email/index";
export default {
  name: "email",
  dicts: [],
  data() {
    return {
      open: false,
      editOpen: false,
      form: {},
      // 遮罩层
      loading: true,
      list: [],
      queryParams: {
        pageNumber: 1,
        pageSize: 10,
      },
      editForm: {},
    };
  },
  created() {
    this.getList();
    // this.onGetDict()
  },
  methods: {
    // 编辑
    cancelEdit() {
      this.editOpen = false;
      this.editForm = {};
    },

    // 修改
    handleEdit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateList(this.addDateRange(this.editForm)).then((response) => {
            this.$modal.msgSuccess("编辑成功");
            this.getList();
            this.cancelEdit();
          });
        }
      });
    },
    showEdit(row) {
      this.editOpen = true;
      this.editForm = JSON.parse(JSON.stringify(row)) ;
    },

    // 新增
    handleAdd() {
      this.open = true;
    },
    cancel() {
      this.form = {};
      this.open = false;
    },
    onAdd() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          add(this.addDateRange({ ...this.form })).then((response) => {
            this.$modal.msgSuccess("添加成功");
            this.getList();
            this.cancel();
          });
        }
      });
    },

    // 获取数据字典
    // onGetDict() {
    //     getDict(this.addDateRange({type:'memberBenefitType'})).then((response) => {

    //   });
    // },
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      emailList(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
        this.list = response.data;
        this.loading = false;
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return deleteList(row.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$modal
        .confirm("是否确认清空所有登录日志数据项？")
        .then(function () {
          return cleanLogininfor();
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("清空成功");
        })
        .catch(() => {});
    },
  },
};
</script>
