import { Component, OnInit, Input } from '@angular/core';
import { MainService } from '../../../services/main.service';
import { NzMessageService } from 'ng-zorro-antd';
import { ToolService } from 'src/app/services/tool.service';

@Component({
  selector: 'app-incident-handle-repair',
  templateUrl: './incident-handle-repair.component.html',
  styleUrls: ['./incident-handle-repair.component.less']
})
export class IncidentHandleRepairComponent implements OnInit {
  @Input() incidentData: any = {};

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

  hosId:any;

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.incidentData.callID && this.getCallLog();
    this.incidentData.reqAttachment && this.getRepairImgs();
  }

  // 获取通话音频
  audioSrc =  '';//音频
  getCallLog() {
    let postData = {
      idx: 0,
      sum: 1,
      callLog: {callAccept: this.incidentData.callID},
    };
    this.mainService
      .getFetchDataList('simple/data', 'callLog', postData)
      .subscribe((result) => {
        if(result.status == 200){
          result.list = result.list || [];
          if(result.list.length){
            if(result.list[0].path){
              this.mainService.getCallLogPath({ path: result.list[0].path, hosId: result.list[0].hosId }).subscribe((result) => {
                if (result["state"] == 200) {
                  this.audioSrc = location.origin + result["relativePath"];
                }else{
                  this.audioSrc = '';
                }
              });
            }else{
              this.audioSrc = '';
            }
          }else{
            this.audioSrc = '';
          }
        }else{
          this.message.error(result.msg || '请求数据失败！');
        }
      });
  }

  // 获取报修图片
  repairImgs:any[] = [];//报修图片
  getRepairImgs() {
    this.mainService
      .getPreviewImage('wechatRequesterIncident', this.incidentData.id)
      .subscribe((res:any) => {
        res.data = res.data || [];
        res.data.forEach(v => {
          v.previewUrl = location.origin + "/file" + v.relativeFilePath;
          v.thumbFilePath = location.origin + "/file" + v.thumbFilePath;
        })
        this.repairImgs = res.data;
      });
  }

  // 预览图片
  imgs = [];
  isPreview = false;
  initialViewIndex:number = 0;
  previewImageHandler(data = [], index = 0) {
    this.initialViewIndex = index;
    console.log(index)
    this.isPreview = false;
    data = data || [];
    this.imgs = data.map((v) => location.origin + '/file' + v.relativeFilePath);
    setTimeout(() => {
      this.isPreview = true;
    }, 0)
  }
}




