import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {FormBuilder, FormControl, FormGroup} from '@angular/forms';
import {array, selectType, validateFormArray} from '../../perfor.constants';
import {PerforManageService} from '../../perfor.manage.service';
import {NzMessageService} from 'ng-zorro-antd';
import {MonitorService} from '../../../monitor/monitor.service';

@Component({
    selector: 'smart-video-meet-modal',
    templateUrl: './video-meet-modal.component.html',
    styles: []
})
export class VideoMeetModalComponent implements OnInit {
    @Output() done = new EventEmitter<any>();

    modal = {
        isVisiable: false,
        title: '',
        id: '',
        isOkLoading: false,
    };

    validateForm: FormGroup;
    controlArray = [];
    selectType = selectType;

    constructor(private fb: FormBuilder, private perforSer: PerforManageService,
                private message: NzMessageService,
                private monitorSer: MonitorService) {
    }

    ngOnInit() {
        this.initForm();
    }

    //字段初始化
    initForm() {
        this.controlArray = [...array,
            {label: '2016网页访问量（次）', value: 'userLogincount', type: 'input', isRequire: false},
            {label: '视频会议使用次数', value: 'meetingCount', type: 'input', isRequire: false},
            {label: '2017网页访问量（次）', value: 'userCount17', type: 'input', isRequire: false},
            {label: '2018网页访问量（次）', value: 'userCount18', type: 'input', isRequire: false},
            {label: '2019网页访问量（次）', value: 'userCount19', type: 'input', isRequire: false},
        ];
        const fb = Object.assign(validateFormArray, {
            userLogincount: [null],
            meetingCount: [null],
            userCount17: [null],
            userCount18: [null],
            userCount19: [null],
        });
        console.log(fb);
        this.validateForm = this.fb.group(fb);
    }

    //新增
    showAddModal(title) {
        this.modal.isVisiable = true;
        this.modal.title = title;
    }

    //编辑
    showEditModal(title, item) {
        this.modal.isVisiable = true;
        this.modal.title = title;
        this.modal.id = item.id;
        this.validateForm.patchValue(item);
    }

    //取消
    handleCancel() {
        this.modal.isVisiable = false;
        this.modal.isOkLoading = false;
        this.initForm();
    }

    //判断
    handleOk() {
        for (let i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
        if (this.validateForm.invalid) {
            return false;
        }
        this.modal.isOkLoading = true;
        if (this.modal.title.includes('新增')) this.create();
        if (this.modal.title.includes('编辑')) this.update();
    }

    //新增
    create() {
        console.log(this.validateForm.value);
        this.perforSer.addVideoMeeting(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.initForm();
                    this.done.emit();
                    this.modal.isVisiable = false;
                    this.message.success('新增成功');
                }
                this.modal.isOkLoading = false;
            }
        );
    }

    //更新
    update() {
        this.validateForm.addControl('id', new FormControl(this.modal.id));
        this.perforSer.updateVideoMeeting(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.initForm();
                    this.done.emit();
                    this.modal.isVisiable = false;
                    this.message.success('更新成功');
                }
                this.modal.isOkLoading = false;
            }
        );
    }
}
