<template>
  <div id="detail">
    <data-form :fields="fields" :data="data" @save="toSave" returns="/adminCenter/roadShow">
      <template #custom="{ item }">
        <template v-if="item.model === 'picture'">
          <e-upload url="/files/cysci/roadShow_image/upload" v-model="data[item.model]"></e-upload>
        </template>
        <template v-if="item.model === 'filepath'">
          <e-upload url="/files/cysci/roadShow_file/upload" v-model="data[item.model]" type="text"></e-upload>
        </template>
      </template>
    </data-form>
  </div>
</template>

<script>
const _ = require('lodash');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: roadShow } = createNamespacedHelpers('roadShow');
export default {
  name: 'roadShowDetail',
  props: {},
  components: {},
  data: function () {
    return {
      data: {
        picture: [],
        filepath: [],
      },
      fields: [
        { label: '标题', model: 'title' },
        { label: '简介', model: 'brief', type: 'textarea', options: { maxRows: 5, minRows: 3 } },
        { label: '来源', model: 'origin' },
        { label: '发布时间', model: 'publish_time', type: 'datetime' },
        { label: '内容', model: 'content' },
        { label: '图片', model: 'picture', custom: true },
        { label: '附件', model: 'filepath', custom: true },
      ],
    };
  },
  created() {
    if (this.id) this.search();
  },
  methods: {
    ...roadShow(['fetch', 'create', 'update']),
    async search() {
      const res = await this.fetch(this.id);
      if (this.$checkRes(res)) {
        this.$set(this, `data`, res.data);
      }
    },
    async toSave({ data }) {
      let dup = _.cloneDeep(data);
      let res;
      if (_.get(dup, 'id')) {
        res = await this.update(dup);
      } else {
        res = await this.create(dup);
      }
      if (this.$checkRes(res, '保存成功', '保存失败')) {
        if (!this.$dev_mode) this.$router.push('/adminCenter/news');
      }
    },
  },
  computed: {
    ...mapState(['user', 'menuParams']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
    id() {
      return this.$route.query.id;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

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