<template>
  <div id="form-1">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <el-form :model="form" :rules="rules" ref="form" label-width="100px">
            <el-form-item label="信息标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入信息标题"></el-input>
            </el-form-item>
            <el-form-item label="信息来源" prop="origin">
              <el-input v-model="form.origin" placeholder="请输入信息来源"></el-input>
            </el-form-item>
            <el-form-item label="发布时间" prop="publish_date">
              <el-date-picker v-model="form.publish_date" placeholder="请选择" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" style="width: 100%">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="信息简介" prop="brief">
              <el-input v-model="form.brief" placeholder="请输入信息简介" type="textarea" rows="2"></el-input>
            </el-form-item>
            <el-form-item label="信息内容" prop="content">
              <wang-editor v-model="form.content"></wang-editor>
            </el-form-item>
            <el-col :span="24" class="btn">
              <el-button type="danger" size="small" @click="close">取消保存</el-button>
              <el-button type="primary" size="small" @click="onSubmit('form')">提交保存</el-button>
            </el-col>
          </el-form>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import wangEditor from '@common/src/components/frame/wang-editor.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
export default {
  name: 'form-1',
  props: { form: { type: Object } },
  components: {
    wangEditor,
  },
  data: function () {
    return {
      rules: {
        title: [{ required: true, message: '请输入信息标题', trigger: 'blur' }],
        origin: [{ required: true, message: '请输入信息来源', trigger: 'blur' }],
        publish_date: [{ required: true, message: '请选择', trigger: 'change' }],
        brief: [{ required: true, message: '请输入信息简介', trigger: 'blur' }],
      },
    };
  },
  created() {},
  methods: {
    // 取消保存
    close() {
      this.$emit('close');
    },
    // 提交保存
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('onSubmit', { data: this.form });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    .btn {
      text-align: center;
      .el-button {
        margin: 0 10px;
      }
    }
  }
}
</style>
