import { Component, OnInit } from "@angular/core";
import { MainService } from "../../services/main.service";
import { format } from "date-fns/esm";
import { NzMessageService } from "ng-zorro-antd";
import { ToolService } from "src/app/services/tool.service";
@Component({
  selector: "app-call-log",
  templateUrl: "./call-log.component.html",
  styleUrls: ["./call-log.component.less"],
})
export class CallLogComponent implements OnInit {
  constructor(
    private message: NzMessageService,
    private mainService: MainService,
    private tool: ToolService
  ) {}

  searchCriteria: any = {
    //搜索条件
    callDept: "", //来电科室
    dTMFA: "", //主叫号码
    responseTime: null, //开始时间
    callState: null, //已接/未接
    callType: null, //呼入/呼出
  };
  hosId;
  audioPath = ""; //录音文件地址
  listOfData: any[] = []; //表格数据
  pageIndex: number = 1; //表格当前页码
  pageSize: number = 10; //表格每页展示条数
  listLength: number = 10; //表格总数据量
  modal: boolean = false; //新增/编辑模态框

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.getList(1);
  }
  // 选择已接/未接
  changeHospital(id) {
    this.searchCriteria.callState = id;
  }
  // 选择呼入/呼出
  changeCall(id) {
    this.searchCriteria.callType = id;
  }
  // 重置
  reset() {
    this.searchCriteria = {
      //搜索条件
      callDept: "", //来电科室
      dTMFA: "", //主叫号码
      responseTime: null, //开始时间
      callState: null, //已接/未接
      callType: null, //1呼入/0呼出/2-
    };
    this.getList(1);
  }

  // 表格数据
  loading1 = false;
  getList(type) {
    if (type == 1) {
      this.pageIndex = 1;
    }
    let data = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      callLog: {
        hosId: this.hosId,
        callDept: this.searchCriteria.callDept, //来电科室
        dTMFA: this.searchCriteria.dTMFA, //主叫号码
        responseTime:
          this.searchCriteria.responseTime &&
          format(this.searchCriteria.responseTime, "yyyy-MM-dd HH:mm:ss"), //开始时间
        callState: this.searchCriteria.callState, //已接/未接
        callType: this.searchCriteria.callType, //已接/未接
      },
    };
    //过滤掉空字符串和null
    for (let key in data.callLog) {
      if (data.callLog[key] === "" || data.callLog[key] === null) {
        delete data.callLog[key];
      }
    }
    this.loading1 = true;
    this.mainService
      .getFetchDataList("callLog", "callLog", data)
      .subscribe((result) => {
        this.loading1 = false;
        if (result["status"] == 200) {
          this.listOfData = result["list"];
          this.listLength = result["totalNum"];
        }
      });
  }

  // 播放录音弹框
  maskFlag: any = false;
  addModal(path) {
    this.maskFlag = this.message.loading("正在加载中..", {
      nzDuration: 0,
    }).messageId;
    this.mainService.getCallLogPath({ path, hosId: this.hosId }).subscribe((result) => {
      this.message.remove(this.maskFlag);
      this.maskFlag = false;
      this.modal = true;
      if (result["state"] == 200) {
        this.audioPath = location.origin + result["relativePath"];
      }
    });
  }
  //关闭播放录音弹框
  hideAddModal() {
    this.modal = false;
  }
}
