<template>
  <div id="detail">
    <data-form :fields="fields" :data="data" @save="toSave" returns="/adminCenter/expert" 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>
      <template #custom="{ item }">
        <template v-if="item.model === 'img_path'">
          <!-- <e-upload url="/files/cysci/expert_image/upload" :limit="1" v-model="data[item.model]"></e-upload> -->
          <img v-for="(i, index) in data[item.model]" :key="`icon-${index}`" :src="i.url" width="150px" height="150px" />
        </template>
      </template>
    </data-form>
  </div>
</template>

<script>
const { status } = require('@common/dict/index');
const _ = require('lodash');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: expert } = createNamespacedHelpers('expert');
export default {
  name: 'expertDetail',
  props: {},
  components: {},
  data: function () {
    return {
      data: {
        img_path: [],
      },
      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: 'img_path', custom: true },
        { label: 'qq&微信', model: 'qqwx', type: 'text' },
        { label: '出生日期', model: 'birthDate', type: 'date', type: 'text' },
        { label: '毕业院校', model: 'school', type: 'text' },
        { label: '最高学历', model: 'education', type: 'text' },
        { label: '职务职称', model: 'zwzc', type: 'text' },
        { label: '擅长领域', model: 'expertise', type: 'text' },
        { label: '工作单位', model: 'company', type: 'text' },
        { label: '工作经历', model: 'workexperience', type: 'textarea', options: { maxRows: 5, minRows: 3 }, type: 'text' },
        { label: '科研综述', model: 'scientific', type: 'textarea', options: { maxRows: 5, minRows: 3 }, type: 'text' },
        { label: '承担项目', model: 'undertakingproject', type: 'text' },
        { label: '科技奖励', model: 'scienceaward', type: 'text' },
        { label: '社会任职', model: 'social', type: 'text' },
        { label: '账号状态', model: 'status', type: 'radio' },
      ],
      statusList: status,
    };
  },
  created() {
    if (this.id) this.search();
  },
  methods: {
    ...expert(['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/expert');
      }
    },
  },
  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>
