import {
  loginIntercept,
  menuIntercept,
  loginSetup,
  menuTrigger,
  intercept,
  validateTableList,
  validateTableSearch,
  selectClick,
  optionsIntercept,
  validatePut,
  successIntercept,
  exportIntercept,
  validateSelect,
  validateDelete,
  validateExport,
  tableBtnClick,
  validateModalOperation,
  closeModal,
} from './utils';

describe('容器管理', function() {
  beforeEach(function() {
    loginIntercept();
    menuIntercept();
    loginSetup();
    optionsIntercept();
    menuTrigger(2, 2);
  });

  beforeEach(function() {
    intercept(
      '/container/getContainer',
      function({search, reply}) {
        if (search.has('containerName') && search.get('containerName').length)
          return reply({fixture: 'container/nameSearch'});
        if (search.has('type') && search.get('type').length)
          return reply({fixture: 'container/typeSearch'});
        if (search.has('department') && search.get('department').length)
          return reply({fixture: 'container/depSearch'});

        const page = search.get('page');
        reply({fixture: `container/list${page}`});
      },
    );

    intercept(
      '/container/getScrap',
      function({search, reply}) {
        if (search.has('startTime') && search.get('startTime').length)
          return reply({fixture: 'container/scrapSearch'});

        const page = search.get('page');
        reply({fixture: `container/scrapList${page}`});
      },
    );

    successIntercept('container/addContainer');
    successIntercept('container/updateContainer');
    successIntercept('container/delCon');
    successIntercept('container/delContainer');
    exportIntercept('container/export');
    exportIntercept('container/exportScrap');
  });

  const TABLE_NAME = 'container_table',
        MODAL_NAME = 'container_modal',
        LABEL = '容器';

  it('列表', function() {
    validateTableList(TABLE_NAME);

    const validate = validateTableSearch(TABLE_NAME);

    cy.get('#filter_containerName').type('searchName');
    validate('nameSearch');

    cy.get('#filter_containerName').clear();
    cy.get('#filter_containerType').type('searchType');
    validate('typeSearch');

    cy.get('#filter_containerType').clear();
    selectClick('filter_containerNameDepartment', 2);
    validate('depSearch');

    validateExport();
  });

  it('表格操作', function() {
    const {validateAdd, validateEdit} = validatePut(
      MODAL_NAME,
      LABEL,
    );

    validateAdd(function() {
      cy.getTestId('field_containerName').type('name');
      cy.getTestId('field_containerType').type('type');
      cy.getTestId('field_containerNum').clear().type('2');
      selectClick('select_containerDepartment', 2);
    });

    validateEdit(TABLE_NAME, function() {
      cy.getTestId('field_containerName').should('have.value', '仓库1');
      cy.getTestId('field_containerType').should('have.value', '随便');
      cy.getTestId('field_containerNum').should('have.value', '5');
      validateSelect('select_containerDepartment', '仓储部门');
    });

    validateDelete(TABLE_NAME, LABEL);

    // #region 报废
    tableBtnClick(TABLE_NAME, 2);

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

    cy.getTestId('field_scrapNum').clear().type('2');
    cy.get('#operation_scrapNote').type('报废理由');

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

  it('报废列表', function() {
    tableBtnClick(TABLE_NAME, 3);

    validateTableList('container_scrap_table', 2, 1);

    const validate = validateTableSearch(
      'container_scrap_table',
      'timeSearch',
      {
        btnTestId: 'container_scrap_search',
      },
    );

    cy.getTestId('date_filter_containerDate').click();
    cy.get('.ant-picker-date-panel')
      .eq(0).find('.ant-picker-content').find('tbody').find('td')
      .first().click();

    cy.get('.ant-picker-date-panel')
      .eq(1).find('.ant-picker-content').find('tbody').find('td')
      .first().click();

    validate();

    validateExport('container_scrap_export');

    closeModal();
    cy.getTestId('container_scrap_table').should('not.exist');
  });
});
