<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 v-if="type==='add'" label="所属学段" :label-col="{ span: 3 }" :wrapper-col="{ span: 20 }">
                        <a-select placeholder="请选择所属学段" @change="handleProvinceChange" v-decorator="[
                        'aa',
                        {rules: [{ required: true, message: '此项为必填!' },
                        ]}
                        ]">
                            <a-select-option v-for="item in Object.keys($store.state.baseInfo.SubjectsGroup)"
                                :key="item">
                                {{ item }}
                            </a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item v-if="type==='add'" label="学科" :label-col="{ span: 3 }" :wrapper-col="{ span: 20 }">
                        <a-select placeholder="请选择学科" :disabled="!currentLearning" v-decorator="[
                        'disciplineId',
                        {rules: [{ required: true, message: '此项为必填!' },
                        ]}
                        ]">
                            <a-select-option v-for="v in $store.state.baseInfo.SubjectsGroup[currentLearning]"
                                :key="v.id">
                                {{ v.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: '此项为必填!' },
                         ],initialValue:record.name||''}
                         ]" />
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>
<script>
    import Editor from '@/components/Editor.vue'
    import { TextbookCreate, TextbookUpdate } from '@/api/api'
    export default {
        components: {
            Editor
        },
        data() {
            return {
                confirmLoading: false,
                visible: false,
                form: this.$form.createForm(this),
                type: '',
                record: {},
                currentLearning: ''
            };
        },
        mounted() {

        },
        methods: {
            showModal(type, record = {}) {
                console.log(type, 'type')
                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') {
                            TextbookCreate(values).then(res => {
                                this.$message.success('创建成功！')
                                this.confirmLoading = false;
                                this.$parent._TextbookGetList();
                                this.visible = false;
                            }).catch(err => {
                                this.confirmLoading = false;
                            })
                        }
                        if (this.type === 'edit') {
                            let params = {
                                id: this.record.id
                            }
                            TextbookUpdate(values, params).then(res => {
                                this.$message.success('修改成功！')
                                this.confirmLoading = false;
                                this.$parent._TextbookGetList();
                                this.visible = false;
                            }).catch(err => {
                                this.confirmLoading = false;
                            })
                        }
                    }
                })
            },
            handleProvinceChange(value) {
                console.log(value)
                this.currentLearning = value;
                this.form.setFieldsValue({
                    disciplineId: ''
                })
            },
        }
    };
</script>
<style scoped>
    .file-row {
        display: flex;
        flex-direction: row;
        align-items: flex-end;
    }
</style>