import Store from '@/store'
import modal from '@/plugins/modal'

export function addWarehouseApplyDetails(item, showMessage = true) {
    if (Store.state.data.warehouseApplyDetails[item.warehouseId] == undefined) {
        item.warehouseApplyQuantity = 1
        Store.state.data.warehouseApplyDetails[item.warehouseId] = item
    } else {
        Store.state.data.warehouseApplyDetails[item.warehouseId].warehouseApplyQuantity++
    }
    Store.commit('data/SET_WAREHOUSEAPPLYDETAILS', JSON.parse(JSON.stringify(Store.state.data.warehouseApplyDetails)))
    if (showMessage) {
        modal.msgSuccess('添加成功')
    }
}

export function updateWarehouseApplyDetails(item, showMessage = true) {
    if (Store.state.data.warehouseApplyDetails[item.warehouseId] == undefined) {
        item.warehouseApplyQuantity = 1
        Store.state.data.warehouseApplyDetails[item.warehouseId] = item
    } else {
        Store.state.data.warehouseApplyDetails[item.warehouseId].warehouseApplyQuantity = parseInt(item.warehouseApplyQuantity)
    }
    Store.commit('data/SET_WAREHOUSEAPPLYDETAILS', JSON.parse(JSON.stringify(Store.state.data.warehouseApplyDetails)))
    if (showMessage) {
        modal.msgSuccess('修改成功')
    }
}

export function clearWarehouseApplyDetails(showMessage = true) {
    Store.commit('data/SET_WAREHOUSEAPPLYDETAILS', {})
    if (showMessage) {
        modal.msgSuccess('已清空')
    }
}

export function removeWarehouseApplyDetails(item, showMessage = true) {
    delete Store.state.data.warehouseApplyDetails[item.warehouseId]
    Store.commit('data/SET_WAREHOUSEAPPLYDETAILS', JSON.parse(JSON.stringify(Store.state.data.warehouseApplyDetails)))
    if (showMessage) {
        modal.msgSuccess('已删除')
    }
}
