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

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

 
  formModel: FormGroup;
  examtimeList = [];
  item: any;
  oper_type = 19;//机考

  TIME_URL = "pl/plExamTime/listtime.htm";
  ADD_URL = "cf/cfOperateTime/add.htm";
  GET_URL = "cf/cfOperateTime/get.htm";

  constructor(fb: FormBuilder) {
    this.item = {};
    this.formModel = fb.group({
      oper_type: [null],
      exam_time_id: [null, Validators.compose([Validators.required])],
      start_time: [null, Validators.compose([Validators.required])],
      end_time: [null, Validators.compose([Validators.required])]
    });
  }

  ngOnInit() {
    //考试日期
    FrameRequest.requestArray(this.TIME_URL, {}, (arrary) => {
      this.examtimeList = arrary;
    });
  }

  loadTime(exam_time_id: number) {
    FrameRequest.requestEntity(this.GET_URL, { exam_time_id: exam_time_id, oper_type: this.oper_type },
      (entity) => {
        this.item = entity;
        if (!this.item) {
          this.item = {};
          this.item.exam_time_id = exam_time_id;
        }
      });
  }

  
  save() {
    if (!FrameFunction.validForm(this.formModel)) {
      return;
    }
    if (this.item) {
      this.item.oper_type = this.oper_type;
    }
    FrameRequest.request(this.ADD_URL, this.item, () => {
      AppComponent.GobalComponent.successMsg('时间设置成功');
    });

  }

}
