import { Component, OnInit, ViewChild } from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import {BsModalRef} from 'ngx-bootstrap/modal';
import { FrameDetailComponent } from '../../../frame/core/detail/frame.detail';
import { AjaxService } from '../../../../core/service/ajax.service';
import { FuncService } from '../../../../core/service/func.service';
import { MsgService } from '../../../../core/service/msg.service';
import { FramePager } from 'src/app/core/frame/pager/frame-pager/frame-pager';

@Component({
  selector: 'app-cf-set-aspect-optr',
  templateUrl: './set-aspect-optr.component.html',
  styleUrls: ['./set-aspect-optr.component.scss']
})
export class SetAspectOptrComponent extends FrameDetailComponent implements OnInit {
  @ViewChild('optrPager', {static: false}) optrPager: FramePager;

  currMajorId : any;
  query = {
    school_id : '',
    batch_id : ''
  };
  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
  }

  ngOnInit() {
    let _this = this;
    setTimeout(function() {
      //debugger;
      if (_this.ResponseEntity.optr_id) {
        let arr = _this.ResponseEntity.optr_id.split(",");
        for (const id of arr) {
          for (const optr of _this.optrPager.getRecords()) {
            if (parseInt(id,10) == optr.optr_id) {
              optr['isChecked'] = true;
            }
          }
        }
      }
    }, 500);
  }

  initDefautEntity() {
    if (this.detailMode === 'add') {

    }
  }

  selectMajor(v) {
    debugger;
    this.currMajorId = v.major_id;
    this.optrPager.reload();
  }

  save() {
    let checkedArray = this.optrPager.getCheckedArray();
		if (checkedArray.length == 0) {
      MsgService.errorToastr("请至少选择一条记录!");
      return ;
    }
    let _this = this;
    checkedArray = this.optrPager.getCheckedArray((item) => item.optr_id);
    let param = {
      ids: checkedArray, 
      school_id: this.ResponseEntity.school_id,
      batch_id: this.ResponseEntity.batch_id,
      aspect_id: this.ResponseEntity.aspect_id
    };
      MsgService.showLoading();
      AjaxService.request('cf/aspect/optr/set.htm', param, (entity) => {
        MsgService.hideLoading();
        if(entity.success == true){
          MsgService.successToastr("设置成功!");
          _this.closeWindow();
        } else {
          MsgService.errorToastr(entity.errorMsg);
        }
      });
  }

}
