<template>
  <div class="cmain">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="通知标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入标题" clearable maxlength="30" show-word-limit />
      </el-form-item>
      <el-form-item label="通知内容" prop="contents">
        <editor v-model="form.contents" placeholder="请输入内容"></editor>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="通知状态" prop="state">
            <el-select v-model="form.state" placeholder="请选择">
              <el-option label="正常" :value="0"></el-option>
              <el-option label="停用" :value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="通知置顶" prop="top">
            <el-select v-model="form.top" placeholder="请选择">
              <el-option label="否" :value="0"></el-option>
              <el-option label="是" :value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="mfooter">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="$layer.close(layerid)">取 消</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: { state: 0, top: 0 },
      rules: {
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        type: [{ required: true, message: '类型不能为空', trigger: 'blur' }],
        contents: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
        top: [{ required: true, message: '是否置顶不能为空', trigger: 'blur' }],
        state: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
      }
    };
  },
  props: {
    param: {
      type: Object,
      default: () => {
        return {};
      }
    },
    layerid: {
      type: String
    }
  },
  mounted() {
    if (this.param.id) {
      this.ajax({ url: '/work/notice/detail/' + this.param.id }).then((response) => {
        this.form = response.data;
        this.form.contents = response.data.contents.replace(new RegExp('/profile/upload/', 'g'), this.baseUrl + '/profile/upload/');
      });
    }
  },
  methods: {
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            this.ajax({ method: 'post', url: '/work/notice/edit', data: this.form }).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          } else {
            this.ajax({ method: 'post', url: '/work/notice/add', data: this.form }).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          }
        }
      });
    }
  }
};
</script>
