import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {OverAllService} from '../../overAll/overAll.service';
import { DatePipe } from '@angular/common';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
    selector: 'smart-pause',
    templateUrl: './pause.component.html',
    styles: []
})
export class PauseComponent implements OnInit {
    @Output() done = new EventEmitter<any>();

    modalTitle = '临时暂停(服务器时间)';
    typeTitle;
    isShow = false;
    isOkLoading = false;
    data = {
        startTime: null,
        time: ''
    };
    id;  //id
    constructor(public overAllSer: OverAllService,public datePipe:DatePipe,
                public message:NzMessageService) {
    }

    ngOnInit() {
    }

    initForm(){
        this.data = {
            startTime: null,
            time: ''
        };
    }

    showModal(id,type) {
        this.id = id;
        this.typeTitle = type;
        this.isShow = true;
    }

    handleCancel() {
        this.isShow = false;
        this.isOkLoading = false;
    }

    handleOk() {
        this.isOkLoading = true;
        if(this.typeTitle == '基础资源'){
            this.basic();
        }
        if(this.typeTitle == '网站监测'){
            this.web();
        }
    }

    basic(){
        const data = {
            hostId: Number(this.id),
            startTime: this.datePipe.transform(this.data.startTime,'yyyy-MM-dd HH:mm:ss'),
            time: Number(this.data.time)
        };
        this.overAllSer.tempStop(data).subscribe(
            (res) => {
                if(res.errCode == 10000){
                    this.isShow = false;
                    this.initForm();
                    this.done.emit();
                    this.message.success("临时暂停成功");
                }
                this.isOkLoading = false;
            }
        );
    }

    web(){
        const data = {
            httptestid: Number(this.id),
            startTime: this.datePipe.transform(this.data.startTime,'yyyy-MM-dd HH:MM:ss'),
            time: Number(this.data.time)
        };
        this.overAllSer.tempStopWebscenario(data).subscribe(
            (res) => {
                if(res.errCode == 10000){
                    this.isShow = false;
                    this.done.emit();
                    this.message.success("临时暂停成功");
                }
                this.isOkLoading = false;
            }
        );
    }

}
