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

@Component({
  selector: "app-configuration-checkout-project",
  templateUrl: "./configuration-checkout-project.component.html",
  styleUrls: ["./configuration-checkout-project.component.less"],
})
export class ConfigurationCheckoutProjectComponent implements OnInit {
  constructor(
    private mainService: MainService,
    private fb: FormBuilder,
    private tool: ToolService,
    private message: NzMessageService,
  ) {}

  coopData: any = {}; //当前操作列
  hosId: any;
	changeInpSubject = new Subject(); //防抖

  ngOnInit() {
		this.hosId = this.tool.getCurrentHospital().id;
		this.changeInpSubject.pipe(debounceTime(500)).subscribe((v) => {
		  this.getDept(v[0]);
		});
    setTimeout(() => {
      this.tableWechatHeight = document.querySelector('#wechatTable').clientHeight - document.querySelector('#wechatTable .list-template__top').clientHeight - 8 - document.querySelector('#wechatTable .thead').clientHeight;
    }, 0)
    this.getTypes();
		this.getTubeTypeList();
		this.getDept();
    this.getList();
  }

  // 新增弹框
  modelName = ""; //模态框名称
  modalWechat: boolean = false; //新增/编辑模态框
  add: boolean; //true:新增；false:编辑
  addWechatModal() {
    this.modelName = "新增";
    this.add = true; //新增
    this.modalWechat = true;
    this.initWechatForm();
  }
  //关闭新增/编辑弹框
  hideWechatModal() {
    this.modalWechat = false;
  }

  // 新增/编辑表单提交
  btnLoading: boolean = false; //提交按钮loading状态
  submitWechatForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
    if (this.validateForm.invalid) {
      return;
    }
    this.btnLoading = true;
    let postData = {
			orders: this.validateForm.value.orders,
			name: this.validateForm.value.name,
			value: this.validateForm.value.value,
			extra4: this.validateForm.value.extra4,
			extra5: this.validateForm.value.extra5,
			extra7: this.validateForm.value.extra7.toString(),
			desc: this.validateForm.value.desc,
			key: "examination_items"
    };

    this.mainService
      .simplePost("addData", "dictionary", postData)
      .subscribe((result) => {
        this.btnLoading = false;
        this.hideWechatModal();
        let msg = "新增";
        if (result.status == 200) {
          this.showPromptModal(msg, true, '');
        } else {
          this.showPromptModal(msg, false, result.msg);
        }
      });
  }
	
	// 隐藏删除框
	hideDelModal() {
	  this.delModal = false;
	}
	// 确认删除
	confirmDel() {

	}
	
  // 初始化新增form表单
  validateForm: FormGroup; //新增/编辑表单
  initWechatForm() {
    this.validateForm = this.fb.group({
      orders: [null, [Validators.required]],
      name: [null, [Validators.required]],
      value: [null, [Validators.required]],
			extra4: [null, [Validators.required]],
      extra5: [null, [Validators.required]],
      extra7: [null, [Validators.required]],
      desc: [null, [Validators.required]],
    });
  }

  // 获取列表
  loading1:boolean = false;
  dataList: any[] = []; //表格数据
  tableWechatHeight:number = 0;
  getList() {
    let data = {
      idx: 0,
      sum: 9999,
      dictionary: {
        key: "examination_items",
      },
    };
    this.loading1 = true;
    this.mainService
      .getFetchDataList("simple/data", "dictionary", data)
      .subscribe((data) => {
        this.loading1 = false;
        if (data.status == 200) {
					data.list.forEach(i=>{
						i.dept = i.deptList.map(x=>{
							return x.dept
						})
						
					})
          this.dataList = data.list || [];
        }else{
          this.message.error(data.msg || "请求数据失败");
        }
      });
  }

  isLoading = false;
  extra7List: any = [
    { name: '无', id: '0' },
    { name: '科室码', id: '1' },
    { name: '动态码', id: '2' },
    { name: '填写工号', id: '3' },
  ];
	
	// 获取试管类型
	tubeTypeList: any = [];
	getTubeTypeList() {
	  this.mainService.getDictionary('list', 'specimen_tube_type').subscribe((data) => {
	    this.tubeTypeList = data || [];
	  });
	}
	
	// 获取标本类型
	typeList: any = [];
	getTypes() {
	  this.mainService.getDictionary('list', 'specimen_type').subscribe((data) => {
	    this.typeList = data || [];
	  });
	}
	
	// 获取检验科室
	deptList: any = [];
	getDept(dept?) {
	  let data = {
	    idx: 0,
	    sum: 10,
	    department: {
	      hospital: { id: this.hosId },
	      dept: dept,
	    },
	  };
	  
	  this.mainService
	    .getFetchDataList("data", "department", data)
	    .subscribe((data) => {
	      this.isLoading = false;
	      this.deptList = data.list;
	    });
	}
	
	changeInp(e){
		this.isLoading = true;
		this.changeInpSubject.next([e]);
	}
	
  delModal: boolean = false; //删除模态框
  tipsMsg1: string; //提示框信息
  tipsMsg2: string; //操作后信息
  confirmDelType: string; //确认的类型（启用/停用，删除）
  showDelModal(
    data,
    tipsMsg1: string,
    tipsMsg2: string,
    type: string,
  ) {
    this.confirmDelType = type;
    this.delModal = true;
    this.coopData = data;
    this.tipsMsg1 = tipsMsg1;
    this.tipsMsg2 = tipsMsg2;
  }

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