<template>
    <div>
        <div class="top-box flex-align">
            <div>
                <a-icon type="arrow-left" class="blue-hover m-r8" style="font-size: 18px;" @click="changeTabs" />
                <a-button type="primary" style="margin-right: 24px;" @click="showModal('add')">创建章节</a-button>
                <!-- <a-button type="primary" style="margin-right: 24px;">导入章节</a-button>
                <a-button type="primary">删除</a-button> -->
            </div>
            <a-input-search style="width:400px;" placeholder="搜索章节" enter-button @search="onSearch" />
        </div>
        <a-table @expand="myExpand" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
            :columns="columns" :loading="loading" :dataSource="data" size="small" :rowKey="record => record.id"
            :pagination="pagination" @change="handleChange">
            <template slot="action" slot-scope="text,record">
                <a @click="showModal('edit',record)">修改</a>
                <a-divider type="vertical" />
                <a @click="_ChapterDelete(record)">删除</a>
                <a-divider type="vertical" />
                <a @click="showModal('addChild',record)">添加子章节</a>
            </template>
        </a-table>
        <EditChapter ref="EditChapter" />
    </div>
</template>
<script>
    const columns = [
        {
            title: "章节名称",
            dataIndex: "name",
            key: "name",
            scopedSlots: { customRender: "name" },
            ellipsis: true,
            width: 180
        },
        {
            title: "所属教材版本",
            dataIndex: "textbook",
            key: "textbook",
            scopedSlots: { customRender: "textbook" },
            ellipsis: true,
            width: 100
        },
        {
            title: "所属学科",
            dataIndex: "discipline",
            key: "discipline",
            scopedSlots: { customRender: "discipline" },
            ellipsis: true,
            width: 100
        },
        {
            title: "操作",
            key: "action",
            dataIndex: "action",
            scopedSlots: { customRender: "action" },
            width: 100,
            align: "center"
        },
    ]
    import { myPagination } from '@/mixins/myPagination.js'
    import EditChapter from './EditChapter.vue'
    import { ChapterGetList, ChapterDelete } from '@/api/api'
    import { searchOption } from '@/utils/tools'
    export default {
        mixins: [myPagination],
        components: {
            EditChapter
        },
        props: {
            bookObj: {
                type: Object
            }
        },
        data() {
            return {
                columns,
                loading: false,
                data: [],
                selectedRowKeys: [],
                KeyWord: ''
            }
        },
        watch: {
        },
        mounted() {
            console.log(this.bookObj, 'bookObj')
            this._ChapterGetList();
        },
        methods: {
            //删除章节
            _ChapterDelete(record) {
                let _this = this;
                this.$confirm({
                    okText: '确认',
                    cancelText: '取消',
                    mask: false,
                    title: `您确认要删除"${record.name}"章节吗?`,
                    onOk() {
                        _this.loading = true;
                        let data = {
                            id: record.id
                        }
                        ChapterDelete(data).then(res => {
                            searchOption(record, _this.data);
                            _this.$message.success('删除成功！');
                            _this.loading = false;
                        }).catch(err => {
                            _this.loading = false;
                        })
                    }
                });
            },
            //跳转教材版本管理
            changeTabs() {
                this.$parent.currentTabComponent = 'TextbookManage'
            },
            //列表
            _ChapterGetList() {
                this.loading = true;
                let data = {
                    ParentId: '',//父级知识点id
                    TextbookId: this.bookObj.id,//教材id
                    KeyWord: this.KeyWord,
                    SkipCount: (this.pagination.current - 1) * this.pagination.pageSize,
                    MaxResultCount: this.pagination.pageSize,
                }
                ChapterGetList(data).then(res => {
                    console.log(res, '章节列表')
                    this.data = res.items.map(item => {
                        if (item.hasChild) {
                            item.children = []
                        }
                        return item
                    });
                    this.pagination.total = res.totalCount;
                    this.loading = false;
                }).catch(err => {
                    this.loading = false;
                })
            },
            myExpand(expanded, record) {
                console.log(expanded, record)
                if (expanded && record.children.length === 0) {
                    this.loadChild(record);
                }
            },
            loadChild(record, obj = {}) {
                console.log(record, 'record66666')
                this.loading = true;
                let data = {
                    ParentId: record.id || '',//父级知识点id
                    TextbookId: this.bookObj.id,//教材id
                    KeyWord: '',
                    SkipCount: 0,
                    MaxResultCount: 99999,
                    ...obj
                }
                ChapterGetList(data).then(res => {
                    console.log(res, '子节点')
                    if (res.items.length == 0 && record.hasOwnProperty('children')) {
                        delete record.children;
                    } else {
                        record.children = res.items.map(item => {
                            if (item.hasChild) {
                                item.children = []
                            }
                            return item
                        });
                    }
                    this.loading = false;
                }).catch(err => {
                    this.loading = false;
                })
            },
            showModal(type, record = {}) {
                this.$refs.EditChapter.showModal(type, record)
            },
            onSearch(val) {
                this.KeyWord = val;
                const pager = { ...this.pagination };
                pager.current = 1;
                this.pagination = pager;
                this._ChapterGetList();
            },
            //表格选项选中
            onSelectChange(selectedRowKeys) {
                this.selectedRowKeys = selectedRowKeys;
            },
            //表格change
            handleChange(pagination, filters, sorter) {
                const pager = { ...this.pagination };
                pager.current = pagination.current;
                this.pagination = pager;
                this._ChapterGetList();
            },
        }
    }
</script>
<style scoped>
    .top-box {
        height: 64px;
        padding: 0 24px;
    }
</style>