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

@Component({
  selector: 'app-admit-province-score',
  templateUrl: './admit-province-score.component.html',
  styleUrls: ['./admit-province-score.component.scss']
})
export class AdmitProvinceScoreComponent extends FrameDetailComponent implements OnInit {

  AspectArray = [];

  constructor(bsModalRef: BsModalRef) {
    super(bsModalRef);
  }

  ngOnInit() {
  }


  addAspect() {
    const array = [];
    array.push({});
    for (const a of this.ResponseMap.AmAspectArray) {
      let has = false;
      for (const b of this.ResponseMap.AspectArray) {
        if (a.aspect_code === b.aspect_code) {
          has = true;
          break;
        }
      }
      if (!has) {
        array.push(a);
      }
    }
    this.AspectArray = array;
    this.ResponseMap.AspectArray.push({province_id: this.ResponseMap.AmProvince.province_id});
  }

  // 更改了专业
  changAspect($event, v) {
    for (const a of this.ResponseMap.AmAspectArray) {
      if (a.aspect_code === $event.target.value) {
        v.aspect_full_name = a.aspect_full_name;
        v.aspect_code = a.aspect_code;
      }
    }
  }

  // 删除专业
  deleteAspect(v) {
    for (let index = 0; index < this.ResponseMap.AspectArray.length; index++) {
      if (this.ResponseMap.AspectArray[index].aspect_code === v.aspect_code) {
        this.ResponseMap.AspectArray.splice(index, 1);
        break;
      }
    }
  }

  confirmScore() {
    for (const s of this.ResponseMap.ScoreArray) {
      if (!s.qualified_score_liberal) {
        MsgService.errorToastr(`请输入【${s.dict_text}】的文科分数线`);
        break;
      }

      if (!s.qualified_score_science) {
        MsgService.errorToastr(`请输入【${s.dict_text}】的理科分数线`);
        break;
      }
    }
    for (const s of this.ResponseMap.AspectArray) {
      if (!s.qualified_score_liberal) {
        MsgService.errorToastr(`请输入【${s.aspect_full_name}】的文科分数线，如该专业不需要单独划线，请直接删除`);
        break;
      }
    }
    for (const s of this.ResponseMap.AspectArray) {
      if (!s.qualified_score_science) {
        MsgService.errorToastr(`请输入【${s.aspect_full_name}】的理科分数线，如该专业不需要单独划线，请直接删除`);
        break;
      }
    }


    const fn = () => {
      AjaxService.request('admit/province/score.htm', {
        category_json: JSON.stringify(this.ResponseMap.ScoreArray),
        aspect_json: JSON.stringify(this.ResponseMap.AspectArray),
        province_id: this.ResponseMap.AmProvince.province_id
      }, () => {
        this.closeWindow();
        MsgService.successAlert(`${this.ResponseMap.AmProvince.province_name} 文化分数线保存成功！`);
      }, { RequestWithLoading: true });
    };
    MsgService.confirmAlert('请仔细核对文化线，确定要保存吗？', fn);
  }


  makeWish() {
    const fn = () => {
      AjaxService.request('admit/province/wish/make.htm', {
        province_id: this.ResponseMap.AmProvince.province_id
      }, () => {
        MsgService.successAlert(`${this.ResponseMap.AmProvince.province_name} 志愿信息重新合成完成！`);
      }, { RequestWithLoading: true });
    };
    MsgService.confirmAlert('确定要重新合成该省的志愿吗？', fn);
  }
}
