<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="邮件模板名称" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入邮件模板名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮件模板标题" prop="templateTitle">
        <el-input
          v-model="queryParams.templateTitle"
          placeholder="请输入邮件模板标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          v-no-more-click
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索
        </el-button>
        <el-button
          v-no-more-click
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-no-more-click
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['iscs:template:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-no-more-click
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['iscs:template:remove']"
          >批量删除
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="templatesList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        prop="templateCode"
        label="邮件模板编号"
      ></el-table-column>
      <el-table-column prop="templateName" label="邮件模板名称"></el-table-column>
      <el-table-column prop="templateTitle" label="邮件模板标题"></el-table-column>
      <el-table-column prop="templateContent" label="内容">
        <template slot-scope="scope">
          <el-button type="text" @click="check(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="添加时间"
        align="center"
        prop="createTime"
        width="200"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-no-more-click
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['iscs:template:edit']"
            >编辑
          </el-button>
          <el-button
            v-no-more-click
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['iscs:template:remove']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
    <!-- 添加或修改邮件模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="邮件模板编号" prop="templateCode">
          <el-input
            :disabled="isEdit"
            v-model="form.templateCode"
            placeholder="请输入邮件模板编号"
          />
        </el-form-item>
        <el-form-item label="邮件模板名称" prop="templateName">
          <el-input v-model="form.templateName" placeholder="请输入邮件模板名称" />
        </el-form-item>
        <el-form-item label="邮件模板标题" prop="templateTitle">
          <el-input v-model="form.templateTitle" placeholder="请输入邮件模板标题" />
        </el-form-item>
        <el-form-item label="邮件模板内容" prop="templateContent">
          <el-input
            type="textarea"
            :rows="20"
            v-model="form.templateContent"
            placeholder="请输入邮件模板内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-no-more-click type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button v-no-more-click @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="内容查看" :visible.sync="openCheck" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" >
        <el-input type="textarea" autosize v-model="form.templateContent" ></el-input>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-no-more-click @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEmailTemplates,
  getEmailTemplatesInfo,
  addEmailTemplates,
  updateEmailTemplates,
  delEmailTemplates,
} from "@/api/mes/email/templates";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { genCode } from "@/api/system/autocode/rule";
import Template from "@/views/print/printtemplate/list.vue";

export default {
  name: "Dept",
  dicts: ["sys_normal_disable", "sop_type"],
  components: { Template, Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      //自动生成编码
      autoGenFlag: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 选中数组
      codes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 邮件模板列表
      templatesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openCheck:false,//内容查看
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        templateName: "",
        templateTitle: "",
      },
      isEdit: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        templateCode: [
          { required: true, message: "邮件模板编号不能为空", trigger: "blur" },
        ],
        name: [
          { required: true, message: "邮件模板名称不能为空", trigger: "blur" },
        ],
        content: [
          { required: true, message: "邮件模板内容不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },

  methods: {
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      listEmailTemplates(this.queryParams).then((response) => {
        debugger;
        this.templatesList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
        console.log(response.data.records, "列表内容");
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.openCheck = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        templateName: undefined,
        templateTitle: undefined,
        templateContent: undefined,
      };
      this.autoGenFlag = false;
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.templateName = "";
      this.queryParams.templateTitle = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.codes = selection.map((item) => item.templateId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.isEdit = false;
      this.title = "添加邮件模板";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isEdit = true;
      console.log(row,'row');
      getEmailTemplatesInfo(row.templateId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改邮件模板";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            updateEmailTemplates(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEmailTemplates(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /* 查看 */
    check(row){
        console.log(row);
        this.openCheck = true;
        this.form = row;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const templateIds = row.templateId || this.codes;
      this.$modal
        .confirm('是否确认删除名称为"' + templateIds + '"的数据项？')
        .then(function () {
          return delEmailTemplates(templateIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
