import cluster from 'cluster';
import { test, expect } from '../../fixtures/index';
import { ClientPage } from '../../pom/client.page';
import { LoginPage } from '../../pom/login.page';
import { normalizeAddress, normalizeText, sortArrayAsc } from '../../pom/utils/utlisFunc';
import { getPagingMessage } from '../../utils/paging';
import { getRandomString } from '../../utils/random';
import { randomString } from '../../utils/array';

test.describe('Function case for client tests', async () => {
    let loginPage: LoginPage;
    let clientPage: ClientPage;

    test.beforeEach(async ({ page, conf }) => {
        loginPage = new LoginPage(page);
        clientPage = new ClientPage(page);
        clientPage.loadBusinessNames(conf.data.business_singular, conf.data.business_plural);

        await loginPage.open();
        await loginPage.login(conf.data.username, conf.data.password);
        await expect(loginPage.baseLoc.dashboardContainer).toBeVisible({ timeout: 15_000 });
        await clientPage.open();
    });


    test('CLIENT_003 - Verify màn client function - list screen', {
        tag: ['@CLIENT_003', '@client', '@function']
    }, async ({ conf }) => {
        test.setTimeout(200_000);
        // Get the stat at the beginning: 
        // - Total entries
        // - Total archived entries
        await test.step('Verify show correct number of entries and order', async () => {
            const stats = await clientPage.initialStats();
            console.log(stats);

            // Verify show correct number of entries
            const defaultPerPage = 10;
            const defaultPage = 1;
            const pagingMessage = getPagingMessage(stats.entries.total, defaultPerPage, defaultPage);
            console.log(pagingMessage);
            await expect(clientPage.dashboardLoc.table.entriesTotal).toContainText(pagingMessage);

            // Verify show correct number of archived entries
            await clientPage.clientLoc.btnThreedot.click();
            await expect(clientPage.clientLoc.archivedClient).toBeVisible();
            await clientPage.clientLoc.archivedClient.click();
            await expect(clientPage.clientLoc.archivedClientFilter).toBeVisible();

            const pagingMessageArchived = getPagingMessage(stats.archived_entries.total, defaultPerPage, defaultPage);
            console.log(pagingMessageArchived);
            await expect(clientPage.dashboardLoc.table.entriesTotal).toContainText(pagingMessageArchived);

            // Clear archive filter
            await clientPage.clientLoc.btnThreedot.click();
            await expect(clientPage.clientLoc.unarchivedClient).toBeVisible();
            await clientPage.clientLoc.unarchivedClient.click();
            await expect(clientPage.clientLoc.archivedClientFilter).not.toBeVisible();

            // Verify sorting by each column
            // sorting asc
            const displayNames = ['Name', 'DoB', 'Phone', 'Address'];
            const propertyNames = ['full_name', 'birthday', 'phone_number', 'full_address'];
            const directions = ['asc', 'asc', 'asc', 'asc'];
            for (let i = 0; i < displayNames.length; i++) {
                await expect(clientPage.dashboardLoc.table.headingColumn(displayNames[i])).toBeVisible();
                await clientPage.dashboardLoc.table.headingColumn(displayNames[i]).click();
                await expect(clientPage.page).toHaveURL(
                    new RegExp(`.*sort=${propertyNames[i]}&direction=${directions[i]}`)
                );

                let apiDataUrl = `clients?return_type=json&per_page=${defaultPerPage}&page=${defaultPage}&is_archived=false&sort=${propertyNames[i]}&direction=${directions[i]}`;
                const data = await clientPage.getDataInTable(displayNames[i], propertyNames[i], apiDataUrl);
                const dataUI = sortArrayAsc(data.dataUI);
                console.log(`Sort by: ${displayNames[i]}, direction: ${directions[i]}, property: ${propertyNames[i]}`)
                expect(data.dataApi).toEqual(dataUI);
            }
        });

        // Verify search
        await test.step('Verify search', async () => {
            // Search invalid value
            for (const [key, value] of Object.entries(conf.data.search as Record<string, any>)) {
                await clientPage.search(value.invalid);
                await expect(clientPage.clientLoc.inputSearch).toHaveValue(value.invalid);
                await expect(clientPage.clientLoc.noResult).toBeVisible();
            }

            // Search valid value
            for (const [key, value] of Object.entries(conf.data.search as Record<string, any>)) {
                await clientPage.search(value.valid);
                await expect(clientPage.clientLoc.inputSearch).toHaveValue(value.valid);
                await expect(clientPage.clientLoc.noResult).not.toBeVisible();
            }
            
            // Clear search term
            await clientPage.clientLoc.inputSearch.clear();
            await expect(clientPage.clientLoc.inputSearch).toHaveValue('');
            await expect(clientPage.clientLoc.noResult).not.toBeVisible();
            await clientPage.waitForSecond(1.5)
        });

        // Verify filter
        await test.step('Verify filter', async () => {
            // Filter by name
            // Fill invalid value
            await clientPage.clientLoc.btnFilter.click();
            await expect(clientPage.clientLoc.filterDialog.container).toBeVisible();
            await clientPage.clientLoc.filterDialog.name.type(conf.data.filter.name.invalid);
            await expect(clientPage.clientLoc.filterDialog.name).toHaveValue(conf.data.filter.name.invalid);
            await clientPage.clientLoc.filterDialog.btnApply.click();
            await expect(clientPage.clientLoc.noResult).toBeVisible();

            // Fill valid value
            await clientPage.clientLoc.btnFilter.click();
            await expect(clientPage.clientLoc.filterDialog.container).toBeVisible();
            await clientPage.clientLoc.filterDialog.name.fill(conf.data.filter.name.valid);
            await expect(clientPage.clientLoc.filterDialog.name).toHaveValue(conf.data.filter.name.valid);
            await clientPage.clientLoc.filterDialog.btnApply.click();
            await expect(clientPage.clientLoc.noResult).not.toBeVisible();

            // Filter by created
            // Fill invalid value
            await clientPage.clientLoc.btnFilter.click();
            await expect(clientPage.clientLoc.filterDialog.container).toBeVisible();
            await clientPage.clientLoc.filterDialog.name.fill('');
            await clientPage.clientLoc.filterDialog.created.fill(conf.data.filter.created.invalid);
            await expect(clientPage.clientLoc.filterDialog.created).toHaveValue(conf.data.filter.created.invalid);
            await clientPage.clientLoc.filterDialog.btnApply.click();
            await expect(clientPage.clientLoc.noResult).toBeVisible();

            // Fill valid value
            await clientPage.clientLoc.btnFilter.click();
            await expect(clientPage.clientLoc.filterDialog.container).toBeVisible();
            await clientPage.clientLoc.filterDialog.created.fill(conf.data.filter.created.valid);
            await expect(clientPage.clientLoc.filterDialog.created).toHaveValue(conf.data.filter.created.valid);
            await clientPage.clientLoc.filterDialog.btnApply.click();
            const dataFilterCreated = await clientPage.filterSingleField("clients", "created", conf.data.filter.created.valid, "created_at");
            expect(dataFilterCreated.every(created => created === conf.data.filter.created.valid)).toBeTruthy();
            await expect(clientPage.clientLoc.noResult).not.toBeVisible();

            // Filter by updated
            // Fill invalid value
            await clientPage.clientLoc.btnFilter.click();
            await expect(clientPage.clientLoc.filterDialog.container).toBeVisible();
            await clientPage.clientLoc.filterDialog.created.fill('');
            await clientPage.clientLoc.filterDialog.updated.fill(conf.data.filter.updated.invalid);
            await expect(clientPage.clientLoc.filterDialog.updated).toHaveValue(conf.data.filter.updated.invalid);
            await clientPage.clientLoc.filterDialog.btnApply.click();
            await expect(clientPage.clientLoc.noResult).toBeVisible();

            // Fill valid value
            await clientPage.clientLoc.btnFilter.click();
            await expect(clientPage.clientLoc.filterDialog.container).toBeVisible();
            await clientPage.clientLoc.filterDialog.updated.fill(conf.data.filter.updated.valid);
            await expect(clientPage.clientLoc.filterDialog.updated).toHaveValue(conf.data.filter.updated.valid);
            await clientPage.clientLoc.filterDialog.btnApply.click();
            await expect(clientPage.clientLoc.noResult).not.toBeVisible();

            // Filter by tag
            // Fill invalid value
            await clientPage.open();
            await clientPage.clientLoc.btnFilter.click();
            await expect(clientPage.clientLoc.filterDialog.container).toBeVisible();
            await clientPage.clientLoc.filterDialog.updated.fill('');
            await clientPage.clientLoc.filterDialog.tag.fill(conf.data.filter.tag.invalid);
            await clientPage.clientLoc.optionInSelect(conf.data.filter.tag.invalid).click();
            await clientPage.clientLoc.headerFilter.click();
            await clientPage.clientLoc.filterDialog.btnApply.click();
            await expect(clientPage.clientLoc.noResult).toBeVisible();

            // Fill valid value
            await clientPage.open();
            await clientPage.clientLoc.btnFilter.click();
            await expect(clientPage.clientLoc.filterDialog.container).toBeVisible();
            await clientPage.clientLoc.filterDialog.tag.fill(conf.data.filter.tag.valid);
            await clientPage.clientLoc.optionInSelect(conf.data.filter.tag.valid).click();
            await clientPage.clientLoc.headerFilter.click();
            await clientPage.clientLoc.filterDialog.btnApply.click();
            await expect(clientPage.clientLoc.noResult).not.toBeVisible();
        });

        // Verify customized column
        await test.step('Verify customized column and pin', async () => {
            await clientPage.open();
            // verify customized column show
            await clientPage.clientLoc.btnThreedot.click();
            await expect(clientPage.clientLoc.customizedColumnMenu).toBeVisible();
            await clientPage.clientLoc.customizedColumnMenu.click();
            await expect(clientPage.clientLoc.customizedColumnDialog.container).toBeVisible();

            for (const columnName of conf.data.customize.columns) {
                const columnHtmlName = clientPage.clientLoc.customizedColumnDialog.checkboxMap[columnName];
                const checkbox = clientPage.clientLoc.customizedColumnDialog.checkboxItem(columnHtmlName);
                await expect(checkbox).toBeVisible();
                await clientPage.waitForSecond(1);
                await checkbox.setChecked(true);
            }
            await clientPage.clientLoc.customizedColumnDialog.btnApply.click();
            await expect(clientPage.clientLoc.customizedColumnDialog.container).not.toBeVisible();

            for (const columnName of conf.data.customize.columns) {
                await expect(clientPage.dashboardLoc.table.headingColumn(columnName)).toBeVisible();
            }

            // verify customized column hide
            await clientPage.clientLoc.btnThreedot.click();
            await expect(clientPage.clientLoc.customizedColumnMenu).toBeVisible();
            await clientPage.clientLoc.customizedColumnMenu.click();
            await expect(clientPage.clientLoc.customizedColumnDialog.container).toBeVisible();

            for (const columnName of conf.data.customize.columns) {
                const columnHtmlName = clientPage.clientLoc.customizedColumnDialog.checkboxMap[columnName];
                const checkbox = clientPage.clientLoc.customizedColumnDialog.checkboxItem(columnHtmlName);
                await expect(checkbox).toBeVisible();
                await checkbox.setChecked(false);
            }
            await clientPage.clientLoc.customizedColumnDialog.btnApply.click();
            await expect(clientPage.clientLoc.customizedColumnDialog.container).not.toBeVisible();

            for (const columnName of conf.data.customize.columns) {
                await expect(clientPage.dashboardLoc.table.headingColumn(columnName)).not.toBeVisible();
            }

            await clientPage.clientLoc.inputSearch.fill(conf.data.pin.search_term);
            await expect(clientPage.dashboardLoc.table.headingColumn('Pin')).toBeVisible();
            await expect(clientPage.dashboardLoc.table.loadingSkeleton).not.toBeVisible();
        });
    });


    test('CLIENT_004 - Verify màn client - function create', {
        tag: ['@CLIENT_004', '@client', '@function']
    }, async ({ conf }) => {
        const clientFirstNames: any[] = [];
        const clientData = conf.data.info_client_without_create_another;
        clientData.first_name = `${clientData.first_name} ${randomString(5)}`;
        clientFirstNames.push(clientData.first_name);
        test.slow()
        await test.step('Verify feature create patient', async () => {
            await clientPage.clientLoc.btnNewClient.click();
            await clientPage.createNewClient(clientData);

            // Verify show notification
            await expect(clientPage.clientLoc.notificationAddNewClientSuccess).toBeVisible();
            await clientPage.clientLoc.btnClosePopupNotification.click();
            await expect(clientPage.clientLoc.notificationAddNewClientSuccess).not.toBeVisible();

            // Verify show client info in table
            const { name, dob, phone, address } = clientPage.getXpathClientInfoBy(normalizeText(clientData.first_name), clientData.last_name, clientData.dob, clientData.phone, clientData.address_1);
            await expect(clientPage.page.locator(name)).toBeVisible();
            await expect(clientPage.page.locator(dob)).toBeVisible();
            await expect(clientPage.page.locator(phone)).toBeVisible();
            await expect(clientPage.page.locator(address)).toBeVisible();
        });

        await test.step("Verify feature create patient when duplicate patient", async () => {
            // clientData.first_name = `${clientData.first_name}_${getRandomString(10)}`;
            await clientPage.clientLoc.btnNewClient.click();
            await clientPage.createNewClient(conf.data.info_client_without_create_another);

            // Verify notification
            await expect(clientPage.clientLoc.notificationAddNewClientSuccess).not.toBeVisible();
            await expect(clientPage.clientLoc.notificationDuplicateClient).toBeVisible();

            await clientPage.dashboardLoc.buttonByText("Cancel").last().click();
            await clientPage.clientLoc.btnLeave.click();
        });

        await test.step('Verify feature create patient when tick "Create another patient"', async () => {
            const clientData = conf.data.info_client_with_create_another;
            const randomFirstName = `${clientData.first_name} ${randomString(5)}`;
            clientData.first_name = randomFirstName;
            clientFirstNames.push(randomFirstName);
            await clientPage.clientLoc.btnNewClient.click();
            await clientPage.createNewClient(clientData);

            // Verify show notification
            await expect(clientPage.clientLoc.notificationAddNewClientSuccess).toBeVisible();


            // Verify show new client form
            await expect(clientPage.dashboardLoc.modal.headerModal("New Patient")).toBeVisible();
            await clientPage.dashboardLoc.buttonByText("Cancel").last().click();
            await clientPage.clientLoc.btnLeave.click();
            await clientPage.clientLoc.btnClosePopupNotification.click();
            await expect(clientPage.clientLoc.notificationAddNewClientSuccess).not.toBeVisible();
            await expect(clientPage.clientLoc.newClientForm).not.toBeVisible();

            // Verify show client info in table
            const { name, dob, phone, address } = clientPage.getXpathClientInfoBy(normalizeText(randomFirstName), clientData.last_name, clientData.dob, clientData.phone, clientData.address_1);
            await expect(clientPage.page.locator(name)).toBeVisible();
            await expect(clientPage.page.locator(dob)).toBeVisible();
            await expect(clientPage.page.locator(phone)).toBeVisible();
            await expect(clientPage.page.locator(address)).toBeVisible();
        });

        await test.step('Verify feature create patient when create with tags', async () => {
            const clientData = conf.data.info_client_with_tags;
            clientData.first_name = `${clientData.first_name} ${randomString(5)}`;
            clientFirstNames.push(clientData.first_name);
            await clientPage.clientLoc.btnNewClient.click();
            await clientPage.createNewClient(clientData);

            // Verify show notification
            await expect(clientPage.clientLoc.notificationAddNewClientSuccess).toBeVisible();
            await clientPage.clientLoc.btnClosePopupNotification.click();
            await expect(clientPage.clientLoc.notificationAddNewClientSuccess).not.toBeVisible();

            // Verify show client info in table
            const { name, dob, phone, address } = clientPage.getXpathClientInfoBy(normalizeText(clientData.first_name), normalizeText(clientData.last_name), clientData.dob, clientData.phone, clientData.address_1);
            await expect(clientPage.page.locator(name)).toBeVisible();
            await expect(clientPage.page.locator(dob)).toBeVisible();
            await expect(clientPage.page.locator(phone)).toBeVisible();
            await expect(clientPage.page.locator(address)).toBeVisible();

            // Verify list tags
            await clientPage.clickClientDetail(clientData.first_name, clientData.last_name);
            const listTags = await clientPage.clientLoc.listTag.textContent();
            expect(listTags).toContain(clientData.tags);
        });

        // await test.step('Clean up data', async () => {
        //     for (const client of [conf.data.info_client_without_create_another, conf.data.info_client_with_create_another, conf.data.info_client_with_tags]) {
        //         await clientPage.clientLoc.clientSearch.fill(client.first_name + ' ' + client.last_name);
        //         await clientPage.clickClientDetail(client.first_name, client.last_name);
        //         await clientPage.clientLoc.btnUpdateClientDetail.click();
        //         await clientPage.clientLoc.btnArchiveClientDetail.click();
        //         await clientPage.clientLoc.btnYesArchiveClientDetail.click();
        //         await clientPage.clientLoc.btnClosePopupNotification.click();
        //         await clientPage.clientLoc.btnBack.click();

        //         await clientPage.clientLoc.clientSearch.fill(client.first_name + ' ' + client.last_name);
        //         await expect(clientPage.clientLoc.msgNoClientFound).toBeVisible();
        //     }
        // });

    });

    test('CLIENT_005 - Verify màn client - function update', {
        tag: ['@CLIENT_005', '@client', '@function']
    }, async ({ conf }) => {
        conf.data.info_client.first_name = `${conf.data.info_client.first_name} ${randomString(5)}`;
        conf.data.new_info_client.first_name = `${conf.data.new_info_client.first_name} ${randomString(5)}`;

        await test.step('Create patient', async () => {
            await clientPage.clientLoc.btnNewClient.click();
            await clientPage.createNewClient(conf.data.info_client);

            // Verify show notification
            await expect(clientPage.clientLoc.notificationAddNewClientSuccess).toBeVisible();
            await clientPage.clientLoc.btnClosePopupNotification.click();
            await expect(clientPage.clientLoc.notificationAddNewClientSuccess).not.toBeVisible();
        });

        await test.step('Verify feature update patient success', async () => {
            await clientPage.clickClientDetail(normalizeText(conf.data.info_client.first_name), normalizeText(conf.data.info_client.last_name));
            await clientPage.updateInfoClient(conf.data.new_info_client);

            // Verify show notification
            await expect(clientPage.clientLoc.notificationUpdateClientSuccess).toBeVisible();
            await clientPage.clientLoc.btnClosePopupNotification.click();
            await expect(clientPage.clientLoc.notificationUpdateClientSuccess).not.toBeVisible();

            // Verify new info client updated in Client Detail
            // await expect(clientPage.clientLoc.clientName).toContainText(conf.data.new_info_client.first_name + ' ' + conf.data.new_info_client.last_name);
            await expect(clientPage.clientLoc.clientDob).toContainText(conf.data.new_info_client.dob);
            await expect(clientPage.clientLoc.clientPhone).toContainText(conf.data.new_info_client.phone);
            await expect(clientPage.clientLoc.clientGender).toContainText(conf.data.new_info_client.gender);
            await expect(clientPage.clientLoc.clientAddress).toContainText(normalizeAddress(conf.data.new_info_client.address_1));
        });


        await test.step('Verify feature update patient fail', async () => {
            // TODO: Have bug here
        });

        await test.step('Verify feature archived patient', async () => {
            await clientPage.dashboardLoc.buttonByText("Update").first().click();
            await clientPage.dashboardLoc.buttonByText("Archive").click();
            await clientPage.dashboardLoc.buttonByText("Yes").click();

            // Verify show notification
            // TODO: Have bug here
            // await expect(clientPage.clientLoc.notificationArchiveClientSuccess).toBeVisible();
            await clientPage.clientLoc.btnClosePopupNotification.click();
            // await expect(clientPage.clientLoc.notificationArchiveClientSuccess).not.toBeVisible();

            await clientPage.clientLoc.btnBack.click();
            await clientPage.clientLoc.clientSearch.fill(conf.data.new_info_client.first_name);
            await expect(clientPage.clientLoc.msgNoClientFound).toBeVisible();
        });
    });

    test("CLIENT_008 - Verify that the client's address update process successfully normalizes the address", {
        tag: ['@CLIENT_008', '@client', '@function']
    }, async ({ conf }) => {
        await test.step("Get search patient and go to update modal", async () => {
            await clientPage.getDetailCase(conf.data.patient_name);
            await clientPage.dashboardLoc.buttonByText("Update").first().click();
            await expect(clientPage.dashboardLoc.modal.headerModal("Patient Detail")).toBeVisible();
        });

        await test.step("Verify update address and city to normalizes text", async () => {
            await clientPage.clientLoc.inputAddressNewClient.fill(conf.data.address);
            await clientPage.clientLoc.inputCityNewClient.fill(conf.data.city);
            await clientPage.dashboardLoc.buttonByText("Update").nth(1).click();

            await expect(clientPage.dashboardLoc.modal.headerModal("Patient Detail")).not.toBeVisible();
            await expect(clientPage.clientLoc.clientAddress).toContainText(conf.expect.address);
            await expect(clientPage.clientLoc.clientAddress).toContainText(conf.expect.city);
        })
    });

    test("CLIENT_009 - Verify when click add new case and request in detail client", {
        tag: ['@CLIENT_009', '@client', '@function']
    }, async ({ conf }) => {
        await test.step("Verify when create new case in detail patient", async () => {
            await clientPage.getDetailCase(conf.data.patient_name);
            await clientPage.clientLoc.detail.tabName("Cases").click();
            await clientPage.clientLoc.detail.threeDotsBtn.click();
            await clientPage.clientLoc.detail.dropdownOptionAddNew.click();

            await expect(clientPage.dashboardLoc.common.spanText("New Case")).toBeVisible();
            await expect(clientPage.clientLoc.detail.inputByName("customer_id")).toHaveValue(conf.data.patient_name);

            //Verify info of client
            await expect(clientPage.dashboardLoc.root).toMatchAriaSnapshot(clientPage.clientSnapshot.infoClientPreview);
        });

        await test.step("Verify create new request in detail patient", async () => {
            await clientPage.open();
            await clientPage.getDetailCase(conf.data.patient_name);
            await clientPage.clientLoc.detail.tabName("Request").click();
            await clientPage.clientLoc.detail.threeDotsBtn.click();
            await clientPage.clientLoc.detail.dropdownOptionAddNew.click();

            await expect(clientPage.clientLoc.detail.lableNameCreateRequest).toContainText(conf.data.patient_name);
        })
    });

    test("CLIENT_010 - Verify when add tag in client detail", {
        tag: ['@CLIENT_010', '@client', '@function']
    }, async ({ conf }) => {
        await test.step("Navigate to client detail", async () => {
            await clientPage.getDetailCase(conf.data.patient_name);
        });

        await test.step("Verify add tags with tag count greater than 9 (invalid)", async () => {
            await clientPage.addMultipleTags(10);
            await clientPage.saveTags();
            await expect(clientPage.dashboardLoc.tag.errorMaxTag).toBeVisible();
        });

        await test.step("Verify when add < 9 tag (valid)", async () => {
            await clientPage.removeOneTag();
            await clientPage.saveTags();
            await expect(clientPage.dashboardLoc.msgSuccess.first()).toBeVisible();
            await clientPage.dashboardLoc.btnClosePopupNotification.click();
            await expect(clientPage.dashboardLoc.tag.btnSaveTag).not.toBeVisible();

            const tagCount = await clientPage.getTagCount();
            expect(tagCount).toBe(9);
        });

        await test.step("Clear all tag", async () => {
            await clientPage.clearAllTags();
            await clientPage.saveTags();
            await expect(clientPage.dashboardLoc.msgSuccess.first()).toBeVisible();
            await clientPage.dashboardLoc.btnClosePopupNotification.click();
            await expect(clientPage.dashboardLoc.tag.btnSaveTag).not.toBeVisible();

            const tagCount = await clientPage.getTagCount();
            expect(tagCount).toBe(0);
        });
    });

    test("CLIENT_012 - Verify update client", {
        tag: ['@CLIENT_012', '@client', '@function']
    }, async ({ conf }) => {
        await test.step("Verify clear data", async () => {
            await clientPage.getDetailCase(conf.data.patient_name);
            await clientPage.dashboardLoc.buttonByText("Update").first().click();
            await expect(clientPage.dashboardLoc.modal.headerModal("Patient Detail")).toBeVisible();
            // Clear data
            for (const key of Object.keys(conf.data.data_update)) {
                await clientPage.clientLoc.detail.inputByName(key).clear();
                await expect(clientPage.clientLoc.detail.inputByName(key)).toHaveValue("");
            }
            await clientPage.dashboardLoc.buttonByText("Update").nth(1).click();
            await expect(clientPage.dashboardLoc.msgSuccess.first()).toBeVisible();
        });

        await test.step("Reopen modal and update new data", async () => {
            await clientPage.dashboardLoc.buttonByText("Update").first().click();
            await expect(clientPage.dashboardLoc.modal.headerModal("Patient Detail")).toBeVisible();

            //Verify some fields empty 
            for (const key of Object.keys(conf.data.data_update)) {
                await expect(clientPage.clientLoc.detail.inputByName(key)).toHaveValue("");
            }

            //Fill data to fields
            for (const [key, value] of Object.entries(conf.data.data_update)) {
                await clientPage.clientLoc.detail.inputByName(key).fill(String(value));
                await expect(clientPage.clientLoc.detail.inputByName(key)).toHaveValue(String(value));
            }
            await clientPage.dashboardLoc.buttonByText("Update").nth(1).click();
            await expect(clientPage.dashboardLoc.msgSuccess.first()).toBeVisible();
            await expect(clientPage.dashboardLoc.modal.headerModal("Patient Detail")).not.toBeVisible();
        });

        await test.step("Verify data updated successfully", async () => {
            await clientPage.dashboardLoc.buttonByText("Update").first().click();
            await expect(clientPage.dashboardLoc.modal.headerModal("Patient Detail")).toBeVisible();
            for (const [key, value] of Object.entries(conf.data.data_update)) {
                await expect(clientPage.clientLoc.detail.inputByName(key)).toHaveValue(String(value));
            }
        });
    })
});