import { Component, OnInit, ViewChild, Input } from "@angular/core";
import { ToolService } from 'src/app/services/tool.service';
import { OverlayScrollbarsComponent } from 'overlayscrollbars-ngx';
import { NzMessageService } from 'ng-zorro-antd';
import { v4 as uuidv4, validate as uuidValidate } from 'uuid';
import { addDays, startOfMinute, endOfMinute, set, getYear, getMonth, getDate, format } from 'date-fns';
import { MainService } from 'src/app/services/main.service';

@Component({
  selector: "app-configuration-inspect-batch",
  templateUrl: "./configuration-inspect-batch.component.html",
  styleUrls: ["./configuration-inspect-batch.component.less"],
})
export class ConfigurationInspectBatchComponent implements OnInit {
  @Input() activeDictionaryKey: any;
  @Input() dictionaryKeyList: any;
  constructor(
    private mainService: MainService,
    private tool: ToolService,
    private message: NzMessageService,
  ) {}

  ngOnInit() {
    this.tabModal('characteristics');
    this.init();
  }

 tabModalName:string = 'characteristics'; //当前选中的tab
 loading:boolean = false; //页面加载的loading
 hosId = this.tool.getCurrentHospital().id; //当前院区
 tasktype:any = {};// 任务类型
 configs:any = {};// 配置
 // 切换tab
 tabModal(tabModalName:string){
   this.tabModalName = tabModalName;
 }
 // 保存
 btnLoading: boolean = false; //提交按钮loading状态
 submitForm() {
   if(!this.tasktype.id){
     this.message.create("warning", "请先配置任务类型！");
     return;
   }

   // 默认时间区间1

   if(
     (this.activeDictionaryKey.postData.ibActiveStartTime1 && !this.activeDictionaryKey.postData.ibActiveEndTime1) ||
     (!this.activeDictionaryKey.postData.ibActiveStartTime1 && this.activeDictionaryKey.postData.ibActiveEndTime1) ||
     (this.activeDictionaryKey.postData.ibActiveStartTime2 && !this.activeDictionaryKey.postData.ibActiveEndTime2) ||
     (!this.activeDictionaryKey.postData.ibActiveStartTime2 && this.activeDictionaryKey.postData.ibActiveEndTime2)
   ){
     this.message.create("warning", "【默认时间区间1】的【有效时间范围】请完整填写开始时间与结束时间！");
     return;
   }else if(
     (this.activeDictionaryKey.postData.ibActiveStartTime1 && this.activeDictionaryKey.postData.ibActiveEndTime1 && +this.activeDictionaryKey.postData.ibActiveStartTime1 > +this.activeDictionaryKey.postData.ibActiveEndTime1) ||
     (this.activeDictionaryKey.postData.ibActiveStartTime2 && this.activeDictionaryKey.postData.ibActiveEndTime2 && +this.activeDictionaryKey.postData.ibActiveStartTime2 > +this.activeDictionaryKey.postData.ibActiveEndTime2)
   ){
    // 王满测试说有问题，要求去掉校验
    // 开发无法复现问题，暂时注释
    //  this.message.create("warning", "【默认时间区间1】的【有效时间范围】开始时间不能大于结束时间！");
    //  return;
   }

   if(
     (this.activeDictionaryKey.postData.ibDefaultStartTime && !this.activeDictionaryKey.postData.ibDefaultEndTime) ||
     (!this.activeDictionaryKey.postData.ibDefaultStartTime && this.activeDictionaryKey.postData.ibDefaultEndTime)
   ){
     this.message.create("warning", "【默认时间区间1】的【默认时间】请完整填写开始时间与结束时间！");
     return;
   }else if(
     this.activeDictionaryKey.postData.ibDefaultStartTime &&
     this.activeDictionaryKey.postData.ibDefaultEndTime &&
     (this.activeDictionaryKey.postData.ibDefaultDayStart[0].checked ? +addDays(this.activeDictionaryKey.postData.ibDefaultStartTime, 1) : +this.activeDictionaryKey.postData.ibDefaultStartTime) > (this.activeDictionaryKey.postData.ibDefaultDayEnd[0].checked ? +addDays(this.activeDictionaryKey.postData.ibDefaultEndTime, 1) : +this.activeDictionaryKey.postData.ibDefaultEndTime)
   ){
     this.message.create("warning", "【默认时间区间1】的【默认时间】开始时间不能大于结束时间！");
     return;
   }

   // 默认时间区间2

   if(
     (this.activeDictionaryKey.postData.ib2ActiveStartTime1 && !this.activeDictionaryKey.postData.ib2ActiveEndTime1) ||
     (!this.activeDictionaryKey.postData.ib2ActiveStartTime1 && this.activeDictionaryKey.postData.ib2ActiveEndTime1) ||
     (this.activeDictionaryKey.postData.ib2ActiveStartTime2 && !this.activeDictionaryKey.postData.ib2ActiveEndTime2) ||
     (!this.activeDictionaryKey.postData.ib2ActiveStartTime2 && this.activeDictionaryKey.postData.ib2ActiveEndTime2)
   ){
     this.message.create("warning", "【默认时间区间1】的【有效时间范围】请完整填写开始时间与结束时间！");
     return;
   }else if(
     (this.activeDictionaryKey.postData.ib2ActiveStartTime1 && this.activeDictionaryKey.postData.ib2ActiveEndTime1 && +this.activeDictionaryKey.postData.ib2ActiveStartTime1 > +this.activeDictionaryKey.postData.ib2ActiveEndTime1) ||
     (this.activeDictionaryKey.postData.ib2ActiveStartTime2 && this.activeDictionaryKey.postData.ib2ActiveEndTime2 && +this.activeDictionaryKey.postData.ib2ActiveStartTime2 > +this.activeDictionaryKey.postData.ib2ActiveEndTime2)
   ){
    // 王满测试说有问题，要求去掉校验
    // 开发无法复现问题，暂时注释
    //  this.message.create("warning", "【默认时间区间1】的【有效时间范围】开始时间不能大于结束时间！");
    //  return;
   }

   if(
     (this.activeDictionaryKey.postData.ib2DefaultStartTime && !this.activeDictionaryKey.postData.ib2DefaultEndTime) ||
     (!this.activeDictionaryKey.postData.ib2DefaultStartTime && this.activeDictionaryKey.postData.ib2DefaultEndTime)
   ){
     this.message.create("warning", "【默认时间区间1】的【默认时间】请完整填写开始时间与结束时间！");
     return;
   }else if(
     this.activeDictionaryKey.postData.ib2DefaultStartTime &&
     this.activeDictionaryKey.postData.ib2DefaultEndTime &&
     (this.activeDictionaryKey.postData.ib2DefaultDayStart[0].checked ? +addDays(this.activeDictionaryKey.postData.ib2DefaultStartTime, 1) : +this.activeDictionaryKey.postData.ib2DefaultStartTime) > (this.activeDictionaryKey.postData.ib2DefaultDayEnd[0].checked ? +addDays(this.activeDictionaryKey.postData.ib2DefaultEndTime, 1) : +this.activeDictionaryKey.postData.ib2DefaultEndTime)
   ){
     this.message.create("warning", "【默认时间区间1】的【默认时间】开始时间不能大于结束时间！");
     return;
   }

   let postData:any = {
     ...this.configs,
     taskType: this.tasktype.id,
     hosId: this.hosId,
     ibActiveStartTime1: this.activeDictionaryKey.postData.ibActiveStartTime1 ? format(startOfMinute(this.activeDictionaryKey.postData.ibActiveStartTime1), 'yyyy-MM-dd HH:mm:ss') : undefined,
     ibActiveEndTime1: this.activeDictionaryKey.postData.ibActiveEndTime1 ? format(endOfMinute(this.activeDictionaryKey.postData.ibActiveEndTime1), 'yyyy-MM-dd HH:mm:ss') : undefined,
     ibActiveStartTime2: this.activeDictionaryKey.postData.ibActiveStartTime2 ? format(startOfMinute(this.activeDictionaryKey.postData.ibActiveStartTime2), 'yyyy-MM-dd HH:mm:ss') : undefined,
     ibActiveEndTime2: this.activeDictionaryKey.postData.ibActiveEndTime2 ? format(endOfMinute(this.activeDictionaryKey.postData.ibActiveEndTime2), 'yyyy-MM-dd HH:mm:ss') : undefined,
     ibDefaultStartTime: this.activeDictionaryKey.postData.ibDefaultStartTime ? format(startOfMinute(this.activeDictionaryKey.postData.ibDefaultStartTime), 'yyyy-MM-dd HH:mm:ss') : undefined,
     ibDefaultEndTime: this.activeDictionaryKey.postData.ibDefaultEndTime ? format(endOfMinute(this.activeDictionaryKey.postData.ibDefaultEndTime), 'yyyy-MM-dd HH:mm:ss') : undefined,
     ibDefaultDayStart: this.activeDictionaryKey.postData.ibDefaultDayStart[0].checked ? 1 : 0,
     ibDefaultDayEnd: this.activeDictionaryKey.postData.ibDefaultDayEnd[0].checked ? 1 : 0,
     ib2ActiveStartTime1: this.activeDictionaryKey.postData.ib2ActiveStartTime1 ? format(startOfMinute(this.activeDictionaryKey.postData.ib2ActiveStartTime1), 'yyyy-MM-dd HH:mm:ss') : undefined,
     ib2ActiveEndTime1: this.activeDictionaryKey.postData.ib2ActiveEndTime1 ? format(endOfMinute(this.activeDictionaryKey.postData.ib2ActiveEndTime1), 'yyyy-MM-dd HH:mm:ss') : undefined,
     ib2ActiveStartTime2: this.activeDictionaryKey.postData.ib2ActiveStartTime2 ? format(startOfMinute(this.activeDictionaryKey.postData.ib2ActiveStartTime2), 'yyyy-MM-dd HH:mm:ss') : undefined,
     ib2ActiveEndTime2: this.activeDictionaryKey.postData.ib2ActiveEndTime2 ? format(endOfMinute(this.activeDictionaryKey.postData.ib2ActiveEndTime2), 'yyyy-MM-dd HH:mm:ss') : undefined,
     ib2DefaultStartTime: this.activeDictionaryKey.postData.ib2DefaultStartTime ? format(startOfMinute(this.activeDictionaryKey.postData.ib2DefaultStartTime), 'yyyy-MM-dd HH:mm:ss') : undefined,
     ib2DefaultEndTime: this.activeDictionaryKey.postData.ib2DefaultEndTime ? format(endOfMinute(this.activeDictionaryKey.postData.ib2DefaultEndTime), 'yyyy-MM-dd HH:mm:ss') : undefined,
     ib2DefaultDayStart: this.activeDictionaryKey.postData.ib2DefaultDayStart[0].checked ? 1 : 0,
     ib2DefaultDayEnd: this.activeDictionaryKey.postData.ib2DefaultDayEnd[0].checked ? 1 : 0,
   };
   console.log(postData);
   this.btnLoading = true;
   this.mainService
     .simplePost("addData", "taskTypeConfig", postData)
     .subscribe((result) => {
       this.btnLoading = false;
       if (result.status == 200) {
         this.message.success("保存成功！");
         this.getConfig();
       }else{
         this.message.success("保存失败！");
       }
     });
 }

