list-excell.component.ts 8.81 KB
Newer Older
1 2
import { Component, OnInit } from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
3 4 5 6
import { ExcelModel } from 'src/app/model/excel.model';
import { ExcelService } from 'src/app/service/excel.service';
import { ConfirmModalComponent } from '../confirm-modal/confirm-modal.component';
import { AlertModalComponent } from '../alert-modal/alert-modal.component';
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 { GroupService } from 'src/app/service/group.service';
import { GroupModel } from 'src/app/model/group.mmodel';
Nakarin Luankla committed
12
import { OpenImageComponent } from '../open-image/open-image.component';
13 14
declare var require: any
const FileSaver = require('file-saver');
15

16 17 18 19 20 21
@Component({
  selector: 'app-list-excel',
  templateUrl: './list-excell.component.html',
  styleUrls: ['./list-excell.component.scss']
})
export class ListExcelComponent implements OnInit {
22 23 24 25 26 27
  page = 1;
  pageSize = 10;
  listExcel:ExcelModel[]=[]
  modelExcel:ExcelModel = new ExcelModel({});
  checkEdit:boolean = false
  search:string = ''
28 29
  
  listTag:TagModel[]=[]
30 31
  listGroup:GroupModel[]=[]
  constructor(private modalService: NgbModal,private excelService:ExcelService,private uploadService:UploadService ,private tagService:TagService,private groupService:GroupService) {
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  }
 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.modelExcel.thumbnail = data.body.fileId
        }
      }
    }
  } catch (error) {
    console.error('Error loading data:', error);
51
  }
52
}
53 54 55 56 57 58 59 60
  async getListTag(){
    try{
      const data = await this.tagService.getList().toPromise();
      this.listTag = data.map(x => new TagModel(x))
    }catch(error){
      console.error('Error loading data:', error);
    }
  }
61 62 63 64 65 66 67 68 69 70 71 72
  async getListGroup(){
    try{
      const data = await this.groupService.getList().toPromise();
      this.listGroup = data.map(x => new GroupModel(x))
    }catch(error){
      console.error('Error loading data:', error);
    }
  }
  selectGroup(){
    let findData = this.listGroup.find(x => x.groupId == this.modelExcel.group.groupId) 
    this.modelExcel.group = findData?new GroupModel(findData):new GroupModel({})
  }
73 74
  onSelectFile(event:any) {
    if (event.target.files && event.target.files[0]) {
75 76
      const reader = new FileReader();
      const file: File = event.target.files[0];
Nakarin Luankla committed
77
      console.log("🚀 ~ ListExcelComponent ~ onSelectFile ~ file:", file)
78 79 80
      reader.readAsDataURL(event.target.files[0]); // read file as data url
      reader.onload = (event) => { // called once readAsDataURL is completed
        if(event){
81
          const allowedTypes = ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.ms-excel.sheet.macroEnabled.12'];
82
          if (!allowedTypes.includes(file.type)) {
83
            this.openAlertModal('อัพโหลดได้เฉพาะไฟล์ *.xlsx *.xlsm เท่านั้น')
84 85 86
          } else {
            let base64 = event.target!.result as string
            this.modelExcel.excelObj = base64.split(',')[1];
Nakarin Luankla committed
87
            this.modelExcel.fileType = file.name.split('.')[file.name.split('.').length-1]
88
          }
89 90 91
       }
      }
    }
92
  }
93
  async downloadFile(logId:string){
94 95
    try {
      const data = await this.excelService.downloadFile(logId).toPromise();
96 97 98
      if(data){
        FileSaver.saveAs(new Blob([data]), "file_download.xlsx");
      }
99 100 101
    } catch (error) {
      console.error('Error loading data:', error);
    }
102
  }
103 104


105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  deleteFile(item:ExcelModel){
    const modalRef = this.modalService.open(ConfirmModalComponent, {
      centered: true,
      backdrop: 'static',
    })
    modalRef.componentInstance.message = 'คุณต้องการลบข้อมูลหรือไม่'
    modalRef.result.then(result => {
      this.excelService.deleteExcel(item).subscribe(result => {
        if (result) {
          this.openAlertModal('ลบข้อมูลสำเร็จ')
          this.getListExcel();
        } else {
      this.openAlertModal('ไม่สามารถลบข้อมูลได้')
        }
      }, error => {
        this.openAlertModal(error.message)
      })
    }, reject => { })
123
  }
