import { Component, OnInit } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { AjaxService } from 'src/app/core/service/ajax.service';
import { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';
import { dateValidator } from '../../../../../shared/validators/date.validator';
import { MsgService } from 'src/app/core/service/msg.service';

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

  appointmentArr = [];
  appointment_date: any;

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
   }

  ngOnInit(): void {
  }

  initDefautEntity() {
    AjaxService.requestArray('conf/agent/aspect/appointment/detail.htm', {
      agent_id : this.currentEntity.agent_id,
      aspect_id : this.currentEntity.aspect_id
    }, (array) => {
      this.appointmentArr = array;
    })
  }

  //增加时段
  addTime(item) {
    let arr = [];
    if(item) {
      arr = item.appointments;
      arr.push({start_time: '', end_time: '', appointment_capacity: 0});
    }
  }

  //删除时段
  deleteTime(item,v, index) {
    let arr = [];
    if(item) {
      arr = item.appointments;
      arr.splice(index,1);
    }
  }

  //增加考试日期
  addDate() {
    if(!this.appointment_date) {
      MsgService.errorToastr('请填写预约日期！');
      return;
    }
    if (this.appointment_date.length !== 10) {
      MsgService.errorToastr('请填写正确的预约日期');
      return;
    }
    if (!isNaN(this.appointment_date) || isNaN(Date.parse(this.appointment_date))) {
      MsgService.errorToastr('请填写正确的预约日期');
      return;
    }
    for(const app of this.appointmentArr) {
      if(app.appointment_date == this.appointment_date) {
        MsgService.errorToastr('填写的预约日期已经存在，请不要重复增加！');
        return ;
      }
    }
    let arr = [];
    if(this.appointmentArr && this.appointmentArr.length == 1 && !this.appointmentArr[0].appointment_date) {
      this.appointmentArr[0]['appointment_date'] = this.appointment_date;
    } else {
      this.appointmentArr.push(
        {
          appointment_date: this.appointment_date,
          agent_id : this.currentEntity.agent_id,
          aspect_id : this.currentEntity.aspect_id,
          appointments: arr
        }
        );
    }
      this.appointmentArr.sort(function(a,b) {
        return new Date(Date.parse(a.appointment_date)).getTime()- new Date(Date.parse(b.appointment_date)).getTime();
      });
  }
  

  //保存容量
  saveAppointment() {
    for(const app of this.appointmentArr) {
      if(app.appointments && app.appointments.length >0) {
        for(const time of app.appointments) {
          if(!time.start_time  || !time.end_time ) {
            MsgService.errorToastr('请填写时间段！');
            return;
          }
        }
      }
    }
    AjaxService.request('conf/agent/aspect/appointment/save.htm', {
      agent_id: this.currentEntity.agent_id,
      aspect_id: this.currentEntity.aspect_id,
      data_json: JSON.stringify(this.appointmentArr)
    }, () => {
      MsgService.successToastr('预约容量保存成功！');
      this.closeWindow();
    });
  }
}
