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

export function addSparePartAccessDetailsOut(item, showMessage = true) {
    if (Store.state.data.sparePartAccessDetailsOut[item.sparePartId] == undefined) {
        item.sparePartRetrievalQuantity = 1
        Store.state.data.sparePartAccessDetailsOut[item.sparePartId] = item
    } else {
        Store.state.data.sparePartAccessDetailsOut[item.sparePartId].sparePartRetrievalQuantity++
    }
    Store.commit('data/SET_SPAREPARTACCESSDETAILSOUT', JSON.parse(JSON.stringify(Store.state.data.sparePartAccessDetailsOut)))
    if (showMessage) {
        modal.msgSuccess('添加成功')
    }
}

export function updateSparePartAccessDetailsOut(item, showMessage = true) {
    if (Store.state.data.sparePartAccessDetailsOut[item.sparePartId] == undefined) {
        item.sparePartRetrievalQuantity = 1
        Store.state.data.sparePartAccessDetailsOut[item.sparePartId] = item
    } else {
        Store.state.data.sparePartAccessDetailsOut[item.sparePartId].sparePartRetrievalQuantity = parseInt(item.sparePartRetrievalQuantity)
    }
    Store.commit('data/SET_SPAREPARTACCESSDETAILSOUT', JSON.parse(JSON.stringify(Store.state.data.sparePartAccessDetailsOut)))
    if (showMessage) {
        modal.msgSuccess('修改成功')
    }
}

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

export function removeSparePartAccessDetailsOut(item, showMessage = true) {
    delete Store.state.data.sparePartAccessDetailsOut[item.sparePartId]
    Store.commit('data/SET_SPAREPARTACCESSDETAILSOUT', JSON.parse(JSON.stringify(Store.state.data.sparePartAccessDetailsOut)))
    if (showMessage) {
        modal.msgSuccess('已删除')
    }
}
