<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="[
                        'gradeId',
                        {rules: [{ required: true, message: '此项为必填!' }],
                        initialValue:record.gradeId||''
                    }
                        ]">
                            <a-select-option v-for="(item,index) in $store.state.baseInfo.Grades"
                                :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-item label="简称：" :label-col="{ span: 3 }" :wrapper-col="{ span: 20 }">
                        <a-input v-decorator="[
                             'ShortName',
                             ]" />
                    </a-form-item>
                    <a-form-item label="备注：" :label-col="{ span: 3 }" :wrapper-col="{ span: 20 }">
                        <Editor v-model="detail" />
                    </a-form-item> -->
                </a-form>
            </div>
        </a-modal>
    </div>
</template>
<script>
    import Editor from '@/components/Editor.vue'
    import { DisciplineCreate,DisciplineUpdate } 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') {
                            DisciplineCreate(values).then(res => {
                                this.confirmLoading = false;
                                this.$parent._DisciplineGetList();
                                this.$message.success('创建成功！')
                                this.visible = false;
                            }).catch(err => {
                                this.confirmLoading = false;
                            })
                        }
                        if (this.type === 'edit') {
                            let params = {
                                id: this.record.id
                            }
                            DisciplineUpdate(values, params).then(res => {
                                this.confirmLoading = false;
                                this.$parent._DisciplineGetList();
                                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>