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: [
        `            
           :host ::ng-deep.ant-btn-primary.disabled, .ant-btn-primary[disabled]{
                color: #fff;
                background-color: #1890ff;
                border-color: #1890ff;
            }
        `
    ]
})
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();
        this.getParentList();
    }

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

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

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

    showEditModal(title,id) {
        this.isVisible = true;
        this.title = title;
        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.initForm();
        this.isVisible = false;
    }

    handleOk() {
        if(this.isParent == 'primary'){
            this.validateForm.patchValue({
                parentid:null
            })
        }
        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("新增分类成功");
                }
            }
        )
    }

    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("修改分类成功");
                }
            }
        )
    }
}
