<template>
    <div>
        <a-modal :visible="visible" @ok="handOK" :confirmLoading="confirmLoading" @cancel="handCancle" 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'&&JSON.stringify(parentObj)=='{}'" label="所属学段"
                        :label-col="{ span: 3 }" :wrapper-col="{ span: 20 }">
                        <a-select placeholder="请选择所属学段" @change="handleProvinceChange" v-decorator="[
                    'learningSection',
                    {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 label="学科" :label-col="{ span: 3 }" :wrapper-col="{ span: 20 }">
                        <a-select v-if="JSON.stringify(record)=='{}'" 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>
                        <span v-if="JSON.stringify(record)!='{}'">{{record.discipline}}</span>
                    </a-form-item>
                    <a-form-item v-if="type!=='edit'" label="父级知识点：" :label-col="{ span: 3 }"
                        :wrapper-col="{ span: 20 }">
                        {{record.name?record.name:'无'}}
                    </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:type!='addChild'&&record.name?record.name:''}
                         ]" />
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>
<script>
    import Editor from '@/components/Editor.vue'
    import { KnowledgePointGetCreate, KnowledgePointGetUpdate } from '@/api/api'
    export default {
        components: {
            Editor
        },
        data() {
            return {
                confirmLoading: false,
                visible: false,
                form: this.$form.createForm(this),
                type: '',
                detail: '',
                record: {},
                currentLearning: '',
                parentObj: {},
            };
        },
        mounted() {

        },
        methods: {
            showModal(type, record = {}, parentObj = {}) {
                console.log(record, 'record')
                this.visible = true;
                this.type = type;
                this.record = record;
                this.parentObj = parentObj;
                console.log(parentObj, 'parentObj')
            },
            //表单确认
            handOK(e) {
                e.preventDefault();
                this.form.validateFields((err, values) => {
                    if (!err) {
                        this.confirmLoading = true;
                        console.log(values)
                        if (this.type === 'add') {
                            let data = {
                                ...values,
                                parentId: this.record.id || ''
                            }
                            KnowledgePointGetCreate(data).then(res => {
                                console.log(res, '知识点id')
                                this.$message.success('创建成功！')
                                this.confirmLoading = false;
                                this.$parent._KnowledgePointGetList();
                                this.visible = false;
                            }).catch(err => {
                                this.confirmLoading = false;
                            })
                        }
                        if (this.type === 'edit') {
                            let params = {
                                id: this.record.id
                            }
                            KnowledgePointGetUpdate(values, params).then(res => {
                                console.log(res, '知识点id')
                                this.$message.success('修改成功！')
                                this.confirmLoading = false;
                                // this.$parent._KnowledgePointGetList();
                                this.record.name = values.name
                                this.visible = false;
                            }).catch(err => {
                                this.confirmLoading = false;
                            })
                        }
                        if (this.type === 'addChild') {
                            let data = {
                                ...values,
                                parentId: this.record.id || '',
                                disciplineId: this.record.disciplineId || '',
                            }
                            KnowledgePointGetCreate(data).then(res => {
                                console.log(res, '知识点id')
                                this.$message.success('创建成功！')
                                this.confirmLoading = false;
                                let obj = {
                                    ParentId: this.record.id,
                                    DisciplineId: this.record.disciplineId
                                }
                                console.log(obj, 'obj')
                                this.$parent.loadChild(this.record, obj);
                                this.visible = false;
                            }).catch(err => {
                                this.confirmLoading = false;
                            })
                        }
                    }
                })
            },
            classChange(value) {
                console.log(value, 'value');
            },
            handCancle() {
                this.visible = false
            },
            //select框筛选
            filterOption(input, option) {
                return (
                    option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
                );
            },
            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>