<template>
    <div>
        <a-modal :visible="visible" @ok="handOK" :confirmLoading="confirmLoading" @cancel="visible=false" width="700px"
            :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-input placeholder="请输入" v-decorator="[
                         'name',
                         {rules: [{ required: true, message: '此项为必填!' }],
                         initialValue:record.name||''
                        }
                         ]" />
                    </a-form-item>
                    <a-form-item v-if="type==='add'" label="是否启用：" :label-col="{ span: 3 }" :wrapper-col="{ span: 20 }">
                        <a-radio-group v-decorator="[
                        'isActive',
                        {rules: [{ required: true, message: '此项为必填!' }],
                        initialValue:record.isActive
                    }
                        ]">
                            <a-radio :value="true">是</a-radio>
                            <a-radio :value="false">否</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>
<script>
    import Editor from '@/components/Editor.vue'
    import { EducationCategoryCreate, EducationCategoryUpdate } from '@/api/api'
    export default {
        components: {
            Editor
        },
        data() {
            return {
                confirmLoading: false,
                visible: false,
                form: this.$form.createForm(this),
                type: '',
                record: {},
                detail: '',
            };
        },
        mounted() {

        },
        methods: {
            showModal(type, record) {
                console.log(record, 'record')
                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') {
                            EducationCategoryCreate(values).then(res => {
                                this.$message.success('创建成功！')
                                this.confirmLoading = false;
                                this.$parent._EducationCategoryGetList();
                                this.$store.dispatch('EducationCategoryGetList', true);
                                this.visible = false;
                            }).catch(err => {
                                this.confirmLoading = false;
                            })
                        }
                        if (this.type === 'edit') {
                            let params = {
                                id: this.record.id
                            }
                            EducationCategoryUpdate(values, params).then(res => {
                                this.$message.success('修改成功！')
                                this.confirmLoading = false;
                                this.$parent._EducationCategoryGetList();
                                this.$store.dispatch('EducationCategoryGetList', true);
                                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>