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

describe('库位管理', function() {
  beforeEach(function() {
    loginIntercept();
    menuIntercept();
    loginSetup();
    menuTrigger(2, 0);
  });

  beforeEach(function() {
    intercept(
      '/storage/getStorage',
      function({url: reqUrl, reply}) {
        const url = new URL(reqUrl);
        const search = new URLSearchParams(url.search);

        if (search.has('storageLocationName') && search.get('storageLocationName').length)
          return reply({fixture: 'storage/nameSearch'});
        if (search.has('storageLocationCode') && search.get('storageLocationCode').length)
          return reply({fixture: 'storage/codeSearch'});
        if (search.has('storageLocationType') && search.get('storageLocationType').length)
          return reply({fixture: 'storage/typeSearch'});
        if (search.has('isNotDisable') && search.get('isNotDisable').length)
          return reply({fixture: 'storage/disabledSearch'});

        if (search.has('id') && search.get('id').length)
          return reply({fixture: 'storage/info'});

        const page = search.get('page');
        reply({fixture: page === '1' ? 'storage/list1' : 'storage/list2'});
      },
    );

    successIntercept('/storage/addStorage');
    successIntercept('/storage/updateStorage');
    successIntercept('/storage/delStorage');
    exportIntercept('/storage/export');
  });

  const TABLE_NAME = 'storage_table',
        MODAL_NAME = 'storage_modal',
        LABEL = '库位';

  it('表格', function() {
    validateTableList(TABLE_NAME);
  });

  it('搜索', function() {
    const validate = validateTableSearch(TABLE_NAME);

    cy.get('#filter_storageName').type('库位');
    validate('searchName');

    cy.get('#filter_storageName').clear();
    cy.get('#filter_storageCode').type('code');
    validate('searchCode');

    cy.get('#filter_storageCode').clear();
    cy.get('#filter_storageType').type('type');
    validate('searchType');

    cy.get('#filter_storageType').clear();
    selectClick('filter_storageState', 1);
    validate('searchDisabled');
  });

  it('删除', function() {
    validateDelete(TABLE_NAME, LABEL);
  });

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

  it('新增', function() {
    validateAdd(function() {
      cy.getTestId('field_storageLocationCode').type('0001');
      cy.getTestId('field_storageLocationName').type('名称');
      cy.getTestId('field_storageLocationType').type('类型');
      cy.getTestId('field_storageWarehouseWhere').type('1号仓库');
      cy.getTestId('field_storageCapacity').clear().type('5');
      selectClick('select_storageIsNotDisable', 1);
    });
  });

  it('修改', function() {
    validateEdit(TABLE_NAME, function() {
      cy.getTestId(MODAL_NAME).should('exist').and('be.visible');
      cy.getTestId(MODAL_NAME).find('h3').should('include.text', '修改库位');

      cy.getTestId('field_storageLocationCode').should('have.value', '0001');
      cy.getTestId('field_storageLocationName').should('have.value', '1号库位');
      cy.getTestId('field_storageLocationType').should('have.value', '原材料库位');
      cy.getTestId('field_storageWarehouseWhere').should('have.value', '1号仓库');
      cy.getTestId('field_storageCapacity').should('have.value', '8');
      validateSelect('select_storageIsNotDisable', '启用');
    });
  });

  it('导出', function() {
    validateExport();
  });
});
