import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {FormBuilder, FormControl, FormGroup} from '@angular/forms';
import {array, selectType, validateFormArray} from '../../perfor.constants';
import {PerforManageService} from '../../perfor.manage.service';
import {NzMessageService} from 'ng-zorro-antd';
import {MonitorService} from '../../../monitor/monitor.service';

@Component({
    selector: 'smart-data-storage-modal',
    templateUrl: './data-storage-modal.component.html',
    styles: []
})
export class DataStorageModalComponent 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: '2015年数据存储量（G）', value: 'storageCapacity1', type: 'input', isRequire: false},
            {label: '2016年数据存储量（G）', value: 'storageCapacity2', type: 'input', isRequire: false},
            {label: '2017年数据存储量（G）', value: 'storageCapacity3', type: 'input', isRequire: false},
            {label: '2018年数据存储量（G）', value: 'storageCapacity18', type: 'input', isRequire: false},
            {label: '2019年数据存储量（G）', value: 'storageCapacity19', type: 'input', isRequire: false},
            {label: '2015年数据交换量（G）', value: 'exchangeCapacity1', type: 'input', isRequire: false},
            {label: '2016年数据交换量（G）', value: 'exchangeCapacity2', type: 'input', isRequire: false},
            {label: '2017年数据交换量（G）', value: 'exchangeCapacity3', type: 'input', isRequire: false},
            {label: '2018年数据交换量（G）', value: 'exchangeCapacity18', type: 'input', isRequire: false},
            {label: '2019年数据交换量（G）', value: 'exchangeCapacity19', type: 'input', isRequire: false},
        ];
        const fb = Object.assign(validateFormArray, {
            assetCount: [null],
            normalCount: [null],
            yearAssetcount: [null],
            maintenanceCount: [null],
            maintenanceUsetime: [null],
            storageCapacity1: [null],
            storageCapacity2: [null],
            storageCapacity3: [null],
            storageCapacity18: [null],
            storageCapacity19: [null],
            exchangeCapacity1: [null],
            exchangeCapacity2: [null],
            exchangeCapacity3: [null],
            exchangeCapacity18: [null],
            exchangeCapacity19: [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.selectBackgroundDataStorageCountsById(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.addBackgroundDataStorage(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.updateBackgroundDataStorage(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;
            }
        );
    }
}
