<!--
 * @Description: 新增/编辑弹框
 * @Author: 空白格
 * @Date: 2023-07-04 13:55:00
 * @LastEditors: 空白格
 * @LastEditTime: 2023-07-06 09:08:39
 * @FilePath: \cattle_webui\src\views\system\noticeMgr\Dialog\AddOrEditDialog.vue
 * @Copyright: Copyright (c) 2016~2023 by 空白格, All Rights Reserved.
-->
<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="700px"
    append-to-body
    :close-on-click-modal="false"
    @close="cancel"
  >
    <div class="dialog">
      <el-form :model="form" ref="form" :rules="rules" label-width="120px">
        <el-form-item label="名称：" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" disabled />
        </el-form-item>
        <el-form-item label="模块：" prop="type">
          <el-select
            v-model="form.type"
            placeholder="请选择模块"
            disabled
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in moduleList"
              :key="index"
              :value="item.dictValue"
              :label="item.dictLabel"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="内容详情：" prop="content">
          <editor v-model="form.content" :fileSize="20" :min-height="200" />
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button
        type="primary"
        @click="submitForm"
        v-loading.fullscreen.lock="loading"
        element-loading-text="提交中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <span v-if="loading">提交中...</span>
        <span v-else>保存</span>
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updateNoticeMgr } from "@/api/system/noticeMgr";
import Editor from "@/components/Editor";
export default {
  name: "AddOrEditDialog",
  props: {
    moduleList: {
      type: Array,
      default: () => [],
    },
  },
  components: {
    Editor,
  },
  data() {
    return {
      title: "编辑",
      model: "EDIT",
      open: false,
      loading: false,
      form: {
        id: undefined,
        name: "",
        type: "",
        content: "",
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: ["change","blur"] }],
        content: [
          { required: true, message: "请输入内容详情", trigger: ["change","blur"] },
        ],
      },
    };
  },
  methods: {
    /**
     * 打开弹框
     * @date 2022-09-20
     * @param {any} obj
     * @returns {any}
     */
    openDialog(model, obj) {
      this.model = model;
      if (model === "ADD") {
        this.title = "添加";
        this.open = true;
        this.$nextTick(() => {
          this.$refs["form"].clearValidate();
        });
      }
      if (model === "EDIT") {
        this.title = "编辑";
        this.open = true;
        this.$nextTick(() => {
          this.form.id = obj.id;
          this.form.name = obj.name;
          this.form.type = obj.type;
          this.form.content = obj.content;
        });
      }
    },
    /**
     * 保存
     * @date 2022-09-20
     * @returns {any}
     */
    submitForm() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          try {
            this.loading = true;
            const { code } = await updateNoticeMgr({ ...this.form });
            if (code === 200) {
              this.$message.success("修改成功!");
              this.$emit("getList");
              this.cancel();
            }
          } catch (error) {
          } finally {
            this.loading = false;
          }
        }
      });
    },
    /**
     * 重置
     * @date 2022-09-20
     * @returns {any}
     */
    reset() {
      this.$refs["form"].resetFields();
      this.form.id = undefined;
    },
    /**
     * 关闭弹框
     * @date 2022-09-20
     * @returns {any}
     */
    cancel() {
      this.reset();
      this.open = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.dialog {
  padding: 0 30px;
  max-height: 65vh;
  overflow-y: auto;
}
</style>
