import {Component, OnInit} from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import {DomSanitizer} from '@angular/platform-browser';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';

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

  constructor(bsModalRef: BsModalRef, fb: FormBuilder, sanitizer: DomSanitizer) {
    super(bsModalRef, sanitizer);
    this.detailForm = fb.group({
      'rule_name': [null, Validators.compose([Validators.required])],
      'judge_total': [null, Validators.compose([Validators.required, Validators.min(1), Validators.max(30)])],
      'gobal_priortiy': [null, Validators.compose([Validators.required])],
      'group_priortiy': [null, Validators.compose([Validators.required])],
      'rule_sex_value': [null],
      'rule_chief': [null],
      'rule_school_flag': [null],
      'rule_school_value': [null],
      'rule_title_value': [null],
      'rule_breeds_value': [null],
      'rule_special_flag': [null]
    });
  }

  ngOnInit() {
  }

  initDefautEntity() {
    if (this.detailMode === 'add') {
      this.currentEntity.rule_name = '规则' + (this.ResponseMap['RuleArray'].length + 1);
      this.currentEntity.gobal_priortiy = 1;
      this.currentEntity.group_priortiy = (this.ResponseMap['RuleArray'].length + 1);
      this.currentEntity.ly_group_id = this.ResponseMap['LyGroup'].ly_group_id;
      this.currentEntity.agent_id = this.ResponseMap['LyGroup'].ly_agent_id;
    } else {
      if (this.currentEntity.rule_school !== undefined)
        this.currentEntity.rule_school_value = this.currentEntity.rule_school.split(',');
      if (this.currentEntity.rule_title !== undefined)
        this.currentEntity.rule_title_value = this.currentEntity.rule_title.split(',');
      if (this.currentEntity.rule_breeds !== undefined)
        this.currentEntity.rule_breeds_value = this.currentEntity.rule_breeds.split(',');
      if (this.currentEntity.rule_sex !== undefined)
        this.currentEntity.rule_sex_value = this.currentEntity.rule_sex.split(',');
    }
  }

  beforeSubmitForm(): boolean {
    if (this.currentEntity.rule_school_value !== undefined)
      this.currentEntity.rule_school = this.currentEntity.rule_school_value.join();
    if (this.currentEntity.rule_title_value !== undefined)
      this.currentEntity.rule_title = this.currentEntity.rule_title_value.join();
    if (this.currentEntity.rule_breeds_value !== undefined)
      this.currentEntity.rule_breeds = this.currentEntity.rule_breeds_value.join();
    if (this.currentEntity.rule_sex_value !== undefined)
      this.currentEntity.rule_sex = this.currentEntity.rule_sex_value.join();

    return true;
  }

}
