
<template>
    <div class="form-container">
        <Form :model="formTop" label-position="top">
            <FormItem label="科目">
                <Select v-model="formTop.subject">
                    <Option v-for="(item,index) in currentPeriod.subjects" :value="item.id" :key="index">{{ item.name }}</Option>
                </Select>
            </FormItem>
            <FormItem label="名称">
                <Input v-model="formTop.name" placeholder="请输入知识块名称，必填项"></Input>
            </FormItem>
            <FormItem>
                <Button @click="handleSubmit" :loading="isLoading">确认</Button>
            </FormItem>
        </Form>
    </div>
</template>

<script>
    import '@/utils/Math.uuid'
    export default {
        props: ['originData', 'periodIndex', 'subjectIndex', 'addType', 'editBlock'],
        data() {
            return {
                originDatas: {},
                currentPeriod: {},
                editItem: null,
                addPointType: 'school',
                isLoading: false,
                uuid: '',
                formTop: {
                    subject: '',
                    grade: '',
                    semester: '',
                    name: ''
                }
            }
        },

        created() {
			if(this.originData && this.originData.period){
				this.currentPeriod = this.originData.period[this.periodIndex]
				this.originDatas = this.originData
			}
        },
        methods: {
            // 提交添加
            handleSubmit() {
                let newName = this.formTop.name
                if (!newName) {
                    this.$Message.warning('知识块名称不能为空！')
                } else {
                    this.isLoading = true
                    this.uuid = Math.uuid()
                    let params = {
                        type: 0,
                        name: this.formTop.name,
                        alias: this.formTop.name,
                        subjectId: this.formTop.subject,
                        code: this.addPointType === 'school' ? this.$store.state.userInfo.schoolCode : this.$store.state.userInfo.TEAMModelID,
                        order: 706,
                        status: 1,
                        knowledgeId: this.uuid,
                        periodId: this.currentPeriod.id,
                        points: [],
                        source: 1
                    }
                    if (this.editItem && this.editItem.code) {
                        params.knowledgeId = this.editItem.knowledgeId
                        params.points = this.editItem.points
                        params.id = this.editItem.id
                    }
                    this.$api.knowledge.SaveOrUpdateKnowledge([params]).then(res => {
                        if (!res.error && res.knowledges) {
                            this.$emit('addFinish', false)
                        } else {
                            this.$Message.warning('操作失败,错误代码：' + res.error.code + ',错误信息:' + res.error.message)
                        }
                        this.closeModal()
                        this.editItem = null
                        this.isLoading = false
                    }).catch(err => {
                        console.log(err)
                        this.$Message.warning('操作失败')
                        this.isLoading = false
                    })
                }
            },

            // 添加完成 关闭窗口
            closeModal() {
                this.formTop.name = ''
            }
        },
        watch: {
            addType: {
                handler(newValue, oldValue) {
                    if (newValue) {
                        this.addPointType = newValue
                    }
                }
            },
            originData: {
                handler(newValue, oldValue) {
                    this.originDatas = newValue
                }
            },
            periodIndex: {
                handler(newValue, oldValue) {
                    this.currentPeriod = this.originDatas.period[newValue]
                    this.formTop.subject = this.currentPeriod.subjects.length ? this.currentPeriod.subjects[0].id : ''
                    this.formTop.name = ''
                }
            },
            subjectIndex: {
                handler(newValue, oldValue) {
                    this.formTop.subject = this.currentPeriod.subjects ? this.currentPeriod.subjects[newValue].id : ''
                }
            },
            editBlock: {
                handler(newValue, oldValue) {
                    if (newValue) { // 有编辑册别传过来则为编辑状态
                        this.editItem = newValue
                        this.formTop.subject = this.formTop.subject || newValue.subjectId
                        this.formTop.name = newValue.name
                    } else { // 否则为新增状态
                        this.formTop.subject = this.formTop.subject || this.currentPeriod.subjects[0].id
                        this.formTop.name = ''
                        this.editItem = 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>
