<template>
    <div>
        <div class="top-box flex-align">
            <div>
                <a-button type="primary" @click="showModal('add')">创建</a-button>
                <a-button type="primary" style="margin: 0 24px;" @click="_GradeDeleteList">删除</a-button>
                <a-button type="danger" v-if="isSource" @click="dataSyncClick">同步</a-button>
            </div>
            <div class="flex">
                <a-select :default-value="receiveData.id" show-search placeholder="请选择" option-filter-prop="children"
                    style="width: 200px;margin-right:8px" :filter-option="filterOption" @change="xdChange">
                    <a-select-option :value="''" :key="'all'">
                        所有学段
                    </a-select-option>
                    <a-select-option v-for="item in $store.state.baseInfo.LearningSection" :value="item.id"
                        :key="item.id">
                        {{item.name}}
                    </a-select-option>
                </a-select>
                <a-input-search style="width:400px;" placeholder="搜索年级" enter-button @search="onSearch" />
            </div>
        </div>
        <a-table :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}" :columns="columns"
            :loading="loading" :dataSource="data" size="small" :rowKey="record => record.id" :pagination="pagination"
            @change="handleChange">
            <template slot="disciplineCount" slot-scope="text,record">
                <a @click="changeComponent('SubjectManage',record)">管理</a>
            </template>
            <template slot="action" slot-scope="text,record">
                <a class="m-r8" @click="showModal('edit',record)">修改</a>
                <a @click="_GradeDelete(record)">删除</a>
            </template>
        </a-table>
        <EditGrade ref="EditGrade" />
    </div>
</template>
<script>
    const columns = [
        {
            title: "学段",
            dataIndex: "educationCategoryName",
            key: "educationCategoryName",
            scopedSlots: { customRender: "educationCategoryName" },
            ellipsis: true,
            width: 180
        },
        {
            title: "名称",
            dataIndex: "name",
            key: "name",
            scopedSlots: { customRender: "name" },
            ellipsis: true,
            width: 180
        },
        {
            title: "学科",
            dataIndex: "disciplineCount",
            key: "disciplineCount",
            scopedSlots: { customRender: "disciplineCount" },
            ellipsis: true,
            width: 100
        },
        {
            title: "操作",
            key: "action",
            dataIndex: "action",
            scopedSlots: { customRender: "action" },
            width: 100,
            align: "center"
        },
    ]
    import { myPagination } from '@/mixins/myPagination.js'
    import { GradeGetList, GradeDelete, GradeDeleteList,GradesDataSync } from '@/api/api'
    import EditGrade from './EditGrade.vue'
    export default {
        props: {
            receiveData: {
                type: Object,
                default: () => { }
            }
        },
        mixins: [myPagination],
        components: {
            EditGrade
        },
        data() {
            return {
                columns,
                loading: false,
                data: [],
                selectedRowKeys: [],
                KeyWord: '',
                isSource:false,
                EducationCategoryId: this.receiveData.id,//学段id
            }
        },
        mounted() {
            this._GradeGetList();
            this.isSource=localStorage.getItem("isSource")==='1'
        },
        methods: {
            changeComponent(name, record) {
                console.log(record, 'record')
                this.$emit('callBack', name, record)
            },
            //删除（多个）
            _GradeDeleteList() {
                if (this.selectedRowKeys.length === 0) {
                    this.$message.error('请选择需要删除的年级！')
                    return;
                }
                let _self = this;
                this.$confirm({
                    okText: '确认',
                    cancelText: '取消',
                    mask: false,
                    title: `您确认要删除这"${_self.selectedRowKeys.length}"个年级吗?`,
                    onOk() {
                        let data = {
                            idList: _self.selectedRowKeys
                        }
                        GradeDeleteList(data).then(res => {
                            _self.selectedRowKeys = [];
                            _self.$message.success('删除成功！');
                            _self._GradeGetList();
                            _self.$store.dispatch('GradeGetList', true);
                        })
                    }
                });
            },
            //删除（单个）
            _GradeDelete(record) {
                let _this = this;
                this.$confirm({
                    okText: '确认',
                    cancelText: '取消',
                    mask: false,
                    title: `您确认要${record.name}"年级吗?`,
                    onOk() {
                        let data = {
                            id: record.id
                        }
                        GradeDelete(data).then(res => {
                            _this.$message.success('删除成功！');
                            _this._GradeGetList();
                            _this.$store.dispatch('GradeGetList', true);
                        })
                    }
                });
            },
            //年级列表
            _GradeGetList() {
                this.loading = true;
                let data = {
                    EducationCategoryId: this.EducationCategoryId,
                    KeyWord: this.KeyWord,
                    SkipCount: (this.pagination.current - 1) * this.pagination.pageSize,
                    MaxResultCount: this.pagination.pageSize,
                }
                GradeGetList(data).then(res => {
                    console.log(res, '年级列表')
                    this.data = res.items;
                    this.pagination.total = res.totalCount;
                    this.loading = false;
                }).catch(err => {
                    this.loading = false;
                })
            },
            showModal(type, record = {}) {
                this.$refs.EditGrade.showModal(type, record)
            },
            onSearch(val) {
                this.KeyWord = val;
                const pager = { ...this.pagination };
                pager.current = 1;
                this.pagination = pager;
                this._GradeGetList();
            },
            //表格选项选中
            onSelectChange(selectedRowKeys) {
                this.selectedRowKeys = selectedRowKeys;
            },
            //表格change
            handleChange(pagination, filters, sorter) {
                const pager = { ...this.pagination };
                pager.current = pagination.current;
                this.pagination = pager;
                this._GradeGetList();
            },
            //学段改变
            xdChange(val) {
                console.log(val, 'val')
                this.EducationCategoryId = val;
                const pager = { ...this.pagination };
                pager.current = 1;
                this.pagination = pager;
                this._GradeGetList();
            },
            filterOption(input, option) {
                return (
                    option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
                );
            },
            dataSyncClick(){
              let _this = this;
                this.$confirm({
                    mask: false,
                    title: `同步数据会清空现有数据，确定要同步数据吗?`,
                    onOk() {
                        GradesDataSync().then(res => {
                            _this.$message.success('同步成功！');
                            _this._GradeGetList();
                            _this.$store.dispatch('GradeGetList', true);
                        })
                    }
                });
            }
        }
    }
</script>
<style scoped>
    .top-box {
        height: 64px;
        padding: 0 24px;
    }
</style>