list-doc.component.ts 7.28 KB
Newer Older
1 2
import { Component, OnInit } from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
3 4 5 6
import { ConfirmModalComponent } from '../confirm-modal/confirm-modal.component';
import { AlertModalComponent } from '../alert-modal/alert-modal.component';
import { DocumentService } from 'src/app/service/document.service';
import { DocumentModel } from 'src/app/model/document.model';
7 8 9
import { UploadService } from 'src/app/service/upload.service';
declare var require: any
const FileSaver = require('file-saver');
10 11 12 13 14 15
@Component({
  selector: 'app-list-doc',
  templateUrl: './list-doc.component.html',
  styleUrls: ['./list-doc.component.scss']
})
export class ListDocComponent implements OnInit {
16 17 18 19 20 21
  page = 1;
  pageSize = 10;
  listDoc:DocumentModel[]=[]
  modelDoc:DocumentModel = new DocumentModel({});
  checkEdit:boolean = false
  search:string = ''
22
  constructor(private modalService: NgbModal,private documentService:DocumentService, private uploadService:UploadService) {
23
  }
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  async onUploadImage(event:any){
    try{
      if (event.target.files.length > 0) {
        let fileData = event.target.files[0];
        const formData = new FormData();
        formData.append('file', fileData);
        const allowedTypes = ['image/png', 'image/jpeg', 'image/jpg'];
        if (!allowedTypes.includes(fileData.type)) {
          this.openAlertModal('อัพโหลดได้เฉพาะไฟล์ *.jpeg, *.jpg, *.png เท่านั้น')
        } else {
          const data = await this.uploadService.uploadImage(formData).toPromise();
          if(data){
            this.modelDoc.thumbnail = data.body.fileId
          }
        }
      }
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }
  onSelectFile(event:any,lang:string) {
45
    if (event.target.files && event.target.files[0]) {
46 47
      const reader = new FileReader();
      const file: File = event.target.files[0];
48 49 50
      reader.readAsDataURL(event.target.files[0]); // read file as data url
      reader.onload = (event) => { // called once readAsDataURL is completed
        if(event){
51 52 53 54 55 56 57 58 59 60 61
          const allowedTypes = ['application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
          if (!allowedTypes.includes(file.type)) {
            this.openAlertModal('อัพโหลดได้เฉพาะไฟล์ *.doc เท่านั้น')
          } else {
            let base64 = event.target!.result as string
            if(lang == 'th'){
              this.modelDoc.thDocObj = base64.split(',')[1];
            }else {
              this.modelDoc.engDocObj = base64.split(',')[1];
            }
          }
62 63 64
       }
      }
    }
65
  }
66
  async downloadFile(logId:string,lang:string){
67
    try {
68 69 70 71
      const data = await this.documentService.downloadFile(logId,lang).toPromise();
      if(data){
        FileSaver.saveAs(new Blob([data]), "file_download.doc");
      }
72 73 74
    } catch (error) {
      console.error('Error loading data:', error);
    }
75
  }
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  deleteFile(item:DocumentModel){
    const modalRef = this.modalService.open(ConfirmModalComponent, {
      centered: true,
      backdrop: 'static',
    })
    modalRef.componentInstance.message = 'คุณต้องการลบข้อมูลหรือไม่'
    modalRef.result.then(result => {
      this.documentService.deleteExcel(item).subscribe(result => {
        if (result) {
          this.openAlertModal('ลบข้อมูลสำเร็จ')
          this.getListDoc();
        } else {
      this.openAlertModal('ไม่สามารถลบข้อมูลได้')
        }
      }, error => {
        this.openAlertModal(error.message)
      })
    }, reject => { })
94 95

  }
96 97 98 99 100 101 102
  openAlertModal(message?: string) {
    const modalRef = this.modalService.open(AlertModalComponent, {
      centered: true,
      backdrop: 'static'
    })
    modalRef.componentInstance.message = message ? message : ""
    modalRef.result.then(result => {
103
      // this.modalService.dismissAll()
104
    }, reason => {
105
      // this.modalService.dismissAll()
106
    })
107 108
  }

109 110
  onUpdate(){
    const modalRef = this.modalService.open(ConfirmModalComponent, {
111 112
      centered: true,
      backdrop: 'static',
113 114 115 116 117 118
    })
    modalRef.componentInstance.message = 'คุณต้องการอัพเดทข้อมูลหรือไม่'
    modalRef.result.then(result => {
      console.log(this.modelDoc);
      this.documentService.createDoc(this.modelDoc).subscribe(result => {
        if (result) {
119
          this.modalService.dismissAll()
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
          this.openAlertModal('อัพเดทข้อมูลสำเร็จ')
          this.getListDoc();
        } else {
      this.openAlertModal('ไม่สามารถอัพเดทข้อมูลได้')
        }
      }, error => {
        this.openAlertModal(error.message)
      })
    }, reject => { })
  }
  onCreate(){
    const modalRef = this.modalService.open(ConfirmModalComponent, {
      centered: true,
      backdrop: 'static',
    })
    modalRef.componentInstance.message = 'คุณต้องการบันทึกข้อมูลหรือไม่'
    modalRef.result.then(result => {
      this.documentService.createDoc(this.modelDoc).subscribe(result => {
        if (result) {
139
          this.modalService.dismissAll()
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
          this.openAlertModal('บันทึกข้อมูลสำเร็จ')
          this.getListDoc();
        } else {
      this.openAlertModal('ไม่สามารถสร้างเอกสารได้')
        }
      }, error => {
        this.openAlertModal(error.message)
      })
    }, reject => { })
  }
  onSumit() {
   if(this.checkEdit){
    this.onUpdate();
   }else{
    this.onCreate();
   }
  }
  async getExcelById(targetModal: NgbModal,id:string){
    try {
      const data = await this.documentService.getDocById(id).toPromise();
      this.modelDoc = new DocumentModel(data)
      if(data){
        this.modalService.open(targetModal, {
          centered: true,
          backdrop: 'static',
          size: 'lg'
        });
      }
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }
  openModal(targetModal: NgbModal,item?:DocumentModel) {
    if(item){
      this.getExcelById(targetModal,item.logId)
      this.checkEdit = true
    }else{
      this.modelDoc = new DocumentModel({});
      this.checkEdit = false
      this.modalService.open(targetModal, {
        centered: true,
        backdrop: 'static',
        size: 'lg'
      });
    }
   
  }
  getStatus(status:string){
    if(status == '0'){
      return 'รออนุมัติ'
    } else if(status == '1'){
      return 'เปิดใช้งาน'
    } else if(status == '2'){
      return 'ไม่อนุมัติ'
    }
  }
  async getListDoc(){
    try {
      const data = await this.documentService.getListDoc().toPromise();
199
      this.listDoc = data.map(x => new DocumentModel(x))
200 201 202 203 204 205 206 207 208 209
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }
  filterListDoc(){
    return this.listDoc.filter(x => x.thName.toLowerCase().includes(this.search.toLowerCase())||x.engName.toLowerCase().includes(this.search.toLowerCase()))
  }
  
  ngOnInit() {
    this.getListDoc();
210 211 212 213 214 215
  }


  closeBtnClick() {
    this.modalService.dismissAll();
  }
216 217 218
  openLink(url:string){
    window.open(url, "_blank");
  }
219
}