<template>
  <div class="app-container">
    <div>{{ editPage ? '修改' : '添加' }}资讯</div>
    <el-form
      style="width:50%;margin:15px 0 0 15px"
      label-position="left"
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="80px"
    >
      <el-form-item label="资讯标题" prop="title">
        <el-input placeholder="请输入" v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="正文" prop="text">
        <editor v-model="ruleForm.text" :min-height="192" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确定</el-button
        >
        <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import Editor from '@/components/Editor';
import { read, getPolicyReadInfo } from '@/api/front/unscramble';
export default {
  components: {
    Editor
  },
  data() {
    return {
      editPage: false,
      ruleForm: {
        title: '',
        text: ''
      },
      rules: {
        title: [{ required: true, message: '请输入资讯标题', trigger: 'blur' }],
        text: [{ required: true, message: '请填写富文本内容', trigger: 'blur' }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.editPage) {
            // 修改
            read(this.ruleForm).then(({ message }) => {
              this.msgSuccess(message);
            });
          } else {
            // 添加
            read(this.ruleForm).then(({ message }) => {
              this.msgSuccess(message);
            });
          }
          this.$router.go(-1);
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
    // resetForm(formName) {
    //   this.$refs[formName].resetFields();
    // }
  },
  created() {
    let { id } = this.$route.query;
    if (id) {
      this.editPage = true;
      getPolicyReadInfo({ readId: id }).then(({ data }) => {
        this.ruleForm = data;
      });
    }
  }
};
</script>
