<template>
  <div id="detail">
    <data-form :fields="fields" :data="data" @save="toSave" returns="/adminCenter/product" @filterReturn="toSelect">
      <template #options="{ item }">
        <template v-if="item.model === 'type'">
          <el-option v-for="(i, index) in typeList" :key="`type-${index}`" :label="i.label" :value="i.value"></el-option>
        </template>
      </template>
      <template #custom="{ item }">
        <template v-if="item.model === 'image'">
          <e-upload url="/files/cysci/product_image/upload" v-model="data[item.model]"></e-upload>
        </template>
        <template v-if="item.model === 'roadshow'">
          <e-upload url="/files/cysci/product_roadshow/upload" v-model="data[item.model]" type="text"></e-upload>
        </template>
        <template v-if="item.model === 'patent'">
          <el-row type="flex" justify="end" style="padding-bottom: 10px">
            <el-col :span="2">
              <el-button type="primary" @click="data[item.model].push({})">添加</el-button>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8" v-for="(i, index) in data[item.model]" :key="`patent-${index}`">
              <el-form-item label="专利信息">
                <el-input v-model="i.patentinfo" placeholder="请填写专利信息"></el-input>
              </el-form-item>
              <el-form-item label="专利状态">
                <el-input v-model="i.patentstatus" placeholder="请填写专利状态"></el-input>
              </el-form-item>
              <el-form-item>
                <el-row type="flex" justify="space-around">
                  <el-col :span="2">
                    <el-button type="danger" @click="data[item.model].splice(index, 1)">删除</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>
        </template>
      </template>
    </data-form>
  </div>
</template>

<script>
const _ = require('lodash');
const { productType } = require('@common/dict/index');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: product } = createNamespacedHelpers('product');
export default {
  name: 'productDetail',
  props: {},
  components: {},
  data: function () {
    return {
      typeList: productType,
      data: {
        image: [],
        roadshow: [],
        patent: [],
      },
      fields: [],
      fieldsPublic: [
        { label: '名称', model: 'name' },
        { label: '联系人', model: 'contacts' },
        { label: '联系电话', model: 'phone' },
        { label: 'qq&微信', model: 'qqwx' },
        { label: '邮箱', model: 'email' },
        { label: '类型', model: 'type', type: 'select', filterReturn: true },
        // 共同
        { label: '所属领域', model: 'field' },
        { label: '合作方式', model: 'cooperation' },
        { label: '企业名称', model: 'company' },
        { label: '合作条件及要求', model: 'condition', type: 'textArea', options: { maxRows: 5, minRows: 3 } },
        { label: '图片', model: 'image', custom: true },
        { label: '预期', model: 'expect' },
        { label: '需求程度', model: 'demand' },
      ],

      // 科技需求
      fields0: [
        { label: '投资预算', model: 'budget' },
        { label: '需求说明', model: 'requirementdesc', options: { maxRows: 5, minRows: 3 } },
        { label: '需求现状', model: 'present' },
      ],
      // 技术成果
      fields1: [
        { label: '成果状态', model: 'achievestatus' },
        { label: '成果权属', model: 'achieveown' },
        { label: '成果来源', model: 'achievesource' },
        { label: '意向价格', model: 'intentionprice' },
        { label: '专利', model: 'patent', custom: true },
        { label: '项目路演', model: 'roadshow', custom: true },
        { label: '成果简介', model: 'achievebrief', options: { maxRows: 5, minRows: 3 } },
        { label: '技术特点', model: 'features', options: { maxRows: 5, minRows: 3 } },
        { label: '技术团队', model: 'team', options: { maxRows: 5, minRows: 3 } },
      ],
      // 商务需求
      fields2: [
        { label: '信息属性', model: 'messattribute' },
        { label: '信息描述', model: 'informationdesc' },
        { label: '核心要素', model: 'coreelements' },
        { label: '价格信息', model: 'priceinfo' },
      ],
    };
  },
  created() {
    this.$set(this, 'fields', this.fieldsPublic);
    if (this.id) this.search();
  },
  methods: {
    ...product(['fetch', 'create', 'update']),
    toSelect({ data, model }) {
      if (model === 'type') {
        const fields = _.get(this, `fields${data}`);
        if (fields) {
          const dup = _.cloneDeep(this.fieldsPublic);
          const last = dup.concat(fields);
          this.$set(this, 'fields', last);
        }
      }
    },
    async search() {
      const res = await this.fetch(this.id);
      if (this.$checkRes(res)) {
        this.$set(this, `data`, res.data);
        const { type } = res.data;
        if (type) this.toSelect({ data: type, model: 'type' });
      }
    },
    async toSave({ data }) {
      let dup = _.cloneDeep(data);
      // 需要根据type,过滤出对应的数据
      dup = this.filterData(dup);
      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/product');
      }
    },
    filterData(data) {
      const { type, id } = data;
      const publics = _.cloneDeep(this.fieldsPublic);
      const fields = _.get(this, `fields${type}`, []);
      const keys = publics.map((i) => i.model).concat(fields.map((i) => i.model));
      const obj = {};
      for (const key of keys) {
        if (data[key]) obj[key] = data[key];
      }
      if (id) obj.id = id;
      return obj;
    },
  },
  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>
