import { Component, OnInit, Output, Input, ViewChild, ElementRef } from '@angular/core';
import { EventEmitter } from '@angular/core';
import { OverlayScrollbarsComponent } from 'overlayscrollbars-ngx';
import { FormGroup, FormBuilder, FormControl } from '@angular/forms';
import { MainService } from 'src/app/services/main.service';
import { ToolService } from 'src/app/services/tool.service';
import { Subject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';

@Component({
  selector: 'app-edit-work-order-remark',
  templateUrl: './edit-work-order-remark.component.html',
  styleUrls: ['./edit-work-order-remark.component.less']
})
export class EditWorkOrderRemarkComponent implements OnInit {
  @ViewChild("osComponentRef1", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef1: OverlayScrollbarsComponent;
  @ViewChild("remarksEle", { static: false }) remarksEle: ElementRef;
  @Output() cancelModal = new EventEmitter();
  @Output() confirmModal = new EventEmitter();
  @Input() buildQuickConfirmData: any = {};//数据
  @Input() loading5: boolean = false;//loading
  hosLoading: boolean = false;//确定按钮的loading
  constructor(
    private mainService: MainService,
    private fb: FormBuilder,
    public tool: ToolService,
  ) { }

  ngOnInit() {
    console.log(this.buildQuickConfirmData);
    this.workOrderRemark = this.buildQuickConfirmData.workOrderRemark || "";
    this.newShortcutOrder(this.buildQuickConfirmData.taskType);
  }

  // 建单条件
  workOrderRemark = ""; //备注
  customRemarks = []; //备注快速输入
  buildMsg: any = {}; //一键发起返回信息
  historyCustomRemarks = []; //历史快捷输入
  workOrderRemarkTips = ""; //备注提示
  nLoading = false;
  newShortcutOrder(taskType) {
    console.log(taskType);
    this.nLoading = true;
    this.buildMsg = taskType;
    if (taskType.remarksSwitch == 1) {
      if (taskType.customRemarks === null || taskType.customRemarks === "") {
        this.customRemarks = [];
      } else {
        this.customRemarks = taskType.customRemarks.split("$");
      }
      this.workOrderRemarkTips = taskType.remarksPrompts || "请填写工单备注,不超过100个字符";
      let user = JSON.parse(localStorage.getItem("user"));
      this.mainService.postCustom("nurse", "workOrder/recentRemarks", {
          deptId: this.buildQuickConfirmData.createDept,
          taskTypeId: taskType.id,
        })
        .subscribe((result) => {
          this.nLoading = false;
          if (result.state == 200) {
            this.historyCustomRemarks = result.data;
          }
        });
    } else {
      this.nLoading = false;
    }
  }

  // 添加备注
  addRemarks(item) {
    this.remarksEle.nativeElement.focus();
    this.workOrderRemark += item;
  }

  // 取消
  cancel() {
    this.cancelModal.emit(false)
  }
  // 确认
  confirm() {
    this.confirmModal.emit({workOrderRemark: this.workOrderRemark})
  }
}