 //初始化
 init() {
   this.loading = true;
   this.getTaskType();
 }

 //获取任务类型
 getTaskType() {
   this.loading = true;
   let postData = {
     idx: 0,
     sum: 1,
     taskType: {
       simpleQuery: true,
       hosId: {
         id: this.hosId
       },
       associationType: {
         key: 'association_types',
         value: 'inspect',
       }
     }
   };
   this.mainService
     .getFetchDataList("simple/data", "taskType", postData)
     .subscribe((result) => {
       this.loading = false;
       if (result.status == 200) {
         this.tasktype = result.list[0] || {};
         this.getConfig();
       }
     });
 }

 // 获取配置
 getConfig() {
   this.loading = true;
   let postData = {
     idx: 0,
     sum: 1,
     taskTypeConfig: {
       taskTypeDTO: {
         hosId: {
           id: this.hosId
         },
         associationType: this.tasktype.associationType,
       }
     }
   };
   this.mainService
     .getFetchDataList("simple/data", "taskTypeConfig", postData)
     .subscribe((result) => {
       this.loading = false;
       if (result.status == 200) {
         this.configs = result.list[0] || {};
         this.activeDictionaryKey.postData.ibActiveStartTime1 = this.configs.ibActiveStartTime1 ? new Date(this.configs.ibActiveStartTime1) : undefined;
         this.activeDictionaryKey.postData.ibActiveEndTime1 = this.configs.ibActiveEndTime1 ? new Date(this.configs.ibActiveEndTime1) : undefined;
         this.activeDictionaryKey.postData.ibActiveStartTime2 = this.configs.ibActiveStartTime2 ? new Date(this.configs.ibActiveStartTime2) : undefined;
         this.activeDictionaryKey.postData.ibActiveEndTime2 = this.configs.ibActiveEndTime2 ? new Date(this.configs.ibActiveEndTime2) : undefined;
         this.activeDictionaryKey.postData.ibDefaultStartTime = this.configs.ibDefaultStartTime ? new Date(this.configs.ibDefaultStartTime) : undefined;
         this.activeDictionaryKey.postData.ibDefaultEndTime = this.configs.ibDefaultEndTime ? new Date(this.configs.ibDefaultEndTime) : undefined;
         this.activeDictionaryKey.postData.ibDefaultDayStart[0].checked = this.configs.ibDefaultDayStart == 1;
         this.activeDictionaryKey.postData.ibDefaultDayEnd[0].checked = this.configs.ibDefaultDayEnd == 1;

         this.activeDictionaryKey.postData.ib2ActiveStartTime1 = this.configs.ib2ActiveStartTime1 ? new Date(this.configs.ib2ActiveStartTime1) : undefined;
         this.activeDictionaryKey.postData.ib2ActiveEndTime1 = this.configs.ib2ActiveEndTime1 ? new Date(this.configs.ib2ActiveEndTime1) : undefined;
         this.activeDictionaryKey.postData.ib2ActiveStartTime2 = this.configs.ib2ActiveStartTime2 ? new Date(this.configs.ib2ActiveStartTime2) : undefined;
         this.activeDictionaryKey.postData.ib2ActiveEndTime2 = this.configs.ib2ActiveEndTime2 ? new Date(this.configs.ib2ActiveEndTime2) : undefined;
         this.activeDictionaryKey.postData.ib2DefaultStartTime = this.configs.ib2DefaultStartTime ? new Date(this.configs.ib2DefaultStartTime) : undefined;
         this.activeDictionaryKey.postData.ib2DefaultEndTime = this.configs.ib2DefaultEndTime ? new Date(this.configs.ib2DefaultEndTime) : undefined;
         this.activeDictionaryKey.postData.ib2DefaultDayStart[0].checked = this.configs.ib2DefaultDayStart == 1;
         this.activeDictionaryKey.postData.ib2DefaultDayEnd[0].checked = this.configs.ib2DefaultDayEnd == 1;
       }
     });
 }
}
