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';

declare let laydate;

@Component({
  selector: 'app-cf-material-rule-detail',
  templateUrl: './material-rule-detail.component.html',
  styleUrls: ['./material-rule-detail.component.scss']
})
export class CfMaterialRuleDetailComponent extends FrameDetailComponent implements OnInit {
  schoolArray = [];
  batchArray = [];
  query = {
    school_id : '',
    batch_id : ''
  };
  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
  }

  ngOnInit() {
    this.AjaxService.requestArray('./cf/school/list.htm', {}, (array) => {
      this.schoolArray = array;
      //debugger;
      if (this.schoolArray.length == 1) {
        this.query.school_id = this.schoolArray[0].school_id;
        this.queryBatchArray();
      } 
    });
  }

  queryBatchArray() {
    //查询班级老师列表
    AjaxService.requestArray("cf/batch/list.htm",{school_id: this.query.school_id},(array)=>{
      this.batchArray = array;
      //debugger;
      if (this.batchArray.length == 1) {
        this.query.batch_id = this.batchArray[0].batch_id;
      }
    });   
  }

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

    }
  }

  save() {
    let checkedArray = this.pager.getCheckedArray();
		if (checkedArray.length == 0) {
      MsgService.errorToastr("请至少选择一条记录!");
      return ;
    }
    let _this = this;
    checkedArray = this.pager.getCheckedArray((item) => item.material_def_id);
    let param = {ids: checkedArray, school_id: this.query.school_id, batch_id: this.query.batch_id};
    MsgService.confirmAlert("确定添加材料规则吗", () => {
      MsgService.showLoading();
      AjaxService.requestEntity('cf/material/rule/add.htm', param, (entity) => {
        MsgService.hideLoading();
        //debugger;
        MsgService.successToastr("添加成功!");
        _this.closeWindow();
        //_this.pager.reload();
      });
    });
  }


}
