import {
  getAddressDataAPI,
  getLogisticsDataAPI,
  getSettlementDataAPI,
  getInvoiceInfoDataAPI,
  getOrderCartDataAPI,
  sumbtOrderAPI,
  saveTempOrderAPI,
  getSupplierDataAPI,
  orderEditAPI,
  setDefaultAddressAPI,
  orderReturnForBaseAPI,
  orderEditForCopyAPI,
  queryAllReturnCauseAPI,
  getCustDocDefsByCodeAPI,
  fillItemBomInfoAPI
} from "../services/api";
import { getCustomer } from "@/utils/request.js";
import { fillDefalutInfoForUpload } from "@/pages/Buyer/orderEdit/orderTools";

export default {
  namespace: "returnOrderEdit",

  state: {
    addressData: [],
    invoiceInfoData: [],
    orderCartData: [],
    supplierData: [],
    orderEditData: {}
  },

  effects: {
    // 获取供应商
    *getSupplierData({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getSupplierDataAPI, payload);
      yield put({
        type: "supplierData",
        supplierData: response
      });
    },
    // 请求收货人地址
    *getAddressData({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getAddressDataAPI, payload);
      if (response && Array.isArray(response)) {
        response.forEach(item => (item.isSelected = item.isDefault));
      }
      yield put({
        type: "addressData",
        addressData: response
      });
    },
    // 请求结发票信息
    *getInvoiceInfoData({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getInvoiceInfoDataAPI, payload);
      yield put({
        type: "invoiceInfoData",
        invoiceInfoData: response
      });
    },
    // 暂存订单
    *saveTempOrder({ payload }, { call, put }) {
      const response = yield call(saveTempOrderAPI, payload);
      yield put({
        type: "resultOrder",
        resultOrder: response
      });
    },
    // 提交订单
    *submitOrder({ payload }, { call, put }) {
      const response = yield call(sumbtOrderAPI, payload);
      yield put({
        type: "resultOrder",
        resultOrder: response
      });
    },
    // 退货单编辑
    *getOrderEditData({ payload }, { call, put }) {
      const response = yield call(orderEditAPI, payload);
      // 对附件进行处理
      if (response) {
        response.reqOrderAttachments = fillDefalutInfoForUpload(response.reqOrderAttachments);
        if (response.reqOrderItems) {
          response.reqOrderItems.forEach(item => {
            item = fillDefalutInfoForUpload(item.reqOrderAttachments);
          });
        }
      }
      yield put({
        type: "orderEditData",
        orderEditData: response
      });
    },
    // 退货订单复制
    *getOrderEditDataForCopy({ payload }, { call, put }) {
      const response = yield call(orderEditForCopyAPI, payload);
      yield put({
        type: "orderEditData",
        orderEditData: response
      });
    },
    // 基于原单退货
    *getOrderEditDataForBase({ payload }, { call, put }) {
      const response = yield call(orderReturnForBaseAPI, payload);
      yield put({
        type: "orderEditData",
        orderEditData: response
      });
    },
    // 获取退货原因
    *getAllReturnCause({ payload }, { call, put }) {
      const response = yield call(queryAllReturnCauseAPI, payload);
      const datas = (response || {}).content;
      yield put({
        type: "returnCauseData",
        returnCauseData: datas
      });
    },
    *getAllReturnType({ payload }, { call, put }) {
      payload = {
        cust_doc_code: "QY135"
      };
      const response = yield call(getCustDocDefsByCodeAPI, payload);
      yield put({
        type: "returnTypeData",
        returnTypeData: response || []
      });
    },
    // 设置默认收获地址
    *setDefaultAddress({ payload }, { call, put }) {
      let response = yield call(setDefaultAddressAPI, payload);
      response = response || [];
      yield put({
        type: "addressData",
        addressData: response
      });
    },
    // 补充BOM信息
    *getItemBomInfo({ payload }, { call, put }) {
      const { reqOrderItems } = payload;
      const newReqOrderItems = [].concat(reqOrderItems);
      if (newReqOrderItems && newReqOrderItems.length > 0) {
        for (let i = 0; i < newReqOrderItems.length; i++) {
          const params = {
            parentGoodId: newReqOrderItems[i].goodsId
          };
          const response = yield call(fillItemBomInfoAPI, params);
          if (response && response.length > 0) {
            newReqOrderItems[i].reqOrderItemBoms = response[0].goodsBomChildren || [];
            if (response[0].goodsBomChildren) {
              const optionalBom = response[0].goodsBomChildren.find(item => item.isOptional == 1);
              if (optionalBom) {
                newReqOrderItems[i].isOptional = 1;
              }
            }
          }
        }
      }
      yield put({
        type: "orderCartData",
        orderCartData: newReqOrderItems
      });
    }
  },

  reducers: {
    supplierData(state, payload) {
      return {
        ...state,
        supplierData: payload.supplierData
      };
    },
    addressData(state, payload) {
      return {
        ...state,
        addressData: payload.addressData
      };
    },
    logisticsData(state, payload) {
      return {
        ...state,
        logisticsData: payload.logisticsData
      };
    },
    settlementData(state, payload) {
      return {
        ...state,
        settlementData: payload.settlementData
      };
    },
    invoiceInfoData(state, payload) {
      return {
        ...state,
        invoiceInfoData: payload.invoiceInfoData
      };
    },
    orderCartData(state, payload) {
      return {
        ...state,
        orderCartData: payload.orderCartData
      };
    },
    resultOrder(state, payload) {
      return {
        ...state,
        resultOrder: payload.resultOrder
      };
    },
    orderEditData(state, payload) {
      return {
        ...state,
        orderEditData: payload.orderEditData
      };
    },
    returnCauseData(state, payload) {
      return {
        ...state,
        returnCauseData: payload.returnCauseData
      };
    },
    returnTypeData(state, payload) {
      return {
        ...state,
        returnTypeData: payload.returnTypeData
      };
    }
  }
};
