import { Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { ConfScorePanelDetailComponent } from '../conf-score-panel-detail/conf-score-panel-detail.component';
import { ConfScorePanelRangeComponent } from '../conf-score-panel-range/conf-score-panel-range.component';
import { ConfScorePanelSyncComponent } from '../conf-score-panel-sync/conf-score-panel-sync.component';
@Component({
  selector: 'app-conf-score-panel',
  templateUrl: './conf-score-panel.component.html',
  styleUrls: ['./conf-score-panel.component.scss']
})
export class ConfScorePanelComponent extends FramePageComponent implements OnInit {

  schoolArr = [];
  rangeArr = [];
  currentStep = 'main';
  panel_id : any;
  school_id: any;

  constructor(private modalService: BsModalService) {
    super(modalService);
  }

  ngOnInit(): void {
    this.requestSchool();
  }
  //学校初始化
  requestSchool() {
    this.AjaxService.requestArray('cf/school/list.htm',{}, (array) => {
      this.schoolArr = array;
      if(this.schoolArr && this.schoolArr.length == 1) {
        this.school_id = this.schoolArr[0].school_id;
        setTimeout(() => {
          this.requestPanel();
        }, 10);
      }
    });
  }

  //跳转步骤
  goStep(step?) {
    this.currentStep = step;
  }

  requestPanel() {
    this.pager.reload({school_id: this.school_id});
  }

  //请求评分区间
  requestRange(panel_id) {
    this.AjaxService.requestArray('ex/score/panel/range/list.htm', {panel_id: panel_id}, (array) => {
      this.rangeArr = array;
      this.panel_id = panel_id;
      this.currentStep = 'range';
    });
  }

  //评分面板
  showAddEditPanel(v?) {
    this.onModalClose = () => {
      this.pager.reload({school_id: this.school_id});
    };
    this.showComponentModal(ConfScorePanelDetailComponent,  {ResponseEntity: v, ResponseArray: this.schoolArr});
  }

  //评分区间
  showAddEditRange(v?) {
    this.onModalClose = () => {
      this.requestRange(this.panel_id);
    };
    if(v) {
      this.showComponentModal(ConfScorePanelRangeComponent,  {ResponseEntity: v, ResponseArray: this.schoolArr});
    } else {
      localStorage.setItem('panel_id',this.panel_id);
      this.showComponentModal(ConfScorePanelRangeComponent,  {ResponseArray: this.schoolArr});
    }
  }

  //删除评分区间
  deleteRange(v) {
    this.MsgService.confirmAlert('确定要删除该评分区间吗？', () => {
      this.AjaxService.request('ex/score/range/delete.htm', {score_id: v.score_id}, () => {
        this.MsgService.successAlert('删除成功！');
        this.requestRange(this.panel_id);
      });
    });
  }

  //展示区间同步窗口
  showSyncRange() {
    this.onModalClose = () => {
      this.requestRange(this.panel_id);
    };
    localStorage.setItem('panel_id',this.panel_id);
    this.showComponentModal(ConfScorePanelSyncComponent, {ResponseArray: this.schoolArr});
  }

}
