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

        <template slot="picUrl" slot-scope="text, record">
          <img :src="record.picUrl" width="50" height="50" />
        </template>

        <template slot="serviceType" slot-scope="text, record">
          <span v-if="record.serviceType === 1">创新平台</span>
          <span v-if="record.serviceType === 2">孵化平台</span>
          <span v-if="record.serviceType === 3">产业平台</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.list.length">
            <a-icon class="ml-4 pointer" theme="twoTone" two-tone-color="#ff0000" type="delete" />
          </a-popconfirm>
        </template>
        <div class="d-flex flex-nowrap justify-space-between" slot="expandedRowRender" slot-scope="record" style="margin: 0">
          {{ getselContent(record.id) }}
        </div>
      </a-table>
    </div>
    <a-empty v-else />

    <!-- 编辑 -->
    <r-d-member-edit :edit-visible="editVisible" :edit-data="editData" @getData="getData" :contents="contents" @closeModal="closeModal" />
  </div>
</template>

<script>
import RDMemberEdit from 'components/innovativeService/innovativeServiceEdit.vue';
import { deleteService, selContent } from 'config/api';
const columns = [
  {
    title: '序号',
    align: 'center',
    dataIndex: 'id',
    key: 'id',
    width: '7%',
    scopedSlots: { customRender: 'id' },
  },
  {
    title: '服务名称',
    align: 'center',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '服务简介',
    align: 'center',
    dataIndex: 'intro',
    key: 'intro',
  },
  {
    title: '服务图片',
    align: 'center',
    dataIndex: 'picUrl',
    key: 'picUrl',
    scopedSlots: { customRender: 'picUrl' },
  },
  {
    title: '服务类型',
    align: 'center',
    dataIndex: 'serviceType',
    key: 'serviceType',
    scopedSlots: { customRender: 'serviceType' },
  },
  {
    title: '服务排序',
    align: 'center',
    dataIndex: 'orders',
    key: 'orders',
    scopedSlots: { customRender: 'orders' },
  },
  {
    title: '编辑',
    align: 'center',
    dataIndex: 'edit',
    key: 'edit',
    scopedSlots: { customRender: 'edit' },
  },
];

export default {
  name: 'innovativeServiceDate',
  components: {
    RDMemberEdit,
  },
  // props: { lists: { type: Array, default: () => {} } },
  props: { lists: { type: Object, default: () => {} }, pagination: { type: Object, default: () => {} } },

  data() {
    return {
      columns,
      loading: false,
      editingKey: '',
      height: '',
      editVisible: false,
      editData: null,
      contents: '',
    };
  },

  mounted() {
    let th = 250;
    let wh = window.innerHeight;
    this.height = wh - th;
    window.onresize = () => {
      return (() => {
        wh = window.innerHeight;
        this.height = wh - th;
      })();
    };
  },
  methods: {
    showEditModal(data) {
      this.editData = data;
      this.contents = this.getselContent(data.id);
      this.editVisible = true;
    },

    closeModal() {
      this.editVisible = false;
    },
    handleTableChange(pagination) {
      const { current, pageSize } = pagination;
      const condition = { current, pageSize };
      this.$emit('getInnovativeServiceSearch', condition);
    },
    getData() {
      this.$emit('getInnovativeServiceSearch');
    },
    // 删除
    async onDelete(id) {
      try {
        const params = { param: { id } };
        const res = await deleteService(params);
        const { data, msg, code } = res.data;
        if (code === 200) {
          this.$emit('getInnovativeServiceSearch');
          this.$message.success('删除成功');
          // TODO: 填到列表中
        } else {
          throw msg;
        }
      } catch (error) {
        this.$message.error(error || '删除失败');
      }
    },
    // 获取服务内容
    getselContent(id) {
      try {
        const parmas = {
          param: {
            id: +id,
            serviceType: 1,
          },
        };
        const res = selContent(params);
        const { code, msg, data } = res.data;
        if (code === 200) {
          // console.log(data);
          return data.content;
        } else {
          return '暂无内容';
        }
      } catch (error) {
        return '暂无内容';
      }
    },
  },
};
</script>

<style scoped lang="stylus"></style>
