import { WorklistAttachService } from './../../absence/service/work-list-attach.service';
import { ModalController } from '@ionic/angular';
import { NotificationGetAttachResponse } from 'src/app/eit/models/NotificationGetAttachRes';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { WorkListButtonRequest } from 'src/app/eit/models/NotificationButtonReq';
import { Component, OnInit } from '@angular/core';
import { WorkListAttachViewComponent } from '../work-list-attach-view/work-list-attach-view.component';

@Component({
  selector: 'app-work-list-attach',
  templateUrl: './work-list-attach.component.html',
  styleUrls: ['./work-list-attach.component.scss'],
})
export class WorkListAttachComponent implements OnInit {

  private WorkListAttachObj: WorkListButtonRequest;
  getPassNotificationDetails: any;
  attachmentRes: any;

  constructor(public cs: CommonService, public modalCtrl: ModalController, public worklistAttachService: WorklistAttachService) {
    this.getPassNotificationDetails = this.cs.sharedService.getSharedData('passNotificationInfo');
    this.WorkListAttachObj = new WorkListButtonRequest();
    this.WorkListAttachObj.P_NOTIFICATION_ID = this.getPassNotificationDetails.NOTIFICATION_ID;
  }

  ngOnInit() {
    this.getAttachmentNotification(this.WorkListAttachObj);
  }

  async OpenAttachFiles(value, Type) {
    // let modal: Modal = this.modalCtrl.create('WorkListAttachViewPage', { displayData: value, TypeData: Type });
    // modal.present();

    this.cs.sharedService.setSharedData({ displayData: value, TypeData: Type }, 'WorkListAttachViewPage')
    const modal = await this.modalCtrl.create({
      component: WorkListAttachViewComponent
    });

    return await modal.present();
  }

  getAttachmentNotification(WorkListAttachObj) {

    this.worklistAttachService.getAttach(WorkListAttachObj).
      subscribe((result: NotificationGetAttachResponse) => {
        this.handleWorkListAttachResult(result);
      });
  }

  handleWorkListAttachResult(result) {
    if (this.cs.validResponse(result)) {
      // this.sharedData.setSharedData(result, WorKListResponse.SHARED_DATA);
      if (result.GetAttachementList != null) {
        this.attachmentRes = result.GetAttachementList;
      } // if result == null
    } // valid it
  }

}
