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-kjupload-time',
  templateUrl: './upload-time.component.html',
  styleUrls: ['./upload-time.component.scss']
})
export class KjUploadTimeComponent implements OnInit {

  formModel: FormGroup;
  examtimeList = [];
  item: any;
  operTypeList = [];//业务类型

  TIME_URL = "pl/plExamTime/listtime.htm";
  EDIT_URL = "cf/cfOperateTime/edit.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, Validators.compose([Validators.required])],
      exam_time_id: [null, Validators.compose([Validators.required])],
      start_time: [null, Validators.compose([Validators.required])],
	  end_time: [null, Validators.compose([Validators.required])],
	  batch_name: [null, Validators.compose([Validators.required])],
	  remark: [null,],
    });
  }

  ngOnInit() {
    //考试日期
    FrameRequest.requestArray(this.TIME_URL, {}, (arrary) => {
      this.examtimeList = arrary;
    });
    //构造业务类型下拉框---参照CfConst.java常量类
    let practical = {typeName: "实践课成绩上报", typeId: 7};
    let communicate = {typeName: "沟通课成绩上报", typeId: 3};
    let paper = {typeName: "毕业论文成绩上报", typeId: 6};
    let join = {typeName: "专本衔接成绩上报", typeId: 8};
    let assist = {typeName: "网络助学成绩上报", typeId: 9};
    let unified = {typeName: "统考成绩上报", typeId: 10};
    this.operTypeList.push(practical);
    this.operTypeList.push(communicate);
    this.operTypeList.push(paper);
    this.operTypeList.push(join);
    this.operTypeList.push(assist);
    this.operTypeList.push(unified);
  }

  freshData(oper_type, exam_time_id) {
    if (!oper_type || !exam_time_id) {
      return;
    }
    // FrameRequest.requestEntity(this.GET_URL, {oper_type: oper_type, exam_time_id: exam_time_id},
    //   (entity) => {
	// 	  debugger
    //     if (entity) {
    //       this.item = entity;
    //     } else {
    //       this.item = {};
    //       this.item.oper_type = oper_type;
    //       this.item.exam_time_id = exam_time_id;
    //     }
	//   });
	FrameRequest.requestMap(this.GET_URL, {oper_type: oper_type, exam_time_id: exam_time_id},
      (map) => {
		debugger
		if (map.item) {
          this.item = map.item;
        } else {
          this.item = {};
          this.item.oper_type = oper_type;
          this.item.exam_time_id = exam_time_id;
		}
		if (map.batch_name) {
			this.item.batch_name = map.batch_name;
		}
		if (map.remark) {
			this.item.remark = map.remark;
		}
      });
  }

  /**
   * 时间控件无法获取数据 TODO...
   */
  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, () => {
        AppComponent.GobalComponent.successMsg('上报时间设置成功!');
      });
    }



  }

}
