import { Component, OnInit } from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { FrameService } from '../../../../../core/service/frame.service';
import { ModalService } from '../../../../../core/service/modal.service';
import { MsgService } from '../../../../../core/service/msg.service';
import { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';

@Component({
  selector: 'app-layout-agent-detail',
  templateUrl: './layout-agent-detail.component.html',
  styleUrls: ['./layout-agent-detail.component.scss']
})
export class LayoutAgentDetailComponent  extends FrameDetailComponent implements OnInit {

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);

    this.detailForm = fb.group({
      'ly_agent_name': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(128)])],
      'ly_agent_addr': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(128)])],
      'ticket_flag': [null],
      'pagerless_flag': [null, Validators.required],
      'ly_ticket_explain': [null]
    });
  }

  ngOnInit() {
  }

  initDefautEntity () {
    if (this.currentEntity.ly_agent_name === undefined) {
      this.currentEntity.ly_agent_name = this.currentEntity.agent_name;
    }
    if (this.currentEntity.ly_agent_addr === undefined) {
      this.currentEntity.ly_agent_addr = this.currentEntity.agent_addr;
    }
    this.currentEntity.init_flag = 'InActive';
    this.currentEntity.pagerless_flag = 'Active';

    this.currentEntity.ticket_flag = FrameService.getParamValue('LayoutTicketInitFlag' , 'Active');
  }


  initAgent() {
    if (!this.validForm()) {
      return;
    }
    MsgService.confirmAlert('确定要进行编排初始化操作吗？', () => {
      AjaxService.requestEntity('layout/agent/init.htm', this.currentEntity, (entity) => {
        ModalService.showThreadWindow(this.framePageComponent.bsService, entity, {threadTitle: '正在整理数据，请稍候'});
        this.closeWindow();
      });
    });

  }
}
