import {Component, OnInit, Output, EventEmitter, ViewChild} from '@angular/core';
import {EarlyWarningService} from '../../earlyWarning.service';
import {NzMessageService} from 'ng-zorro-antd';
import {WarningTrackInfoComponent} from '../warning-track-info/warning-track-info.component';
import {LocalStorageService} from 'ngx-webstorage';

@Component({
    selector: 'smart-notation',
    templateUrl: './notation.component.html',
    styles: []
})
export class NotationComponent implements OnInit {
    @ViewChild('smartWarningTrackInfo') smartWarningTrackInfo: WarningTrackInfoComponent;
    @Output() done = new EventEmitter();
    modal = {
        isVisible: false,
        title: '',
        isOkLoading: false,
        type: ''
    };
    tabNum = 0;
    msgId;
    msgObj;
    newestRemark;
    remarkPerson;  //批注人
    remarkTime;  //批注时间

    constructor(private earlySer: EarlyWarningService, private message: NzMessageService,
                private $localStorage: LocalStorageService) {
    }

    ngOnInit() {
        this.remarkPerson = this.$localStorage.retrieve('userInfo').userName;
    }

    showAddModal(item) {
        this.modal.isVisible = true;
        this.modal.type = 'add';
        this.msgId = item.id;
        this.smartWarningTrackInfo.outGet(item.id);
        this.getDetail();
    }

    showLookModal(item) {
        this.remarkTime = Date.now();
        this.modal.isVisible = true;
        this.modal.type = 'look';
        this.msgId = item.id;
        this.smartWarningTrackInfo.outGet(item.id);
        this.getDetail();
    }

    //详情
    getDetail() {
        this.earlySer.findWarningMsg(this.msgId).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.msgObj = res.data;
                }
            }
        );
    }

    //确定
    handleOk() {

    }

    //添加批注
    saveNotation() {
        if (!this.newestRemark) {
            this.message.warning('请输入批注');
            return;
        }
        this.modal.isOkLoading = true;
        const data = {
            newestRemark: this.newestRemark,
            id: this.msgId
        };
        this.earlySer.updateWarningMsg(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.done.emit();
                    this.message.success('添加批注成功');
                    this.handleCancel();
                    this.smartWarningTrackInfo.getList();
                }
            }
        );
    }

    //取消
    handleCancel() {
        this.modal.isOkLoading = false;
        this.modal.isVisible = false;
        this.newestRemark = '';
        this.tabNum = 0;
    }

    //切换tab
    tabsChange(e) {
        this.tabNum = e;
    }

}
