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 '../../../../..';
import { nullSafeIsEquivalent } from '@angular/compiler/src/output/output_ast';

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

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

  TIME_URL = "pl/plExamTime/listPlExamTimeForBy.htm";
  ADD_URL = "by/byTimeConfig/add.htm";
  EDIT_URL = "by/byTimeConfig/edit.htm";
  GET_URL = "by/byTimeConfig/fresh.htm";

  constructor(fb: FormBuilder) {
    this.item = {};
    this.formModel = fb.group({
      student_type: [null, Validators.compose([Validators.required])],
      exam_time_id: [null, Validators.compose([Validators.required])],
      apply_start_time: [null],
      apply_end_time: [null],
      sch_chk_start_time: [null],
      sch_chk_end_time: [null],
      data_submit_start_time: [null],
      data_submit_end_time: [null],
      confirm_start_time: [null],
      confirm_end_time: [null],
      paper_start_time: [null],
	  paper_end_time: [null],
	  certificate_start_time: [null],
      certificate_end_time: [null],
      by_date: [null],
      city_seal_date: [null],
      school_seal_date: [null],
      prov_seal_date: [null],
      status: [null]
    });
  }

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

  //清空上次选择
  clearData() {
    this.item.exam_time_id = "";
    this.item.apply_start_time = "";
    this.item.apply_end_time = "";
    this.item.sch_chk_start_time = "";
    this.item.sch_chk_end_time = "";
    this.item.data_submit_start_time = "";
    this.item.data_submit_end_time = "";
    this.item.confirm_start_time = "";
    this.item.confirm_end_time = "";
    this.item.by_date = "";
    this.item.paper_start_time = "";
	this.item.paper_end_time = "";
	this.item.certificate_start_time = "";
    this.item.certificate_end_time = "";
  }

  loadTime(examTimeId: number,studentType: number) {
    FrameRequest.requestEntity(this.GET_URL, { "exam_time_id": examTimeId, "student_type": studentType },
      (entity) => {
        if (entity) {
          this.item = entity;
        } else {
          this.item = {};
          this.item.student_type = studentType;
          this.item.exam_time_id = examTimeId;
        }
      });
  }

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

  selectStudentType() {

  }
}
