/**
 *  下拉组件动态过滤方法定义
 *  用于集中定义需要过滤的过滤方法
 */

import { binApi, binSizeApi } from 'src/api/bin/binApi';
import { storageApi } from 'src/api/storage/storageApi';
import { supplierApi } from 'src/api/supplier/supplierApi';
import { areaApi } from 'src/api/warehouse/areaApi';
import { warehouseApi } from 'src/api/warehouse/warehouseApi';
import { ref } from 'vue';

// ========================================================================================
export const filterFunction: any = {
  // 查询 库存
  wareHouseList: (val?: any, update?: any) => {
    update(() => {
      filterOption.value.wareHouseList = []; // 赋值前清理缓存
      warehouseApi.getWarehouseList(val).then((res: any) => {
        filterOption.value.wareHouseList = res;
      });
    });
  },
  // 查询 库区
  areaList: (val?: any, update?: any) => {
    update(() => {
      filterOption.value.areaList = []; // 赋值前清理缓存
      if (filterJoin['areaListJoin']) {
        areaApi
          .getAreaList(filterJoin['areaListJoin'], val)
          .then((res: any) => {
            filterOption.value.areaList = res;
          });
      }
    });
  },
  // 查询 库位
  binList: (val?: any, update?: any) => {
    update(() => {
      filterOption.value.binList = []; // 赋值前清理缓存
      if (filterJoin['binListJoin']) {
        binApi.getBinList(filterJoin['binListJoin'], val).then((res: any) => {
          filterOption.value.binList = res;
        });
      }
    });
  },
  // 查询 库存地点
  storageList: (val?: any, update?: any) => {
    update(() => {
      filterOption.value.storageList = []; // 赋值前清理缓存
      if (filterJoin['storageListJoin']) {
        storageApi
          .getStorageListByWhere(filterJoin['storageListJoin'], val)
          .then((res: any) => {
            filterOption.value.storageList = res;
          });
      }
    });
  },
  binSizeList: (val?: any, update?: any) => {
    update(() => {
      filterOption.value.binSizeList = []; // 赋值前清理缓存
      binSizeApi.getBinSizeList(val).then((res: any) => {
        filterOption.value.binSizeList = res;
      });
    });
  },
  supplierList: (val?: any, update?: any) => {
    update(() => {
      filterOption.value.supplierList = []; // 赋值前清理缓存
      supplierApi.getSupplierList(val).then((res: any) => {
        filterOption.value.supplierList = res;
      });
    });
  },
  // 用于 调拨中（库内、库外） 查询所有库位
  targetBinList: (val: any, update: any, addData: any) => {
    update(() => {
      binApi
        .getTargetBin(addData.warehouseId, addData.moveType, val)
        .then((res: any) => {
          filterOption.value.targetBinList = res;
        });
    });
  }
};

// ========================================================================================
export const filterOption: any = ref({
  wareHouseList: [], // 工厂 集合
  binSizeList: [],
  storageList: [],
  areaList: [], // 库区 集合
  supplierList: [],
  binList: [], // 库位 集合
  targetBinList: [] // 用于 调拨中（库内、库外） 查询所有库位
});

// ========================================================================================
export const filterLabel: any = {
  wareHouseList: 'warehouseName',
  binSizeList: 'size',
  storageList: 'storageName',
  areaList: 'areaName',
  supplierList: 'supplierName',
  binList: 'binName',
  targetBinList: 'combinationName'
};

// ========================================================================================
export const filterValue: any = {
  wareHouseList: 'id',
  binSizeList: 'id',
  storageList: 'id',
  areaList: 'id',
  supplierList: 'id',
  binList: 'id',
  targetBinList: 'endBinId'
};
// ========================================================================================
export const filterJoin: any = {
  areaListJoin: '', // 入参为 工厂ID
  binListJoin: '', // 入参为 库区ID
  storageListJoin: '' // 入参为 工厂ID
};
// ========================================================================================
// 下拉改变时 触发此方法
export const filterUpdate: any = {
  areaListJoin: (details?: any) => {
    filterJoin['areaListJoin'] = '';
    filterJoin['binListJoin'] = '';
    filterJoin['areaListJoin'] = details; // 赋值 工厂ID
  },
  binListJoin: (details?: any) => {
    filterJoin['binListJoin'] = '';
    filterJoin['binListJoin'] = details; // 赋值 库区ID
  },
  storageListJoin: (details?: any) => {
    filterJoin['binListJoin'] = '';
    filterJoin['storageListJoin'] = '';
    filterJoin['areaListJoin'] = details; // 赋值 工厂ID
    filterJoin['storageListJoin'] = details; // 赋值 库区ID
  }
};

export const init = () => {
  // 初始化默认下拉
  filterOption.value.wareHouseList = []; // 赋值前清理缓存
  warehouseApi.getWarehouseList('').then((res: any) => {
    filterOption.value.wareHouseList = res;
  });
};

export const initAreaList = () => {
  filterOption.value.areaList = []; // 赋值前清理缓存
  if (filterJoin['areaListJoin']) {
    areaApi.getAreaList(filterJoin['areaListJoin'], '').then((res: any) => {
      filterOption.value.areaList = res;
    });
  }
};

export const initBinList = () => {
  filterOption.value.binList = []; // 赋值前清理缓存
  if (filterJoin['binListJoin']) {
    binApi.getBinList(filterJoin['binListJoin'], '').then((res: any) => {
      filterOption.value.binList = res;
    });
  }
};

export const initStorageList = () => {
  filterOption.value.storageList = []; // 赋值前清理缓存
  if (filterJoin['storageListJoin']) {
    storageApi
      .getStorageListByWhere(filterJoin['storageListJoin'], '')
      .then((res: any) => {
        filterOption.value.storageList = res;
      });
  }
};

export const initBinSizeList = () => {
  filterOption.value.binSizeList = []; // 赋值前清理缓存
  binSizeApi.getBinSizeList('').then((res: any) => {
    filterOption.value.binSizeList = res;
  });
};

export const selectLabelByValue = (val: string, val2: string) => {
  return filterOption.value[val].filter((item: any) => {
    if (item.id === val2) {
      return item;
    }
  })[0].warehouseName;
};
