import { Component, OnInit } 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';

@Component({
  selector: 'app-cf-set-stdedu',
  templateUrl: './set-stdedu.component.html',
  styleUrls: ['./set-stdedu.component.scss']
})
export class SetStdEduComponent extends FrameDetailComponent implements OnInit {
  stdEduArray = [];

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({
    });
  }

  //TODO
  ngOnInit() {
    // this.stdEduArray.push({code: "MasterGrad",caption: "硕士毕业生",isChecked: false});
    // this.stdEduArray.push({code: "CurrGrad",caption: "应届硕士生",isChecked: false});
    // this.stdEduArray.push({code: "EquEdu",caption: "同等学力考生",isChecked: false});
    // this.stdEduArray.push({code: "EquEduMaster",caption: "同等学力申请硕士学位的考生",isChecked: false});
    AjaxService.requestArray("cf/stdEduArray.htm",{}, (array)=>{
      for (const d of array) {
        this.stdEduArray.push({code: d.dict_value,caption: d.dict_text,isChecked: false});
      }

      let param = {
        material_rule_id: this.ResponseEntity.material_rule_id,
        material_def_id: this.ResponseEntity.material_def_id,
        rule_type: "std_edu"
      };

      AjaxService.requestArray("cf/material/rule/detail/list.htm",param, (array)=>{
        debugger;
        for (const d of array) {
          for (const edu of this.stdEduArray) {
            if (d.rule_value == edu.code) {
              edu.isChecked = true;
            }
          }
        }
      });
    });



  }

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

    }
  }

  public checkedMasterToggle(event){
    this.setCheckedAll(event.currentTarget.checked);
  }

  public setCheckedAll(checkStatus){
    this.stdEduArray.forEach((item) => {
      item['isChecked'] = checkStatus;
     });
  }

  //获取checked数组
  public getCheckedArray(func?){
    let checkedArray = [];
    this.stdEduArray.forEach((item) => {
      if(item['isChecked'] === true)
      if(func){
        checkedArray.push(func.call(this,item));
      }else{
        checkedArray.push(item);
      }
    });
    return checkedArray;
  }

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


}
