<template>
    <div>
        <a-modal :visible="visible" @ok="handOK" :confirmLoading="confirmLoading" @cancel="visible=false"
            :maskClosable="false" :destroyOnClose="true" :width="900">
            <div slot="title">
                {{type==='add'?'创建':'编辑'}}年级
            </div>
            <div class="modal-box">
                <a-form :form="form" @submit="handOK" layout="horizontal">
                    <a-form-item label="所属学段：" :label-col="{ span: 3 }" :wrapper-col="{ span: 20 }">
                        <a-select placeholder="请选择" showSearch :filterOption="filterOption" @change="classChange"
                            v-decorator="[
                        'educationCategoryId',
                        {rules: [{ required: true, message: '此项为必填!' }],
                        initialValue:record.educationCategoryId||''
                    }
                        ]">
                            <a-select-option v-for="(item,index) in $store.state.baseInfo.LearningSection"
                                :key="item.id" :value="item.id">
                                {{item.name}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="年级名称：" :label-col="{ span: 3 }" :wrapper-col="{ span: 20 }">
                        <a-input placeholder="请输入" v-decorator="[
                         'name',
                         {rules: [{ required: true, message: '此项为必填!' },{max:30,message: '最大长度不能超过30个字符'}],
                         initialValue:record.name||''
                        }
                         ]" />
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>
<script>
    import Editor from '@/components/Editor.vue'
    import { GradeCreate,GradeUpdate } from '@/api/api'
    export default {
        components: {
            Editor
        },
        data() {
            return {
                confirmLoading: false,
                visible: false,
                form: this.$form.createForm(this),
                type: '',
                detail: '',
                record: {},
            };
        },
        mounted() {

        },
        methods: {
            showModal(type, record) {
                console.log(this.$store.state.LearningSection)
                this.visible = true;
                this.type = type;
                this.record = record;
            },
            //表单确认
            handOK(e) {
                e.preventDefault();
                this.form.validateFields((err, values) => {
                    if (!err) {
                        this.confirmLoading = true;
                        console.log(values)
                        if (this.type === 'add') {
                            GradeCreate(values).then(res => {
                                this.confirmLoading = false;
                                this.$parent._GradeGetList();
                                this.$message.success('创建成功！')
                                this.visible = false;
                            }).catch(err => {
                                this.confirmLoading = false;
                            })
                        }
                        if (this.type === 'edit') {
                            let params = {
                                id: this.record.id
                            }
                            GradeUpdate(values, params).then(res => {
                                this.confirmLoading = false;
                                this.$parent._GradeGetList();
                                this.$message.success('修改成功！')
                                this.visible = false;
                            }).catch(err => {
                                this.confirmLoading = false;
                            })
                        }
                    }
                })
            },
            classChange(value) {
                console.log(value, 'value');
            },
            //select框筛选
            filterOption(input, option) {
                return (
                    option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
                );
            },
        }
    };
</script>
<style scoped>
    .file-row {
        display: flex;
        flex-direction: row;
        align-items: flex-end;
    }
</style>