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

describe('货品管理', function() {
  beforeEach(function() {
    loginIntercept();
    menuIntercept();
    loginSetup();
    optionsIntercept();
    menuTrigger(2, 1);
  });

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

      if (search.has('itemNumber') && search.get('itemNumber').length)
        return reply({fixture: 'goods/search'});

      const page = search.get('page');
      reply({fixture: page === '1' ? 'goods/list1' : 'goods/list2'});
    });
    successIntercept('/goods/addGoods');
    successIntercept('/goods/delGoods');
    successIntercept('/goods/updateGoods');
    exportIntercept('/goods/export');
  });

  const TABLE_NAME = 'goods_table',
        MODAL_NAME = 'goods_modal',
        LABEL = '货品';

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

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

    cy.get('#filter_goodsCode').type('P00012');
    validate();
  });

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

  it('新增', function() {
    function inputField() {
      cy.getTestId('field_goodsItemNumber').type('品号');
      cy.getTestId('field_goodsName').type('goodsName');
      cy.getTestId('field_goodsType').type('goodsType');
      cy.getTestId('field_goodsMinAccommodate').clear().type('1');
      cy.getTestId('field_goodsAccommodateNum').clear().type('1');
      cy.getTestId('field_goodsMaxAccommodate').clear().type('1');
      cy.getTestId('field_goodsSupplier').type('goodsSupplier');
      cy.getTestId('field_goodsInputType').type('goodsInputType');
      cy.getTestId('field_goodsLowReserves').clear().type('1');
      cy.getTestId('field_goodsHighReserves').clear().type('1');
      cy.getTestId('field_goodsGrade').clear().type('1');
      cy.getTestId('field_goosdPackingType').type('goosdPackingType');
      cy.getTestId('field_goodsUnit').type('goodsUnit');
      selectClick('select_goodsStorageLocation');
      selectClick('select_goodsUserDepartment');
    }

    validateAdd(inputField);
  });

  it('修改', function() {
    function validateField() {
      cy.getTestId('field_goodsItemNumber').should('have.value', 'P00012');
      cy.getTestId('field_goodsName').should('have.value', '测试货品');
      cy.getTestId('field_goodsType').should('have.value', '类型1');
      cy.getTestId('field_goodsMinAccommodate').should('have.value', '2');
      cy.getTestId('field_goodsAccommodateNum').should('have.value', '1');
      cy.getTestId('field_goodsMaxAccommodate').should('have.value', '4');
      cy.getTestId('field_goodsSupplier').should('have.value', '测试1');
      cy.getTestId('field_goodsInputType').should('have.value', '随便');
      cy.getTestId('field_goodsLowReserves').should('have.value', '1');
      cy.getTestId('field_goodsHighReserves').should('have.value', '3');
      cy.getTestId('field_goodsGrade').should('have.value', '等级2');
      cy.getTestId('field_goosdPackingType').should('have.value', '无');
      cy.getTestId('field_goodsUnit').should('have.value', '公斤');
      validateSelect('select_goodsStorageLocation', '1号库位');
      validateSelect('select_goodsUserDepartment', '物流部门');
    }

    validateEdit(TABLE_NAME, validateField);
  });

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

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