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

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

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

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

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