<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="请输入标题"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="新闻类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择" style="width: 100%">
              <el-option v-for="item in typeList" :key="item.value + 'typeList'" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="新闻来源">
            <el-input v-model="form.source" placeholder="请输入来源"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="新闻封面">
        <cropper v-model="form.picture" :fixed_number="[4, 2]"></cropper>
      </el-form-item>
      <el-form-item label="新闻内容" prop="content">
        <editor v-model="form.content" />
      </el-form-item>
    </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>
import { listNEWS, getNEWS, delNEWS, addNEWS, updateNEWS, upload, setTop, newCommit, updateAndex, downloadpic } from '@/api/portal/news/NEWS.js';
export default {
  data() {
    return {
      typeList: [
        { label: '要闻咨询', value: '1' },
        { label: '政策法规', value: '2' },
        { label: '通知公告', value: '3' },
        { label: '政策解读', value: '4' },
        { label: '投资导航', value: '5' }
      ],
      form: {},
      rules: {
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        content: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
        fileUrl: [{ required: true, message: '附件不能为空', trigger: 'blur' }],
        type: [{ required: true, message: '类型不能为空', trigger: 'change' }],
        typeName: [{ required: true, message: '字典类型名称不能为空', trigger: 'blur' }],
        picture: [{ required: true, message: '图片不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '状态(0.草稿，1.下架，2.审核中，3.审核驳回，4.已发布，5.已上报)不能为空', trigger: 'blur' }]
      }
    };
  },
  props: {
    param: {
      type: Object,
      default: () => {
        return {};
      }
    },
    layerid: {
      type: String
    }
  },
  mounted() {
    if (this.param.id) {
      getNEWS(this.param.id).then(response => {
        this.form = response.data;
        this.form.content = response.data.content.replace(new RegExp('/profile/upload/', 'g'), this.baseUrl + '/profile/upload/');
      });
    }
  },
  methods: {
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAndex(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          } else {
            this.form.status = '1';
            addNEWS(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          }
        }
      });
    }
  }
};
</script>
