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

const basicData = {
  id: '83',
  code: 'YH000083',
  userName: 'admin2',
  password: 'F6BE46C2F900E518',
  newPassword: null,
  realName: '测试2',
  email: '1211@163.com',
  landline: null,
  phone: '1336131680',
  department: '采购中心（公共） ',
  role: 'test账户',
  roleId: '11',
  departmentId: '00029999',
  createTime: '2023-04-03 14:01:45',
  token: null,
  menu: null,
  modifyUser: 'admin',
  modifyTime: '2023-04-03 14:18:21',
  page: 0,
  limit: 0,
};

const TABLE_NAME = 'user_table',
  MODAL_NAME = 'user_add_modal',
  LABEL = '用户',
  TOOL_ID = 'user_filter';

describe('首页', function () {
  beforeEach(function () {
    beforeSetup();
    intoMenu('系统设置', '用户管理');
  });

  beforeEach(function () {
    intercept('/user/getAllUser', function ({search, reply}) {
      generateNetworkResult({search, reply, basicData, title: 'code'});
    });

    exportIntercept('/user/resetPassword');
    successIntercept([
      '/user/addUser',
      '/user/updateUser',
      '/user/delUser',
      '/user/resetPassword',
    ]);
  });

  it('table', function () {
    selectAllFilters(TOOL_ID, 8);
    validateTableList(TABLE_NAME);

    validateTableSearch(
      TABLE_NAME,
      [
        'userName',
        'realName',
        'code',
        {id: 'department', type: 'select', value: '00029999'},
        'role',
        'email',
        'phone',
        'modifyUser',
      ],
      {toolId: TOOL_ID, url: '/user/getAllUser'},
    );
  });

  it('operation', function () {
    validateExport(TABLE_NAME);

    const {validateAdd, validateEdit} = validatePut(MODAL_NAME, TABLE_NAME, {
      label: LABEL,
    });

    validateAdd([
      'userName',
      'userRealName',
      'userPassword',
      {id: 'userDepartment', type: 'select'},
      {id: 'userRole', type: 'select'},
      'userEmail',
      'userPhone',
    ]);

    validateEdit([
      {id: 'userName', type: 'field', value: basicData.userName},
      {id: 'userRealName', type: 'field', value: basicData.realName},
      {id: 'userDepartment', type: 'select', value: basicData.department},
      {id: 'userRole', type: 'select', value: basicData.role},
      {id: 'userEmail', type: 'field', value: basicData.email},
      {id: 'userPhone', type: 'field', value: basicData.phone},
    ]);

    // 重置密码
    const resetBtn = tableBtnClick(TABLE_NAME, 1);
    validateDialog('重置密码', '你确定要将测试2的密码重置为123456吗？');
    resetBtn().should('have.class', 'ant-btn-loading');
    validateMessageContent('重置成功');

    validateDelete(TABLE_NAME, LABEL, {eq: 2, title: 'admin2'});
  });
});
