import {Component, OnInit} from '@angular/core';
import {SystemService} from '../system.service';
import {pageSize} from '../../app.constants';
import {LayoutService} from '../../layouts/layout.service';
import {DatePipe} from '@angular/common';
import {OverAllService} from '../../overAll/overAll.service';

@Component({
    selector: 'jhi-audit',
    templateUrl: './audit.component.html',
    styles: []
})
export class AuditComponent implements OnInit {
    timeFormat = 'yyyy-MM-dd';
    dataSet: any[];
    timeType = '1';
    pageSize = pageSize;
    pageNumber = 1;

    menuList = [];
    groupList = [];
    startTime;
    endTime;

    totalNum;

    obj = {
        end: null,
        start: null,
        content: null,
        moduleId: null
    };

    constructor(private systemSer: SystemService, private layoutSer: LayoutService,
                private datePipe: DatePipe, private overAllSer: OverAllService) {
        const today = new Date();
        this.obj.start = new Date(this.datePipe.transform(today, 'yyyy-MM-dd') + ' 00:00:00');
        this.obj.end = new Date(this.datePipe.transform(today, 'yyyy-MM-dd') + ' 23:59:59');
    }

    ngOnInit() {
        this.getList();
        this.getMenu();
    }

    //菜单栏
    getMenu() {
        this.layoutSer.getAllMenu().subscribe(
            (res) => {
                this.menuList = res.data;
            }
        );
    }

    getList() {
        const data = {
            pageSize: this.pageSize,
            pageNum: this.pageNumber,
            end: this.obj.end = this.obj.end == '' ? null : this.obj.end,
            start: this.obj.start = this.obj.start == '' ? null : this.obj.start,
            content: this.obj.content = this.obj.content == '' ? null : this.obj.content,
            moduleId: this.obj.moduleId = this.obj.moduleId == '' ? null : this.obj.moduleId
        };
        this.systemSer.logList(data).subscribe(
            (res) => {
                this.dataSet = res.data.data;
                this.totalNum = res.data.totalCount;
            }
        );
    }

    searchKey() {
        this.pageNumber = 1;
        this.search();
    }

    changePage(e) {
        if (e > 0) {
            this.pageNumber = e;
            this.getList();
        }
    }

    //时间改变
    changeType() {
        const nowDate = new Date().getTime();
        let day1, day2;
        switch (this.timeType) {
            case'1': {
                this.obj.start = new Date(this.datePipe.transform(nowDate, 'yyyy-MM-dd') + ' 00:00:00');
                this.obj.end = new Date(this.datePipe.transform(nowDate, 'yyyy-MM-dd') + ' 23:59:59');
                break;
            }
            case'2': {
                day1 = nowDate - 1 * 24 * 60 * 60 * 1000;
                this.obj.start = new Date(this.datePipe.transform(day1, 'yyyy-MM-dd') + ' 00:00:00');
                this.obj.end = new Date(this.datePipe.transform(day1, 'yyyy-MM-dd') + ' 23:59:59');
                break;
            }
            case'99': {
                break;
            }
        }
    }

    //查询
    search() {
        if (this.timeType == '99') {
            this.obj.start = new Date(this.datePipe.transform(this.startTime, 'yyyy-MM-dd') + ' 00:00:00');
            this.obj.end = new Date(this.datePipe.transform(this.endTime, 'yyyy-MM-dd') + ' 23:59:59');
        }
        this.getList();
    }

}
