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

  coopData: any = {}; //当前操作列
  currentHospital; //当前院区

  ngOnInit() {
    this.currentHospital = this.tool.getCurrentHospital();
    this.getList();
    this.getClassList(1);
    this.getClassList(2);
  }

  // 新增/编辑表单提交
  btnLoading: boolean = false; //提交按钮loading状态
  dataInfo:any = {};
  submitMessageForm(): void {
    this.btnLoading = true;
    let postData:any = {
      workdayClassId: this.dataInfo.workdayClassId,
      holidayClassId: this.dataInfo.holidayClassId,
      socketUrl: this.dataInfo.socketUrl,
    };
    postData = { ...this.dataInfo, ...postData };
    this.mainService
      .simplePost("addData", "callCenterConfig", postData)
      .subscribe((result) => {
        this.btnLoading = false;
        let msg = "保存";
        if (result.status == 200) {
          this.showPromptModal(msg, true, '');
        } else {
          this.showPromptModal(msg, false, result.msg);
        }
      });
  }

  // 获取班次
  isLoading:boolean = false;
  classList1: any = [];
  classList2: any = [];
  // type: 1工作日，2节假日
  getClassList(type:number) {
    let postData = {
      idx: 0,
      sum: 9999,
      scheduleClass: {
        hospital: { id: this.currentHospital.id },
        bindCall: 1,
        type,
      },
    };
    this.mainService
      .getFetchDataList("simple/data", "scheduleClass", postData)
      .subscribe((data) => {
        this.isLoading = false;
        this['classList' + type] = data.list || [];
      });
  }

  // 获取列表
  loading1:boolean = false;
  getList() {
    let data = {
      idx: 0,
      sum: 9999,
      callCenterConfig: {},
    };
    this.loading1 = true;
    this.mainService
      .getFetchDataList("simple/data", "callCenterConfig", data)
      .subscribe((data) => {
        this.loading1 = false;
        if (data.status == 200) {
          let list = data.list || [];
          this.dataInfo = list.length ? list[0] : {};
        }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();
  }
}
