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

@Component({
    selector: 'smart-media-type',
    templateUrl: './media-type.component.html',
    styles: []
})
export class MediaTypeComponent implements OnInit {
    @Output() done = new EventEmitter<any>();
    isVisible = false;
    title;
    validateForm: FormGroup;

    mediatypeid;

    constructor(private fb: FormBuilder, private alarmSer: AlarmService,
                private message: NzMessageService) {
    }

    ngOnInit() {
        this.initForm();
    }

    initForm() {
        this.validateForm = this.fb.group({
            description: [null, [Validators.required]],
            type: ['0'],
            smtp_server: [null, [Validators.required]],
            smtp_helo: [null, [Validators.required]],
            smtp_email: [null, [Validators.email]],
            gsm_modem: [null],
            exec_path: [null],
            username: [null],
            passwd: [null],
            status: [0]
        });
    }

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

    //编辑
    showEditModal(item, title) {
        this.title = title;
        this.isVisible = true;
        this.mediatypeid = item.mediatypeid;
        this.alarmSer.getMediaType(this.mediatypeid).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    const data = res.data[0];
                    data.type += "";
                    this.validateForm.patchValue(data);
                }else{
                    this.message.info(res.errMsg);
                }
            }
        )
    }

    //change
    changeType(e) {
        switch (e) {
            case '0': {
                this.validateForm.get('smtp_server').setValidators(Validators.required);
                this.validateForm.get('smtp_helo').setValidators(Validators.required);
                this.validateForm.get('smtp_email').setValidators(Validators.email);

                this.validateForm.get('exec_path').clearValidators();
                this.validateForm.get('gsm_modem').clearValidators();
                this.validateForm.get('username').clearValidators();
                this.validateForm.get('passwd').clearValidators();
                break;
            }
            case '1': {
                this.validateForm.get('exec_path').setValidators(Validators.required);

                this.validateForm.get('smtp_server').clearValidators();
                this.validateForm.get('smtp_helo').clearValidators();
                this.validateForm.get('smtp_email').clearValidators();
                this.validateForm.get('gsm_modem').clearValidators();
                this.validateForm.get('username').clearValidators();
                this.validateForm.get('passwd').clearValidators();
                break;
            }
            case '2': {
                this.validateForm.get('gsm_modem').setValidators(Validators.required);

                this.validateForm.get('smtp_server').clearValidators();
                this.validateForm.get('smtp_helo').clearValidators();
                this.validateForm.get('smtp_email').clearValidators();
                this.validateForm.get('exec_path').clearValidators();
                this.validateForm.get('username').clearValidators();
                this.validateForm.get('passwd').clearValidators();
                break;
            }
            case '3':
            case '300': {
                this.validateForm.get('username').setValidators(Validators.required);
                this.validateForm.get('passwd').setValidators(Validators.required);

                this.validateForm.get('smtp_server').clearValidators();
                this.validateForm.get('smtp_helo').clearValidators();
                this.validateForm.get('smtp_email').clearValidators();
                this.validateForm.get('exec_path').clearValidators();
                this.validateForm.get('gsm_modem').clearValidators();
                break;
            }
        }
    }

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

    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.alarmSer.mediaTypeCreate(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isVisible = false;
                    this.initForm();
                    this.message.info('添加成功');
                    this.done.emit();
                }
            }
        );
    }

    update() {
        this.validateForm.addControl('mediatypeid',new FormControl(this.mediatypeid));
        this.alarmSer.mediaTypeUpdate(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isVisible = false;
                    this.initForm();
                    this.message.info('修改成功');
                    this.done.emit();
                }
            }
        );
    }

}
