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

@Component({
  selector: "app-configuration-sys",
  templateUrl: "./configuration-sys.component.html",
  styleUrls: ["./configuration-sys.component.less"],
})
export class ConfigurationSysComponent implements OnInit {
  constructor(
    private route: ActivatedRoute,
    private mainService: MainService,
    private tool: ToolService,
    private message: NzMessageService,
  ) {}

  coopData: any = {}; //当前操作列
  currentHospital; //当前院区
	maintainDept:any=[];
	hospitalModel:any = 1;
	@Input() hospitalTabList: any[] = [];
	checkOptionsOne = [
		{ label: '科内报修', value: 'deptRepair', checked:false},
		{ label: '公共报修', value: 'publicRepair', checked:false },
	];
	government:any = [
    {label:'是否开启',value: 0}
  ];
	
	// 申请人是否必填
	applicantMustFillIn:any= 0;
	
  ngOnInit() {
		// this.activeDictionaryKey = this.hospitalTabList[0];
		this.maintainDept = this.tool.getUserInfoPermission().dutyList
    this.currentHospital = this.tool.getCurrentHospital();
		this.getSysConfig();
  }
	
	// 刷新耗材缓存
	cacheLoading:boolean = false;
	resetCache(){
		this.cacheLoading = true;
		this.mainService.resetCache('commonlyUseConsumables').subscribe(
		  (result) => {
		    this.cacheLoading = false;
		    if (result["status"] == 200) {
		      this.showPromptModal("刷新", true, "");
		    } else {
		      this.showPromptModal("刷新", false, "");
		    }
		  },
		  (error) => {
		    this.cacheLoading = false;
		    this.showPromptModal("刷新", false, "");
		  }
		);
	}
	
  // 新增/编辑表单提交
  btnLoading: boolean = false; //提交按钮loading状态
  submitMessageForm(): void {
		let filterData = this.initConfig.filter(
		  (item) =>
				item.keyconfig === "deptRepair" ||
				item.keyconfig === "publicRepair" ||
				item.keyconfig === "hospitalModel" ||
				item.keyconfig === "applicantMustFillIn"||
				item.keyconfig === "governmentDuty"
		)
		filterData.forEach((item) => {
		 if (item.keyconfig === "deptRepair") {
				let value = null
				if(this.checkOptionsOne[0].checked){
					value = '1'
				}else{
					value = '0'
				}
		    item.valueconfig = value;
		  } else if (item.keyconfig === "publicRepair") {
			  let value = null
			  if(this.checkOptionsOne[1].checked){
			  	value = '1'
			  }else{
			  	value = '0'
			  }
			  item.valueconfig = value;
			}else if (item.keyconfig === "hospitalModel") {
		    item.valueconfig = this.hospitalModel;
		  }else if (item.keyconfig === "applicantMustFillIn") {
		    item.valueconfig = this.applicantMustFillIn;
		  }else if (item.keyconfig === "governmentDuty") {
				let value = null
				if(this.government[0].checked){
					value = '1'
				}else{
					value = '0'
				}
				item.valueconfig = value;
		  }
		});
		const postData = filterData;
		this.btnLoading = true;
		this.mainService.changeSysConfig(postData).subscribe(
		  (result) => {
		    this.btnLoading = false;
		    if (result["status"] == 200) {
		      this.showPromptModal("保存", true, "");
		    } else {
		      this.showPromptModal("保存", false, "");
		    }
		  },
		  (error) => {
		    this.btnLoading = false;
		    this.showPromptModal("保存", false, "");
		  }
		);
  }

 // 获取系统配置
 loading1:any = false
 initConfig:any;
 config:any;
 getSysConfig() {
	const postData = { idx: 0, sum: 99 };
	this.mainService
		.getFetchDataList("simple/data", "systemConfiguration", postData)
		.subscribe((result) => {
			this.loading1 = false;
			if (result.status == 200) {
				if (result.list && Array.isArray(result.list)) {
					this.initConfig = JSON.parse(JSON.stringify(result.list));
					this.config = result.list.map((item) => {
						return [item.keyconfig, item.valueconfig, item.id, item.label];
					});
					this.config.forEach((c) => {
						switch (c[0]) {
							case "deptRepair":
								if(c[1]=='1'){
									this.checkOptionsOne[0].checked = true
								}else{
									this.checkOptionsOne[0].checked = false
								}
								break;
							case "publicRepair":
								if(c[1]=='1'){
									this.checkOptionsOne[1].checked = true
								}else{
									this.checkOptionsOne[1].checked = false
								}
								break;
							case "hospitalModel":
								this.hospitalModel =  Number(c[1])
								break;	
							case "applicantMustFillIn":
								this.applicantMustFillIn =  Number(c[1])
								break;
							case "governmentDuty":
								if(c[1]=='1'){
									this.government[0].checked = true
								}else{
									this.government[0].checked = false
								}
								break;
						}
					});
				} else {
					this.initConfig = [];
					this.config = [];
				}
			}
		});
  }

  // 展示信息提示框（con:提示信息，success:操作是否成功，promptInfo:操作结果提示信息）
  promptContent: string; //操作提示框提示信息
  ifSuccess: boolean; //操作成功/失败
  promptInfo: string; //操作结果提示信息
  promptModalShow: boolean; //操作提示框是否展示
  showPromptModal(con, success, promptInfo?) {
    this.promptModalShow = false;
    this.promptContent = con;
    this.ifSuccess = success;
    this.promptInfo = promptInfo;
    setTimeout(() => {
      this.promptModalShow = true;
    }, 100);
    this.getSysConfig();
  }
}
