import { Component, OnInit, Output, Input } from '@angular/core';
import { EventEmitter } from '@angular/core';
import { MainService } from 'src/app/services/main.service';
import { DomSanitizer } from '@angular/platform-browser';
import { ToolService } from 'src/app/services/tool.service';
import { format } from 'date-fns';

@Component({
  selector: 'app-knowledge-look',
  templateUrl: './knowledge-look.component.html',
  styleUrls: ['./knowledge-look.component.less']
})
export class KnowledgeLookComponent implements OnInit {
  @Output() cancelKnowledgeModal = new EventEmitter();
  @Input() knowledgeData: any = {};
  constructor(
    private mainService: MainService,
    private sanitizer: DomSanitizer,
    private tool: ToolService,
  ) { }

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.getDetail(this.knowledgeData);
  }

  // 获取知识库详情
  isSpinning: boolean = false; //页面loading状态
  vIndex:number = 0;//版本号下标
  hosId: number;
  rowId=null;
  detailData:any;
  richText:any;
  lookData:any;
  solutionLogs:any;
  stepLength: number = 0; //步骤条
  getDetail(data){
    this.rowId = data.id
		this.vIndex = 0
    let query = {
			idx: 0,
			sum: 9999,
			solution: {
				// hosId: data.hosId,
				operationType:'lookOver',
				solutionNumber:data.solutionNumber
			}
		};
		this.isSpinning = true
		this.mainService
			.getFetchDataList("data", "solution", query)
			.subscribe((res) => {
				this.getFile()
				let data = res.list
				this.detailData = data
				let content = data.length ? data[0].content : ''
				if(content){
					this.richText = this.sanitizer.bypassSecurityTrustHtml(content);
				}else{
					this.richText = ''
				}
				for(let i of data){
					if(i.solutionLogs.length>0){
						for(let t of i.solutionLogs){
							t.time = format(t.operationTime, 'yyyy-MM-dd HH:mm')
						}
					}
				}
				this.lookData = data[0]
				this.solutionLogs = data.length ? data[0].solutionLogs : []
				this.stepLength = this.solutionLogs.length
				this.isSpinning = false
			});
  }

  fileData: any = []; //上传的附件数据
  getFile(){
		this.mainService
			.getPreviewImage('solution', this.rowId)
			.subscribe((res:any)=> {
				this.fileData = res.data.map((v) =>{
					return{
						name: v.name,
						url: location.origin + '/file' + v.relativeFilePath
					}
				});
			});
	}

  // 版本号点击
	versionsClick(data, index){
		this.vIndex = index
		this.lookData = data
		let content = data.content
		if(content){
			this.richText = this.sanitizer.bypassSecurityTrustHtml(content);
		}else{
			this.richText = ''
		}
	}

  // 点击附件
	fileClick(data){
		console.log(222,data)
		let a: any = document.createElement('a') // 创建一个元素
		a.style = 'display: none' // 不能在页面中被看到，把他隐藏起来
		a.style.height = '0px' // 给个0高度，避免影响页面布局
		a.download = data.name;
		a.href = data.url // 文件url地址
		document.body.appendChild(a)  //  将其绑定在body上才能发挥作用
		a.click() // 触发a标签的click事件
		document.body.removeChild(a) // 删除该元素
	}
  // 隐藏模态框
  hideModal() {
    this.cancelKnowledgeModal.emit(false)
  }
}
