import {Component, ElementRef, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {ProjectService} from '../../project.service';
import {TeamPeopleComponent} from '../team-people/team-people.component';
import {NzMessageService} from 'ng-zorro-antd';
import {CommonService} from '../../../shared/common/common.service';

@Component({
    selector: 'smart-team-modal',
    templateUrl: './team-modal.component.html',
    styles: []
})
export class TeamModalComponent implements OnInit {
    @ViewChild('smartTeamPeople') smartTeamPeople: TeamPeopleComponent;
    @ViewChild('modalFooter') modalFooter: ElementRef;
    @Output() done = new EventEmitter<any>();

    isVisiable = false;
    title;

    delete = {
        isVisiable: false,
        isOkLoading: false,
        leaveDate: ''
    };
    obj = {
        teamId: '',
        userName: ''  //
    };

    page = {
        list: [],
        isLoading: false,
        totalNum: 0,
        pageNum: 1,
        pageCount: 5,
    };

    inSelect;
    showModalFooter;

    constructor(private projectSer: ProjectService, private commoneSer: CommonService,
                private message: NzMessageService) {
    }

    ngOnInit() {
    }

    showModal(data, title) {
        this.isVisiable = true;
        this.obj.teamId = data.id;
        this.title = title;
        this.showModalFooter = this.title == '团队成员' ? null : this.modalFooter;
        this.getList();
    }

    getList() {
        this.page.isLoading = true;
        const obj = {
            obj: this.obj,
            pageNum: this.page.pageNum,
            pageCount: this.page.pageCount
        };
        this.projectSer.selectMaintainTeamUser(obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.page.list = res.data.data;
                    this.page.totalNum = res.data.totalNum;
                    this.page.isLoading = false;
                }
            }
        );
    }

    PageDataChange(e) {

    }

    changePage(e) {
        this.page.pageNum = e;
        this.getList();
    }

    selectItem(item, e) {
        if (e) {
            const arr = this.page.list.map(data => {
                return data.id;
            });
            const old = this.inSelect;
            if (old) {
                const index = arr.indexOf(old.id);
                this.page.list[index].checked = false;
            }
            this.inSelect = item;
        } else {
            this.inSelect = null;
        }
    }

    handleOk() {
        this.isVisiable = false;
        this.done.emit(this.inSelect);
    }

    handleCancel() {
        this.isVisiable = false;
        this.done.emit();
    }

    showAddModal() {
        this.smartTeamPeople.showAddModal('新增团队成员', this.obj.teamId);
    }

    showDeleteModal() {
        if (!this.inSelect) {
            this.message.info('请选择人员');
            return;
        }
        this.delete.isVisiable = true;
    }

    //删除团队成员
    deleteTaamPeople() {
        this.commoneSer.confirmThing('删除', `确定移除${this.inSelect.userName}`, () => {
            const data = {
                id: this.inSelect.id
            };
            this.projectSer.deleteMaintainTeamUser(data).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.delete.leaveDate = null;
                        this.delete.isVisiable = false;
                        this.getList();
                    }
                }
            );
        });
    }

}
