import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {EmitService} from '../../../shared/event/eventEmitter';
import {OverAllService} from '../../../overAll/overAll.service';
import {NzMessageService} from 'ng-zorro-antd';
import {BusinessService} from '../../business.service';
import {SelectTriggerComponent} from '../../../modal/select-trigger/select-trigger.component';

@Component({
    selector: 'smart-business',
    templateUrl: './business.component.html',
    styles: []
})
export class BusinessComponent implements OnInit {
    @ViewChild('smartSelectTrigger') smartSelectTrigger:SelectTriggerComponent;
    @Output() add = new EventEmitter<any>();

    title;
    isVisiable = false;
    isOkLoading = false;
    validateForm: FormGroup;

    checked = false;  //选择SLA
    serviceId;
    triggerName;

    parentList;

    constructor(private fb: FormBuilder, private busineSer: BusinessService,
                private message: NzMessageService, private emitService: EmitService) {
        this.emitService.eventEmit.subscribe((value: any) => {
            if (value == 'add') {
                this.getServiceList();
            }
        });
    }

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

    changeModal() {
        if (this.checked) {
            this.validateForm.get('showsla').enable();
        } else {
            this.validateForm.get('showsla').disable();
        }
    }

    //初始化
    initForm() {
        this.checked = false;
        this.validateForm = this.fb.group({
            name: ['', [Validators.required]],
            algorithm: ['1'],
            showsla: ['99.9000'],
            sortorder: ['1', [Validators.maxLength(3)]],
            parentid: [null],
            userRequest: [""],
            triggerid:[null],
        });
        this.validateForm.get('showsla').disable();
    }

    //获取服务列表
    getServiceList() {
        const data = {
            serviceids: []
        };
        if(this.serviceId){
            data.serviceids.push(this.serviceId);
        }
        this.busineSer.findList(data).subscribe(
            (res) => {
                this.parentList = res.data;
            }
        );
    }

    showModal(title, item): void {
        this.title = title;
        this.isVisiable = true;
        if (item) {
            this.validateForm.patchValue({
                parentid: item.serviceid
            });
        }
    }

    showEditModal(title, item) {
        item.algorithm += '';
        item.showsla += '';
        item.sortorder += '';
        this.title = title;
        this.serviceId = item.serviceid;
        if (item.parent) {
            this.validateForm.patchValue({
                parentid: item.parent.serviceid
            });
        }
        if (item.serviceUserRequest) {
            this.validateForm.patchValue({
                userRequest: item.serviceUserRequest.userRequest
            });
        }
        if(item.trigger){
            this.triggerName = item.trigger.description;
            this.validateForm.patchValue({
                triggerid: item.trigger.triggerid
            });
        }
        this.getServiceList();
        this.isVisiable = true;
        this.validateForm.patchValue(item);
    }

    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.isOkLoading = true;

        if (this.title == '添加业务') {
            this.create();
        } else {
            this.update();
        }
    }

    create() {
        const data = {
            algorithm: Number(this.validateForm.value.algorithm),
            name: this.validateForm.value.name,
            sortorder: Number(this.validateForm.value.sortorder),
            showsla: this.checked ? this.validateForm.value.showsla : 0,
            parentid: Number(this.validateForm.value.parentid),
            userRequest: this.validateForm.value.userRequest,
            triggerid:this.validateForm.value.triggerid
        };
        this.busineSer.createService(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.success('添加成功');
                    this.isVisiable = false;
                    this.add.emit('add');
                    this.emitService.eventEmit.emit('add');
                    this.initForm();
                } else {
                    this.message.error(res.errMsg);
                }
                this.isOkLoading = false;
            }
        );
    }

    update() {
        const data = {
            algorithm: Number(this.validateForm.value.algorithm),
            name: this.validateForm.value.name,
            sortorder: Number(this.validateForm.value.sortorder),
            showsla: this.checked ? this.validateForm.value.showsla : 0,
            parentid: Number(this.validateForm.value.parentid),
            serviceid: this.serviceId,
            userRequest: this.validateForm.value.userRequest,
            triggerid:this.validateForm.value.triggerid
        };
        this.busineSer.updateService(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.success('修改成功');
                    this.isVisiable = false;
                    this.add.emit();
                    this.initForm();
                } else {
                    this.message.error(res.errMsg);
                }
                this.isOkLoading = false;
            }
        );
    }

    //选择阈值
    showTriggerModal(){
        this.smartSelectTrigger.showModal();
    }

    set(e){
        this.triggerName = e.description;
        this.validateForm.patchValue({
            triggerid:e.triggerid
        })
    }

    handleCancel(): void {
        this.isVisiable = false;
        this.initForm();
    }

}
