import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {WorkService} from '../../work.service';
import {NzMessageService} from 'ng-zorro-antd';
import {DatePipe} from '@angular/common';

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

    isVisible = false;
    title;
    parentId;
    validateForm: FormGroup;

    parentTypeList;
    typeList;

    invertoryId;     //资产Id

    constructor(private workSer: WorkService, private fb: FormBuilder, private datePipe: DatePipe,
                private message: NzMessageService) {
    }

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

    initForm() {
        this.invertoryId = null;
        this.validateForm = this.fb.group({
            serialnoA: [null],
            invertoryname: [null],
            name: [null, [Validators.required, Validators.maxLength(20)]],
            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, [Validators.required]],
        });
    }

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

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

    showAddModal(title, data) {
        this.parentId = data.parentId;
        this.getTypeByParent(this.parentId);
        this.validateForm.patchValue({inventoryTypeid: data.inventoryTypeid});
        this.title = title;
        this.isVisible = true;
    }

    showEditModal(title, id) {
        this.title = title;
        this.invertoryId = id;
        this.isVisible = true;
        this.workSer.selectByPrimaryKey(this.invertoryId).subscribe(
            (res) => {
                this.parentId = res.data.typeparentid;
                this.validateForm.patchValue(res.data);
                if (res.data.typeparentid) {
                    this.getTypeByParent(res.data.typeparentid);
                }
            }
        );
    }

    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) => {
                if (res.errCode == 10000) {
                    this.message.success('添加资产成功');
                    this.isVisible = false;
                    this.done.emit();
                    this.initForm();
                }
            }
        );
    }

    update() {
        this.validateForm.addControl('id', new FormControl(this.invertoryId));
        this.validateForm.patchValue({
            maintenanceExpiration: this.datePipe.transform(this.validateForm.value.maintenanceExpiration, 'yyyy-MM-dd HH:mm:ss')
        });
        this.workSer.updateInventory(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.success('编辑资产成功');
                    this.isVisible = false;
                    this.initForm();
                    this.done.emit();
                }
            }
        );
    }

    handleEditCancel() {
        this.initForm();
        this.isVisible = false;
    }

}
