<template>
  <el-dialog
    :title="title"
    :visible.sync="modal"
    width="80%"
    :close-on-click-modal="false"
    @close="
      res => {
        $emit('cancel');
      }
    "
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="brief">
        <el-input v-model="form.brief" type="textarea" :rows="2"></el-input>
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input v-model="form.author" :disabled="id !== ''"></el-input>
      </el-form-item>
      <el-form-item label="封面" prop="cover">
        <upload v-model="form.cover" params="prefix=/tweet-cover" />
        <el-row>建议尺寸：900 * 383</el-row>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <RichText v-model="form.content" />
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="form.isShow" prop="isShow">
          <el-radio :label="true">显示</el-radio>
          <el-radio :label="false">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number
          controls-position="right"
          v-model="form.sort"
          :precision="0"
          :min="1"
          :max="9999"
          label="序号"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="modal = false">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getItem, saveItem } from '@/api/base/info';

export default {
  name: 'InfoItemModal',

  props: {
    id: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      modal: true,

      form: {
        author: '',
        title: '',
        brief: '',
        cover: [],
        content: '',
        isShow: false,
        sort: 1
      },
      rules: {
        author: [{ required: true, message: '请输入作者', trigger: 'change' }],
        title: [{ required: true, message: '请输入标题', trigger: 'change' }],
        brief: [{ required: true, message: '请输入简介', trigger: 'change' }],
        content: [{ required: true, message: '请输入内容', trigger: 'change' }],
        cover: [
          {
            type: 'array',
            required: true,
            message: '请上传图片',
            trigger: 'change'
          }
        ]
      },

      typeOptions: []
    };
  },

  computed: {
    title() {
      if (this.id) {
        return '编辑资讯';
      } else {
        return '新增资讯';
      }
    }
  },

  watch: {
    id: {
      handler(id) {
        id && this.loadData();
      },
      immediate: true
    }
  },

  mounted() {},

  methods: {
    async loadData() {
      const { success, data, msg } = await getItem({
        id: this.id
      });
      if (success) {
        let result = data;
        result.cover = [
          {
            url: data.cover
          }
        ];
        this.form = result;
      }
    },

    handleConfirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let params = Object.assign({}, this.form);
          params.cover = this.form.cover[0].url;
          params.content = this.$filterRichTxt(params.content);
          if (this.id) params.id = this.id;
          const { success, msg } = await saveItem(params);
          if (success) {
            this.$success('保存成功！');
            this.modal = false;
            this.$g_emit('base_info_reload');
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped></style>
