import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {TrackInfoComponent} from '../../../project-manager/modal/track-info/track-info.component';
import {WorkService} from '../../work.service';
import {OverAllService} from '../../../overAll/overAll.service';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
    selector: 'smart-spare-parts',
    templateUrl: './spare-parts.component.html',
    styles: []
})
export class SparePartsComponent implements OnInit {
    @ViewChild('smartTrackInfo') smartTrackInfo: TrackInfoComponent;
    @Output() done = new EventEmitter<any>();

    modal = {
        isVisible: false,
        title: '',
        isOkLoading: false,
        tabNum: 0,
        id: null,
    };
    validateForm: FormGroup;
    parentTypeList = [];
    typeList = [];
    hostTypeList = [];

    constructor(private fb: FormBuilder, private workSer: WorkService,
                private overAllSer: OverAllService,
                private message: NzMessageService) {
    }

    ngOnInit() {
        this.initForm();
        this.getTree();
    }

    initForm() {
        this.validateForm = this.fb.group({
            inventoryNo: [""], inventoryName: [""],
            inventoryTypeId: [""], inventoryType: [""],
            amount: [""], contact: [""], name: [""],
            brand: [""], model: [""], serialNo: [""],
            versionNo: [""], price: [""], fundsSource: [""],
            purchaseUser: [""], storageNum: [""], storeroom: [""],
            storageLocation: [""], installLocation: [""], receiveUser: [""],
            receiveNum: [""], operatUser: [""], relateFacility: [""],
            buyDate: [""], warrantyDate: [""], unitPrice: [""],
            belongObject: [""],
            belongObjectId: [""],
            id: [""],
        });
        this.modal.tabNum = 0;
    }

    //获取设备类型
    getTree() {
        this.overAllSer.findTree().subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.hostTypeList = res.data;
                }
            }
        );
    }

    showAddModal(title) {
        this.modal.title = title;
        this.modal.isVisible = true;
    }

    showEditModa(title, id) {
        this.modal.title = title;
        this.modal.id = id;
        this.modal.isVisible = true;
        this.smartTrackInfo.diffResource(id);
        this.getDetail();
    }

    //查询登记详情
    getDetail() {
        this.workSer.selectComponents({id: this.modal.id}).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.validateForm.patchValue(res.data);
                }
            }
        );
    }

    handleCancel() {
        this.modal.isVisible = false;
        this.modal.isOkLoading = false;
        this.initForm();
    }

    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.validateForm.value.buyDate) this.validateForm.patchValue({buyDate: new Date(this.validateForm.value.buyDate).getTime()});
        if (this.validateForm.value.warrantyDate) this.validateForm.patchValue({warrantyDate: new Date(this.validateForm.value.warrantyDate).getTime()});

        if (this.modal.title.includes('入库登记')) this.create();
        if (this.modal.title.includes('编辑信息')) this.update();
    }

    create() {
        this.workSer.insertComponents(this.validateForm.value).subscribe(
            (res) => {
                this.modal.isOkLoading = false;
                if (res.errCode == 10000) {
                    this.initForm();
                    this.done.emit();
                    this.modal.isVisible = false;
                    this.message.success('登记成功');
                }
            }
        );
    }

    update() {
        this.validateForm.patchValue(
            {id: this.modal.id}
        );
        this.workSer.updateComponents(this.validateForm.value).subscribe(
            (res) => {
                this.modal.isOkLoading = false;
                if (res.errCode == 10000) {
                    this.initForm();
                    this.done.emit();
                    this.modal.isVisible = false;
                    this.message.success('信息更新成功');
                }
            }
        );
    }

    tabsChange(e) {
        this.modal.tabNum = e;
    }
}
