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

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

  ticketDownload = 23;//准考证打印
  formModel: FormGroup;
  examtimeList = [];
  ticketObj: any;
  items: any;
  examTime: any;

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

  constructor() {
    this.items = {};
    this.ticketObj = {};
    const fb = new FormBuilder();
    this.formModel = fb.group({
      exam_time_id: ["",Validators.required],
      start_time: ["",Validators.required],
      end_time: ["",Validators.required]
    });
  }


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

  loadTime(exam_time_id: number) {
    this.ticketObj = {};
    FrameRequest.requestEntity(this.TIME_FIND_URL, { id: exam_time_id }, (entity) => {
      this.examTime = entity;
    });
   
    FrameRequest.requestEntity(this.APPLY_URL, {oper_type:this.ticketDownload, exam_time_id: exam_time_id }, (entity) => {
		if (entity) {
			this.ticketObj =  entity;
		}
          
    });
  }

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

  }

}
