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-aspect-detail',
  templateUrl: './aspect-detail.component.html',
  styleUrls: ['./aspect-detail.component.scss']
})
export class CfAspectDetailComponent extends FrameDetailComponent implements OnInit {
  deptArray = [];
  old_pass_percent : 0;
  old_pass_cnt = 0;

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({
      'aspect_code': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(12)])],
      'aspect_name': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(64)])],
      'pass_percent': [null,Validators.compose([Validators.required])],
      'dept_id': [null,Validators.compose([Validators.required])],
      // 'is_practice': [null,Validators.compose([Validators.required])],
      'ly_order': [null, Validators.compose([ Validators.min(0)])],
      'pass_cnt': [null]
    });
  }

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

  }

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

    } else if (this.detailMode === 'edit') {
      this.old_pass_percent = this.currentEntity.pass_percent;
      this.old_pass_cnt = this.currentEntity.pass_cnt;
    }
    AjaxService.requestArray("cf/school/second/list.htm",{school_id:this.currentEntity.school_id},(array)=>{
      this.deptArray = array;
      // if (this.deptArray.length == 1) {
      //   this.currentEntity.dept_id = this.deptArray[0].dept_id;
      // }
    });
  }

  changePercent() {
    if (this.currentEntity.initial_pass_cnt && this.currentEntity.pass_percent) {
      //debugger;
      this.currentEntity.pass_cnt = Math.ceil(parseFloat(this.currentEntity.pass_percent)/100 * parseInt(this.currentEntity.initial_pass_cnt, 10));
    }

  }

  doModify() {
    debugger;
    // if (this.old_pass_percent != this.currentEntity.pass_percent || this.old_pass_cnt != this.currentEntity.pass_cnt) {
      MsgService.confirmAlert('通过率或通过人数已修改，确认保存？', () => {
        AjaxService.request("cf/aspect/edit.htm",this.currentEntity,(array)=>{
          this.MsgService.successToastr("保存成功!");
          this.closeWindow();
        });
      });
    // }
  }

}
