import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { format, startOfDay, endOfDay } from 'date-fns';
import { MainService } from 'src/app/services/main.service';
import { ToolService } from 'src/app/services/tool.service';
import { NzMessageService } from 'ng-zorro-antd';
@Component({
  selector: 'app-incident-ser-call',
  templateUrl: './incident-ser-call.component.html',
  styleUrls: ['./incident-ser-call.component.less']
})
export class IncidentSerCallComponent implements OnInit {
  // 切换科室，切换弹窗
  loading = false;
  messageList: any = [];// 通话记录列表
  pageIndex: number = 1;//表格当前页码
  pageSize: number = 10;//表格每页展示条数
  total: number = 0;//表格总数据量

  searchDTO: any = {};

  @Input() itsmData:any;
  @Input() hsmsData:any;
  @Output() closeModelHs = new EventEmitter<any>();//1.组件暴露一个 EventEmitter 属性，当事件发生时，子组件利用该属性 emits(向上弹射)事件

  constructor(
    private mainService: MainService,
    private tool: ToolService,
    private message: NzMessageService,
  ) { }

  ngOnInit() {
    this.getMessageList(1);
  }
  // 关闭弹窗
  hideModal() {
    this.closeModelHs.emit({ show: false });//emits(向上弹射)事件
  }

  // 搜索
  search() {
    this.getMessageList(1);
  }
  // 重置
  reset() {
    this.searchDTO = {};
    this.getMessageList(1);
  }

  // 回放录音
  coopData:any = {};
  recordingModalShow = false; //弹窗开关
  maskFlag:any = false;
  recordcall(data) {
    this.maskFlag = this.message.loading("正在加载中..", {
      nzDuration: 0,
    }).messageId;
    this.mainService.getCallLogPath({ path: data.path, hosId: data.hosId }).subscribe((result) => {
      this.message.remove(this.maskFlag);
      this.maskFlag = false;
      if (result["state"] == 200) {
        this.coopData = { relativePath: result["relativePath"] };
        this.recordingModalShow = true;
      }
    });
  }
  // 关闭弹窗
  closeRecordingModelOrder(e) {
    this.recordingModalShow = JSON.parse(e).show;
  }

  // 分页获取数据
  getList(){
    this.getMessageList();
  }

  // 获取通话记录列表
  getMessageList(idx?) {
    if (idx) {
      this.pageIndex = 1;
    }
    console.log(this.itsmData)
    console.log(this.hsmsData)
    let hosIds;
    if(this.itsmData.mdv2Switch){
      if(this.itsmData.allDuty == 1){
        hosIds = undefined;
      }else{
        hosIds = this.itsmData.checkedHos.map(v => v.id).toString();
      }
    }
    if(this.hsmsData.hsmsSwitch){
      if(this.itsmData.mdv2Switch){
        if(this.itsmData.allDuty == 1){
          hosIds = undefined;
        }else{
          hosIds += ',' + this.hsmsData.checkedHos;
        }
      }else{
        hosIds = this.hsmsData.checkedHos.toString();
      }
    }
    let postData = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      callLog: {
        hosIds,
        dTMFA: this.searchDTO.dTMFA || undefined,
        dTMFB: this.searchDTO.dTMFB || undefined,
        responseTime: this.searchDTO.responseTime ? format(this.searchDTO.responseTime, 'yyyy-MM-dd HH:mm:ss') : undefined,
      }
    }
    this.loading = true;
    this.mainService.getFetchDataList('simple/data','callLog',postData).subscribe(data => {
      this.loading = false;
      this.messageList = data.list || [];
      this.total = data.totalNum;
    })
  }
}



