import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {SystemService} from '../../../system/system.service';
import {NzMessageService} from 'ng-zorro-antd';
import {opr_people_position} from '../../project.constants';
import {SelectPersonComponent} from '../../../modal/select-person/select-person.component';
import {ProjectService} from '../../project.service';

@Component({
    selector: 'smart-team-people',
    templateUrl: './team-people.component.html',
    styles: [
        `
            :host ::ng-deep .ant-input-disabled{
                color: rgba(0,0,0,.65);
                background-color: #fff;
            }
        `
    ]
})
export class TeamPeopleComponent implements OnInit {
    @ViewChild('smartSelectPerson') smartSelectPerson: SelectPersonComponent;
    @Output() done = new EventEmitter<any>();

    title;
    isVisible = false;
    isOkLoading = false;
    validateForm: FormGroup;
    userId;
    peoplePosition = opr_people_position;

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

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

    initForm() {
        this.validateForm = this.fb.group({
            userId: [null, [Validators.required]],
            userName: [null, [Validators.required]],
            teamId: [null, [Validators.required]],
            title: [null],
            gender: [null],
            phone: [null],
            principal: [null],
            arrivalDate: [null],
        });
    }

    showAddModal(title, teamId) {
        this.isVisible = true;
        this.validateForm.patchValue({'teamId': teamId});
        this.title = title;
    }

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

    handleOk() {
        console.log(this.validateForm.value);
        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;
        this.create();
    }

    create() {
        this.validateForm.patchValue({arrivalDate: new Date(this.validateForm.value.arrivalDate).getTime() });
        this.projectSer.addMaintainTeamUser(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isVisible = false;
                    this.initForm();
                    this.done.emit();
                    this.message.success('新增用户成功');
                }
                this.isOkLoading = false;
            }
        );
    }

    //选择人员
    showPeoplemodal() {
        this.smartSelectPerson.showModal('选择人员', null);
    }

    //设置人员
    setPeolpe(e) {
        const d = {
            userId: e[0].id,
            userName: e[0].name,
            gender: e[0].gender,
            phone: e[0].phoneNumber,
        };
        this.validateForm.patchValue(d);
        console.log(e);
    }
}
