<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="_EducationCategoryDeleteList">删除</a-button>
                <a-button type="danger" v-if="isSource" @click="dataSyncClick">同步</a-button>
            </div>
            <div class="flex">
                <a-select style="width: 260px;margin-right:8px;" placeholder="选择状态" @change="statusChange">
                    <a-select-option :key="''">全部</a-select-option>
                    <a-select-option :key="1">启用</a-select-option>
                    <a-select-option :key="0">禁用</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="isActive" slot-scope="text">
                <span :style="{'color':text?'green':'red'}">{{text?'已启用':'已禁用'}}</span>
            </template>
            <template slot="gradeCount" slot-scope="text,record">
                <a v-if="record.isActive" @click="changeComponent('GradeManage',record)">管理</a>
            </template>
            <template slot="action" slot-scope="text,record">
                <a class="m-r8" @click="showModal('edit',record)">修改</a>
                <a class="m-r8" v-if="!record.isActive" @click="_EducationCategoryActive(true,record)">启用</a>
                <a class="m-r8" v-if="record.isActive" @click="_EducationCategoryActive(false,record)">禁用</a>
                <a @click="_EducationCategoryDelete(record)">删除</a>
            </template>
        </a-table>
        <EditLearningSection ref="EditLearningSection" />
    </div>
</template>
<script>
    const columns = [
        {
            title: "名称",
            dataIndex: "name",
            key: "name",
            scopedSlots: { customRender: "name" },
            ellipsis: true,
            width: 180
        },
        {
            title: "年级",
            dataIndex: "gradeCount",
            key: "gradeCount",
            scopedSlots: { customRender: "gradeCount" },
            ellipsis: true,
            width: 100
        },
        // {
        //     title: "用户",
        //     dataIndex: "ssxk",
        //     key: "ssxk",
        //     scopedSlots: { customRender: "ssxk" },
        //     ellipsis: true,
        //     width: 100
        // },
        {
            title: "状态",
            dataIndex: "isActive",
            key: "isActive",
            scopedSlots: { customRender: "isActive" },
            ellipsis: true,
            width: 100
        },
        {
            title: "操作",
            key: "action",
            dataIndex: "action",
            scopedSlots: { customRender: "action" },
            width: 100,
            align: "center"
        },
    ]
    import { myPagination } from '@/mixins/myPagination.js'
    import { EducationCategoryGetList, EducationCategoryActive, EducationCategoryDelete, EducationCategoryDeleteList,EducationCategoryDataSync } from '@/api/api'
    import EditLearningSection from './EditLearningSection.vue'
    export default {
        mixins: [myPagination],
        components: {
            EditLearningSection
        },
        data() {
            return {
                columns,
                loading: false,
                data: [],
                selectedRowKeys: [],
                KeyWord: '',
                isSource:false,
                IsActive: null
            }
        },
        mounted() {
            this.isSource=localStorage.getItem("isSource")==='1'
            this._EducationCategoryGetList();
        },
        methods: {
            changeComponent(name, record) {
                console.log(record, 'record')
                this.$emit('callBack', name, record)
            },
            //删除（多个）
            _EducationCategoryDeleteList() {
                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
                        }
                        EducationCategoryDeleteList(data).then(res => {
                            _self.selectedRowKeys = [];
                            _self.$message.success('删除成功！');
                            _self._EducationCategoryGetList();
                            _self.$store.dispatch('EducationCategoryGetList', true);
                        })
                    }
                });
            },
            //删除（单个）
            _EducationCategoryDelete(record) {
                let _this = this;
                this.$confirm({
                    okText: '确认',
                    cancelText: '取消',
                    mask: false,
                    title: `您确认要${record.name}"学段吗?`,
                    onOk() {
                        let data = {
                            id: record.id
                        }
                        EducationCategoryDelete(data).then(res => {
                            _this.$message.success('删除成功！');
                            _this._EducationCategoryGetList();
                            _this.$store.dispatch('EducationCategoryGetList', true);
                        })
                    }
                });
            },
            //是否启用
            _EducationCategoryActive(active, item) {
                console.log(item)
                let data = {
                    id: item.id,
                    isActive: active
                }
                EducationCategoryActive(data).then(res => {
                    this.$message.success(`${active ? '启用' : '禁用'}成功！`);
                    this._EducationCategoryGetList();
                    this.$store.dispatch('EducationCategoryGetList', true);
                })
            },
            //学段列表
            _EducationCategoryGetList() {
                this.loading = true;
                let data = {
                    IsActive: typeof (this.IsActive) == 'number' ? Boolean(this.IsActive) : this.IsActive,
                    KeyWord: this.KeyWord,
                    SkipCount: (this.pagination.current - 1) * this.pagination.pageSize,
                    MaxResultCount: this.pagination.pageSize,
                }
                EducationCategoryGetList(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.EditLearningSection.showModal(type, record)
            },
            onSearch(val) {
                this.KeyWord = val;
                const pager = { ...this.pagination };
                pager.current = 1;
                this.pagination = pager;
                this._EducationCategoryGetList();
            },
            statusChange(value) {
                console.log(value)
                this.IsActive = value;
                const pager = { ...this.pagination };
                pager.current = 1;
                this.pagination = pager;
                this._EducationCategoryGetList();
            },
            //表格选项选中
            onSelectChange(selectedRowKeys) {
                this.selectedRowKeys = selectedRowKeys;
            },
            //表格change
            handleChange(pagination, filters, sorter) {
                const pager = { ...this.pagination };
                pager.current = pagination.current;
                this.pagination = pager;
                this._EducationCategoryGetList();
            },
            dataSyncClick(){
              let _this = this;
                this.$confirm({
                    mask: false,
                    title: `同步数据会清空现有数据，确定要同步数据吗?`,
                    onOk() {
                        EducationCategoryDataSync().then(res => {
                            _this.$message.success('同步成功！');
                            _this._EducationCategoryGetList();
                            _this.$store.dispatch('EducationCategoryGetList', true);
                        })
                    }
                });
            }
        }
    }
</script>
<style scoped>
    .top-box {
        height: 64px;
        padding: 0 24px;
    }
</style>