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

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

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

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

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