<template>
  <div id="detail">
    <data-form :fields="fields" :data="data" @save="toSave" returns="/adminCenter/patent">
      <template #options="{ item }">
        <template v-if="item.model === 'column_name'">
          <el-option v-for="(i, index) in typeList" :key="`columns-name-${index}`" :label="i" :value="i"></el-option>
        </template>
      </template>
      <template #custom="{ item }">
        <template v-if="item.model === 'img_url'">
          <e-upload url="/files/cysci/patent_image/upload" v-model="data[item.model]"></e-upload>
        </template>
      </template>
    </data-form>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: patent } = createNamespacedHelpers('patent');
export default {
  name: 'patentDetail',
  props: {},
  components: {},
  data: function () {
    return {
      data: {
        img_url: [],
      },
      fields: [
        { label: '申请号', model: 'create_number' },
        { label: '申请日', model: 'create_date', type: 'date' },
        { label: '公开（公告）号', model: 'success_number' },
        { label: '公开（公告）日', model: 'success_date', type: 'date' },
        { label: '标题', model: 'name' },
        { label: '发明人', model: 'inventor' },
        { label: '发明人地址', model: 'address' },
        { label: '申请人', model: 'apply_personal' },
        { label: '专利有效性', model: 'term' },
        { label: '专利类型', model: 'type' },
        { label: '代理人', model: 'agent_personal' },
        { label: '代理机构', model: 'agent' },
        { label: '摘要', model: 'abstract', type: 'textarea', options: { maxRows: 5, minRows: 3 } },
        { label: '图片', model: 'img_url', custom: true },
        { label: '数据来源', model: 'origin' },
      ],
    };
  },
  created() {
    if (this.id) this.search();
  },
  methods: {
    ...patent(['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/patent');
      }
    },
  },
  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>
