import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { FrameRequest } from '../../../../shared/frame/frame-request';
import { FrameFunction } from '../../../../shared/frame/frame_function';
import { AppComponent } from '../../../../app.component';

@Component({
  selector: 'app-score-review-time',
  templateUrl: './score-review-time.component.html',
  styleUrls: ['./score-review-time.component.scss']
})
export class ScoreReviewTimeComponent implements OnInit {

  scoreCheckType = 4;//成绩复查申请时间
  scoreResultType = 5;//复查公布时间
  formModel: FormGroup;
  personalObj: any;
  collectObj: any;
  items: any;
  examtimeList = [];

  TIME_URL = "pl/plExamTime/listtime.htm";
  TIME_FIND_URL = "pl/plExamTime/get.htm";
  APPLY_URL = "cf/cfOperateTime/getScoreTime.htm";
  ADD_URL = "cf/cfOperateTime/add.htm";

  constructor() {
    this.items = {};
    this.personalObj = {};
    this.collectObj = {};
    const fb = new FormBuilder();
    this.formModel = fb.group({
      id: ["",Validators.required],
      collect_start_time: ["",Validators.required],
      collect_end_time: ["",Validators.required],
      personal_start_time: ["",Validators.required],
      personal_end_time: ["",Validators.required]
    });
  }


  ngOnInit() {
    FrameRequest.requestArray(this.TIME_URL, {}, (array) => {
      this.examtimeList = array;
    });
  }

  loadTime(exam_time_id: any) {
    this.personalObj = {};
    this.collectObj = {};
    if(exam_time_id) {
      FrameRequest.requestMap(this.APPLY_URL, { exam_time_id: exam_time_id }, (map) => {
        if(map) {
          if (map['checkTime']) {
            this.collectObj =  map['checkTime'];
          }
          if (map['resultTime']) {
            this.personalObj =  map['resultTime'];
          }
        }
      });
    }
  }

  save() {
    if (!FrameFunction.validForm(this.formModel)) {
      return;
    }
    FrameRequest.request(this.ADD_URL,{
      exam_time_id: this.items.exam_time_id, 
      start_time: this.collectObj.start_time,
      end_time:this.collectObj.end_time,
      oper_type: this.scoreResultType
     },() => {
      FrameRequest.request(this.ADD_URL,{
        exam_time_id: this.items.exam_time_id, 
        start_time: this.personalObj.start_time,
        end_time:this.personalObj.end_time,
        oper_type: this.scoreCheckType
       },() => {
        AppComponent.GobalComponent.successMsg('时间设置成功');
      });
    });
    

  }

}
