import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {PerforManageService} from '../../perfor.manage.service';
import {CommonService} from '../../../shared/common/common.service';
import {NzMessageService} from 'ng-zorro-antd';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {Asset_Location, asset_type, data_type} from '../../../infomationResource/information.constans';
import {MonitorService} from '../../../monitor/monitor.service';
import {array, selectType, validateFormArray} from '../../perfor.constants';

@Component({
    selector: 'smart-admin-office-modal',
    templateUrl: './admin-office-modal.component.html',
    styles: []
})
export class AdminOfficeModalComponent implements OnInit {
    @Output() done = new EventEmitter<any>();

    modal = {
        isVisiable: false,
        title: '',
        id: '',
        isOkLoading: false,
    };

    validateForm: FormGroup;
    controlArray = [];
    selectType = selectType;

    constructor(private fb: FormBuilder, private perforSer: PerforManageService,
                private message: NzMessageService,
                private monitorSer: MonitorService) {
    }

    ngOnInit() {
        this.initForm();
    }

    //字段初始化
    initForm() {
        this.controlArray = [...array,
            {label: '项目运维费中的核心设备总数（件）', value: 'assetCount', type: 'input', isRequire: false},
            {label: '处于正常使用的核心设备数（件）', value: 'normalCount', type: 'input', isRequire: false},
            {label: '使用8年及8年以上的核心设备数（件）', value: 'yearAssetcount', type: 'input', isRequire: false},
            {label: '核心设备维修的次数（次）', value: 'maintenanceCount', type: 'input', isRequire: false},
            {label: '核心设备年均使用时长（小时/件）', value: 'maintenanceUsetime', type: 'input', isRequire: false},
            {label: '系统用户登录次数（次）', value: 'userLogincount', type: 'input', isRequire: false},
            {label: '系统用户数（个）', value: 'userCount', type: 'input', isRequire: false},
            {label: '行政业务办件量（件）', value: 'vusinessVolume', type: 'input', isRequire: false},
        ];
        const fb = Object.assign(validateFormArray, {
            assetCount: [null],
            normalCount: [null],
            yearAssetcount: [null],
            maintenanceCount: [null],
            maintenanceUsetime: [null],
            userLogincount: [null],
            userCount: [null],
            vusinessVolume: [null],
        });
        console.log(fb);
        this.validateForm = this.fb.group(fb);
    }

    //新增
    showAddModal(title) {
        this.modal.isVisiable = true;
        this.modal.title = title;
    }

    //编辑
    showEditModal(title, item) {
        this.modal.isVisiable = true;
        this.modal.title = title;
        this.modal.id = item.id;
        this.validateForm.patchValue(item);
    }

    //取消
    handleCancel() {
        this.modal.isVisiable = false;
        this.modal.isOkLoading = false;
        this.initForm();
    }

    //查询详情
    getDetail() {
        const data = {
            id:this.modal.id
        }
        this.perforSer.selectOfficeServiceCountsById(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.validateForm.patchValue(res.data);
                }
            }
        );
    }

    //判断
    handleOk() {
        for (let i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
        if (this.validateForm.invalid) {
            return false;
        }
        this.modal.isOkLoading = true;
        if (this.modal.title.includes('新增')) this.create();
        if (this.modal.title.includes('编辑')) this.update();
    }

    //新增
    create() {
        console.log(this.validateForm.value);
        this.perforSer.addOfficeService(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.initForm();
                    this.done.emit();
                    this.modal.isVisiable = false;
                    this.message.success('新增成功');
                }
                this.modal.isOkLoading = false;
            }
        );
    }

    //更新
    update() {
        this.validateForm.addControl('id', new FormControl(this.modal.id));
        this.perforSer.updateOfficeService(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.initForm();
                    this.done.emit();
                    this.modal.isVisiable = false;
                    this.message.success('更新成功');
                }
                this.modal.isOkLoading = false;
            }
        );
    }
}
