import {Component, OnInit} from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { FuncService } from '../../../../../core/service/func.service';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { MsgService } from '../../../../../core/service/msg.service';
import { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';
import {LayoutPlanInterviewDetailComponent} from '../layout-plan-interview-detail/layout-plan-interview-detail.component';

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

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

    this.detailForm = fb.group({
      'group_name': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(128)])],
      'appointment_type': [null],
      'once_flag': [null],
      'province_flag': [null],
      'aspect_num_flag': [null],
      'ly_order': [null, Validators.compose([Validators.required, Validators.min(1), Validators.max(999)])]
    });
  }

  initDefautEntity() {
    if (this.detailMode === 'add') {
      this.currentEntity = LayoutPlanInterviewDetailComponent.makeGroup();
    }
    this.currentEntity.group_type = 'Written';
  }

  ngOnInit() {
  }

  saveGroup() {
    if (!this.validForm()) {
      return;
    }

    for (const sb of this.ResponseArray) {
      if (sb.exam_minutes === undefined){
        MsgService.errorToastr(`请输入${sb.subject_name}的考试时长，单位为分钟`);
        return;
      }
    }

    FuncService.sortArray(this.ResponseArray, 'group_order');

    this.currentEntity.subject_json = JSON.stringify(this.ResponseArray);
    MsgService.confirmAlert('确定要将这些科目进行分组吗？', () => {
      AjaxService.request('layout/plan/group/union.htm', this.currentEntity, () => {
        MsgService.successAlert('笔试科目分组操作成功！',() => {
          this.closeWindow();
        })
      }, {RequestWithLoading: true});
    });
  }
}
