<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="coverPicture" slot-scope="text, record">
          <img :src="record.coverPicture" width="50" height="50" />
        </template>

        <template slot="type" slot-scope="text, record">
          <span v-if="record.type === 1">视频</span>
          <span v-else-if="record.type === 2">音频</span>
          <span v-else-if="record.type === 3">ppt</span>
        </template>
        <template slot="whetherShow" slot-scope="text, record">
          <span v-if="record.whetherShow === 0">否</span>
          <span v-else-if="record.whetherShow === 1">是</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.academyId)" 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 />

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

<script>
// import ChallengeEdit from 'components/Challenge/ChallengeEdit.vue';
import { academyDelete } from 'config/api';
const columns = [
  {
    title: '序号',
    align: 'center',
    dataIndex: 'id',
    key: 'id',
    width: '7%',
    scopedSlots: { customRender: 'id' },
  },
  {
    title: '标题',
    align: 'center',
    dataIndex: 'title',
    key: 'title',
  },
  {
    title: '内容链接',
    align: 'center',
    dataIndex: 'contentUrl',
    key: 'contentUrl',
  },
  {
    title: '封面图片',
    align: 'center',
    dataIndex: 'coverPicture',
    key: 'coverPicture',
    scopedSlots: { customRender: 'coverPicture' },
  },
  {
    title: '简介',
    align: 'center',
    dataIndex: 'intro',
    key: 'intro',
    // scopedSlots: { customRender: 'picUrl' },
  },
  {
    title: '类型',
    align: 'center',
    dataIndex: 'type',
    key: 'type',
    scopedSlots: { customRender: 'type' },
  },
  {
    title: '时间',
    align: 'center',
    dataIndex: 'times',
    key: 'times',
    scopedSlots: { customRender: 'times' },
  },
  {
    title: '是否在首页展示',
    align: 'center',
    dataIndex: 'whetherShow',
    key: 'whetherShow',
    scopedSlots: { customRender: 'whetherShow' },
  },
  {
    title: '编辑',
    align: 'center',
    dataIndex: 'edit',
    key: 'edit',
    scopedSlots: { customRender: 'edit' },
  },
];

export default {
  name: 'CollegeDate',
  // components: {
  //   ChallengeEdit,
  // },
  // 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('getListData', condition);
    },
    getData() {
      this.$emit('getListData');
    },
    // 删除
    async onDelete(academyId) {
      try {
        const params = { param: { academyId } };
        const res = await academyDelete(params);
        const { data, msg, code } = res.data;
        if (code === 200) {
          this.$emit('getListData');
          this.$message.success('删除成功');
          // TODO: 填到列表中
        } else {
          throw msg;
        }
      } catch (error) {
        this.$message.error(error || '删除失败');
      }
    },
    // 获取服务内容
    getselContent(id) {
      try {
        const parmas = {
          param: {
            id: +id,
            serviceType: 2,
          },
        };
        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>
