<div class="w-full min-height-50px mb-10px justify-between items-center">
    <div class="flex justify-between">
        <div class="flex">
            <div class="flex items-center">
                <input type="checkbox" class="ti-form-checkbox pointer-events-none" id="hs-default-checkbox"
                    [(ngModel)]="isDataListChecked">
                <label for="hs-default-checkbox" class="text-sm text-gray-500 mx-2 pointer-events-none">
                    {{numDataListChecked}} Selected</label>
            </div>
            <div class="mx-1 flex items-center">
                <button (click)="isDataListCheckedAll = !isDataListCheckedAll;dataListCheckAll()" id='check-boxall'
                    class="focus:ring-2 focus:ring-primary rounded-sm flex item-center">
                    <i class="fs-l transition-all duration-200"
                        [ngClass]="{'ri-checkbox-multiple-line text-gray-500': !isDataListCheckedAll, 'ri-checkbox-multiple-fill text-primary': isDataListCheckedAll}"></i>
                </button>
                <label class="text-sm text-gray-500 ml-2" for="check-boxall">Select All</label>
            </div>
        </div>
        <div class="flex justify-end">
            <div class="px-1">
                <div class="relative shadow-md">
                    <input type="text" id="hs-leading-icon" name="hs-leading-icon"
                        class="ti-form-input ltr:pl-11 rtl:pr-11 focus:z-10 " placeholder="Search by No. or Name"
                        [(ngModel)]="search" (ngModelChange)="searchChange()">
                    <div
                        class="absolute inset-y-0 ltr:left-0 rtl:right-0 flex items-center pointer-events-none z-20 ltr:pl-4 rtl:pr-4">
                        <i class="ri-search-line text-gray"></i>
                    </div>
                </div>
            </div>
            <div class="px-1">
                <button type="button"
                    class=" h-45px ti-btn ti-btn bg-pink-500/10 text-pink-500 hover:text-white hover:bg-pink-500 ring-offset-white focus:ring-pink-500 dark:focus:ring-offset-white/10 h-10 m-0 shadow-md"
                    data-hs-overlay="#department-register-upload-modal"
                    (click)="fileInput.value = '';selectedFile=null;selectedFileName = 'กรุณาเลือกไฟล์'">
                    <i class="ti ti-file-plus"></i>
                    import
                </button>
            </div>
            <div class="px-1">
                <button type="button" class="ti-btn ti-btn-soft-secondary h-45px m-0 shadow-md"
                    data-hs-overlay="#department-register-modal-add" (click)="currentModel='add';selectBu1()">
                    <i class="ri-add-line"></i>
                    Add
                </button>
            </div>
            <!-- <div class="px-1">
                <button href="javascript:void(0);" class="ti-btn ti-btn-soft-info h-45px m-0 shadow-md">
                    <i class="ri-printer-line"></i>
                    Print
                </button>
            </div> -->
            <div class="px-1">
                <button href="javascript:void(0);" class="ti-btn ti-btn-soft-danger h-45px m-0 shadow-md"
                    (click)="currentModel='deleteGroup';selectBu1()"
                    data-hs-overlay="#department-register-delete-alert-modal">
                    <i class="ri-delete-bin-6-line"></i>
                    Delete
                </button>
            </div>
            <!-- <div class="px-1">
                <button href="javascript:void(0);" class="ti-btn ti-btn-soft-warning h-45px m-0 shadow-md">
                    <i class="ti ti-book fs-l"></i>
                    Help
                </button>
            </div> -->
        </div>
    </div>
</div>

