list-doc.component.ts 8.55 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
import { UploadService } from 'src/app/service/upload.service';
8 9
import { TagService } from 'src/app/service/tag.service';
import { TagModel } from 'src/app/model/tag.mmodel';
10 11
import { GroupModel } from 'src/app/model/group.mmodel';
import { GroupService } from 'src/app/service/group.service';
12 13
declare var require: any
const FileSaver = require('file-saver');
14 15 16 17 18 19
@Component({
  selector: 'app-list-doc',
  templateUrl: './list-doc.component.html',
  styleUrls: ['./list-doc.component.scss']
})
export class ListDocComponent implements OnInit {
20 21
  page = 1;
  pageSize = 10;
22 23 24 25 26 27 28 29 30 31
  listDoc: DocumentModel[] = []
  modelDoc: DocumentModel = new DocumentModel({});
  checkEdit: boolean = false
  search: string = ''
  listTag: TagModel[] = []
  listGroup: GroupModel[] = []
  constructor(private modalService: NgbModal, private documentService: DocumentService, private uploadService: UploadService, private tagService: TagService, private groupService: GroupService) {
  }
  async onUploadImage(event: any) {
    try {
32 33 34 35 36 37 38 39 40
      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();
41
          if (data) {
42 43 44 45 46 47 48 49
            this.modelDoc.thumbnail = data.body.fileId
          }
        }
      }
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }
50
  onSelectFile(event: any, lang: string) {
51
    if (event.target.files && event.target.files[0]) {
52 53
      const reader = new FileReader();
      const file: File = event.target.files[0];
54 55
      reader.readAsDataURL(event.target.files[0]); // read file as data url
      reader.onload = (event) => { // called once readAsDataURL is completed
56 57
        if (event) {
          const allowedTypes = ['application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/msword'];
58 59 60 61
          if (!allowedTypes.includes(file.type)) {
            this.openAlertModal('อัพโหลดได้เฉพาะไฟล์ *.doc เท่านั้น')
          } else {
            let base64 = event.target!.result as string
62
            if (lang == 'th') {
63
              this.modelDoc.thDocObj = base64.split(',')[1];
64
            } else {
65 66 67
              this.modelDoc.engDocObj = base64.split(',')[1];
            }
          }
68
        }
69 70
      }
    }
71
  }
72
  async downloadFile(logId: string, lang: string) {
73
    try {
74 75
      const data = await this.documentService.downloadFile(logId, lang).toPromise();
      if (data) {
76 77
        FileSaver.saveAs(new Blob([data]), "file_download.doc");
      }
78 79 80
    } catch (error) {
      console.error('Error loading data:', error);
    }
81
  }
82
  deleteFile(item: DocumentModel) {
83 84 85 86 87 88 89 90 91 92 93
    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 {
94
          this.openAlertModal('ไม่สามารถลบข้อมูลได้')
95 96 97 98 99
        }
      }, error => {
        this.openAlertModal(error.message)
      })
    }, reject => { })
100
  }
101 102 103 104 105 106 107
  openAlertModal(message?: string) {
    const modalRef = this.modalService.open(AlertModalComponent, {
      centered: true,
      backdrop: 'static'
    })
    modalRef.componentInstance.message = message ? message : ""
    modalRef.result.then(result => {
108
      // this.modalService.dismissAll()
109
    }, reason => {
110
      // this.modalService.dismissAll()
111
    })
112 113
  }

114
  onUpdate() {
115
    const modalRef = this.modalService.open(ConfirmModalComponent, {
116 117
      centered: true,
      backdrop: 'static',
118 119 120 121
    })
    modalRef.componentInstance.message = 'คุณต้องการอัพเดทข้อมูลหรือไม่'
    modalRef.result.then(result => {
      this.documentService.createDoc(this.modelDoc).subscribe(result => {
122 123 124 125 126 127 128 129 130 131
        this.modalService.dismissAll()
        this.openAlertModal('อัพเดทข้อมูลสำเร็จ')
        this.getListDoc();
        // if (result) {
        //   this.modalService.dismissAll()
        //   this.openAlertModal('อัพเดทข้อมูลสำเร็จ')
        //   this.getListDoc();
        // } else {
        //   this.openAlertModal('ไม่สามารถอัพเดทข้อมูลได้')
        // }
132 133 134 135 136
      }, error => {
        this.openAlertModal(error.message)
      })
    }, reject => { })
  }
137
  onCreate() {
138 139 140 141 142 143 144 145
    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) {
146
          this.modalService.dismissAll()
147 148 149
          this.openAlertModal('บันทึกข้อมูลสำเร็จ')
          this.getListDoc();
        } else {
150
          this.openAlertModal('ไม่สามารถสร้างเอกสารได้')
151 152 153 154 155 156 157
        }
      }, error => {
        this.openAlertModal(error.message)
      })
    }, reject => { })
  }
  onSumit() {
158 159 160 161 162
    if (this.checkEdit) {
      this.onUpdate();
    } else {
      this.onCreate();
    }
163
  }
164
  async getExcelById(targetModal: NgbModal, id: string) {
165 166 167
    try {
      const data = await this.documentService.getDocById(id).toPromise();
      this.modelDoc = new DocumentModel(data)
168
      if (data) {
169 170 171 172 173 174 175 176 177 178
        this.modalService.open(targetModal, {
          centered: true,
          backdrop: 'static',
          size: 'lg'
        });
      }
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }
179 180 181
  openModal(targetModal: NgbModal, item?: DocumentModel) {
    if (item) {
      this.getExcelById(targetModal, item.logId)
182
      this.checkEdit = true
183
    } else {
184 185 186 187 188 189 190 191
      this.modelDoc = new DocumentModel({});
      this.checkEdit = false
      this.modalService.open(targetModal, {
        centered: true,
        backdrop: 'static',
        size: 'lg'
      });
    }
192

193
  }
194 195
  getStatus(status: string) {
    if (status == '0') {
196
      return 'รออนุมัติ'
197
    } else if (status == '1') {
198
      return 'เปิดใช้งาน'
199
    } else if (status == '2') {
200 201 202
      return 'ไม่อนุมัติ'
    }
  }
203
  async getListDoc() {
204 205
    try {
      const data = await this.documentService.getListDoc().toPromise();
206
      this.listDoc = data.map(x => new DocumentModel(x))
207 208 209 210
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }
211 212
  filterListDoc() {
    return this.listDoc.filter(x => x.thName.toLowerCase().includes(this.search.toLowerCase()) || x.engName.toLowerCase().includes(this.search.toLowerCase()))
213
  }
214

215 216
  ngOnInit() {
    this.getListDoc();
217
    this.getListTag();
218 219
    this.getListGroup();
  }
220 221
  async getListGroup() {
    try {
222 223
      const data = await this.groupService.getList().toPromise();
      this.listGroup = data.map(x => new GroupModel(x))
224
    } catch (error) {
225 226 227
      console.error('Error loading data:', error);
    }
  }
228 229 230
  selectGroup() {
    let findData = this.listGroup.find(x => x.groupId == this.modelDoc.group.groupId)
    this.modelDoc.group = findData ? new GroupModel(findData) : new GroupModel({})
231
  }
232 233
  async getListTag() {
    try {
234 235
      const data = await this.tagService.getList().toPromise();
      this.listTag = data.map(x => new TagModel(x))
236
    } catch (error) {
237 238 239
      console.error('Error loading data:', error);
    }
  }
240 241 242
  closeBtnClick() {
    this.modalService.dismissAll();
  }
243
  openLink(url: string) {
244 245
    window.open(url, "_blank");
  }
246
}