import {Component, OnInit, Sanitizer} from '@angular/core';
import {DomSanitizer} from '@angular/platform-browser';
import {FormBuilder, Validators} from '@angular/forms';
import { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { FrameDeptDetailComponent } from '../../../../frame/dept/frame-dept-detail/frame-dept-detail.component';
import { ConfFeePageComponent } from '../../fee/conf-fee-page/conf-fee-page.component';

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

  deptArray = [];
  majorArray = [];
  feeArray = [];

  constructor(bsModalRef: BsModalRef, fb: FormBuilder, sanitizer: DomSanitizer) {
    super(bsModalRef, sanitizer);
    this.detailForm = fb.group({
      'aspect_id': [null, Validators.compose([Validators.required, Validators.min(1), Validators.max(999999)])],
      'aspect_name': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(128)])],
      'aspect_code': [null, Validators.compose([Validators.required, Validators.maxLength(16)])],
      'private_exam_room': [null, Validators.required],
      'aspect_major_id': [null, Validators.required],
      'aspect_status': [null, Validators.required],
      'aspect_order': [null, Validators.required],
      'aspect_batchs': [null, Validators.required],
      'aspect_group': [null],
      'aspect_fee_id': [null, Validators.required],
      'without_exam_status': [null, Validators.required],
      'region_limit_type': [null, Validators.required],
      'aspect_dept': [null],
      'variety_alias': [null],
      'teach_college': [null],
      'teach_faculty': [null],
      'show_subjects_1': [null],
      'show_subjects_2': [null],
      'show_subjects_3': [null],
      'aspect_alias': [null],
      'aspect_enrol_explain': [null, Validators.maxLength(2000)],
      'online_flag': [null],
      'score_mode': [null, null]
    });
    AjaxService.requestArray('conf/major/all.htm', {}, (array) => {
      this.majorArray = array;
    });
    AjaxService.requestArray('conf/fee/all.htm', {}, (array) => {
      for (const fee of array) {
        fee.fee_note = ConfFeePageComponent.getFeeConfig(fee);
      }
      this.feeArray = array;
    });
  }

  ngOnInit() {
    
    setTimeout(() => {
      this.deptArray = FrameDeptDetailComponent.makeOptrDeptArray(this.ResponseArray);
    }, 10);
  }

  initDefautEntity() {
    if (this.detailMode === 'add') {
      this.currentEntity.aspect_status = 'Active';
      this.currentEntity.aspect_batchs = '1';
      this.currentEntity.private_exam_room = 'Active';
      this.currentEntity.without_exam_status = 'InActive';
      this.currentEntity.show_subjects_1 = 'Active';
      this.currentEntity.show_subjects_2 = 'Active';
      this.currentEntity.show_subjects_3 = 'Active';
      this.currentEntity.score_mode = 'batch';
    }
  }

  beforeSubmitForm() {
    if (this.currentEntity.aspect_enrol_explain !== undefined)
      this.currentEntity.aspect_enrol_explain_value = encodeURIComponent(this.currentEntity.aspect_enrol_explain);
    return true;
  }
}