<div class="page px-rem">
    <div class="overflow-auto shadow-md rounded-t-md">
        <table class="ti-custom-table ti-custom-table-head ti-custom-table-hover table-bordered">
            <thead>
                <tr>
                    <ng-container
                        *ngFor="let item of ['','รหัสฝ่าย','รายละเอียดฝ่าย (ไทย)','รายละเอียดฝ่าย (อังกฤษ)','การจัดการ']; let f = first; let l = last">
                        <th scope="col" class="relative px-10px py-10px bg-soft-secondary text-primary !text-center">
                            <span class="font-size-12px font-weight-700">{{ item }}</span>
                            <div class="absolute top-1/2 transform -translate-y-1/2 right-0" *ngIf="!f&&!l">
                                <i class="ti ti-dots-vertical fs-l"></i>
                            </div>
                        </th>
                    </ng-container>
                </tr>
            </thead>
            <tbody *ngIf="bu1ListLoading">
                <tr>
                    <td class="text-center" colspan="100%">
                        <div *ngFor="let item of [1,2,3]" class="ti-spinner w-8 h-8 text-secondary mx-1" role="status"
                            aria-label="loading">
                            <span class="sr-only">Loading...</span>
                        </div>
                    </td>
                </tr>
            </tbody>
            <tbody *ngIf="!bu1ListLoading&&!bu1ListFilter().length">
                <tr>
                    <td class="text-center" colspan="100%">
                        ไม่พบข้อมูล
                    </td>
                </tr>
            </tbody>
            <tbody *ngIf="!bu1ListLoading&&bu1ListFilter().length">
                <tr
                    *ngFor="let item of bu1ListFilter() | slice:((currentPage-1) * pageSize) : (((currentPage-1) * pageSize) + pageSize);let i = index">
                    <td class="text-center">
                        <input type="checkbox" class="ti-form-checkbox cursor-pointer" id="checkbox-{{item.data.bu1id}}"
                            [(ngModel)]="item.check" (ngModelChange)="dataListCheck()">
                    </td>
                    <td class="text-center">{{item.data.bu1id}}</td>
                    <td>{{item.data.tdesc}}</td>
                    <td>{{item.data.edesc}}</td>
                    <td class="flex justify-center">
                        <i class="ti ti-edit cursor-pointer i-gray fs-l px-1"
                            (click)="currentModel='edit';selectBu1(item.data)"
                            data-hs-overlay="#department-register-modal-edit"></i>
                        <!-- <i class="ti ti-trash cursor-pointer i-gray fs-l px-1"
                            (click)="currentModel='delete';selectBu1(item)"
                            data-hs-overlay="#department-register-delete-alert-modal"></i> -->
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <app-pagination [totalItems]="bu1ListFilter().length" [pageSize]="pageSize" (pageChange)="currentPage = $event"
        (pageSizeChange)="pageSize = $event;currentPage = 1"></app-pagination>
</div>


