<template>
  <div class="main flex-1">
    <div style="width:100%" v-if="lists && lists.length > 0">
      <a-table
        :columns="columns"
        :data-source="lists"
        :loading="loading"
        :pagination="pagination"
        :row-key="record => record.id"
        :scroll="{ y: height }"
        @change="handleTableChange"
        bordered
        class="white"
      >
        <template slot="id" slot-scope="text, record, index">
          <span>{{ index + 1 }}</span>
        </template>

        <!-- logo地址 -->
        <template slot="logoUrl" slot-scope="text, record">
          <img :src="record.logoUrl" height="50" width="50" />
        </template>

        <!-- 合作关系 -->
        <template slot="type" slot-scope="text">
          <span>{{ text === 1 ? '合作伙伴' : text === 2 ? '衍生企业' : '' }}</span>
        </template>

        <!-- 展示位置 -->
        <template slot="typeOfPlatform" slot-scope="text">
          <span>{{ text === 1 ? '产业平台' : text === 2 ? '关于我们' : text === 3 ? '孵化平台' : '' }}</span>
        </template>

        <!-- 公司类型 -->
        <template slot="typeOfTech" slot-scope="text">
          <span>{{ text === 0 ? '高校' : text === 1 ? '院所' : text === 2 ? '企业' : '' }}</span>
        </template>

        <template slot="edit" slot-scope="text, record">
          <a-icon @click="showEditModal(record)" class="pointer" theme="twoTone" type="edit" />
          <a-popconfirm @confirm="() => onDelete(record.id)" title="确定要删除这一条?" v-if="lists.length">
            <a-icon class="ml-4 pointer" theme="twoTone" two-tone-color="#ff0000" type="delete" />
          </a-popconfirm>
        </template>

        <div slot="expandedRowRender" slot-scope="record" style="margin: 0">
          <div>
            <span class="font-bold-14">简介：</span>
            <span v-dompurify-html="record.description"></span>
          </div>
        </div>
      </a-table>
    </div>
    <a-empty v-else />

    <!-- 编辑 -->
    <partner-edit
      :editItem="editItem"
      :editVisible="editVisible"
      @closeModal="closeModal"
      @getBackendSearch="getBackendSearch"
    />
  </div>
</template>

<script>
import PartnerEdit from 'components/Partner/PartnerEdit.vue';
import { backendDelete } from 'config/api';

const columns = [
  {
    title: '序号',
    align: 'center',
    dataIndex: 'id',
    key: 'id',
    scopedSlots: { customRender: 'id' },
  },
  {
    title: 'logo',
    align: 'center',
    dataIndex: 'logoUrl',
    key: 'logoUrl',
    scopedSlots: { customRender: 'logoUrl' },
  },
  {
    title: '公司名',
    align: 'center',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '合作关系',
    align: 'center',
    dataIndex: 'type',
    key: 'type',
    scopedSlots: { customRender: 'type' },
  },
  {
    title: '展示位置',
    align: 'center',
    dataIndex: 'typeOfPlatform',
    key: 'typeOfPlatform',
    scopedSlots: { customRender: 'typeOfPlatform' },
  },
  {
    title: '公司类型',
    align: 'center',
    dataIndex: 'typeOfTech',
    key: 'typeOfTech',
    scopedSlots: { customRender: 'typeOfTech' },
  },
  {
    title: '编辑',
    align: 'center',
    dataIndex: 'edit',
    key: 'edit',
    width: 200,
    scopedSlots: { customRender: 'edit' },
  },
];

export default {
  name: 'PartnerDate',
  components: {
    PartnerEdit,
  },

  props: { lists: { type: Array, default: () => [] }, pagination: { type: Object, default: () => {} } },

  data() {
    return {
      columns,
      loading: false,
      height: '',
      editVisible: false,
      editItem: null, // 修改的那条
    };
  },

  mounted() {
    let th = 250;
    let wh = window.innerHeight;
    this.height = wh - th;
    window.onresize = () => {
      return (() => {
        wh = window.innerHeight;
        this.height = wh - th;
      })();
    };
  },

  methods: {
    showEditModal(record) {
      this.editItem = record;
      this.editVisible = true;
    },

    closeModal() {
      this.editVisible = false;
    },

    async getBackendSearch() {
      await this.$emit('getBackendSearch');
    },

    handleTableChange(pagination) {
      const { current, pageSize } = pagination;
      const condition = { current, pageSize };
      this.$emit('getBackendSearch', condition);
    },

    // 删除
    async onDelete(id) {
      try {
        const params = { param: { id } };
        const res = await backendDelete(params);
        const { data, msg, code } = res.data;
        if (code === 200) {
          this.$message.success('删除成功');
          this.$emit('getBackendSearch');
        } else {
          throw msg;
        }
      } catch (error) {
        this.$message.error(error || '删除失败');
      }
    },

    // 打开报名
    openSignUp() {
      const { query } = this.$route;
      this.$router.push({ path: '/sign-up', query });
    },
  },
};
</script>

<style lang="stylus" scoped>
.main .img {
  height: 65px;
}

.main .big_img {
  width: 200px;
}
</style>
