import { Component, OnInit, Output, Input } from '@angular/core';
import { EventEmitter, ElementRef, AfterViewInit, ViewChild  } 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-smart-dialogue',
  templateUrl: './smart-dialogue.component.html',
  styleUrls: ['./smart-dialogue.component.less']
})
export class SmartDialogueComponent implements OnInit  {
  @Output() cancelModal = new EventEmitter();
  @Input() knowledgeData: any = {};
	@ViewChild('scrollContainer', {static: false}) scrollContainer: ElementRef;
	
  constructor(
    private mainService: MainService,
    private sanitizer: DomSanitizer,
    private tool: ToolService,
  ) { }

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
  }
		
	userContent:any;
  hosId: number;
  messagesData:any = [];
	isDisabled:boolean = false;
	isLoad:boolean = false;
	
	// 导出
	exportLoading:boolean = false;
	exportWord(item){
		this.exportLoading = true;
		this.mainService.exportDeepSeek({
			chatList:[item]
		}).subscribe(
		  (data:any) => {
				this.exportLoading = false;
		    var file = new Blob([data], {
		      type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
		    });
		    var fileURL = URL.createObjectURL(file);
		    var a = document.createElement("a");
		    a.href = fileURL;
		    a.target = "_blank";
		    a.download = new Date().getTime() + ".docx";
		    document.body.appendChild(a);
		    a.click();
		  },
		  (err) => {
		    this.exportLoading = false;
		  }
		);
	}
	
	// 回车
	handleEnter(){
		this.searchDeepSeek()
	}
	
  searchDeepSeek(){
		if(this.userContent=='' || this.userContent == undefined){
			return
		}
		this.isDisabled = true
		this.isLoad = false
		let messagesStr = JSON.parse(JSON.stringify(this.userContent))
		this.messagesData.push({
			role: "user",
			content: messagesStr,
			// timestamp: new Date(), //时间戳
		});
		this.userContent = '';
		this.messagesData.push({
			role: "assistant",
			content: "正在思考中...",
			// timestamp: new Date(), //时间戳
		});
		let arr = this.messagesData.filter((item) => {
			return item.content.indexOf("正在思考中") === -1;
		});
		setTimeout(_=>{
			this.scrollContainer.nativeElement.scrollTop = this.scrollContainer.nativeElement.scrollHeight;
		},100)
		this.mainService.getDeepSeekData({
			chatList:arr
		})
			.subscribe((res:any) => {
				// messageContainer.scrollTop = messageContainer.scrollHeight;
				this.messagesData = this.messagesData.filter((item) => {
				  return item.content.indexOf("正在思考中") === -1;
				});
				
				if (res.state==200) {
					let data = JSON.parse(res.data)
					console.log(564,data)
					this.messagesData.push({
						role: "assistant",
						content: data.choices[0].message.content,
						// timestamp: new Date(), //时间戳
					});
				} else {
					this.messagesData.push({
						role: "assistant",
						content: "抱歉，我现在无法回答您的问题，请稍后再试。",
						// timestamp: new Date(), //时间戳
					});
				}
				this.isLoad = true;
				setTimeout(_=>{
					this.isLoad = false;
				},2000)
				setTimeout(_=>{
					this.scrollContainer.nativeElement.scrollTop = this.scrollContainer.nativeElement.scrollHeight;
					this.isDisabled = false
				},100)
			});
  }

  // 隐藏模态框
  hideModal() {
    this.cancelModal.emit(false)
  }
}
