import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {SelectPersonComponent} from '../../../modal/select-person/select-person.component';
import {WorkService} from '../../work.service';
import {NzMessageService} from 'ng-zorro-antd';
import {SystemChangeService} from '../../../system-change/system-change.service';

@Component({
    selector: 'smart-transfor',
    templateUrl: './transfor.component.html',
    styles: [
            `
            .p-list > span {
                border: 1px solid #ddd;
                padding: 4px 5px;
                border-radius: 4px;
                margin-right: 5px;
            }

            .p-list > span > span {
                vertical-align: middle;
            }
        `
    ]
})
export class TransforComponent implements OnInit {
    @ViewChild('smartSelectPerson') smartSelectPerson: SelectPersonComponent;
    @Output() done = new EventEmitter<any>();

    title;
    transforId;
    isVisiable;
    transforList;

    constructor(private workSer: WorkService, private message: NzMessageService,
                private systemChangeSer: SystemChangeService) {
    }

    ngOnInit() {
    }

    //事件转派--start
    showTransforModal(title, id) {
        this.transforId = id;
        this.title = title;
        this.isVisiable = true;
    }

    //选择处理人弹窗
    selectPerson() {
        this.smartSelectPerson.showModal('选择转派人', null);
    }

    //选择转派人done
    getUser(e) {
        const arr = [];
        e.forEach(res => {
            const data = {
                username: res.name,
                userId: res.id
            };
            arr.push(data);
        });
        this.transforList = arr;
    }

    //取消
    handleCancel() {
        this.isVisiable = false;
        this.transforList = [];
    }

    deletePerson(i) {
        this.transforList.splice(i, 1);
    }

    //确定
    handleOk() {
        if (this.title == '事件转派') this.eventTranfer();
        if (this.title == '变更转派') this.changeTranfer();
    }

    //事件转派
    eventTranfer() {
        const data = {
            id: this.transforId,
            operators: this.transforList
        };
        this.workSer.transfer(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.success('事件转派成功');
                    this.isVisiable = false;
                    this.transforList = [];
                    this.done.emit();
                }
            }
        );
    }

    //变更转派
    changeTranfer() {
        if (this.transforList.length > 1 || this.transforList.length == 0) {
            this.message.error('只能转派给一个人');
            return;
        }
        const data = {
            id: this.transforId,
            manageUserId: this.transforList[0].userId,
            manageUserName: this.transforList[0].username,
            status: 6,
        };
        this.systemChangeSer.sysChangeTransfer(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.success('变更转派成功');
                    this.isVisiable = false;
                    this.transforList = [];
                    this.done.emit();
                }
            }
        );
    }
}
