<template>
    <div>
        <a-modal :visible="visible" @ok="handOK" :confirmLoading="confirmLoading" @cancel="handleCancle" width="700px"
            :maskClosable="false" :destroyOnClose="true" :width="900">
            <div slot="title">
                分配学科
            </div>
            <div class="modal-box">
                <a-form :form="form" @submit="handOK" layout="horizontal">
                    <a-form-item label="学科：" :label-col="{ span: 3 }" :wrapper-col="{ span: 20 }">
                        <a-select mode="multiple" placeholder="请选择" v-decorator="[
                        'disciplineList',
                        {rules: [{ required: true, message: '此项为必填!' },
                        ],initialValue:selectedKeys}
                        ]">
                            <a-select-opt-group v-for="(val,key) in $store.state.baseInfo.SubjectsGroup" :key="key">
                                <span slot="label">{{key}}</span>
                                <a-select-option :value="v.id" v-for="v in val" :key="v.id">
                                    {{v.name}}
                                </a-select-option>
                            </a-select-opt-group>
                        </a-select>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>
<script>
    import { UserManagerUpdateUserDiscipline } from '@/api/api'
    export default {
        data() {
            return {
                confirmLoading: false,
                visible: false,
                form: this.$form.createForm(this),
                selectedKeys: [],
                type: '',
                record: {},
                detail: '',
            };
        },
        mounted() {

        },
        methods: {
            showModal(record) {
                console.log(record, 'record')
                console.log(this.$store.state.baseInfo.SubjectsGroup, '6666')
                this.visible = true;
                this.record = record;
                this.record.userDisciplines.forEach(item => {
                    this.selectedKeys.push(item.disciplineId)
                })
            },
            //表单确认
            handOK(e) {
                e.preventDefault();
                this.form.validateFields((err, values) => {
                    if (!err) {
                        this.confirmLoading = true;
                        console.log(values)
                        let params = {
                            userId: this.record.id
                        }
                        UserManagerUpdateUserDiscipline(values, params).then(res => {
                            this.$message.success('分配成功！');
                            this.$parent._UserGetAll();
                            this.confirmLoading = false;
                            this.visible = false;
                        }).catch(err => {
                            this.confirmLoading = false;
                        })
                    }
                })
            },
            handleCancle() {
                this.visible = false;
                this.selectedKeys = [];
            }
        }
    };
</script>
<style scoped>
    .file-row {
        display: flex;
        flex-direction: row;
        align-items: flex-end;
    }
</style>