<template>
  <a-card :bordered="false" title="资源库">
    <div class="table-page-search-wrapper">
      <SearchCom :form="queryParam" :list="queryOptions" @search="handleRefresh" @reset="
          () => {
            ;(queryParam = {}), handleRefresh()
          }
        "></SearchCom>
      <div style="width: 100%; height: 32px; margin-bottom: 8px">
        <a-button type="primary" @click="coursewareAdd">上传资料</a-button>
      </div>
    </div>
    <s-table ref="table" size="default" rowKey="id" :columns="columns" :data="loadData" :pageNum="Number(this.$route.query.PageNum) || 1">
      <template slot="action" slot-scope="text, record">
        <a href="javascript:;" @click="dow(record)">下载</a>
        <a-divider type="vertical" />
        <a href="javascript:;" @click="edit(record)">编辑</a>
        <a-divider type="vertical" />
        <a-popconfirm title="是否删除?" @confirm="() => del(record)">
          <a href="javascript:;">删除</a>
        </a-popconfirm>
      </template>
    </s-table>
  </a-card>
</template>


<script>
import { STable, SearchCom } from '@/components'
import { resource, deleteCourseware } from '@/api/course/courseware'
import { dictionaryDropDown } from '@/api/sys/dictionaryItem'
import { dictToTree } from '@/utils/util'

export default {
  components: {
    STable,
    SearchCom,
  },
  data() {
    return {
      queryParam: { classify: '' },
      queryOptions: [
        { type: 'select-dic-tree', placeholder: '课件类别', key: 'classify', options: [] },
      ],
      loadData: (parameter) => {
        return resource(Object.assign(parameter, this.queryParam)).then((res) => {
          return res
        })
      },
      columns: [
        { title: '资料名称', width: '160px', align: 'center', dataIndex: 'name', key: 'name' },
        { title: '资料分类', width: 'auto', align: 'center', dataIndex: 'classifyName', key: 'classifyName' },
        { title: '资料大小', width: 'auto', align: 'center', dataIndex: 'sizeStr', key: 'sizeStr' },
        { title: '资料类型', width: '160px', align: 'center', dataIndex: 'typeName', key: 'typeName' },
        { title: '操作', key: 'operation', width: '300px', align: 'center', scopedSlots: { customRender: 'action' } },
      ],
    }
  },
  created() {
    this.dictionaryDropDown()
  },
  methods: {
    // 获取数据词典
    dictionaryDropDown() {
      //课件类别
      dictionaryDropDown({ dictionaryCode: '0009' }).then((res) => {
        console.log('课件类别----', res)
        this.queryOptions[0].options = dictToTree(res.data, [], 0)
      })
    },
    handleRefresh() {
      this.$refs.table.refresh(true)
    },

    //课件上传
    coursewareAdd() {
      this.$router.push({ path: '/course/CoursewareAddOrUpdate', query: {ifResources: 1} })
    },

    //编辑
    edit(record) {
      this.$router.push({
        path: '/course/CoursewareAddOrUpdate',
        query: { coursewareId: record.id, type: record.type ,ifResources: 1},
      })
    },

    //课件下载
    dow() {},

    // 刪除課件
    del(record) {
      deleteCourseware({ id: record.id }).then((res) => {
        if (res.code == 200) {
          this.$refs.table.refresh(true)
          this.$message.success('删除成功！')
        } else {
          this.$message.error('删除失败！')
        }
      })
    },
  },
}
</script>