import {Component, OnInit, Output} from '@angular/core';
import {
    FormBuilder,
    FormControl,
    FormGroup,
    ValidationErrors,
    Validators
} from '@angular/forms';
import {OverAllService} from '../../overAll/overAll.service';
import {NzMessageService} from 'ng-zorro-antd';
import {Observable, Observer} from 'rxjs';
import {EmitService} from '../../shared/event/eventEmitter';
import {EventEmitter} from '@angular/core';

@Component({
    selector: 'smart-create-group',
    templateUrl: './create-group.component.html',
    styles: []
})
export class CreateGroupComponent implements OnInit {
    @Output() add = new EventEmitter<any>();

    title;
    isCreate = false;
    isOkLoading = false;
    validateForm: FormGroup;

    groupid;

    constructor(private fb: FormBuilder, private overAllSer: OverAllService,
                private message: NzMessageService, private emitService: EmitService) {
    }

    ngOnInit() {
        this.initForm();
    }

    showModal(title): void {
        this.title = title;
        this.isCreate = true;
    }

    showEditModal(title,item){
        this.title = title;
        this.isCreate = true;
        this.groupid = item.groupid;
        this.validateForm.patchValue(item);
    }

    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.isOkLoading = true;

        if(this.title == '添加分组' || this.title == '添加模版组'){
            this.create();
        }else{
            this.update();
        }
    }

    create() {
        this.overAllSer.createGroup(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.info('添加成功');
                    this.isCreate = false;
                    this.add.emit();
                    this.emitService.eventEmit.emit("add");
                    this.initForm();
                }
                this.isOkLoading = false;
            }
        );
    }

    update() {
        const data = {
            groupid:this.groupid,
            groupDescribe:this.validateForm.value.groupDescribe,
            name:this.validateForm.value.name
        }
        this.overAllSer.updataGroup(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.info('修改成功');
                    this.isCreate = false;
                    this.add.emit();
                    this.initForm();
                }
                this.isOkLoading = false;
            }
        );
    }

    handleCancel(): void {
        this.isCreate = false;
        this.initForm();
    }

    //初始化
    initForm(){
        this.validateForm = this.fb.group({
            name: ['', [Validators.required]],
            groupDescribe: ['']
        });
    }

}
