import {Component, OnInit} from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../core/service/ajax.service';
import { MsgService } from '../../../../core/service/msg.service';
import { FramePageComponent } from '../../../frame/core/page/frame.page';
import { FrameService } from '../../../../core/service/frame.service';
import {PlanGroupBatchDetailComponent} from '../plan-group-batch-detail/plan-group-batch-detail.component';
import {PlaGroupPublishComponent} from '../pla-group-publish/pla-group-publish.component';
import { FuncService } from '../../../../core/service/func.service';

@Component({
  selector: 'app-plan-group-batch-page',
  templateUrl: './plan-group-batch-page.component.html',
  styleUrls: ['./plan-group-batch-page.component.scss']
})
export class PlanGroupBatchPageComponent extends FramePageComponent implements OnInit {

  batchNameArray: any;
  score: any;
  provinceArray: any;
  planTypeArray = [{
    dict_value: 'Ceil',
    dict_text: '兜底划线'
  }, {
    dict_value: 'Floor',
    dict_text: '向上划线'
  },];

  constructor(modalService: BsModalService) {
    super(modalService);
    const array = FrameService.getFrameDictArray('ExamBatchName');
    if (array.length > 0)
      array.slice(array.length - 1, 1);
    this.batchNameArray = array;
    this.score = {
      batch_id: '1',
      gobal_percent: 5.0,
      plan_type: 'Floor',
      final_flag: 'InActive'
    };
    this.changeBatch();
  }

  //获取专业
  changeBatch() {
    AjaxService.requestArray('plan/group/province/list.htm', this.score, (array) => {
      this.provinceArray = array;
    }, {RequestWithLoading: true});
  }

  changePlanTotal(province) {
    const self = this;
    const obj = Object.assign({}, province, this.score);
    obj.pn_percent = this.score.gobal_percent;
    AjaxService.requestEntity('plan/group/province/score.htm', obj, (entity) => {
      province['qualified_total_' + self.score.batch_id] = entity['qualified_total_' + self.score.batch_id];
      province['qualified_score_value_' + self.score.batch_id] = entity['qualified_score_value_' + self.score.batch_id];
      province['qualified_score_' + self.score.batch_id] = entity['qualified_score_' + self.score.batch_id];
      province['percent_' + self.score.batch_id] = entity['percent_' + self.score.batch_id];
      province['plan_total'] = entity['plan_total'];
    }, );
  }

  saveAllProvince() {
    const self = this;
    MsgService.confirmAlert('确定要保存划线记录吗？将覆盖原有计划，如果已经判断合格，需要重新进行合格判断！谨慎进行此操作！', () => {
      AjaxService.request('plan/group/province/save.htm', {p_json: JSON.stringify(this.provinceArray)}, () => {
        MsgService.successAlert('计划已保存！如果已经进行过合格判断，请重新进行合格判断！');
      }, {RequestWithLoading: true});
    });
  }

  saveQualified(pn_id?) {
    const self = this;
    MsgService.confirmAlert('确定要进行合格判断吗？将自动保存当前的划线计划！', () => {
      AjaxService.request('plan/group/province/qualified.htm', {
        batch_id: self.score.batch_id,
        pn_id: pn_id,
        p_json: JSON.stringify(this.provinceArray)
      }, () => {
        MsgService.successAlert('考生专业合格判断完成！');
      }, {RequestWithLoading: true});
    });
  }

  showPlanDetail(group) {
    this.showComponentModal(PlanGroupBatchDetailComponent, {ResponseEntity: group, closeCallback: this.changeBatch.bind(this)});
  }

  ngOnInit() {
  }

  uploadCallback(ajax) {
    MsgService.successAlert('导入成功!');
  }
   
  //计算排名 
  cacluRank(v) {
    AjaxService.request('plan/group/level/make.htm', {pn_id : v.pn_id}, () => {
      MsgService.successToastr('计算排名成功！');
    },  {RequestWithLoading: true});
  }


  exportStdRegRoom(v) {
    FuncService.ajaxDownload('frame/report/download.htm', {report_name:'ZXScoreExport', report_title :'score_'+v.pn_id, pn_id: v.pn_id});
  }
}
