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

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

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

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

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