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

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

    title;
    isVisible;
    parentList;
    isChildren = 'primary';
    isParent = 'default';
    validateForm:FormGroup;

    updateId;

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

    checkChange(e){
        if(e == 'parent'){
            this.isParent = 'primary';
            this.isChildren = 'default';
            this.validateForm.get("parentid").clearValidators();
        }
        if(e == "children"){
            this.isParent = 'default';
            this.isChildren = 'primary';
            this.validateForm.get('parentid').setValidators(Validators.required);
        }
    }

    ngOnInit() {
        this.initForm();
    }

    getParentList(){
        this.workSer.findByParentType().subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.parentList = res.data;
                }else{
                    this.message.error(res.errMsg);
                }
            }
        )
    }

    initForm(){
        this.isParent = 'primary';
        this.isChildren = 'default';
        this.validateForm = this.fb.group(
            {
                parentid:[null],
                name:[null,[Validators.required]],
                typeno:[null,[Validators.required]],
                tyoeDescribe:[null],
            }
        )
    }

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

    showEditModal(title,id) {
        this.isVisible = true;
        this.title = title;
        this.getParentList();
        this.updateId=id;
        this.workSer.findType(id).subscribe(
            (res)=>{
                if(res.data.parentid){
                    this.isParent = 'default';
                    this.isChildren = 'primary';
                }
                this.validateForm.patchValue(res.data);
            }
        )
    }

    handleCancel() {
        this.isVisible = false;
    }

    handleOk() {
        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.createType(this.validateForm.value).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.isVisible = false;
                    this.done.emit();
                    this.initForm();
                    this.message.success("新增分类成功");
                }else{
                    this.message.error(res.errMsg);
                }
            }
        )
    }

    update(){
        this.validateForm.addControl("id",new FormControl(this.updateId));
        this.workSer.updateType(this.validateForm.value).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.isVisible = false;
                    this.done.emit();
                    this.initForm();
                    this.message.success("修改分类成功");
                }else{
                    this.message.error(res.errMsg);
                }
            }
        )
    }
}
