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';
@Component({
  selector: 'app-incident-ser-message',
  templateUrl: './incident-ser-message.component.html',
  styleUrls: ['./incident-ser-message.component.less']
})
export class IncidentSerMessageComponent implements OnInit {
  // 切换科室，切换弹窗
  loading = false;
  messageList: any = [];// 留言列表
  pageIndex: number = 1;//表格当前页码
  pageSize: number = 10;//表格每页展示条数
  total: number = 0;//表格总数据量

  tabs:any[] = [
    {id: 1, name: '全部', value: '', num: ''},
    {id: 2, name: '未受理', value: 0, num: ''},
    {id: 3, name: '已受理', value: 1, num: ''},
    {id: 4, name: '不受理', value: 2, num: ''},
  ]

  searchDTO: any = {};

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

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

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

  // 点击tab
  activeTabValue:any;
  clickTbab(item){
    this.activeTabValue = item.value;
    this.getMessageList(1);
  }

  onCalendarChangeDate(dateArr){
    console.log(dateArr)
    if(dateArr.length == 2){
      let dateStart = new Date(dateArr[0]);
      let dateEnd = new Date(dateArr[1]);
      dateStart.setHours(0,0,0);
      dateEnd.setHours(23,59,59);
      this.searchDTO.dateRange = [dateStart,dateEnd];
    }
  }

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

  delModal: boolean = false; //删除模态框
  tipsMsg1: string; //提示框信息
  tipsMsg2: string; //操作后信息
  confirmDelType: string; //确认的类型（启用/停用，删除）
  coopData:any = {};
  showDelModal(data, tipsMsg1: string, tipsMsg2: string, type: string) {
    this.confirmDelType = type;
    this.delModal = true;
    this.coopData = data;
    this.tipsMsg1 = tipsMsg1;
    this.tipsMsg2 = tipsMsg2;
  }
  // 隐藏删除框
  hideDelModal() {
    this.delModal = false;
  }
  // 确认删除
  btnLoading: boolean = false; //确认按钮loading状态
  confirmDelFn() {
    this.btnLoading = true;
    if (this.confirmDelType === "notAccepted") {
      //不受理
      this.mainService
        .simplePost("updData", "hjzxRecord", { ...this.coopData, item: undefined, isExcute: 2 })
        .subscribe((data) => {
          this.btnLoading = false;
          this.delModal = false;
          if (data.status == 200) {
            this.showPromptModal(this.tipsMsg2, true, "");
          } else {
            this.showPromptModal(this.tipsMsg2, false, data.msg);
          }
        });
    }
  }

  // 生成工单
  createOrder(data){
    this.closeModelHs.emit({ show: false, data: data });
  }

  // 回放录音
  recordingModalShow = false; //弹窗开关
  recordcall(data) {
    this.coopData = data;
    this.recordingModalShow = true;
  }
  // 关闭弹窗
  closeRecordingModelOrder(e) {
    this.recordingModalShow = JSON.parse(e).show;
  }

  // 展示信息提示框（con:提示信息，success:操作是否成功，promptInfo:操作结果提示信息）（con:提示信息，success:操作是否成功，promptInfo:操作结果提示信息）
  promptContent: string; //操作提示框提示信息
  ifSuccess: boolean; //操作成功/失败
  promptInfo: string; //操作结果提示信息
  promptModalShow: boolean; //是否展示提示框
  showPromptModal(con, success, promptInfo?) {
    this.promptModalShow = false;
    this.promptContent = con;
    this.ifSuccess = success;
    this.promptInfo = promptInfo;
    setTimeout(() => {
      this.promptModalShow = true;
      this.getList();
    }, 100);
  }

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

  // 获取留言列表
  getMessageList(idx?) {
    if (idx) {
      this.pageIndex = 1;
    }
    let postData = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      hjzxRecord: {
        isExcute: this.activeTabValue === '' ? undefined : this.activeTabValue,
        startTime: this.searchDTO.dateRange ? format(startOfDay(this.searchDTO.dateRange[0]), 'yyyy-MM-dd HH:mm:ss') : undefined,
        endTime: this.searchDTO.dateRange ? format(endOfDay(this.searchDTO.dateRange[1]), 'yyyy-MM-dd HH:mm:ss') : undefined,
      }
    }
    this.loading = true;
    this.mainService.getFetchDataList('simple/data','hjzxRecord',postData).subscribe(data => {
      this.loading = false;
      this.messageList = data.list || [];
      this.total = data.totalNum;
    })
  }

  // 查看ITSM工单详情
  detailModalShow = false; //弹窗开关
  openItsmDetails(data) {
    this.coopData = data;
    this.detailModalShow = true;
  }
  // 关闭弹窗
  closeDetailModelOrder(e) {
    this.detailModalShow = JSON.parse(e).show;
  }
  // 弹窗确定
  confirmDetailModelOrder(e){
    console.log(e);
    this.detailModalShow = false;
  }
}



