import { listWarehouseNoPage } from '@/api/wms/warehouse';
import { listMerchantNoPage } from "@/api/wms/merchant";
import { listItemCategory, treeSelectItemCategory } from "@/api/wms/itemCategory";
import { listItemBrand } from "@/api/wms/itemBrand";
import { listProject } from "@/api/wms/project";
import { listCompany,listEmployee,listWork } from "@/api/wms/employee";
import { listOrder,getOrderDetail } from "@/api/wms/product";
import { listItemPage2 } from "@/api/wms/item";
import { listItemSkuPage } from "@/api/wms/itemSku";
import {defineStore} from "pinia";
import {ref} from "vue";

export const useWmsStore = defineStore('wms', () => {

  // 仓库管理
  const warehouseList = ref([]);
  const warehouseMap = ref(new Map());
  const getWarehouseList = () => {
    listWarehouseNoPage({}).then((res) => {
      warehouseList.value = res.data.list;
      const map = new Map();
      warehouseList.value.forEach((supplier) => {
        map.set(supplier.id, supplier);
      });
      warehouseMap.value = map;
    });
  };

  // 企业管理
  const merchantList = ref([]);
  const merchantMap = ref(new Map());
  const getMerchantList = () => {
    listMerchantNoPage({}).then((res) => {
      merchantList.value = res.data.list;
      const map = new Map();
      merchantList.value.forEach((supplier) => {
        map.set(supplier.id, supplier);
      });
      merchantMap.value = map;
    });
  }

  // 产品分类管理
  const itemCategoryList = ref([])
  const itemCategoryTreeList = ref([])
  const itemCategoryMap = ref(new Map())
  const getItemCategoryList = () => {
    return new Promise((resolve, reject) => {
      listItemCategory({page:1,limit:-1}).then(res => {
        itemCategoryList.value = res.data.list;
        const map = new Map()
        itemCategoryList.value.forEach(supplier => {
          map.set(supplier.id, supplier)
        })
        itemCategoryMap.value = map
        resolve()
      }).catch(() => reject())
    })
  }

  const getItemCategoryTreeList = async () => {
    return new Promise((resolve, reject) => {
      treeSelectItemCategory().then(res => {
        itemCategoryTreeList.value = res.data
        resolve(res.data)
      }).catch(() => reject())
    })
  }

  // 产品品牌管理
  const itemBrandList = ref([])
  const itemBrandMap = ref(new Map())
  const getItemBrandList =  (queryParams) => {
    return new Promise((resolve, reject) => {
      listItemBrand(queryParams).then(res => {
        itemBrandList.value = res.data.list
        const map = new Map()
        itemBrandList.value.forEach(supplier => {
          map.set(supplier.id, {...supplier})
        })
        itemBrandMap.value = map
        resolve()
      }).catch(() => reject())
    })
  }

  // 合同管理
  const projectList = ref([])
  const projectMap = ref(new Map())
  const getProjectList =  (queryParams) => {
    return new Promise((resolve, reject) => {
      listProject(queryParams).then(res => {
        projectList.value = res.data.list
        const map = new Map()
        projectList.value.forEach(supplier => {
          map.set(supplier.id, {...supplier})
        })
        projectMap.value = map
        resolve()
      }).catch(() => reject())
    })
  }
  
  // 外包公司管理
  const companyList = ref([]);
  const companyMap = ref(new Map());
  const getCompanyList = () => {
    listCompany({page:1,limit:-1}).then((res) => {
      companyList.value = res.data.list;
      const map = new Map();
      companyList.value.forEach((supplier) => {
        map.set(supplier.id, supplier);
      });
      companyMap.value = map;
    });
  }

  // 员工管理
  const employeeList = ref([]);
  const employeeMap = ref(new Map());
  const getEmployeeList = () => {
    listEmployee({page:1,limit:-1}).then((res) => {
      employeeList.value = res.data.list;
      const map = new Map();
      employeeList.value.forEach((supplier) => {
        map.set(supplier.id, supplier);
      });
      employeeMap.value = map;
    });
  }

  // 生产订单
  const orderList = ref([]);
  const orderMap = ref(new Map());
  const getOrderList = () => {
    listOrder({page:1,limit:-1}).then((res) => {
      orderList.value = res.data.list;
      const map = new Map();
      orderList.value.forEach((supplier) => {
        map.set(supplier.id, supplier);
      });
      orderMap.value = map;
    });
  }

  // 生产订单产品
  const skuList = ref([]);
  const skuMap = ref(new Map());
  const getSkuList = (orderId) => {
    getOrderDetail(orderId).then((res) => {
      skuList.value = res.data;
      const map = new Map();
      skuList.value.forEach((supplier) => {
        map.set(supplier.id, supplier);
      });
      skuMap.value = map;
    });
  }
  
  // 产品管理产品(成品)
  const productList = ref([]);
  const productMap = ref(new Map());
  const getProductList = () => {
    listItemSkuPage({page:1,limit:-1,categoryType:1}).then((res) => {
      productList.value = res.data.list;
      const map = new Map();
      productList.value.forEach((supplier) => {
        map.set(supplier.id, supplier);
      });
      productMap.value = map;
    });
  }

  // 外包人员工种
  const employeeWorkList = ref([]);
  const employeeWorkMap = ref(new Map());
  const getEmployeeWorkList = () => {
    listWork({page:1,limit:-1}).then((res) => {
      employeeWorkList.value = res.data.list;
      const map = new Map();
      employeeWorkList.value.forEach((supplier) => {
        map.set(supplier.id, supplier);
      });
      employeeWorkMap.value = map;
    });
  }

  return {
    // 仓库管理
    warehouseList,
    warehouseMap,
    getWarehouseList,
    // 企业管理
    merchantList,
    merchantMap,
    getMerchantList,
    // 产品分类管理
    itemCategoryList,
    itemCategoryTreeList,
    itemCategoryMap,
    getItemCategoryList,
    getItemCategoryTreeList,
    // 产品品牌管理
    itemBrandList,
    itemBrandMap,
    getItemBrandList,
    // 合同管理
    projectList,
    projectMap,
    getProjectList,
    // 外包公司
    companyList,
    companyMap,
    getCompanyList,
    // 员工管理
    employeeList,
    employeeMap,
    getEmployeeList,
    // 生产订单
    orderList,
    orderMap,
    getOrderList,
    //生产订单产品sku
    skuList,
    skuMap,
    getSkuList,
    //产品管理中产品
    productList,
    productMap,
    getProductList,
    // 外包人员工种
    employeeWorkList,
    employeeWorkMap,
    getEmployeeWorkList,
  };
});