124 125 126 127 128 129 130
  openAlertModal(message?: string) {
    const modalRef = this.modalService.open(AlertModalComponent, {
      centered: true,
      backdrop: 'static'
    })
    modalRef.componentInstance.message = message ? message : ""
    modalRef.result.then(result => {
131
      // this.modalService.dismissAll()
132
    }, reason => {
133
      // this.modalService.dismissAll()
134
    })
135 136
  }

137 138 139 140 141 142 143 144 145 146
  onUpdate(){
    const modalRef = this.modalService.open(ConfirmModalComponent, {
      centered: true,
      backdrop: 'static',
    })
    modalRef.componentInstance.message = 'คุณต้องการอัพเดทข้อมูลหรือไม่'
    modalRef.result.then(result => {
      console.log(this.modelExcel);
      this.excelService.createExcel(this.modelExcel).subscribe(result => {
        if (result) {
147
          this.modalService.dismissAll()
148 149 150 151 152 153 154 155 156 157 158 159
          this.openAlertModal('อัพเดทข้อมูลสำเร็จ')
          this.getListExcel();
        } else {
      this.openAlertModal('ไม่สามารถอัพเดทข้อมูลได้')
        }
      }, error => {
        this.openAlertModal(error.message)
      })
    }, reject => { })
  }
  onCreate(){
    const modalRef = this.modalService.open(ConfirmModalComponent, {
160 161
      centered: true,
      backdrop: 'static',
162 163 164 165 166
    })
    modalRef.componentInstance.message = 'คุณต้องการบันทึกข้อมูลหรือไม่'
    modalRef.result.then(result => {
      this.excelService.createExcel(this.modelExcel).subscribe(result => {
        if (result) {
167
          this.modalService.dismissAll()
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
          this.openAlertModal('บันทึกข้อมูลสำเร็จ')
          this.getListExcel();
        } 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.excelService.getExcelById(id).toPromise();
      this.modelExcel = new ExcelModel(data)
189
      console.log("🚀 ~ ListExcelComponent ~ getExcelById ~ this.modelExcel:", this.modelExcel)
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
      if(data){
        this.modalService.open(targetModal, {
          centered: true,
          backdrop: 'static',
          size: 'lg'
        });
      }
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }
  openModal(targetModal: NgbModal,item?:ExcelModel) {
    if(item){
      this.getExcelById(targetModal,item.logId)
      this.checkEdit = true
    }else{
      this.modelExcel = new ExcelModel({});
      this.checkEdit = false
      this.modalService.open(targetModal, {
        centered: true,
        backdrop: 'static',
        size: 'lg'
      });
    }
   
  }
216 217 218
  openLink(url:string){
    window.open(url, "_blank");
  }
219 220 221 222 223 224 225 226 227 228 229 230
  getStatus(status:string){
    if(status == '0'){
      return 'รออนุมัติ'
    } else if(status == '1'){
      return 'เปิดใช้งาน'
    } else if(status == '2'){
      return 'ไม่อนุมัติ'
    }
  }
  async getListExcel(){
    try {
      const data = await this.excelService.getListExcel().toPromise();
231
      this.listExcel = data.map(x => new ExcelModel(x))
232 233 234 235 236 237 238 239 240 241
    } catch (error) {
      console.error('Error loading data:', error);
    }
  }
  filterListExcel(){
    return this.listExcel.filter(x => x.thName.toLowerCase().includes(this.search.toLowerCase())||x.engName.toLowerCase().includes(this.search.toLowerCase()))
  }
  
  ngOnInit() {
    this.getListExcel();
242
    this.getListTag();
243
    this.getListGroup();
244 245
  }

Nakarin Luankla committed
246 247 248 249 250 251 252 253 254 255 256
  openEmployeeModal(image:string) {
    const modalRef = this.modalService.open(OpenImageComponent, {
      centered: true,
      windowClass: 'my-dialog-img-preview'
    })
    modalRef.componentInstance.linkImage = image
    modalRef.result.then(result => {
    }, reason => {
      this.modalService.dismissAll()
    })
  }
257 258 259 260 261
  closeBtnClick() {
    this.modalService.dismissAll();
  }

}