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

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

  formModel: FormGroup;
  examtimeList = [];
  item: any;
  oper_type = 16;

  TIME_URL = "pl/plExamTime/listtime.htm";
  ADD_URL = "cf/cfOperateTime/add.htm";
  GET_URL = "cf/cfOperateTime/get.htm";
  kwExamPoint_URL = "kw/kwExamPoint/auto.htm";
  kwExamRelation_URL = "kw/kwExamRelation/auto.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;
        }
      });
  }

  /**
   * 时间控件无法获取数据 TODO...
   */
  save() {
    
    if (!FrameFunction.validForm(this.formModel)) {
      return;
    }
    if (this.item) {
      this.item.oper_type = this.oper_type;
	}
	FrameRequest.request(this.kwExamPoint_URL, {exam_time_id:this.item.exam_time_id}, async() => {});
    FrameRequest.request(this.ADD_URL, this.item, async() => {
      AppComponent.GobalComponent.successMsg('时间设置成功');
    });

  }

}
