<template>
  <div id="detail">
    <data-form :fields="fields" :data="data" @save="toSave" labelWidth="150px" returns="/adminCenter/organization" submitText="审核">
      <template #radios="{ item }">
        <template v-if="item.model === 'status'">
          <el-radio v-for="(i, index) in statusList" :key="`status-${index}`" :label="i.value">{{ i.label }}</el-radio>
        </template>
      </template>
    </data-form>
  </div>
</template>

<script>
const { status } = require('@common/dict/index');
const _ = require('lodash');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: organization } = createNamespacedHelpers('organization');
export default {
  name: 'organizationDetail',
  props: {},
  components: {},
  data: function () {
    return {
      data: {},
      fields: [
        { label: '企业名称', model: 'name', type: 'text' },
        { label: '电话号码', model: 'phone', options: { maxLength: 11 }, type: 'text' },
        { label: '邮箱', model: 'email', type: 'text' },
        { label: '地址', model: 'addr', type: 'text' },
        { label: '办公电话', model: 'office_phone', type: 'text' },
        { label: '所属行业', model: 'profession', type: 'text' },
        { label: '机构代码', model: 'institution_code', type: 'text' },

        { label: '注册类型', model: 'companytype', type: 'text' },
        { label: '注册时间', model: 'companydate', type: 'text' },
        { label: '注册资金', model: 'companycapital', type: 'text' },
        { label: '企业法人', model: 'companyperson', type: 'text' },
        { label: '上年度企业总收入', model: 'sndqyzsr', type: 'text' },
        { label: '上年度研发费用', model: 'sndyffy', type: 'text' },
        { label: '企业总人数', model: 'companytotal', type: 'text' },
        { label: '专&兼职研发人数', model: 'zjzyfrs', type: 'text' },
        { label: '企业简介', model: 'companybrief', type: 'textarea', options: { maxRows: 5, minRows: 3 }, type: 'text' },
        { label: '主要产品', model: 'mainproduct', type: 'textarea', options: { maxRows: 5, minRows: 3 }, type: 'text' },
        { label: '企业资质&荣誉', model: 'qualifications', type: 'text' },
        { label: '账号状态', model: 'status', type: 'radio' },
      ],
      statusList: status,
    };
  },
  created() {
    if (this.id) this.search();
  },
  methods: {
    ...organization(['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/organization');
      }
    },
  },
  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>
