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

export function addWarehouseAccessDetailsIn(item, showMessage = true) {
    console.log(item)
    console.log(JSON.parse(JSON.stringify(Store.state.data.warehouseAccessDetailsIn)))
    if (Store.state.data.warehouseAccessDetailsIn[item.warehouseId] == undefined) {
        item.warehouseRetrievalQuantity = 1
        Store.state.data.warehouseAccessDetailsIn[item.warehouseId] = item
    } else {
        Store.state.data.warehouseAccessDetailsIn[item.warehouseId].warehouseRetrievalQuantity++
    }
    Store.commit('data/SET_WAREHOUSEACCESSDETAILSIN', JSON.parse(JSON.stringify(Store.state.data.warehouseAccessDetailsIn)))
    if (showMessage) {
        modal.msgSuccess('添加成功')
    }
}

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

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

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