<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="level">
        <el-select v-model="ruleForm.level" placeholder="请选择">
          <el-option
            v-for="item in levelOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </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">
        <div id="text">
          <editor v-model="ruleForm.text" :min-height="192" />
        </div>
      </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 { addPolicy, updatePolicy, getPolicyInfo } from '@/api/policy/library';
export default {
  components: {
    Editor
  },
  data() {
    return {
      editPage: false,
      ruleForm: {
        title: '',
        level: '',
        attribute: '',
        listDate: '',
        source: '',
        text: ''
      },
      // 归口选项
      attributeOptions: [
        {
          value: 'KJJ',
          label: '科技'
        },
        {
          value: 'JXJ',
          label: '经信'
        },
        {
          value: 'FGW',
          label: '发改'
        },
        {
          value: 'qita',
          label: '其他'
        }
      ],
      // 级别选项
      levelOptions: [
        {
          value: 1,
          label: '省级'
        },
        {
          value: 2,
          label: '市级'
        },
        {
          value: 3,
          label: '区级'
        }
      ],
      rules: {
        title: [{ required: true, message: '请输入政策标题', trigger: 'blur' }],
        level: [{ required: true, message: '请选择级别', trigger: 'change' }],
        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) {
            // 修改
            updatePolicy(this.ruleForm).then(({ message }) => {
              this.msgSuccess(message);
            });
          } else {
            // 添加
            addPolicy(this.ruleForm).then(({ message }) => {
              this.msgSuccess(message);
            });
          }
          this.$router.go(-1);
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
    // resetForm(formName) {
    //   this.$refs[formName].resetFields();
    // }
  },
  watch: {
    ruleForm: {
      handler: function() {
        this.$nextTick(() => {
          this.formatRichText(this.ruleForm.downloadUrl);
        });
      }
    },
    deep: true
  },
  created() {
    let { id } = this.$route.query;
    if (id) {
      this.editPage = true;
      getPolicyInfo({ policyId: id }).then(({ data }) => {
        this.ruleForm = data;
      });
    }
  }
};
</script>
