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

const basicData = {
  id: '5',
  department: '涂装班组',
  departmentId: '000300010012',
  type: '充电桩底座+1',
  containerName: '特来电002',
  code: '00002',
  num: '9',
  createTime: '2023-03-31 10:11:27',
  modifyUser: 'admin',
  modifyTime: '2023-03-31 10:46:30',
  page: 0,
  limit: 0,
};

const scrapBasicData = {
  id: '6',
  operationName: 'admin',
  name: null,
  scrapCause: '1',
  scrapTime: '2023-03-31 10:46:30',
  scrapNum: '1',
  startTime: null,
  endTime: null,
  containerName: '特来电002',
  page: 0,
  limit: 0,
};

describe('容器管理', function () {
  beforeEach(function () {
    beforeSetup();
    intoMenu('基础资料', '容器管理');
  });

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

    intercept('/container/getScrap', function ({reply, search}) {
      generateNetworkResult({
        reply,
        search,
        basicData: scrapBasicData,
        title: 'operationName',
        skipCondition: name => name === 'containerName',
      });
    });

    successIntercept([
      '/container/addContainer',
      '/container/updateContainer',
      '/container/delCon',
      '/container/delContainer',
    ]);

    exportIntercept('/container/exportScrap');
    exportIntercept('/container/export');
  });

  it('table', function () {
    validateTableList('container_table');
    validateTableSearch(
      'container_table',
      [
        'containerCode',
        'containerName',
        'containerType',
        {id: 'department', type: 'select', value: '00029999'},
        'modifyUser',
      ],
      {toolId: 'container_filter', url: '/container/getContainer'},
    );
    validateExport('container_table');
  });

  it('operation', function () {
    const {validateAdd, validateEdit} = validatePut(
      'container_modal',
      'container_table',
      {
        label: '容器',
      },
    );

    validateAdd([
      'containerName',
      'containerCode',
      'containerType',
      {id: 'containerNum', type: 'field', value: '10'},
      {id: 'containerDepartment', type: 'select'},
    ]);

    validateEdit([
      {id: 'containerName', type: 'field', value: basicData.containerName},
      {id: 'containerCode', type: 'field', value: basicData.code},
      {id: 'containerType', type: 'field', value: basicData.type},
      {id: 'containerNum', type: 'field', value: basicData.num},
      {id: 'containerDepartment', type: 'select', value: basicData.department},
    ]);

    validateDelete('container_table', '容器', {
      eq: 3,
      title: basicData.containerName,
    });

    //报废
    tableBtnClick('container_table', 1);
    cy.getTestId('container_scrap_modal')
      .find('h3')
      .should('have.text', '容器报废');

    cy.getTestId('field_scrapNum').clear().type('2');
    cy.getTestId('field_scrapNote').type('报废理由');

    validateModalOperation('container_scrap_modal', '报废成功');
  });

  it('报废列表', function () {
    tableBtnClick('container_table', 2);

    validateTableList('container_scrap_table');

    validateTableSearch(
      'container_scrap_table',
      ['operationName', {id: 'containerDate', type: 'date'}],
      {toolId: 'container_scrap_filter', url: '/container/getScrap'},
    );

    validateExport('container_scrap_table');
  });
});
