import {Component, OnInit, ViewChild} from '@angular/core';
import {OverviewService} from '../overview.service';
import {ModuleConfigComponent} from '../modal/module-config/module-config.component';
import {EarlyWarningService} from '../../early-warning/earlyWarning.service';
import {ProjectService} from '../../project-manager/project.service';
import {SystemChangeService} from '../../system-change/system-change.service';
import {WorkService} from '../../work/work.service';
import {Router} from '@angular/router';

@Component({
    selector: 'smart-my-follow',
    templateUrl: './my-follow.component.html',
    styles: [
            `
            :host ::ng-deep .ant-table-body {
                min-height: 380px;
            }

            .title {
                padding: 10px 15px;
                background: #fff;
                border-top: 5px solid #ddd;
                font-size: 14px;
                font-weight: bold;
            }

            .title span {
                font-size: 12px;
                color: #1a6ffb;
                float: right;
                cursor: pointer;
            }
        `
    ]
})
export class MyFollowComponent implements OnInit {
    @ViewChild('smartModuleConfig') smartModuleConfig: ModuleConfigComponent;

    page = {
        list: null,
        isLoading: false,
        lookModule: ''
    };
    table = {
        eventList: [],
        planList: [],
        childrenList: [],
        spareList: [],
        changeList: [],
        projectList: [],
        companyList: [],
        teamList: [],
        costList: [],
        resourceList: [],
        businessList: [],
        workList: [],
    };
    loading = {
        eventLoading: false,
        planLoading: false,
        childrenLoading: false,
        spareLoading: false,
        changeLoading: false,
        projectLoading: false,
        companyLoading: false,
        teamLoading: false,
        costLoading: false,
        resourceLoading: false,
        businessLoading: false,
        workLoading: false,
    };

    constructor(private overViewSer: OverviewService,
                private projectSer: ProjectService,
                private workSer: WorkService,
                private systemChangeSer: SystemChangeService,
                public router: Router,
                private earningSer: EarlyWarningService) {
    }

    ngOnInit() {
        this.getAllModule();
        this.getEventList();
        this.getPlanList();
        this.getAssetsList();
        this.getSpareList();
        this.getChangeList();
        this.getProjectList();
        this.getCompanyList();
        this.getTeamList();
        this.getCostList();
        this.getWorkList();
        this.getResourceList();
        this.getBusinessList();
    }

    //查询所有的工作模块
    getAllModule() {
        this.overViewSer.selectWorkModule(null).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.page.list = res.data;
                    res.data.forEach(e => {
                        if (e.status == 1) {
                            this.page.lookModule += e.moduleName;
                        }
                    });
                }
            }
        );
    }

    goMore(url) {
        this.router.navigate([`/app/main/${url}`]);
    }

    //运维事件
    getEventList() {
        this.loading.eventLoading = true;
        const obj = {
            pageCount: 1,
            pageNum: 1000,
        };
        this.workSer.find(obj).subscribe(
            (res) => {
                this.table.eventList = res.data.data;
                this.loading.eventLoading = false;
            }
        );
    }

    //巡检计划
    getPlanList() {
        this.loading.planLoading = true;
        const obj = {
            pageNum: 1,
            pageCount: 1000,
        };
        this.workSer.findPlanList(obj).subscribe(
            (res) => {
                this.table.planList = res.data.data;
                this.loading.planLoading = false;
            }
        );
    }

    //资产备件
    getAssetsList() {
        this.loading.childrenLoading = true;
        this.workSer.findByParentidCount('all').subscribe(
            (res) => {
                this.table.childrenList = res.data;
                this.loading.childrenLoading = false;
            }
        );
    }

    //备件管理
    getSpareList() {
        this.loading.spareLoading = true;
        const obj = {
            pageNum: 1,
            pageCount: 1000
        };
        this.workSer.selectComponentsList(obj).subscribe(
            (res) => {
                this.table.spareList = res.data.data;
                this.loading.spareLoading = false;
            }
        );
    }

    //系统变更
    getChangeList() {
        this.loading.changeLoading = true;
        const obj = {
            pageNum: 1,
            pageCount: 1000
        };
        this.systemChangeSer.sysChangePage(obj).subscribe(
            (res) => {
                this.table.changeList = res.data.data;
                this.loading.changeLoading = false;
            }
        );
    }

    //运维项目
    getProjectList() {
        this.loading.projectLoading = true;
        const obj = {
            pageNum: 1,
            pageCount: 1000
        };
        this.projectSer.selectMaintainProject(obj).subscribe(
            (res) => {
                this.table.projectList = res.data.data;
                this.loading.projectLoading = false;
            }
        );
    }

    //运维公司
    getCompanyList() {
        this.loading.companyLoading = true;
        const obj = {
            pageNum: 1,
            pageCount: 1000
        };
        this.projectSer.selectMaintainCompany(obj).subscribe(
            (res) => {
                this.table.companyList = res.data.data;
                this.loading.companyLoading = false;
            }
        );
    }

    //运维团队
    getTeamList() {
        this.loading.teamLoading = true;
        const obj = {
            pageNum: 1,
            pageCount: 1000
        };
        this.projectSer.selectMaintainTeam(obj).subscribe(
            (res) => {
                this.table.teamList = res.data.data;
                this.loading.teamLoading = false;
            }
        );
    }

    //运维费用
    getCostList() {
        this.loading.costLoading = true;
        const obj = {
            pageNum: 1,
            pageCount: 1000
        };
        this.projectSer.findPageCost(obj).subscribe(
            (res) => {
                this.table.costList = res.data.data;
                this.loading.costLoading = false;
            }
        );
    }

    //资源预警
    getResourceList() {
        this.loading.resourceLoading = true;
        const obj = {
            obj: {
                ruleType: '1',
            },
            pageNum: 1,
            pageCount: 1000
        };
        this.earningSer.pageWarningMsg(obj).subscribe(
            (res) => {
                this.table.resourceList = res.data.data;
                this.loading.resourceLoading = false;
            }
        );
    }

    //业务预警
    getBusinessList() {
        this.loading.businessLoading = true;
        const obj = {
            obj: {
                ruleType: '2',
            },
            pageNum: 1,
            pageCount: 1000
        };
        this.earningSer.pageWarningMsg(obj).subscribe(
            (res) => {
                this.table.businessList = res.data.data;
                this.loading.businessLoading = false;
            }
        );
    }

    //工作预警
    getWorkList() {
        this.loading.workLoading = true;
        const obj = {
            obj: {
                ruleType: '3',
            },
            pageNum: 1,
            pageCount: 1000
        };
        this.earningSer.pageWarningMsg(obj).subscribe(
            (res) => {
                this.table.workList = res.data.data;
                this.loading.workLoading = false;
            }
        );
    }

    showModal() {
        this.smartModuleConfig.showModal('关注项配置', this.page.list);
    }

}
