import { Component, OnInit } from "@angular/core";
import { MainService } from "../../../services/main.service";
import { ToolService } from 'src/app/services/tool.service';
import { NzMessageService } from 'ng-zorro-antd';
import { Subject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';

@Component({
  selector: "app-configuration-key",
  templateUrl: "./configuration-key.component.html",
  styleUrls: ["./configuration-key.component.less"],
})
export class ConfigurationKeyComponent implements OnInit {
  loading:boolean = false; //页面加载的loading
  btnLoading:boolean = false; //提交按钮的loading
  tabModalName:string = 'characteristics'; //当前选中的tab
  hosId = this.tool.getCurrentHospital().id; //当前院区
	hosName = this.tool.getCurrentHospital().hosName //当前院区名称
  // 数字密钥
  returnSpecimenWhetherHandover:any[] = [
    {label:'是否开启',value: 0}
  ];

  // 数字密钥位数
  keyNum:any = 4;
	
	// 数字密钥时效
	keyMinute:any = 3;
	
	// 使用有效期
	keyValid:any = 1;
	
	hospitalList:any[] = [];
	
  // 配置
  configs:any = {};
  // 任务类型
  tasktype:any = {};
  searchTimerSubject = new Subject();
  constructor(private mainService: MainService,private tool: ToolService,private msg: NzMessageService) {}

  ngOnInit():void {
		this.getConfig();
  }

  // 用户输入搜索
  isLoading: boolean = false;
  deptNotAlertIds:any[] = [];
  deptList:any[] = [];
	
  // 保存
  submitForm() {
		for(let i of this.hospitalList){
			if(i.key=='digitalSecretKey'){
				if(this.returnSpecimenWhetherHandover[0].checked){
					i.value = 1
				}else{
					i.value = 0
				}
			}else if(i.key=='numberDigitalSecretKey'){
				i.value = this.keyNum
			}else if(i.key=='digitalSecretKeyValidTime'){
				i.value = this.keyMinute
			}else if(i.key=='validityDigitalSecretKey'){
				i.value = this.keyValid
			}
		}
    this.btnLoading = true;
    this.mainService.simplePost("addListData", "hospitalConfig", this.hospitalList).subscribe((result:any) => {
        this.btnLoading = false;
        if (result.status == 200) {
          this.getConfig();
        }
      });
  }

  // 获取配置
  getConfig() {
    this.loading = true;
    let postData = {
      idx: 0,
      sum: 9999,
			hospitalConfig:{
				hosId:this.hosId,
				model:"all"
			}
    };
    this.mainService.getFetchDataList("simple/data", "hospitalConfig", postData).subscribe((result:any) => {
        this.loading = false;
        if (result.status == 200) {
					this.hospitalList = result.list || [];
					for(let i of this.hospitalList){
						if(i.key=='digitalSecretKey'){
							if(i.value==1){
								this.returnSpecimenWhetherHandover[0].checked = true
							}else{
								this.returnSpecimenWhetherHandover[0].checked = false
							}
						}else if(i.key=='numberDigitalSecretKey'){
							this.keyNum = Number(i.value) 
						}else if(i.key=='digitalSecretKeyValidTime'){
							this.keyMinute = Number(i.value)
						}else if(i.key=='validityDigitalSecretKey'){
							this.keyValid = i.value
						}
					}
        }
      });
  }
}