<div id="department-register-modal-add" class="hs-overlay hidden ti-modal">
    <div class="hs-overlay-open:mt-7 ti-modal-box mt-0 ease-out h-[calc(100%-3.5rem)]">
        <div class="max-h-full overflow-hidden ti-modal-content">
            <div class="ti-modal-header">
                <h3 class="text-xxl font-bold text-primary">
                    เพิ่มข้อมูลทะเบียนฝ่าย
                </h3>
                <div class="flex justify-end">
                    <button type="button" class="hs-dropdown-toggle ti-modal-clode-btn text-danger"
                        data-hs-overlay="#department-register-modal-add">
                        <span class="sr-only">Close</span>
                        <i class="ti ti-circle-x fs-xxl"></i>
                    </button>
                </div>
            </div>
            <div class="w-full flex justify-end">
                <div class="absolute flex">
                    <div class="px-1">
                        <button type="button" class="ti-btn ti-btn-soft-indigo h-45px m-0 shadow-md"
                            (click)="selectBu1()">
                            <svg class="svg-indigo" width="16" height="16" viewBox="0 0 64.00 64.00"
                                xmlns="http://www.w3.org/2000/svg" fill="none" stroke="#595BEA" stroke-width="3.84"
                                transform="rotate(45)matrix(-1, 0, 0, 1, 0, 0)">
                                <g id="SVGRepo_bgCarrier" stroke-width="0"></g>
                                <g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
                                <g id="SVGRepo_iconCarrier">
                                    <path d="M15 49A24 24 0 0 1 32 8"></path>
                                    <path d="M49 15a24 24 0 0 1-17 41"></path>
                                    <polyline points="15.03 40 15.03 48.97 8 48.97"></polyline>
                                    <polyline points="48.97 24 48.97 15.03 56 15.03"></polyline>
                                </g>
                            </svg>
                            Clear
                        </button>
                    </div>
                    <!-- <div class="px-1">
                        <button href="javascript:void(0);" class="ti-btn ti-btn-soft-warning h-45px m-0 shadow-md">
                            <i class="ti ti-book fs-l"></i>
                            Help
                        </button>
                    </div> -->
                </div>
            </div>
            <div class="ti-modal-body padding-16px pt-0 overflow-y-0">
                <label for="input-label" class="ti-form-label mt-2rem">ฝ่าย<span class="text-danger">*</span></label>
                <input type="text" id="input-label" class="ti-form-input w-1/2" [(ngModel)]="bu1.bu1id">
                <label for="detail_th" class="ti-form-label mt-2rem">รายละเอียดฝ่าย (ไทย)<span
                        class="text-danger">*</span></label>
                <input type="text" id="detail_th" class="ti-form-input h-16" [(ngModel)]="bu1.tdesc">
                <label for="detail_eng" class="ti-form-label mt-2rem">รายละเอียดฝ่าย (อังกฤษ)</label>
                <input type="text" id="detail_eng" class="ti-form-input h-16" [(ngModel)]="bu1.edesc">
                <div class="flex justify-end mt-2rem mb-1rem">
                    <button type="button"
                        class="hs-dropdown-toggle ti-btn ti-border font-medium bg-white text-gray-700 shadow-sm align-middle hover:bg-gray-50 focus:ring-offset-white focus:ring-primary dark:bg-bgdark dark:hover:bg-black/20 dark:border-white/10 dark:text-white/70 dark:hover:text-white dark:focus:ring-offset-white/10"
                        data-hs-overlay="#department-register-modal-add">
                        ย้อนกลับ
                    </button>
                    <button type="button" class="ti-btn ti-btn-success"
                        data-hs-overlay="#department-register-alert-modal"
                        [class.ti-btn-disabled]="!bu1.bu1id||!bu1.tdesc" [disabled]="!bu1.bu1id||!bu1.tdesc">
                        บันทึกข้อมูล
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="department-register-modal-edit" class="hs-overlay hidden ti-modal">
    <div class="hs-overlay-open:mt-7 ti-modal-box mt-0 ease-out h-[calc(100%-3.5rem)]">
        <div class="max-h-full overflow-hidden ti-modal-content">
            <div class="ti-modal-header">
                <h3 class="text-xxl font-bold text-primary">
                    แก้ไขข้อมูลทะเบียนฝ่าย
                </h3>
                <div class="flex justify-end">
                    <button type="button" class="hs-dropdown-toggle ti-modal-clode-btn text-danger"
                        data-hs-overlay="#department-register-modal-edit">
                        <span class="sr-only">Close</span>
                        <i class="ti ti-circle-x fs-xxl"></i>
                    </button>
                </div>
            </div>
            <div class="w-full flex justify-end">
                <div class="absolute flex">
                    <div class="px-1">
                        <button type="button" class="ti-btn ti-btn-soft-indigo h-45px m-0 shadow-md"
                            (click)="bu1.tdesc='';bu1.edesc=''">
                            <svg class="svg-indigo" width="16" height="16" viewBox="0 0 64.00 64.00"
                                xmlns="http://www.w3.org/2000/svg" fill="none" stroke="#595BEA" stroke-width="3.84"
                                transform="rotate(45)matrix(-1, 0, 0, 1, 0, 0)">
                                <g id="SVGRepo_bgCarrier" stroke-width="0"></g>
                                <g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
                                <g id="SVGRepo_iconCarrier">
                                    <path d="M15 49A24 24 0 0 1 32 8"></path>
                                    <path d="M49 15a24 24 0 0 1-17 41"></path>
                                    <polyline points="15.03 40 15.03 48.97 8 48.97"></polyline>
                                    <polyline points="48.97 24 48.97 15.03 56 15.03"></polyline>
                                </g>
                            </svg>
                            Clear
                        </button>
                    </div>
                    <!-- <div class="px-1">
                        <button href="javascript:void(0);" class="ti-btn ti-btn-soft-warning h-45px m-0 shadow-md">
                            <i class="ti ti-book fs-l"></i>
                            Help
                        </button>
                    </div> -->
                </div>
            </div>
            <div class="ti-modal-body ">
                <label for="input-label" class="ti-form-label mt-2rem">ฝ่าย<span class="text-danger">*</span></label>
                <input type="text" id="input-label" class="ti-form-input w-1/2 bg-input-readonly"
                    [(ngModel)]="bu1.bu1id" readonly>
                <label for="detail_th" class="ti-form-label mt-2rem">รายละเอียดฝ่าย (ไทย)<span
                        class="text-danger">*</span></label>
                <input type="text" id="detail_th" class="ti-form-input h-16" [(ngModel)]="bu1.tdesc">
                <label for="detail_eng" class="ti-form-label mt-2rem">รายละเอียดฝ่าย (อังกฤษ)</label>
                <input type="text" id="detail_eng" class="ti-form-input h-16" [(ngModel)]="bu1.edesc">
                <div class="flex justify-end mt-2rem mb-1rem">
                    <button type="button"
                        class="hs-dropdown-toggle ti-btn ti-border font-medium bg-white text-gray-700 shadow-sm align-middle hover:bg-gray-50 focus:ring-offset-white focus:ring-primary dark:bg-bgdark dark:hover:bg-black/20 dark:border-white/10 dark:text-white/70 dark:hover:text-white dark:focus:ring-offset-white/10"
                        data-hs-overlay="#department-register-modal-edit">
                        ย้อนกลับ
                    </button>
                    <button type="button" class="ti-btn ti-btn-success"
                        data-hs-overlay="#department-register-alert-modal"
                        [class.ti-btn-disabled]="!bu1.bu1id||!bu1.tdesc" [disabled]="!bu1.bu1id||!bu1.tdesc">
                        บันทึกข้อมูล
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="department-register-alert-modal" class="hs-overlay hidden ti-modal">
    <div class="hs-overlay-open:mt-7 ti-modal-box mt-0 ease-out h-[calc(100%-3.5rem)] flex items-center">
        <div class="max-h-full overflow-hidden ti-modal-content w-full">
            <div class="ti-modal-header">
                <h3 class="text-xxl font-bold text-primary">
                    แจ้งเตือน
                </h3>
                <div class="flex justify-end">
                    <button type="button" class="hs-dropdown-toggle ti-modal-clode-btn text-danger"
                        [attr.data-hs-overlay]="currentModel=='add'?'#department-register-modal-add':'#department-register-modal-edit'">
                        <span class="sr-only">Close</span>
                        <i class="ti ti-circle-x fs-xxl"></i>
                    </button>
                </div>
            </div>
            <div class="ti-modal-body ">
                <p class="mt-1 text-gray-800 dark:text-white/70">
                    ยืนยันการบันทึกข้อมูลหรือไม่
                </p>

                <div class="flex justify-end mt-2rem mb-1rem">
                    <button type="button"
                        class="hs-dropdown-toggle ti-btn ti-border font-medium bg-white text-gray-700 shadow-sm align-middle hover:bg-gray-50 focus:ring-offset-white focus:ring-primary dark:bg-bgdark dark:hover:bg-black/20 dark:border-white/10 dark:text-white/70 dark:hover:text-white dark:focus:ring-offset-white/10"
                        [attr.data-hs-overlay]="currentModel=='add'?'#department-register-modal-add':'#department-register-modal-edit'">
                        ย้อนกลับ
                    </button>
                    <button type="button" class="ti-btn ti-btn-success" (click)="addBu1()"
                        data-hs-overlay="#department-register-alert-modal">
                        บันทึกข้อมูล
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="department-register-delete-alert-modal" class="hs-overlay hidden ti-modal">
    <div class="hs-overlay-open:mt-7 ti-modal-box mt-0 ease-out h-[calc(100%-3.5rem)] flex items-center">
        <div class="max-h-full overflow-hidden ti-modal-content w-full">
            <div class="ti-modal-header">
                <h3 class="text-xxl font-bold text-primary">
                    แจ้งเตือน
                </h3>
                <div class="flex justify-end">
                    <button type="button" class="hs-dropdown-toggle ti-modal-clode-btn text-danger"
                        data-hs-overlay="#department-register-delete-alert-modal">
                        <span class="sr-only">Close</span>
                        <i class="ti ti-circle-x fs-xxl"></i>
                    </button>
                </div>
            </div>
            <div class="ti-modal-body ">
                <p class="mt-1 text-gray-800 dark:text-white/70">
                    <ng-container *ngIf="numDataListChecked">
                            ยืนยันการลบข้อมูลหรือไม่
                        </ng-container>
                        <ng-container *ngIf="!numDataListChecked">
                            เลือกข้อมูลที่ต้องการลบ
                        </ng-container>
                </p>

                <div class="flex justify-end mt-2rem mb-1rem">
                    <button type="button"
                        class="hs-dropdown-toggle ti-btn ti-border font-medium bg-white text-gray-700 shadow-sm align-middle hover:bg-gray-50 focus:ring-offset-white focus:ring-primary dark:bg-bgdark dark:hover:bg-black/20 dark:border-white/10 dark:text-white/70 dark:hover:text-white dark:focus:ring-offset-white/10"
                        data-hs-overlay="#department-register-delete-alert-modal">
                        ย้อนกลับ
                    </button>
                    <button type="button" class="ti-btn ti-btn-danger" (click)="deleteBu1()"
                        data-hs-overlay="#department-register-delete-alert-modal"  *ngIf="numDataListChecked">
                        ลบข้อมูล
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="department-register-upload-modal" class="hs-overlay hidden ti-modal">
    <div class="hs-overlay-open:mt-7 ti-modal-box mt-0 ease-out rounded-md">
        <div class="ti-modal-header bg-primary !rounded-none !rounded-t-sm">
            <h5 class="text-xxl font-bold text-white">
                นำเข้าทะเบียนฝ่าย
            </h5>
        </div>
        <div class="ti-modal-body max-h-full overflow-hidden ti-modal-content !rounded-t-none !rounded-b-sm">
            <h1 class="mt-2" style="text-align: center;">ไฟล์</h1>
            <div class="mt-2 p-2">
                <div class="flex rounded-md">
                    <input #fileInput id="fileInput" type="file" (change)="onFileSelected($event)" hidden>
                    <input type="text" [value]="selectedFileName" readonly (click)="fileInput.click()"
                        class="ti-form-input rounded-none ltr:rounded-l-md rtl:rounded-r-md focus:z-10 cursor-pointer">
                    <button type="button" (click)="fileInput.click()"
                        class="inline-flex flex-shrink-0 justify-center items-center h-[2.875rem] w-[2.875rem] ltr:rounded-r-md rtl:rounded-l-md border border-transparent font-semibold bg-secondary text-white hover:bg-secondary focus:z-10 focus:outline-none focus:ring-0 focus:ring-secondary transition-all text-sm">
                        <i class="ti ti-upload"></i>
                    </button>
                </div>
                <div class="flex justify-center mt-2rem ">
                    <h1 class="cursor-pointer justify-center -mb-px inline-flex items-center gap-2 font-weight-500 font-size-12px
                    text-center text-secondary border-secondary border-b-2 align-items-end" (click)="downloadFile()">
                        ดาวน์โหลดตัวอย่างไฟล์</h1>
                </div>
                <div class="flex justify-center mt-2rem mb-1rem space-x-4">
                    <button type="submit" data-hs-overlay="#department-register-upload-modal"
                        class="ti-btn ti-btn-secondary" [class.ti-btn-disabled]="!selectedFile" (click)="uploadFile()"
                        [disabled]="!selectedFile">
                        อัปโหลด
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>