<template>
  <div id="detail">
    <data-form :fields="fields" :data="data" @save="toSave" labelWidth="150px" returns="/adminCenter/policy" 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 === 'desc'">
          <p v-html="data[item.model]"></p>
        </template> -->
        <template>
          <template v-for="(i, index) in data[item.model]">
            <img v-if="isImg(i.url)" :key="`${item.model}-${index}`" :src="i.url" width="150px" height="150px" />
            <el-link v-else :key="`${item.model}-${index}`" type="primary" @click="toOpen(i.url)"> <i class="el-icon-view"></i> {{ i.name }} </el-link>
          </template>
        </template>
      </template>
    </data-form>
  </div>
</template>

<script>
const _ = require('lodash');
const { policyType, policyStatus } = require('@common/dict/index');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: policy } = createNamespacedHelpers('policy');
export default {
  name: 'policyDetail',
  props: {},
  components: {},
  data: function () {
    return {
      data: {},
      fields: [
        { label: '申请单位', model: 'company', type: 'text' },
        { label: '申请类型', model: 'type', type: 'text' },
        { label: '申请人', model: 'apply_personal', type: 'text' },
        { label: '法人复印件', model: 'qyfr', custom: true },
        { label: '企业营业执照', model: 'yyzz', custom: true },
        { label: '企业利润表', model: 'qylr', custom: true },
        { label: '建议', model: 'desc', type: 'textarea' },
        { label: '审核状态', model: 'status', type: 'radio' },
      ],
      statusList: policyStatus,
      imgList: ['jpg', 'jpeg', 'png', 'bmp', 'gif'],
    };
  },
  created() {
    if (this.id) this.search();
  },
  methods: {
    ...policy(['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/policy');
      }
    },
    isImg(url) {
      const arr = url.split('.');
      const suffix = _.last(arr);
      return this.imgList.includes(suffix);
    },
    toOpen(url) {
      window.open(url);
    },
  },
  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>
