import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {WorkService} from '../../work.service';

@Component({
    selector: 'smart-assets',
    templateUrl: './assets.component.html',
    styles: []
})
export class AssetsComponent implements OnInit {

    isVisible = false;
    title;
    validateForm:FormGroup;

    parentTypeList;
    typeList;

    constructor(private workSer:WorkService,private fb:FormBuilder) {
    }

    ngOnInit() {
        this.initForm();
    }

    initForm(){
        this.validateForm = this.fb.group({
            serialnoA:[null],
            invertoryname:[null],
            name:[null],
            hostid:[null],
            person:[null],
            ip:[null],
            equipmentTypeid:[null],
            inventorycount:[null],
            secondlevelTypeid:[null],
            stock:[null],
            usedcount:[null],
            lendcount:[null],
            mac:[null],
            repaircount:[null],
            maintenanceExpiration:[null],
            scrapcount:[null],
            storageLocation:[null],
            groupid:[null],
            inventoryTypeid:[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;
            }
        )
    }

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

    showEditModal(title) {
        this.title = title;
        this.isVisible = true;
        this.getParentType();
    }

    handEditleOk() {
        for(let i in this.validateForm.controls){
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
        if(this.validateForm.invalid){
            return false;
        }
        if(this.title == ""){
            this.create();
        }
        if(this.title == ""){
            this.update();
        }
    }

    create(){
        this.workSer.createInventory(this.validateForm.value).subscribe(
            (res)=>{
                this.isVisible = false;
            }
        )
    }

    update(){

    }

    handleEditCancel() {
        this.isVisible = false;
    }

}
