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-closed-loop",
  templateUrl: "./configuration-inspect-closed-loop.component.html",
  styleUrls: ["./configuration-inspect-closed-loop.component.less"],
})
export class ConfigurationInspectClosedLoopComponent 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;
 }

 // 选择是否开启批量患者回科
 changeBatchPatientBackDept(e){
  if(!e[0].checked){
    this.activeDictionaryKey.postData.batchPatientCheckType = 0;
    this.activeDictionaryKey.postData.batchPatientDynamicKey[0].checked = false;
  }
 }

 // 选择批量患者回科确认方式
 changeBatchPatientCheckType(e){
  this.activeDictionaryKey.postData.batchPatientDynamicKey[0].checked = false;
 }

 // 保存
 btnLoading: boolean = false; //提交按钮loading状态
 submitForm() {
   if(!this.tasktype.id){
     this.message.create("warning", "请先配置任务类型！");
     return;
   }
	 let data = this.activeDictionaryKey.postData;
   let postData:any = {
     ...this.configs,
     taskType: this.tasktype.id,
     hosId: this.hosId,
     batchPatientBackDept: this.activeDictionaryKey.postData.batchPatientBackDept[0].checked ? 1 : 0,
     batchPatientCheckType: this.activeDictionaryKey.postData.batchPatientCheckType || 0,
     batchPatientDynamicKey: this.activeDictionaryKey.postData.batchPatientDynamicKey[0].checked ? 1 : 0,
		 pilAutoBackDept: data.pilAutoBackDept[0].checked ? 1 : 0,
		 pilAutoBackDeptHour: data.pilAutoBackDept[0].checked ? data.pilAutoBackDeptHour : undefined
	 };
   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.batchPatientBackDept[0].checked = this.configs.batchPatientBackDept == 1;
        this.activeDictionaryKey.postData.batchPatientCheckType = this.configs.batchPatientCheckType || 0;
        this.activeDictionaryKey.postData.batchPatientDynamicKey[0].checked = this.configs.batchPatientDynamicKey == 1;
				this.activeDictionaryKey.postData.pilAutoBackDept[0].checked = this.configs.pilAutoBackDept == 1;
				this.activeDictionaryKey.postData.pilAutoBackDeptHour = this.configs.pilAutoBackDeptHour || 0;
			 }
     });
 }
}
