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

  personalType = 1;//个人报考
  collecType = 2;//集体报考
  payType = 24;//个人报考缴费
  formModel: FormGroup;
  examtimeList = [];
  personalObj: any;
  collectObj: any;
  payObj: any;
  items: any;
  examTime: any;

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

  constructor() {
    this.items = {};
    this.personalObj = {};
    this.payObj = {}
    this.collectObj = {};
    const fb = new FormBuilder();
    this.formModel = fb.group({
      exam_time_id: ["",Validators.required],
      collect_start_time: ["",null],
      collect_end_time: ["",null],
      personal_start_time: ["",null],
      personal_end_time: ["",null],
      pay_start_time: ["",null],
      pay_end_time: ["",null]
    });
  }


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

  loadTime(exam_time_id: number) {
    this.personalObj = {};
    this.collectObj = {};
    this.payObj = {};
    FrameRequest.requestEntity(this.TIME_FIND_URL, { id: exam_time_id }, (entity) => {
      this.examTime = entity;
    });
   
    FrameRequest.requestMap(this.APPLY_URL, { exam_time_id: exam_time_id }, (map) => {
      if(map) {
        if (map['collecTime']) {
          this.collectObj =  map['collecTime'];
        }
        if (map['personalTime']) {
          this.personalObj =  map['personalTime'];
        }

        if (map['payTime']) {
          this.payObj =  map['payTime'];
        }
      }
    });
  }

  save() {
    if (!FrameFunction.validForm(this.formModel)) {
      return;
    }
    FrameRequest.request(this.ADD_URL,{
      exam_time_id: this.items.exam_time_id, 
      start_time: this.collectObj.start_time,
      end_time:this.collectObj.end_time,
      oper_type: this.collecType
     },() => {
      FrameRequest.request(this.ADD_URL,{
        exam_time_id: this.items.exam_time_id, 
        start_time: this.personalObj.start_time,
        end_time:this.personalObj.end_time,
        oper_type: this.personalType
       },() => {
        FrameRequest.request(this.ADD_URL,{
          exam_time_id: this.items.exam_time_id, 
          start_time: this.payObj.start_time,
          end_time:this.payObj.end_time,
          oper_type: this.payType
         },() => {
          AppComponent.GobalComponent.successMsg('报考时间设置成功');
        });
        
      });
    });
    

  }

}
