import {Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
import {SendLogComponent} from '../../../alarm/modal/send-log/send-log.component';
import {pageSize} from '../../../app.constants';
import {AlarmService} from '../../../alarm/alarm.service';
import {NzMessageService} from 'ng-zorro-antd';
import {ProjectService} from '../../project.service';
import {WorkService} from '../../../work/work.service';
import {SystemChangeService} from '../../../system-change/system-change.service';
import {CreateTrackInfoComponent} from '../create-track-info/create-track-info.component';

@Component({
    selector: 'smart-track-info',
    templateUrl: './track-info.component.html',
    styles: []
})
export class TrackInfoComponent implements OnInit {
    @Input() maintainType;
    @Input() sysChangeId;
    @Input() componentId;
    @Input() title;
    @ViewChild('smartCreateTrackInfo') smartCreateTrackInfo: CreateTrackInfoComponent;
    @Output() total = new EventEmitter<any>();

    warnList = [];

    page = {
        pageNum: 1,
        pageCount: 1000,
        totalNum: 0,
        loading: false,
    };

    constructor(public alarmSer: AlarmService, public message: NzMessageService,
                private projectSer: ProjectService,
                private workSer: WorkService,
                private systemSer: SystemChangeService) {
    }

    ngOnInit() {
    }

    outGet() {
        this.page.pageNum = 1;
        if (this.title.includes('跟踪信息')) this.getList();
        if (this.title.includes('配件盘点')) this.getInvoList();
    }

    diffResource(id: any) {
        if (this.title.includes('跟踪信息')) {
            this.componentId = id;
            this.getList();
        }
        if (this.title.includes('配件盘点')) {
            this.componentId = id;
            this.getInvoList();
        }
        if (this.title.includes('变更跟踪信息')) this.getChangeList();
    }

    //跟踪信息
    getList() {
        this.page.loading = true;
        const data = {
            pageNum: this.page.pageNum,
            pageCount: this.page.pageCount,
            obj: {
                maintainType: this.maintainType,
                maintainId: this.componentId
            }
        };
        this.projectSer.selectMaintainProjectTrackMsg(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    if (res.data) {
                        this.warnList = res.data.data;
                        this.page.totalNum = res.data.totalNum;
                    }
                }
                this.page.loading = false;
            }
        );
    }

    //配件盘点
    getInvoList() {
        this.page.loading = true;
        const data = {
            pageNum: this.page.pageNum,
            pageCount: this.page.pageCount,
            obj: {
                componentId: this.componentId
            }
        };
        this.workSer.selectTrackMsg(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    if (res.data) {
                        this.warnList = res.data.data;
                        this.page.totalNum = res.data.totalNum;
                    }
                }
                this.page.loading = false;
            }
        );
    }

    //变更跟踪信息
    getChangeList() {
        this.page.loading = true;
        this.systemSer.findMsg(this.sysChangeId).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    if (res.data) {
                        this.warnList = res.data;
                        // this.page.totalNum = res.data.totalNum;
                    }
                }
                this.page.loading = false;
            }
        );
    }

    //添加跟踪信息
    handleAddTrack() {
        this.smartCreateTrackInfo.showAddModal(`添加${this.title}`, this.componentId, this.maintainType);
    }

    //翻页
    change(e) {
        if (e > 0) {
            this.page.pageNum = e;
            if (this.title.includes('跟踪信息')) this.getList();
            if (this.title.includes('配件盘点')) this.getInvoList();
            if (this.title.includes('变更跟踪信息')) this.getChangeList();
        }
    }
}
