import {Component, OnInit, ViewChild} from '@angular/core';
import {CreateProjectComponent} from '../../project-manager/modal/create-project/create-project.component';
import {info_project_status} from '../info-project.constants';
import {pageSize} from '../../app.constants';
import {WorkService} from '../../work/work.service';
import {NzMessageService} from 'ng-zorro-antd';
import {InformationProjectService} from '../information-project.service';
import {Router} from '@angular/router';
import {CommonService} from '../../shared/common/common.service';
import {DatePipe} from '@angular/common';

@Component({
    selector: 'smart-service-personnel',
    templateUrl: './service-personnel.component.html',
    styles: []
})
export class ServicePersonnelComponent implements OnInit {
    @ViewChild('smartCreateProject') smartCreateProject: CreateProjectComponent;

    timeFormat = 'yyyy';

    statusList = [{label: '全部', value: null}, ...info_project_status];

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

    obj = {
        projectStatus: null,
        projectName: null,
        projectNumber: null,
    };

    constructor(private workSer: WorkService, private message: NzMessageService, private infoProSer: InformationProjectService,
                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.infoProSer.selectServiceUser(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();
    }

    //新增运维项目
    handleOpenModal() {
        this.smartCreateProject.showAddModal('新增运维项目');
    }

    //编辑运维项目
    handleEdit(data) {
        this.smartCreateProject.showEditModal(data, '编辑运维项目');
    }

    //删除
    handleDelete(item) {
        this.commonSer.confirmThing('删除', '确定删除?', () => {
            const data = {
                id: item.id
            };
            this.infoProSer.deleteServiceUser(data).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.getList();
                        this.message.success('删除成功');
                    }
                }
            );
        });
    }

}
