export function selectClick(testid: string, eq = 0) {
  cy.getTestId(testid).click();
  cy.getTestId(testid).find('.rc-virtual-list-holder-inner')
    .children().eq(eq).click();
}

export function menuTrigger(parent: number, child: number) {
  cy.getTestId('menu').children().each(function(el, idx) {
    if (idx === parent) {
      el.find('.ant-menu-submenu-title').trigger('click');
      el.find('ul>li').children().eq(child).trigger('click');
    }
  });
}

export function validateSelect(testid: string, value: string) {
  cy.getTestId(testid).find('.ant-select-selection-item')
    .should('have.attr', 'title', value).and('have.text', value);
}

export function validateTableList(tableName: string, firstPageNumber = 3, secondPageNumber = 2) {
  cy.getTestId(tableName).find('table').find('.ant-table-tbody')
    .children('.ant-table-row').should('have.length', firstPageNumber);

  cy.get('.ant-pagination').find('li[title="2"]').click();

  cy.getTestId(tableName).find('table').find('.ant-table-tbody')
    .children('.ant-table-row').should('have.length', secondPageNumber);
}

export function validateTableSearch(tableName: string, value?: string) {
  cy.get('.ant-pagination').find('li[title="2"]').click();

  return function(text?: string) {
    cy.getTestId('search_btn').click();

    cy.getTestId(tableName).find('table').find('.ant-table-tbody')
      .children('.ant-table-row').first().find('td').first()
      .should('include.text', text ?? value);

    cy.get('.ant-pagination').find('li[title="1"]')
      .should('have.class', 'ant-pagination-item-active');
  };
}

export function validatePut(modalName: string, label: string) {
  function validateBtnGroup() {
    cy.getTestId('modal_btn_group').find('.ant-btn').first()
      .should('have.class', 'ant-btn-loading');
    cy.getTestId('modal_btn_group').find('.ant-btn').last()
      .should('have.attr', 'disabled');
  }

  function validateAdd(fn: () => void) {
    cy.getTestId('add_btn').click();

    cy.getTestId(modalName).should('exist').and('be.visible');
    cy.getTestId(modalName).find('h3')
      .should('include.text', `新增${label}`);

    fn();

    cy.getTestId(modalName).find('form').submit();

    validateBtnGroup();

    cy.get('.ant-message-notice-content').should('include.text', '新增成功');
    cy.getTestId(modalName).should('not.exist');
  }

  function validateEdit(tableName: string, validateFn: () => void) {
    cy.getTestId(tableName).find('table').find('.ant-table-tbody')
      .children('.ant-table-row').first().find('td').last().find('.ant-btn')
      .first().click();

    cy.getTestId(modalName).should('exist').and('be.visible');
    cy.getTestId(modalName).find('h3').should('include.text', `修改${label}`);

    validateFn();

    cy.getTestId(modalName).find('form').submit();

    validateBtnGroup();

    cy.get('.ant-message-notice-content').should('include.text', '修改成功');
    cy.getTestId(modalName).should('not.exist');
  }

  return {validateAdd, validateEdit};
}

export function validateDelete(tableName: string) {
  cy.getTestId(tableName).find('table').find('.ant-table-tbody')
    .children('.ant-table-row').first().find('td').last().children().eq(1)
    .click();

  cy.get('.ant-modal-content').should('be.visible');
  cy.get('.ant-modal-confirm-btns').children().last().trigger('click');

  cy.getTestId(tableName).find('table').find('.ant-table-tbody')
    .children('.ant-table-row').first().find('td').last().children().eq(1)
    .should('have.class', 'ant-btn-loading');

  cy.getTestId(tableName).find('table').find('.ant-table-tbody')
    .children('.ant-table-row').first().find('td').last().children().first()
    .should('have.attr', 'disabled');

  cy.get('.ant-message-notice-content').should('include.text', '删除成功');
}
