<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="attribute">
        <el-select v-model="ruleForm.attribute" placeholder="请选择">
          <el-option
            v-for="item in attributeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发布日期" required>
        <el-form-item prop="listDate">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            v-model="ruleForm.listDate"
            style="width: 30%;"
          ></el-date-picker>
        </el-form-item>
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <el-input placeholder="请输入" v-model="ruleForm.source"></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: '',
        attribute: '',
        listDate: '',
        source: '',
        text: ''
      },
      // 归口选项
      attributeOptions: [
        {
          value: 'KJJ',
          label: '科技'
        },
        {
          value: 'JXJ',
          label: '经信'
        },
        {
          value: 'FGW',
          label: '发改'
        },
        {
          value: 'OTHER',
          label: '其他'
        }
      ],
      rules: {
        title: [{ required: true, message: '请输入解读标题', trigger: 'blur' }],
        attribute: [
          { required: true, message: '请选择归口', trigger: 'change' }
        ],
        listDate: [
          {
            // type: 'date',
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }
        ],
        source: [{ 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>
