import { Component, OnInit } from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import {DomSanitizer} from '@angular/platform-browser';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';
import { dateValidator } from '../../../../../shared/validators/date.validator';
import { datetimeValidator } from '../../../../../shared/validators/datetime.validator';

declare let laydate;

@Component({
  selector: 'app-conf-agent-detail',
  templateUrl: './conf-agent-detail.component.html',
  styleUrls: ['./conf-agent-detail.component.scss']
})
export class ConfAgentDetailComponent extends FrameDetailComponent implements OnInit {

  constructor(bsModalRef: BsModalRef, fb: FormBuilder, sanitizer: DomSanitizer) {
    super(bsModalRef, sanitizer);
    this.detailForm = fb.group({
      'agent_province': [null, Validators.compose([Validators.required])],
      'agent_name': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(128)])],
      'agent_addr': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(256)])],
      'agent_id': [null, Validators.compose([Validators.required, Validators.min(1), Validators.max(99999)])],
      'agent_code': [null, Validators.compose([Validators.required, Validators.minLength(1), Validators.maxLength(6)])],
      'enrol_scope': [null, Validators.compose([Validators.required])],
      'enrol_status': [null, Validators.compose([Validators.required])],
      'pay_status': [null, Validators.compose([Validators.required])],
      'subscribe_status': [null, Validators.compose([Validators.required])],
      'enrol_start_time': [null, Validators.compose([datetimeValidator])],
      'enrol_end_time': [null, Validators.compose([datetimeValidator])],
      'pay_start_time': [null, Validators.compose([datetimeValidator])],
      'pay_end_time': [null, Validators.compose([datetimeValidator])],
      'subscribe_start_time': [null, Validators.compose([datetimeValidator])],
      'subscribe_end_time': [null, Validators.compose([datetimeValidator])],
      'ticket_start_time': [null, Validators.compose([datetimeValidator])],
      'ticket_end_time': [null, Validators.compose([datetimeValidator])],
      'exam_start_date': [null, Validators.compose([dateValidator])],
      'exam_end_date': [null, Validators.compose([dateValidator])],
      'works_upload_start_time': [null, Validators.compose([datetimeValidator])],
      'works_upload_end_time': [null, Validators.compose([datetimeValidator])],
      'score_check_start_time': [null, Validators.compose([datetimeValidator])],
      'score_check_limit_time': [null, Validators.compose([datetimeValidator])],
      'agent_enrol_explain': [null, Validators.maxLength(2000)]
    });
  }

  ngOnInit() {
    this.getDate();
  }

  initDefautEntity() {
    if (this.detailMode === 'add') {
      this.currentEntity.enrol_scope = 'Province';
      this.currentEntity.pay_status = 'Active';
      this.currentEntity.enrol_status = 'Active';
      this.currentEntity.subscribe_status = 'InActive';
    } else {
      if (this.currentEntity.exam_start_date) {
        this.currentEntity.exam_start_date = this.currentEntity.exam_start_date.replace(' 00:00:00' , '');
        this.currentEntity.exam_end_date = this.currentEntity.exam_end_date.replace(' 00:00:00' , '');
      }
    }
  }

  //初始化时间控件
  getDate() {
    let _this = this;
    setTimeout(() => {
      laydate.render({
        elem:'#enrol_start_time',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          _this.currentEntity.enrol_start_time = value;
        }
      });
      laydate.render({
        elem:'#enrol_end_time',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          _this.currentEntity.enrol_end_time = value;
        }
      });
      laydate.render({
        elem:'#pay_start_time',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          _this.currentEntity.pay_start_time = value;
        }
      });
      laydate.render({
        elem:'#pay_end_time',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          _this.currentEntity.pay_end_time = value;
        }
      });
      laydate.render({
        elem:'#ticket_start_time',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          _this.currentEntity.ticket_start_time = value;
        }
      });
      laydate.render({
        elem:'#ticket_end_time',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          _this.currentEntity.ticket_end_time = value;
        }
      });
      laydate.render({
        elem:'#exam_start_date',
        type:'date',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          _this.currentEntity.exam_start_date = value;
        }
      });
      laydate.render({
        elem:'#exam_end_date',
        type:'date',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          _this.currentEntity.exam_end_date = value;
        }
      });
      laydate.render({
        elem:'#subscribe_start_time',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          _this.currentEntity.subscribe_start_time = value;
        }
      });
      laydate.render({
        elem:'#subscribe_end_time',
        type:'datetime',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          _this.currentEntity.subscribe_end_time = value;
        }
      });
    }, 500);
  }

}
