<template>
  <div id="edit">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="top">
          <van-nav-bar :title="pageTitle" left-text="取消" @click-left="toCancel" :right-text="id ? '修改' : '发表'" @click-right="toSubmit" />
        </el-col>
        <el-col :span="24">
          <van-form ref="form" style="margin-top:30px">
            <van-field v-model="form.title" name="title" label="标题" placeholder="请输入标题" :rules="[{ required: true, message: '请填写标题' }]" />
            <van-field v-model="form.website" name="website" label="网址" placeholder="请输入标题" />
            <van-field v-model="form.content" rows="2" autosize type="textarea" placeholder="请输入本文内容" />
            <van-field name="uploader" label="封面上传">
              <template #input>
                <van-uploader :fileList="form.imgUrl" :max-count="1" :after-read="file => toUpload(file, 'imgUrl')" />
              </template>
            </van-field>
            <van-field name="uploader" label="文章视频">
              <template #input>
                <van-uploader :fileList="form.fileUrl" :max-count="1" :after-read="file => toUpload(file, 'fileUrl')" accept=".mp4,.flv,.avi,.rmvb,.wmv" />
              </template>
            </van-field>
          </van-form>
        </el-col>
        <el-col :span="24" class="foot">
          <foot></foot>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
import foot from '@/layout/common/foot.vue';
const moment = require('moment');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: upload } = createNamespacedHelpers('upload');
const { mapActions: refute } = createNamespacedHelpers('refute');
export default {
  name: 'edit',
  props: {},
  components: { foot },
  data: function() {
    return {
      form: {},
    };
  },
  created() {
    if (this.id) this.search();
  },
  methods: {
    ...upload(['upload']),
    ...refute(['create', 'fetch', 'update']),
    async search() {
      const res = await this.fetch(this.id);
      if (this.$checkRes(res, null, res.errmsg || '未找到指定数据')) {
        let dup = _.cloneDeep(res.data);
        if (_.get(dup, 'imgUrl')) dup.imgUrl = [{ url: dup.imgUrl }];
        if (_.get(dup, 'fileUrl')) dup.fileUrl = [{ url: dup.fileUrl }];
        this.$set(this, `form`, dup);
      }
    },
    async toSubmit() {
      let dup = _.cloneDeep(this.form);
      let res;
      // 需要处理imgUrl和fileUrl
      if (_.get(dup, 'imgUrl')) dup.imgUrl = _.get(dup.imgUrl, '0.url');
      if (_.get(dup, 'fileUrl')) dup.fileUrl = _.get(dup.fileUrl, '0.url');
      if (this.id) {
        dup.renew_time = moment().format('YYYY-MM-DD HH:mm:ss');
        res = await this.update(dup);
      } else {
        // user_id,origin需要加进数据中
        dup.user_id = this.user._id;
        dup.origin = this.user.name;
        res = await this.create(dup);
      }
      if (this.$checkRes(res, '保存成功', res.errmsg || '保存失败')) {
        this.toCancel();
      }
    },
    async toUpload({ file }, model) {
      // 上传,赋值
      const res = await this.upload({ file, dir: refute });
      if (this.$checkRes(res)) {
        this.$set(this.form, model, [{ url: res.uri }]);
      }
    },
    toCancel() {
      this.$router.push('/adminRefute');
    },
  },
  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>
.main {
  .top {
    height: 40px;
    overflow: hidden;
    border-bottom: 1px solid #f1f1f1;
  }
  .info {
    overflow-x: hidden;
    overflow-y: auto;
  }
  .foot {
    height: 50px;
    overflow: hidden;
    border-top: 1px solid #f1f1f1;
  }
}
</style>
