import {
  beforeSetup,
  exportIntercept,
  generateNetworkResult,
  intercept,
  intoMenu,
  normalIntercept,
  successIntercept,
  tableBtnClick,
  validateDelete,
  validateExport,
  validateModalBtnGroup,
  validatePut,
  validateTableList,
  validateTableSearch,
} from './utils';

const basicData = {
  id: 11,
  roleCode: 'BH000011',
  roleName: 'test账户',
  createTime: '2023-03-30 14:50:46',
  remarks: '测试用账户',
  menu: '78,93,88,79,',
  menuBefore: '93,88,79',
  menuPda: '29,30,31,32,',
  modifyUser: 'admin',
  modifyTime: '2023-04-12 15:16:15',
  page: 0,
  limit: 0,
};

const TABLE_NAME = 'role_table',
  MODAL_NAME = 'role_modal';

describe('角色', function () {
  beforeEach(function () {
    beforeSetup();
    intoMenu('系统设置', '角色管理');
  });

  beforeEach(function () {
    intercept('/role/getRole', function ({search, reply}) {
      generateNetworkResult({search, basicData, reply, title: 'roleCode'});
    });

    successIntercept(['/role/delRole', '/role/addRole', '/role/updateRole']);
    exportIntercept('/role/export');
    normalIntercept('/menu/getMenu', 'menu/all');
    normalIntercept('/menu/getPage', 'menu/pda');
  });

  it('table', function () {
    validateTableList(TABLE_NAME);

    validateTableSearch(TABLE_NAME, ['roleCode', 'roleName', 'modifyUser'], {
      toolId: 'role_filter',
      url: '/role/getRole',
    });
  });

  it('operation', function () {
    const {validateAdd, validateEdit} = validatePut(MODAL_NAME, TABLE_NAME, {
      label: '角色',
    });

    validateAdd(['roleName', 'roleRemarks']);

    validateEdit([
      {id: 'roleName', type: 'field', value: basicData.roleName},
      {id: 'roleRemarks', type: 'field', value: basicData.remarks},
    ]);

    validateDelete(TABLE_NAME, '角色', {eq: 3, title: basicData.roleName});

    validateExport(TABLE_NAME);

    // 菜单权限
    tableBtnClick(TABLE_NAME, 1);

    cy.getTestId('role_tree_modal')
      .find('h3')
      .should('include.html', '菜单权限');
    cy.getTestId('role_tree_modal').should('exist').and('be.visible');

    cy.getTestId('role_tree')
      .find('.ant-tree-list-holder-inner')
      .find('.ant-tree-treenode')
      .each(function (res, idx) {
        idx <= 3
          ? expect(res.hasClass('ant-tree-treenode-checkbox-checked')).to.true
          : expect(res.hasClass('ant-tree-treenode-checkbox-checked')).to.false;
      })
      .should('have.length', 17);

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

    validateModalBtnGroup('role_tree_modal');
    cy.get('.ant-message-notice-content').should('include.text', '设置成功');
    cy.getTestId('role_tree_modal').should('not.exist');

    // pda权限
    tableBtnClick(TABLE_NAME, 2);
    cy.get('.ant-transfer-list')
      .eq(0)
      .find('.ant-transfer-list-content')
      .children('.ant-transfer-list-content-item')
      .should('have.length', 10);
    cy.get('.ant-transfer-list')
      .eq(1)
      .find('.ant-transfer-list-content')
      .children('.ant-transfer-list-content-item')
      .should('have.length', 4);
    cy.getTestId('role_pda_menu_modal').find('form').submit();
    validateModalBtnGroup('role_pda_menu_modal');
    cy.get('.ant-message-notice-content').should('include.text', '设置成功');
    cy.getTestId('role_tree_modal').should('not.exist');
  });
});
