import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {OverAllService} from '../../overAll/overAll.service';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
    selector: 'smart-website',
    templateUrl: './website.component.html',
    styles: []
})
export class WebsiteComponent implements OnInit {

    @Output() done = new EventEmitter<any>();
    isVisible = false;
    title;
    tabNum = 0;
    indexNo = 1;

    validateForm;
    steps = [];

    constructor(private overAllSer:OverAllService,private message:NzMessageService) {
    }

    ngOnInit() {
        this.initForm();
    }

    initForm() {
        this.tabNum = 0;
        this.steps = [
            {
                name:'',
                url:'',
                status_codes:200,
                posts:'',
                no:this.indexNo,
                timeout:'15',
                follow_redirects:'1',
                condition:'',
                faultCondition:'',
            }
        ];
        this.validateForm = {
            name: '',
            delay:'',
            retries:'',
            http_proxy:'',
            ssl_cert_file:'',
            ssl_key_file:'',
            ssl_key_password:'',
            steps: this.steps,
        };
    }

    showAddModal() {
        this.title = '添加网站监测';
        this.isVisible = true;
    }

    showEditModal(id) {
        this.title = '编辑网站监测';
        this.isVisible = true;
        this.overAllSer.findWeb(id).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.validateForm = res.data;
                    this.steps = res.data.steps;
                }
            }
        )
    }

    handleOk() {
        if(!this.validateForm.name){
            this.message.error("请输入网站名称");
            return false;
        }
        if(!this.validateForm.delay){
            this.message.error("请输入更新间隔");
            return false;
        }
        if(this.title == '添加网站监测'){
            this.create();
        }else{
            this.update();
        }
    }

    //创建
    create(){
        const data = {};
        this.overAllSer.createWebscenario(this.validateForm).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.message.success('添加成功');
                    this.isVisible = false;
                    this.done.emit();
                    this.initForm();
                }else{
                    this.message.error(res.errMsg);
                }
            }
        )
    }

    //更新
    update(){
        this.overAllSer.updateWebscenario(this.validateForm).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.message.success('保存成功');
                    this.isVisible = false;
                    this.done.emit();
                    this.initForm();
                }else{
                    this.message.error(res.errMsg);
                }
            }
        )
    }

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

    tabsChange(index) {
        this.tabNum = index;
    }

    //添加步骤
    addStep(){
        this.indexNo += 1;
        const data =
        {
            name:'',
            url:'',
            status_codes:200,
            posts:'',
            no:this.indexNo,
            timeout:'15',
            follow_redirects:'1',
            condition:'',
            faultCondition:'',
        };
        this.steps.push(data);
    }

    deleteStep(index){
        this.steps.splice(index,1);
    }
}
