import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {SystemService} from '../../system.service';
import {NzMessageService} from 'ng-zorro-antd';

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

    title;
    isVisible = false;
    isOkLoading = false;
    validateForm:FormGroup;
    groupList:any[];
    orgId;

    constructor(private fb:FormBuilder,private message:NzMessageService,
                private systemSer:SystemService) {
    }

    ngOnInit() :void{
       this.initForm();
    }

    initForm(){
        this.validateForm = this.fb.group({
            name:[null,[Validators.required,Validators.maxLength(15)]],
            parentId:[null],
        });
    }

    showAddModal(list,title){
        this.groupList = list;
        this.isVisible = true;
        this.title = title;
    }

    showEditModal(list,title,item){
        this.orgId = item.key;
        this.isVisible = true;
        this.title = title;
        this.systemSer.getAllOutOfMine(this.orgId).subscribe(
            (res)=>{
                this.groupList = res.data;
            }
        );
        this.validateForm.patchValue(item.origin);
        this.validateForm.value.parentId = item.key;
    }

    handleCancel(){
        this.initForm();
        this.isVisible = false;
    }

    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.create();
        }
        if(this.title == "编辑部门"){
            this.update();
        }
    }

    create(){
        this.systemSer.createOrg(this.validateForm.value).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.initForm();
                    this.isVisible = false;
                    this.done.emit();
                    this.message.success("新增部门成功");
                }else{
                    this.message.error(res.errMsg);
                }
                this.isOkLoading = false;
            }
        )
    }

    update(){
        this.validateForm.addControl("id",new FormControl(this.orgId));
        this.systemSer.updateOrganization(this.validateForm.value).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.initForm();
                    this.isVisible = false;
                    this.done.emit();
                    this.message.success("部门信息更新成功");
                }else{
                    this.message.error(res.errMsg);
                }
                this.isOkLoading = false;
            }
        )
    }

}
