<template>
  <el-dialog 
    :title="title" 
    :visible.sync="dialogVisible" 
    width="800px" 
    append-to-body 
    :close-on-click-modal="false"
    @close="handleClose"
    @open="handleOpen"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-loading="loading">
      <el-row>
        <form-col :span="24" label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </form-col>
        <form-col :span="span" label="重要程度" prop="level">
          <el-radio-group v-model="form.level" placeholder="请选择重要程度" style="width: 100%;">
            <el-radio-button v-for="dict in dict.type.notice_level" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
          </el-radio-group>
        </form-col>
        <form-col :span="span" label="置顶" prop="top">
          <el-switch v-model="form.top" />
        </form-col>
        <form-col :span="24" label="内容">
          <el-input type="textarea" v-model="form.content" placeholder="请输入内容" maxlength="1000" show-word-limit/>
        </form-col>
        <form-col :span="24" label="附件" prop="attaches">
          <image-upload v-model="form.attaches"/>
        </form-col>
        <form-col :span="24" label="接收用户" prop="receiveUserIds">
          <user-select v-model="form.receiveUserIds" multiple/>
        </form-col>
        <form-col :span="24" label="接收部门" prop="receiveDeptIds">
          <dept-select v-model="form.receiveDeptIds" multiple/>
        </form-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" :loading="submitLoading">确 定</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getNotice, addNotice, updateNotice } from "@/api/bst/notice";
import FormCol from "@/components/FormCol/index.vue";
import DeptSelect from '@/components/Business/Dept/DeptSelect.vue';
import UserSelect from '@/components/Business/User/UserSelect.vue';
import { NoticeLevel } from '@/utils/enums';
export default {
  name: "NoticeEditDialog",
  components: { FormCol, DeptSelect, UserSelect },
  dicts: ['notice_level'],
  props: {
    show: {
      type: Boolean,
      required: true
    },
    id: {
      type: [String, Number],
      default: null
    }
  },
  data() {
    return {
      loading: false,
      submitLoading: false,
      span: 12,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        level: [
          { required: true, message: "重要程度不能为空", trigger: "change" }
        ],
        top: [
          { required: true, message: "是否置顶不能为空", trigger: "blur" }
        ]
      }
    };
  },
  computed: {
    title() {
      return this.id ? '修改公告' : '新增公告';
    },
    dialogVisible: {
      get() {
        return this.show;
      },
      set(value) {
        this.$emit('update:show', value);
      }
    }
  },
  methods: {
    /** 获取详细信息 */
    getInfo(id) {
      this.loading = true;
      getNotice(id).then(response => {
        this.form = response.data;
      }).finally(() => {
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        content: null,
        level: NoticeLevel.MEDIUM,
        attaches: null,
        top: false,
        receiveUserIds: [],
        receiveDeptIds: []
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.submitLoading = true;
            updateNotice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$emit('success');
              this.handleClose();
            }).finally(() => {
              this.submitLoading = false;
            });
          } else {
            this.submitLoading = true;
            addNotice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.$emit('success');
              this.handleClose();
            }).finally(() => {
              this.submitLoading = false;
            });
          }
        }
      });
    },
    // 取消按钮
    handleClose() {
      this.$emit('update:show', false);
    },
    // 打开事件
    handleOpen() {
      if (this.id) {
        this.getInfo(this.id);
      } else {
        this.reset();
      }
    }
  }
};
</script>

<style lang="scss" scoped>

</style>