
<template>
    <div class="form-container">
        <Form :model="formTop" label-position="top">
            <FormItem label="知识块" v-show="!hideBlock">
                <Input v-model="formTop.blockName" disabled></Input>
            </FormItem>
            <FormItem label="名称">
                <Input v-model="formTop.name" placeholder="请输入知识点名称，必填项"></Input>
            </FormItem>
            <FormItem>
                <Button @click="handleAddPointSubmit()" :loading="isLoading">确认</Button>
            </FormItem>
        </Form>
    </div>
</template>

<script>
    import '@/utils/Math.uuid'
    export default {
        props: ['blockData', 'pointData', 'hideBlock', 'addType', 'schoolParams'],
        data() {
            return {
                blockDatas: null,
                currentPeriod: {},
                currentPoint: null,
                schoolInfos: {},
                addPointType: 'school',
                isHideBlock: false,
                isLoading: false,
                uuid: '',
                formTop: {
                    blockName: '',
                    name: ''
                }
            }
        },

        created() {
            this.isHideBlock = this.hideBlock
        },
        methods: {
            // 提交添加
            handleAddPointSubmit() {
                let newName = this.formTop.name
                if (!newName) {
                    this.$Message.warning('知识点名称不能为空！')
                } else {
                    let editPointItem = this.currentPoint
                    this.isLoading = true
                    this.uuid = Math.uuid()
					console.log(this.schoolInfos)
                    let params = {
                        type: 1,
                        name: newName,
                        alias: newName,
                        subjectId: editPointItem ? editPointItem.subjectId : this.schoolInfos.subjectId,
                        code: this.addPointType === 'school' ? this.schoolInfos.schoolCode : this.$store.state.userInfo.TEAMModelID, // //判断当前添加为校本或者个人知识点
                        order: 706,
                        status: 1,
                        knowledgeId: editPointItem ? editPointItem.knowledgeId : this.uuid,
                        periodId: editPointItem ? editPointItem.periodId : this.schoolInfos.period,
                        source: 1,
                        points: this.blockDatas ? [this.blockDatas.id] : [],
                        id: editPointItem ? editPointItem.id : null,
                    }
					console.log(params)
                    this.savePointAndBlock(params)
                }
            },

            /**
             * 保存知识点
             * @param pointItem
             * @param BlockItem
             */
            savePointAndBlock(pointItem) {
                this.$api.knowledge.SaveOrUpdateKnowledge([pointItem]).then(res => {
                    if (!res.error && res.knowledges.length) {
                        this.$emit('addFinish', res.knowledges[0])
                        this.closeModal()
                        this.currentPoint = null
                        this.formTop.name = ''
                        this.isLoading = false
                    } else {
                        this.$Message.warning('操作失败,错误代码：' + res.error.code + ',错误信息:' + res.error.message)
                    }
                }).catch(err => {
                    console.log(err)
                    this.$Message.warning('操作失败')
                })
				this.isLoading = false
            },

            // 添加完成 关闭窗口
            closeModal() {
                this.formTop.name = ''
            }
        },
        mounted() {
        },
        watch: {
            addType: {
                handler(newValue, oldValue) {
                    if (newValue) {
                        this.addPointType = newValue
                    }
                }
            },

            hideBlock: {
                handler(newValue, oldValue) {
                    if (newValue) {
                        this.isHideBlock = newValue
                    }
                }
            },

            blockData: {
                handler(newValue, oldValue) {
                    this.blockDatas = newValue
                    console.log(newValue)
                    if (newValue) {
                        this.formTop.blockName = newValue.name
                    }
                }
            },

            pointData: {
                handler(newValue, oldValue) {
                    if (newValue) {
                        this.currentPoint = newValue
                        this.formTop.name = newValue.name
                    }
                }
            },

            schoolParams: {
                handler(newValue, oldValue) {
                    if (newValue) {
						console.log(newValue)
                        this.schoolInfos = newValue
                    }
                }
            }
        }

    }
</script>

<style>
    .form-container .ivu-form .ivu-form-item-label {
        color: #fff;
        margin: 10px 0;
        font-size: 16px;
    }

    .form-container .ivu-input {
        background: #575757;
        border-color: transparent;
        height: 35px;
        color: #fff;
    }

        .form-container .ivu-input::-webkit-input-placeholder {
            color: #808080;
        }

    .form-container .ivu-select-single .ivu-select-selection {
        background: #575757;
        color: #fbfbfb;
        border-color: transparent;
        height: 35px;
    }

    .form-container .ivu-select-single .ivu-select-arrow {
        color: #fbfbfb;
    }

    .form-container .ivu-btn {
        width: 100%;
        height: 40px;
        background: rgb(11, 151, 117);
        border: none;
        color: #fff;
    }
</style>
