import { Component, OnInit } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { FormBuilder, Validators, FormGroup } from "@angular/forms";

import { ToolService } from "../../services/tool.service";
import { CheckAndExcludeConfigService } from './check-and-exclude-config.service';
@Component({
  selector: "app-check-and-exclude-config",
  templateUrl: "./check-and-exclude-config.component.html",
  styleUrls: ["./check-and-exclude-config.component.less"],
})
export class CheckAndExcludeConfigComponent implements OnInit {
  constructor(
    private fb: FormBuilder,
    private checkAndExcludeConfigService: CheckAndExcludeConfigService,
    private route: ActivatedRoute,
    private tool: ToolService,
  ) {}

  hosId: any; //当前选中院区
  listOfData: any[] = []; //表格数据
  pageIndex: number = 1; //表格当前页码
  pageSize: number = 10; //表格每页展示条数
  listLength: number = 0; //表格总数据量
  modal: boolean = false; //新增/编辑模态框
  add: boolean; //true:新增；false:编辑
  validateForm: FormGroup; //新增/编辑表单
  coopId: number; //当前操作列id

  btnLoading: boolean = false; //提交按钮loading状态

  promptModal:any = {
    content : '', //操作提示框提示信息
    success : false, //操作成功/失败
    show : '', //操作结果提示信息
  }
  //匹配方式
  matchTypes: any[] = [
    {id: 1, name: '模糊匹配'},
    {id: 2, name: '完全匹配'},
  ];

  ngOnInit() {
    this.coopBtns = this.tool.initCoopBtns(this.route);
    this.hosId = this.tool.getCurrentHospital().id;
    this.getList(true);
  }

  // 初始化增删改按钮
  coopBtns: any = {};

  // 批量转换/一键转换/一键恢复
  convertOrRestore(type, data?){
    this.showConvertOrRestoreModal(type, data);
  }
  // 表格数据
  loading = false;
  getList(isInit = false) {
    isInit && (this.pageIndex = 1);
    let postData = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      dataFieldRule: {
        hosId: this.hosId,
      },
    };
    this.loading = true;
    this.checkAndExcludeConfigService
      .query(postData)
      .subscribe((result: any) => {
        this.loading = false;
        if (result.status == 200) {
          this.listOfData = result.list;
          this.listLength = result.totalNum;
        }
      });
  }

  // 新增/编辑弹框
  addModal() {
    this.add = true; //新增
    this.modal = true;
    this.initForm();
  }
  //关闭新增/编辑弹框
  hideAddModal() {
    this.modal = false;
    this.initForm();
  }
  // 初始化新增form表单
  initForm() {
    this.validateForm = this.fb.group({
      key1: [null, [Validators.required]],
      matchType: [null, [Validators.required]],
    });
  }

  // 新增/编辑表单提交
  submitForm(): void {
    this.btnLoading = true;
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
    if (this.validateForm.invalid) {
      this.btnLoading = false;
      return;
    }
    let postData = {};

    if (this.add) {
      //新增
      postData = {
        hosId: this.hosId,
        key1: this.validateForm.controls.key1.value,
        matchType: this.validateForm.controls.matchType.value,
      };
    } else {
      //编辑
      postData = {
        id: this.coopId,
        hosId: this.hosId,
        key1: this.validateForm.controls.key1.value,
        matchType: this.validateForm.controls.matchType.value,
      };
    }
    this.checkAndExcludeConfigService
      .add(postData)
      .subscribe((result: any) => {
        this.btnLoading = false;
        this.hideAddModal();
        if (result.status == 200) {
          this.showPromptModal(this.add ? "新增" : "修改", true);
        } else {
          this.showPromptModal(this.add ? "新增" : "修改", false);
        }
      });
  }

  // 编辑
  edit(data) {
    this.coopId = data.id;

    this.add = false;
    this.modal = true;
    this.initForm();

    this.validateForm.controls.key1.setValue(data.key1);
    this.validateForm.controls.matchType.setValue(data.matchType.toString());
  }

  // 展示信息提示框（content:提示信息，success:操作是否成功，info:操作结果提示信息）
  showPromptModal(content, success, info = '') {
    this.promptModal = {
      content,
      success,
      show: false,
      info,
    }
    setTimeout(() => {
      this.promptModal.show = true;
    }, 100);

    this.getList();
  }

  // 删除快捷建单
  dialogDelete:any = {
    delModal: false, //删除模态框
    content: '', //提示框信息
  }
  showDelModal(id: number, content: string) {
    this.coopId = id;

    this.dialogDelete = {
      delModal: true,
      content,
    }
  }
  // 隐藏删除框
  hideDelModal() {
    this.dialogDelete.delModal = false;
  }
  // 确认删除
  confirmDel() {
    this.dialogDelete.btnLoading = true;
    //删除
    let postData = [this.coopId];
    this.checkAndExcludeConfigService
      .delete(postData)
      .subscribe((data: any) => {
        this.dialogDelete.btnLoading = false;
        this.dialogDelete.delModal = false;
        if (data.status == 200) {
          this.showPromptModal('删除', true);
        } else {
          this.showPromptModal('删除', false, data.msg);
        }
      });
  }
  // ---------------------------------------
  // 批量转换/一键转换/一键恢复
  dialogConvertOrRestore:any = {
    delModal: false, //删除模态框
    content: '', //提示框信息
  }
  coopType:string = '';
  coopData:any = {};
  showConvertOrRestoreModal(type, data?) {

    let content = '';
    switch(type){
      case 'batchConversion':
        content = '您确认要转换最近两个半月的所有排除项匹配的检查数据吗？';
        break;
      case 'oneClickConversion':
        content = `您确认要转换最近两个半月的匹配“${data.key1}”检查数据吗？`;
        break;
      case 'oneClickRecovery':
        content = `您确认要恢复最近两个半月的匹配“${data.key1}”检查数据吗？`;
        break;
    }

    this.coopType = type;
    this.coopData = data;

    this.dialogConvertOrRestore = {
      delModal: true,
      content,
    }
  }
  // 隐藏
  hideConvertOrRestoreModal() {
    this.dialogConvertOrRestore.delModal = false;
  }
  // 确认
  confirmConvertOrRestore() {
    this.dialogConvertOrRestore.btnLoading = true;
    let type_back = '';
    switch(this.coopType){
      case 'batchConversion':
        type_back = 'inspectTrans';
        break;
      case 'oneClickConversion':
        type_back = 'inspectTrans';
        break;
      case 'oneClickRecovery':
        type_back = 'inspectRecover';
        break;
    }
    let postData = {
      hosId:this.hosId,
      type: type_back,
      id: this.coopData ? this.coopData.id : undefined,
    };
    this.checkAndExcludeConfigService
      .convertOrRestore(postData)
      .subscribe((result: any) => {
        this.dialogConvertOrRestore.btnLoading = false;
        this.dialogConvertOrRestore.delModal = false;
        if (result.state == 200) {
          this.showPromptModal('操作', true);
        }else{
          this.showPromptModal('操作', false, result.msg);
        }
      });
  }
}

