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

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

  
  formModel: FormGroup;
  type = 3;
  start_time: any;
  end_time: any;
  phone: any;
  address: any;
  qq: any;
  website: any;
  ADD_URL = "by/byTurnTimePlace/add.htm";
  GET_URL = "by/byTurnTimePlace/find.htm";

  constructor(fb: FormBuilder) {
    this.formModel = fb.group({
      start_time: [null, Validators.compose([Validators.required])],
      end_time: [null, Validators.compose([Validators.required])],
      phone: [null, Validators.compose([Validators.required])],
      address: [null, Validators.compose([Validators.required])],
      qq: [null, null],
      website: [null, null]
    });
  }

  ngOnInit() {
    FrameRequest.requestEntity(this.GET_URL, {type: this.type}, (entity) => {
      if(entity) {
        this.start_time = entity['start_time'];
        this.end_time = entity['end_time'];
        this.phone = entity['phone'];
        this.address = entity['address'];
        this.qq = entity['qq'];
        this.website = entity['website'];
      }
    })
  
  }

  
  save() {
    if (!FrameFunction.validForm(this.formModel)) {
      return;
    }
   
    if(!this.qq){
      this.qq = "";
    }
    if(!this.website){
      this.website = "";
    }

    FrameRequest.request(this.ADD_URL, {
      start_time: this.start_time,
      end_time: this.end_time,
      phone: this.phone,
      address: this.address,
      qq: this.qq,
      website: this.website,
      type: this.type
    }, () => {
      AppComponent.GobalComponent.successMsg('时间设置成功');
    });

  }

}
