import {Component, OnInit, ViewChild} from '@angular/core';
import {NotationComponent} from '../modal/notation/notation.component';
import {warning_leavel, warning_time} from '../early.constants';
import {pageSize} from '../../app.constants';
import {WorkService} from '../../work/work.service';
import {NzMessageService} from 'ng-zorro-antd';
import {EarlyWarningService} from '../earlyWarning.service';
import {Router} from '@angular/router';
import {CommonService} from '../../shared/common/common.service';
import {DatePipe} from '@angular/common';

@Component({
    selector: 'smart-work-warning',
    templateUrl: './work-warning.component.html',
    styles: []
})
export class WorkWarningComponent implements OnInit {

    @ViewChild('smartNotation') smartNotation: NotationComponent;
    warning_leavel = [{label: '全部', value: null}, ...warning_leavel];
    warning_time = [{label: '全部', value: null}, ...warning_time];

    page = {
        list: [],
        isLoading: false,
        pageCount: pageSize,
        pageNum: 1,
        totalNum: null
    };
    time = {
        startTime: null,
        endTime: null,
        timeType: null
    };

    obj = {
        ruleType: '3', //1：资源预警；2：业务预警；3：工作预警；
        ruleLevel: null,
        keyword: '',
        startDate: '',
        endDate: '',
    };

    constructor(private workSer: WorkService, private message: NzMessageService, private earningSer: EarlyWarningService,
                private router: Router, private commonSer: CommonService, private datePipe: DatePipe) {
    }

    ngOnInit() {
        this.getList();
    }

    //获取列表
    getList() {
        this.page.isLoading = true;
        const obj = {
            obj: this.obj,
            pageNum: this.page.pageNum,
            pageCount: this.page.pageCount
        };
        this.earningSer.pageWarningMsg(obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.page.list = res.data.data;
                    this.page.totalNum = res.data.totalNum;
                    this.page.isLoading = false;
                }
            }
        );
    }

    //翻页
    change(e) {
        if (e > 0) {
            this.page.pageNum = e;
            this.getList();
        }
    }

    //搜索
    search() {
        this.page.pageNum = 1;
        this.getList();
    }

    //时间改变
    changeTimeType(e) {
        this.time.timeType = e;
        console.log(this.time.timeType,e)
        if (e == '99') return false;
        const obj = this.commonSer.getTimeByType(e);
        this.obj.startDate = obj.startTime;
        this.obj.endDate = obj.endTime;
        this.page.pageNum = 1;
        this.getList();
    }

    //查询
    searchValue() {
        if (this.time.timeType == '99') {
            this.obj.startDate = this.datePipe.transform(this.time.startTime, 'yyyy-MM-dd HH:mm:ss');
            this.obj.endDate = this.datePipe.transform(this.time.endTime, 'yyyy-MM-dd HH:mm:ss');
        }
        this.getList();
    }

    //新增运维公司
    handleEdit(item) {
        this.smartNotation.showAddModal(item);
    }

    handleLook(item) {
        this.smartNotation.showLookModal(item);
    }


}
