/// <reference types="cypress" />
describe('Client list', () => {
    beforeEach(() => {
        cy.viewport(1280, 720);
        cy.login('developer');
        cy.visit('/#/customer/list', {
            timeout: 5000,
            onBeforeLoad(win) {
                cy.stub(win, 'open')
                    .callsFake((url) => {
                        return win.open.wrappedMethod.call(win, url, '_self');
                    })
                    .as('Open');
            },
        });
    });

    it('Client list create new client', () => {
        cy.get('.q-page-sticky > div > .q-btn > .q-btn__content > .q-icon').click();
        const randomInt = Math.floor(Math.random() * 90) + 10;

        const data = {
            Name: { val: `Name ${randomInt}` },
            'Social name': { val: `TEST ${randomInt}` },
            'Tax number': { val: `20852${randomInt}3Z` },
            'Web user': { val: `user_test_${randomInt}` },
            Street: { val: `C/ STREET ${randomInt}` },
            Email: { val: `user.test${randomInt}@cypress.com` },
            'Sales person': { val: 'employee', type: 'select' },
            Location: { val: '46000, Valencia(Province one), España', type: 'select' },
            'Business type': { val: 'Otros', type: 'select' },
        };
        cy.fillInForm(data);

        cy.get('.q-mt-lg > .q-btn--standard').click();

        cy.checkNotification('Data created');
        cy.url().should('include', '/summary');
    });
    it('Client list search client', () => {
        const search = 'Jessica Jones';
        cy.searchByLabel('Name', search);

        cy.get('.title > span').should('have.text', search);
        let id = null;
        cy.get('.q-item > .q-item__label').then((text) => {
            id = text.text().trim().split('#')[1];
            cy.get('.q-item > .q-item__label').should('have.text', ` #${id}`);
            cy.url().should('include', `/customer/${id}/summary`);
        });
    });

    it('Client founded create ticket', () => {
        const search = 'Jessica Jones';
        cy.searchByLabel('Name', search);
        cy.openActionDescriptor('Create ticket');
        cy.waitForElement('#formModel');
        cy.waitForElement('.q-form');
        cy.checkValueForm(1, search);
        cy.checkValueForm(2, search);
    });
    it('Client founded create order', () => {
        const search = 'Jessica Jones';
        cy.searchByLabel('Name', search);
        cy.openActionDescriptor('New order');
        cy.waitForElement('#formModel');
        cy.waitForElement('.q-form');
        cy.checkValueForm(1, search);
        cy.checkValueForm(2, search);
    });
});
