<template>
  <div id="detail">
    <data-form :fields="fields" :data="data" @save="toSave" returns="/adminCenter/mechanism" 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/mechanism_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: mechanism } = createNamespacedHelpers('mechanism');
export default {
  name: 'mechanismDetail',
  props: {},
  components: {},
  data: function () {
    return {
      data: {
        img_path: [],
      },
      fields: [
        { label: '机构名称', model: 'name', type: 'text' },
        { label: '联系人', model: 'contacts', type: 'text' },
        { label: '联系电话', model: 'phone', type: 'text' },
        { label: '电子邮箱', model: 'email', type: 'text' },
        { label: '联系地址', model: 'address', type: 'text' },
        { label: '所属行业', model: 'industry', type: 'text' },
        { label: '账号状态', model: 'status', type: 'radio' },
      ],
      statusList: status,
    };
  },
  created() {
    if (this.id) this.search();
  },
  methods: {
    ...mechanism(['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/mechanism');
      }
    },
  },
  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>
