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

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

  curStep = 'page';
  allSubjectArray = [];
  batchArray = [];
  subject: any;

  @ViewChild('ajax', {static: false}) ajax: FrameAjax;

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({
      'subject_id': [null],
      'variety_id': [null],
      'aspect_batch': [null, Validators.required],
      'subject_order': [null, Validators.required],
      'subject_score': [null, Validators.compose([Validators.required, Validators.min(0), Validators.max(100000)])],
      'total_status': [null, Validators.required],
      'subject_group': [null],
      'subject_group_num': [null, Validators.compose([Validators.min(1), Validators.max(9)])],
      'score_group': [null],
      'score_group_type': [null],
      'score_group_value': [null],
      'master_group': [null],
      'master_group_score': [null],
      'master_group_auto': [null],
      'as_enrol_explain': [null, Validators.maxLength(2000)]
    });
    this.subject = {};
  }

  ngOnInit() {

  }

  initDefautEntity() {
    const array = [];
    for (let s = 1; s <= this.currentEntity.aspect_batchs; s++) {
      array.push({batch_id: s, batch_name: FrameService.getFrameDictText('ExamBatchName', '' + s)});
    }
    this.batchArray = array;
  }

  getBatchSubject(batch) {
    const array = [];
    for (const s of this.ajax.getArray()) {
      if (s.aspect_batch === batch) {
        array.push(s);
      }
    }
    return array;
  }

  addSubject() {
    AjaxService.requestArray('conf/aspect/subject/notinclude.htm', {
      aspect_id: this.currentEntity.aspect_id
    }, (array) => {
      for (const sb of array) {
        let subject_name = sb.subject_name;
        if (sb.subject_alias!==undefined && sb.subject_alias.length > 0)
          subject_name = sb.subject_alias;
        sb.subject_name = sb.subject_code + ' ' + subject_name;
      }
      this.allSubjectArray = array;
      this.curStep = 'add';
      this.subject = {
        aspect_id: this.currentEntity.aspect_id,
        total_status: 'Active'
      };
      if (this.currentEntity.aspect_batchs === 1) {
        this.subject['aspect_batch'] = 1;
      }
      this.detailForm.markAsUntouched();
    });

  }

  editSubject(subject) {
    const array = [subject];
    this.allSubjectArray = array;
    this.curStep = 'edit';
    this.subject = subject;
    this.detailForm.markAsUntouched();
  }

  saveSubject() {
    this.detailForm.markAllAsTouched();
    if (!this.validForm()) {
      return;
    }
    AjaxService.request('conf/aspect/subject/save.htm', this.subject, () => {
      this.ajax.request();
      MsgService.successToastr('专业方向考试科目配置成功！');
      this.curStep = 'page';
    });
  }

  deleteSubject(sb) {
    MsgService.confirmAlert('确定要删除该考试科目吗？', () => {
      AjaxService.request('conf/aspect/subject/delete.htm', sb, () => {
        this.ajax.reload();
        MsgService.successToastr('科目已删除！');
      });
    });

  }
}
