import { ChangeDetectorRef, Component, ElementRef, OnInit } from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { DatasourceTableModel, MyDatasourceTableModel } from 'src/app/model/datasource-table.model';
import { DatasourseTableService } from 'src/app/service/datasourse-table.service';
import { ConfirmModalComponent } from '../confirm-modal/confirm-modal.component';
import { AlertModalComponent } from '../alert-modal/alert-modal.component';

export interface ModalDetail {
    text: { cardHead: string, search: string[], tableHead: string[] }
}
@Component({
    selector: 'app-datasource-table',
    templateUrl: './datasource-table.component.html',
    styleUrls: ['./datasource-table.component.scss']
})
export class DatasourceTableComponent implements OnInit {
    datasourceTable: { data: DatasourceTableModel[], select: DatasourceTableModel, loading: boolean, search: string } = { data: [], select: new MyDatasourceTableModel(), loading: false, search: '' }
    page = 1
    pageSize = 10
    tableHover: Map<string, boolean> = new Map<string, boolean>()
    showPassword = false
    tableShowPassword: Map<string, boolean> = new Map<string, boolean>()
    constructor(private datasourseTableService: DatasourseTableService,
        private modalService: NgbModal,
        private cdr: ChangeDetectorRef) {

    }
    ngOnInit(): void {
        this.getList()
    }
    getList() {
        this.datasourceTable.loading = true
        this.datasourceTable.data = []
        this.datasourseTableService.getList().subscribe(response => {
            this.datasourceTable.data = response.map(e => new MyDatasourceTableModel(e))
            this.datasourceTable.loading = false
        }, error => {
            this.datasourceTable.loading = false
        })
    }
    postData() {
        const modalConfirmRef = this.modalService.open(ConfirmModalComponent, {
            centered: true,
            backdrop: 'static',
        })
        modalConfirmRef.componentInstance.message = 'คุณต้องการบันทึกข้อมูลหรือไม่'
        modalConfirmRef.result.then(result => {
            this.datasourceTable.loading = true
            this.datasourseTableService.post(this.datasourceTable.select).subscribe(response => {
                this.openAlertModal(response.message)
                if (response.success) {
                    this.getList()
                } else {
                    this.datasourceTable.loading = false
                }
            }, error => {
                this.openAlertModal(error.message)
                this.datasourceTable.loading = false
            })
        }, reject => { })
    }
    deleteData() {
        const modalConfirmRef = this.modalService.open(ConfirmModalComponent, {
            centered: true,
            backdrop: 'static',
        })
        modalConfirmRef.componentInstance.message = 'คุณต้องการลบข้อมูลหรือไม่'
        modalConfirmRef.result.then(result => {
            this.datasourceTable.loading = true
            this.datasourseTableService.delete(this.datasourceTable.select).subscribe(response => {
                this.openAlertModal(response.message)
                if (response.success) {
                    this.getList()
                } else {
                    this.datasourceTable.loading = false
                }
            }, error => {
                this.openAlertModal(error.message)
                this.datasourceTable.loading = false
            })
        }, reject => { })
    }
    selectData(data: DatasourceTableModel) {
        this.datasourceTable.select = new MyDatasourceTableModel(data)
        this.scrollTop()
    }
    datasourceTableFilter() {
        return this.datasourceTable.data.filter(e =>
            Object.entries(e)
                .filter(([key]) => key !== 'password') // ตัด field 'password' ออก
                .some(([_, value]) =>
                    value != null && value.toString().toLowerCase().includes(this.datasourceTable.search.toLowerCase())
                )
        )
    }
    scrollTop() {
        window.scrollTo({ top: 0, behavior: 'smooth' });
    }
    openAlertModal(message?: string) {
        const modalRef = this.modalService.open(AlertModalComponent, {
            centered: true,
            backdrop: 'static'
        })
        modalRef.componentInstance.message = message ? message : ""
        modalRef.result.then(result => {
        }, reason => {
        })
    }
    checkDatasourceTable() {
        return Object.values(this.datasourceTable.select).some(value => value === '' || value == null)

    }

}