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

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

    title;
    isVisible = false;
    isOkLoading = false;
    groupList:any[];
    validateForm:FormGroup;
    userId;
    genderList = [{value:1,label:'男'},{value:0,label:'女'}];
    positionList = [{value:1,label:'组长'},{value:2,label:'组员'}];

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

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

    initForm(){
        this.validateForm = this.fb.group({
            name:[null,[Validators.required,Validators.minLength(6),Validators.maxLength(16)]],
            phoneNumber:[null,[Validators.required,Validators.minLength(11),Validators.maxLength(11)]],
            gender:[null,[Validators.required]],
            email:[null,],
            organizationId:[null,[Validators.required]],
            position:[null,[Validators.required]],
            // status:[null,[Validators.required]],
        })
    }

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

    showEditModal(data,title,id){
        this.userId = id;
        this.groupList = data;
        this.isVisible = true;
        this.title = title;
        this.systemSer.getUser(this.userId).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.validateForm.patchValue(res.data);
                }
            }
        )
    }

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

    handleOk(){
        for(let i in this.validateForm.controls){
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
        this.validateForm.value.gender = Number(this.validateForm.value.gender);
        // this.validateForm.value.status = Number(this.validateForm.value.status);
        if(this.validateForm.invalid){
            return false;
        }
        this.isOkLoading = true;
        if(this.title == "新增用户"){
            this.create();
        }
        if(this.title == "编辑用户"){
            this.update();
        }
    }

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

    update(){
        this.validateForm.addControl('id',new FormControl(this.userId));
        this.systemSer.updateUser(this.validateForm.value).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.isVisible = false;
                    this.initForm();
                    this.done.emit();
                    this.message.success("更新用户成功");
                }else{
                    this.message.error(res.errMsg);
                }
                this.isOkLoading = false;
            }
        )
    }
}
