import {Component, OnInit, ViewChild} from '@angular/core';
import {opr_company_type, project_status} from '../project.constants';
import {pageSize} from '../../app.constants';
import {WorkService} from '../../work/work.service';
import {NzMessageService} from 'ng-zorro-antd';
import {ProjectService} from '../project.service';
import {Router} from '@angular/router';
import {CommonService} from '../../shared/common/common.service';
import {DatePipe} from '@angular/common';
import {CreateCompanyComponent} from '../modal/create-company/create-company.component';
import {LocalStorageService} from 'ngx-webstorage';
import {LookCompanyComponent} from '../modal/look-company/look-company.component';

@Component({
    selector: 'app-opr-company',
    templateUrl: './opr-company.component.html',
    styleUrls: ['./opr-company.component.css']
})
export class OprCompanyComponent implements OnInit {

    @ViewChild('smartCreateCompany') smartCreateCompany: CreateCompanyComponent;
    @ViewChild('smartLookCompany') smartLookCompany: LookCompanyComponent;
    companyType = [{label: '全部', value: null}, ...opr_company_type];

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

    obj = {
        name: '',
        type: null
    };
    userId;

    constructor(private workSer: WorkService, private message: NzMessageService, private projectSer: ProjectService,
                private $localStorage: LocalStorageService,
                private router: Router, private commonSer: CommonService, private datePipe: DatePipe) {
    }

    ngOnInit() {
        this.userId = this.$localStorage.retrieve('userInfo').userId;
        this.getList();
    }

    //获取列表
    getList() {
        this.page.isLoading = true;
        const obj = {
            obj: this.obj,
            pageNum: this.page.pageNum,
            pageCount: this.page.pageCount
        };
        this.projectSer.selectMaintainCompany(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();
    }

    //新增运维公司
    handleAddModal() {
        this.smartCreateCompany.showAddModal('新增运维公司');
    }

    handleEditModa(item) {
        this.smartCreateCompany.showEditModal(item, '编辑运维公司');
    }

    //查看
    handleLook(item){
        this.smartLookCompany.showModal(item);
    }

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

}
