<template>
  <el-dialog
    :title="title"
    width="800px"
	top="20px"
    :visible.sync="dialogVisible"
    @close="diaClose"
    :close-on-click-modal="true"
  >
    <div>
      <el-form ref="form" :rules="rules" :model="form" label-width="100px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="title">
          <el-radio-group v-model="form.type">
            <el-radio
              :label="item.value"
              v-for="(item, index) in types"
              :key="index"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
		<el-form-item label="状态" required>
          <el-switch
            v-model="form.state"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor
            ref="myTextEditor"
            v-model="form.content"
            :options="quillOption"
            style="
              padding-bottom: 50px;
              height: 300px;
              width: 100%;
              display: inline-table;
            "
          >
          </quill-editor>
        </el-form-item>

      </el-form>
    </div>

    <div class="dialog-footer">
      <el-button @click="diaClose">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { $announcement as $api } from "@/api/announcement.js";
import { $types } from "../data.js";
import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import quillConfig from "../../locality/quill-config.js";
export default {
  components: {
    quillEditor,
  },

  data() {
    return {
      quillOption: quillConfig,
      types: $types,
      dialogVisible: false,
      title: "",
      rules: {
        title: [{ required: true, message: "请输入公告标题", trigger: "blur" }],
        content: [
          { required: true, message: "请输入公告内容", trigger: "blur" },
        ],
      },
      form: {
        title: "",
        content: "",
        state: 1,
        type: "",
      },
    };
  },
  methods: {
    open(item) {
      console.log(item);
      this.dialogVisible = true;
      Object.assign(this.form, item);
      this.title = item ? "修改公告" : "添加公告";
    },
    diaClose() {
      this.dialogVisible = false;
      this.form = {
        title: "",
        content: "",
        state: 1,
        type: "",
      };
      this.$refs.form.resetFields();
    },
    async confirm() {
      if (!this.form.title) {
        return this.$message.error("请输入公告标题");
      }
      if (!this.form.content) {
        return this.$message.error("请输入公告内容");
      }
      if (this.form.type === "") {
        return this.$message.error("请选择公告类型");
      }
      this.submit();
    },
    async submit() {
      let res = { data: { code: 1 } };
      const submitForm = {
        ...this.form,
      };
      if (this.form.id) {
        res = await $api.update(submitForm);
      } else {
        res = await $api.add(submitForm);
      }
      console.log(res);
      const { data } = res;
      if (data.code == 0) {
        this.$message.success(this.form.id ? "修改成功" : "添加成功");
        this.$emit("refresh");
        this.diaClose();
      } else {
        this.$message.error(data.msg);
      }
    },
  },
};
</script>
<style scoped lang="scss">
.el-form-item__label {
  text-align: left;
}

::v-deep .el-form-item__label {
  text-align: left;
}

.dialog-footer {
  padding-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.upload-file-box {
  border-radius: 6px;
  width: 148px;
  height: 148px;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  border: 1px solid #c0c4cc;

  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}
</style>
