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

const basicData = {
  id: '57',
  askGoodsId: 'LLDID123',
  entryNumber: '1234',
  productionCode: 'LLDCODE123',
  storageLocationName: null,
  userName: null,
  realName: null,
  materialId: '00000000000000012522',
  wbs: '111',
  measurementId: null,
  num: '2',
  outNum: '0',
  askGoodsCode: 'LLDCODE123',
  companyNumber: '8130',
  companyCode: null,
  sqrq: '2023-04-13',
  departmentId: null,
  departmentName: '采购中心（公共） ',
  department: null,
  departmentCode: null,
  name: null,
  code: null,
  sourceType: '8',
  moveType: '207',
  materialName: '加强筋;8T.K42.600.0210',
  materialCode: '8TK426000210',
  uniqueCode: null,
  storageLocationCode: null,
  supplierId: null,
  serial: null,
  type: null,
  userId: null,
  scrq: null,
  supplierName: null,
  producDate: null,
  wllbCode: null,
  page: 0,
  limit: 0,
  askGoodsType: null,
  startTime: null,
  endTime: null,
  process: null,
  askGoodsApplyType: null,
  partType: null,
  attribute: null,
  deliveryId: null,
  customerName: null,
  customerCode: null,
  warehouseWhere: null,
  companyName: '青岛特来电智能设备有限公司',
  storageCode: null,
  accountSleeve: null,
  inventoryId: null,
  storageLocationCodeRk: null,
  accountName: null,
  requestTypeNumber: null,
  requestTypeName: null,
  ifCommodity: null,
  amount: '0',
};

describe('生产领料明细', function () {
  beforeEach(function () {
    beforeSetup();
    intoMenu('出库管理', '生产领料明细');
  });

  beforeEach(function () {
    intercept('/queryList/getAskGoods', function ({reply, search}) {
      generateNetworkResult({
        search,
        reply,
        basicData,
        title: 'askGoodsCode',
        skipCondition: name => name === 'type',
      });
    });
    intercept('/queryList/getInvite', function ({reply, search}) {
      generateNetworkResult({
        search,
        reply,
        basicData,
        title: 'askGoodsCode',
        skipCondition: name => name === 'type',
      });
    });

    successIntercept(['/askGoods/delAskGoods', '/invite/delAskGoods']);
    exportIntercept([
      '/queryList/getAskGoodsExcel',
      '/queryList/getInviteExcel',
    ]);
  });

  function validateTable(label?: string, callback?: () => void) {
    selectAllFilters('production_requisition_filter', 11);
    validateTableList('production_requisition_table');
    validateTableSearch(
      'production_requisition_table',
      [
        'askGoodsCode',
        'productionCode',
        'materialCode',
        'materialName',
        {id: 'companyCode', type: 'select', value: '8130'},
        {id: 'departmentCode', type: 'select', value: '00029999'},
        'moveType',
        'sourceType',
        'entryNumber',
        'wbs',
      ],
      {
        toolId: 'production_requisition_filter',
        url: '/queryList/getAskGoods',
      },
    );

    // eslint-disable-next-line callback-return
    callback?.();

    validateExport('production_requisition_table');

    const {validateAdd} = validatePut(
      'production_requisition_modal',
      'production_requisition_table',
      {label: label ?? '生产领料单'},
    );

    validateAdd([
      'requestOrderId',
      'requestOrderNo',
      'prodOrderNo',
      'requestTypeName',
      'requestTypeNumber',
      {id: 'materialId', type: 'keySelect'},
      {id: 'measUnitId', type: 'keySelect'},
      {id: 'orderCount', type: 'field', value: '10'},
      {id: 'companyNo', type: 'keySelect'},
      {id: 'ifCommodity', type: 'select'},
      {id: 'useDepartment', type: 'select'},
      'entryNumber',
      'WBS',
    ]);

    validateDelete('production_requisition_table', label ?? '生产领料单', {
      eq: 0,
      title: 'page-1',
    });
  }

  it('table', () => validateTable());

  it('non-standard table', function () {
    selectClick('filter_type', 1);
    cy.getTestId('search_btn').click();

    validateTable('非生产领料单', function () {
      selectClick('filter_type', 1);
      cy.getTestId('search_btn').click();
    });
  });
});
