import Actions from '../../../types/Store/Actions';
import Order, { AllOrderItem } from '../../../types/Order/Order';
import {
  SELECT_ORDER,
  UPDATE_CONSUME_ORDERS,
  APPEND_CONSUME_ORDERS,
  UPDATE_RECHARGE_ORDERS,
  APPEND_RECHARGE_ORDERS,
  UPDATE_CONSUME_LASTID,
  UPDATE_RECHARGE_LASTID,
} from './actions';
import { RechargeOrderItem } from '@/api/order';

export type ConsumeOrderInfo = {
  consumeOrders: AllOrderItem[];
  lastOrderId: number;
};

export type RechargeOrderInfo = {
  rechargeOrders: RechargeOrderItem[];
  lastOrderId: number;
};

export interface OrderStoreState {
  selectedOrder: Order;
  consume: ConsumeOrderInfo;
  recharge: RechargeOrderInfo;
}
const initOrder = () => ({
  actualMoney: 0,
  areaId: 0,
  areaName: '',
  consumeType: '',
  createAt: '',
  customerCellphone: '',
  customerId: 0,
  customerName: '',
  deductionBean: 0,
  deductionMoney: 0,
  equipmentNum: '',
  equipmentPosition: '',
  id: 0,
  operateId: 0,
  operateName: '',
  operationMode: '',
  orderName: '',
  orderNumber: '',
  orderState: '',
  outTradeNo: '',
  payType: '',
  payableMoney: 0,
  serviceId: 0,
  serviceName: '',
  thirdDiscountMoney: 0,
  thirdTradeNumber: '',
  updateDate: '',
});

const INITIAL_STATE = {
  selectedOrder: initOrder(),
  consume: {
    consumeOrders: [],
    lastOrderId: 0,
  },
  recharge: {
    rechargeOrders: [],
    lastOrderId: 0,
  },
};

const getLastId = (arr): number => {
  const itemIdx = arr.length - 1;
  if (itemIdx >= 0) {
    const orderIdx = arr[itemIdx].data.length - 1;
    if (orderIdx >= 0) {
      let lastOrderId = arr[itemIdx].data[orderIdx].id;
      return lastOrderId;
    }
  }
  return 0;
};

export default function OrderList(
  state: OrderStoreState = INITIAL_STATE,
  actions: Actions,
) {
  switch (actions.type) {
    case SELECT_ORDER:
      return {
        ...state,
        selectedOrder: actions.payload,
      };
    case UPDATE_CONSUME_ORDERS:
      if (actions.payload.length) {
        let arr = actions.payload;
        let lastOrderId = getLastId(arr);
        return {
          ...state,
          consume: {
            lastOrderId: lastOrderId,
            consumeOrders: actions.payload,
          },
        };
      }
      return {
        ...state,
        consume: {
          ...state.consume,
          consumeOrders: actions.payload,
        },
      };
    case APPEND_CONSUME_ORDERS:
      if (actions.payload.length) {
        let arr = actions.payload;
        let lastOrderId = getLastId(arr);
        return {
          ...state,
          consume: {
            lastOrderId: lastOrderId,
            consumeOrders: [...state.consume.consumeOrders, ...actions.payload],
          },
        };
      }
      return {
        ...state,
        consume: {
          ...state.consume,
          consumeOrders: [...state.consume.consumeOrders, ...actions.payload],
        },
      };
    case UPDATE_CONSUME_LASTID:
      return {
        ...state,
        consume: {
          ...state.consume,
          lastOrderId: actions.payload,
        },
      };
    case UPDATE_RECHARGE_ORDERS:
      if (actions.payload.length) {
        let arr = actions.payload;
        let lastOrderId = getLastId(arr);
        return {
          ...state,
          recharge: {
            lastOrderId: lastOrderId,
            rechargeOrders: actions.payload,
          },
        };
      }
      return {
        ...state,
        recharge: {
          ...state.recharge,
          rechargeOrders: actions.payload,
        },
      };
    case APPEND_RECHARGE_ORDERS:
      if (actions.payload.length) {
        let arr = actions.payload;
        let lastOrderId = getLastId(arr);
        return {
          ...state,
          recharge: {
            lastOrderId: lastOrderId,
            rechargeOrders: [
              ...state.recharge.rechargeOrders,
              ...actions.payload,
            ],
          },
        };
      }
      return {
        ...state,
        recharge: {
          ...state.recharge,
          rechargeOrders: [
            ...state.recharge.rechargeOrders,
            ...actions.payload,
          ],
        },
      };
    case UPDATE_RECHARGE_LASTID:
      return {
        ...state,
        recharge: {
          ...state.recharge,
          lastOrderId: actions.payload,
        },
      };
    default:
      return state;
  }
}
