<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'&&'创建子')||(type==='edit'&&'编辑')}}知识点
            </div>
            <div class="modal-box">
                <a-spin :spinning="spinning">
                    <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:type=='edit'?record.name:''}
                                 ]" />
                        </a-form-item>
                        <!-- <a-form-item label="备注：" :label-col="{ span: 3 }" :wrapper-col="{ span: 20 }">
                            <Editor v-model="detail" />
                        </a-form-item> -->
                    </a-form>
                </a-spin>
            </div>
        </a-modal>
    </div>
</template>
<script>
    import Editor from '@/components/Editor.vue'
    import { KnowledgePointGetCreate, KnowledgePointGetOne, KnowledgePointGetUpdate } from '@/api/api'
    export default {
        components: {
            Editor
        },
        data() {
            return {
                confirmLoading: false,
                spinning: false,
                visible: false,
                form: this.$form.createForm(this),
                type: '',
                record: {},
                detail: '',
            };
        },
        mounted() {

        },
        methods: {
            showModal(type, record) {
                // if (type === 'edit') {
                //     this.spinning = true;
                //     let data = {
                //         id: record.id
                //     }
                //     KnowledgePointGetOne(data).then(res => {
                //         console.log(res, '详情')
                //         this.spinning = false;
                //         this.detail = res.remark;
                //     }).catch(err => {
                //         this.spinning = false;
                //     })
                // }
                console.log(record, 'record')
                this.record = record;
                this.visible = true;
                this.type = type;
            },
            //表单确认
            handOK(e) {
                e.preventDefault();
                this.form.validateFields((err, values) => {
                    if (!err) {
                        this.confirmLoading = true;
                        if (this.type === 'add') {
                            let data = {
                                ...values,
                                // remark: this.detail,
                                disciplineId: this.$parent.activeSubject.id,
                                parentId: this.record.id
                            }
                            KnowledgePointGetCreate(data).then(res => {
                                this.$message.success('创建成功！')
                                this.confirmLoading = false;
                                console.log(this.record, 'this.record55555')
                                this.$parent.loadChild(this.record);
                                this.handCancle();
                            }).catch(err => {
                                this.confirmLoading = false;
                            })
                        }
                        if (this.type === 'edit') {
                            let data = {
                                ...values,
                                // remark: this.detail
                            }
                            let params = {
                                id: this.record.id
                            }
                            KnowledgePointGetUpdate(data, params).then(res => {
                                this.$message.success('修改成功！')
                                this.confirmLoading = false;
                                // this.$parent._ChapterGetList();
                                this.record.dataRef.title = values.name;
                                this.record.dataRef.name = values.name;
                                this.handCancle();
                            }).catch(err => {
                                this.confirmLoading = false;
                            })
                        }
                    }
                })
            },
            handCancle() {
                console.log('添加了进这了')
                this.visible = false;
                this.record = {};
                this.detail = '';
            }
        }
    };
</script>
<style scoped>
    .file-row {
        display: flex;
        flex-direction: row;
        align-items: flex-end;
    }
</style>