import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {FormBuilder, FormGroup} 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;
    inventoryParentId;
    hostTypeList;

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

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

    initForm() {
        this.validateForm = this.fb.group({
            inventoryNo: [null], inventoryName: [null], secondlevelTypeId: [null],
            secondlevelType: [null], inventoryTypeId: [null], inventoryType: [null],
            amount: [null], contact: [null], name: [null],
            brand: [null], model: [null], serialNo: [null],
            versionNo: [null], price: [null], fundsSource: [null],
            purchaseUser: [null], storageNum: [null], storeroom: [null],
            storageLocation: [null], installLocation: [null], receiveUser: [null],
            receiveNum: [null], operatUser: [null], relateFacility: [null],
            buyDate: [null], warrantyDate: [null], price1: [null],
            belongObject: [null],
        });
    }

    //父级分类
    getParentType() {
        this.workSer.findByParentType().subscribe(
            (res) => {
                const data = res.data;
                this.parentTypeList = data;
            }
        );
    }

    //通过父级分类查询子级分类
    getTypeByParent(parentid) {
        this.workSer.findByParentidCount(parentid).subscribe(
            (res) => {
                const data = res.data;
                this.typeList = data;
            }
        );
    }

    //获取设备类型
    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.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;
        this.validateForm.value.secondlevelType = this.typeList.filter(e => e.id == this.validateForm.value.inventoryTypeId)[0].name;
        this.validateForm.patchValue({
            buyDate: new Date(this.validateForm.value.buyDate).getTime(),
            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;
    }
}
