import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {SystemChangeService} from '../../system-change.service';
import {TrackInfoComponent} from '../../../project-manager/modal/track-info/track-info.component';
import {FormControl} from '@angular/forms';
import {NzMessageService} from 'ng-zorro-antd';
import {TransforComponent} from '../../../work/modal/transfor/transfor.component';
import {WorkService} from '../../../work/work.service';
import {CommonService} from '../../../shared/common/common.service';

@Component({
    selector: 'smart-change-verity',
    templateUrl: './change-verity.component.html',
    styles: [
            `
            .changeStatus {
                padding: 0 15px 15px;
                font-size: 20px;
            }

            .changeStatus > span:first-child {
                border-right: 1px solid #dddddd;
            }

            .changeStatus > span {
                padding: 0 15px;
            }

            .changeStatus > span > span {
                font-weight: bold;
            }
        `
    ]
})
export class ChangeVerityComponent implements OnInit {
    @ViewChild('smartTrackInfo') smartTrackInfo: TrackInfoComponent;
    @ViewChild('smartTransfor') smartTransfor: TransforComponent;
    @Output() done = new EventEmitter<any>();

    modal = {
        isVisible: false,
        title: '',
        isOkLoading: false
    };

    checkModal = {
        isVisible: false,
        checkOpinion: '',
        title: '审核意见',
        status: null,
        isOkLoading: false
    };

    dealObj = {
        changeResult: '',
        finishTime: null,
        fileId: ''
    };

    id;

    changeObj;
    msgList;

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

    ngOnInit() {

    }

    showModal(id, title) {
        this.modal.title = title;
        this.id = id;
        this.modal.isVisible = true;
        this.getDetail();
        this.getMsg();
    }

    //查询变更详情
    getDetail() {
        this.sysChangeSer.find(this.id).subscribe(
            (res) => {
                this.changeObj = res.data;
            }
        );
    }

    //查询变更跟踪信息
    getMsg() {
        this.sysChangeSer.findMsg(this.id).subscribe(
            (res) => {
                this.msgList = res.data;
            }
        );
    }

    //转派
    showTransforModal() {
        this.smartTransfor.showTransforModal('变更转派', this.id);
    }

    //打开checkmodal
    openCheckModal(status) {
        this.checkModal.isVisible = true;
        this.checkModal.status = status;
    }

    //审核确定
    handleOk() {
        this.checkModal.isOkLoading = true;
        const d = {
            id: this.id,
            status: this.checkModal.status,
            checkOpinion: this.checkModal.checkOpinion
        };
        this.systemChangeSer.sysChangeCheck(d).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.modal.isVisible = false;
                    this.done.emit();
                    this.checkModal.isVisible = false;
                    this.message.success('审核成功');
                }
                this.checkModal.isOkLoading = false;
            }
        );
    }

    //变更处理
    dealChange(status) {
        if (status == 7 && !this.dealObj.changeResult && !this.dealObj.finishTime) {
            this.message.info('请选择完成情况和完成时间');
            return;
        }
        this.checkModal.isOkLoading = true;
        const d = {
            id: this.id,
            status: status,
            changeResult: this.dealObj.changeResult,
            finishTime: new Date(this.dealObj.finishTime).getTime()
        };
        this.systemChangeSer.sysChangeFinish(d).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.modal.isVisible = false;
                    this.done.emit();
                    this.message.success('变更成功');
                }
                this.checkModal.isOkLoading = false;
            }
        );
    }

    //取消
    handleCancle() {
        this.done.emit();
        this.modal.isVisible = false;
    }

    //附件下载
    downLoad(file) {
        this.workSer.sysFileDownload(file.id).subscribe(
            (data) => {
                this.commonSer.downloadFile(file.name, data);
            }
        );
    }

}
