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-distribution-hospital",
  templateUrl: "./configuration-distribution-hospital.component.html",
  styleUrls: ["./configuration-distribution-hospital.component.less"],
})
export class ConfigurationDistributionHospitalComponent implements OnInit {
  constructor(
    private route: ActivatedRoute,
    private mainService: MainService,
    private tool: ToolService,
    private message: NzMessageService,
  ) {}

  coopData: any = {}; //当前操作列
  currentHospital; //当前院区
	maintainDept:any=[];
	@Input() hospitalTabList: any[] = [];
	
  ngOnInit() {
		this.activeDictionaryKey = this.hospitalTabList[0];
		this.maintainDept = this.tool.getUserInfoPermission().dutyList
    this.currentHospital = this.tool.getCurrentHospital();
		this.getDicType();
  }
	
	activeDictionaryKey:any;
	clickDictionaryKey(item){
	  this.activeDictionaryKey = item;
	  this.getList();
	}
	
	// 获取评价字典
	dicData:any=[];
	getDicType(){
		this.mainService.getDictionary("list", "workorderEvaluation").subscribe((data) => {
			this.dicData = data
			this.getList();
		});
	}
	
  // 新增/编辑表单提交
  btnLoading: boolean = false; //提交按钮loading状态
  submitMessageForm(): void {
    this.btnLoading = true;
		for(let i of this.hospitalList){
			if(i.key=='hsmsUserBadEvaluationRange' || i.key=='hsmsCallbackBadEvaluationRange'){
				i.value = i.value.join(',')
			}
		}
		setTimeout(_=>{
			this.mainService
			  .simplePost("addListData", "hospitalConfig", this.hospitalList)
			  .subscribe((result) => {
			    this.btnLoading = false;
			    let msg = "保存";
			    if (result.status == 200) {
			      this.showPromptModal(msg, true, '');
			    } else {
			      this.showPromptModal(msg, false, result.msg);
			    }
			  });
		},50)
  }

  // 获取列表
  loading1:boolean = false;
  hospitalList:any[] = [];
  getList() {
    let data = {
      idx: 0,
      sum: 9999,
      hospitalConfig: {
        hosId: this.currentHospital.id,
        model: 'hsms',
      },
    };
    this.loading1 = true;
    this.mainService
      .getFetchDataList("simple/data", "hospitalConfig", data)
      .subscribe((data) => {
        this.loading1 = false;
        if (data.status == 200) {
					data.list.forEach(i =>{
					  i.show = false
					})
					let newData = data.list
					for(let i of newData){
						if(i.key=='hsmsUserEvaluation' || i.key=='hsmsCallbackEvaluation'){
							i.value = Number(i.value)
						}
						
						if(i.key=='hsmsUserBadEvaluationRange' || i.key=='hsmsCallbackBadEvaluationRange'){
							let arr = i.value.split(',')
							arr = arr.map(i=>i = parseInt(i))
							i.value = arr
						}
					}

          this.hospitalList = newData || [];
        }else{
          this.message.error(data.msg || "请求数据失败");
        }
      });
  }

  // 展示信息提示框（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.getList();
  }
}
