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

export function addMaintenanceApplyDetails(item, showMessage = true) {
    if (item.maintenanceProjectFile == undefined) {
        item.maintenanceProjectFile = ''
    }
    if (item.maintenanceId != undefined) {
        item.materialWarehouseImpaCode = item.maintenanceId
    }
    if (Store.state.data.maintenanceApplyDetails[item.maintenanceProjectId] == undefined) {
        item.quantity = 1
        Store.state.data.maintenanceApplyDetails[item.maintenanceProjectId] = item
    } else {
        Store.state.data.maintenanceApplyDetails[item.maintenanceProjectId].quantity++
    }
    Store.commit('data/SET_MAINTENANCEAPPLYDETAILS', JSON.parse(JSON.stringify(Store.state.data.maintenanceApplyDetails)))
    if (showMessage) {
        modal.msgSuccess('添加成功')
    }
}

export function updateMaintenanceApplyDetails(item, showMessage = true) {
    if (Store.state.data.maintenanceApplyDetails[item.maintenanceProjectId] == undefined) {
        item.quantity = 1
        Store.state.data.maintenanceApplyDetails[item.maintenanceProjectId] = item
    } else {
        Store.state.data.maintenanceApplyDetails[item.maintenanceProjectId].quantity = parseInt(item.quantity)
    }
    Store.commit('data/SET_MAINTENANCEAPPLYDETAILS', JSON.parse(JSON.stringify(Store.state.data.maintenanceApplyDetails)))
    if (showMessage) {
        modal.msgSuccess('修改成功')
    }
}

export function clearMaintenanceApplyDetails(showMessage = true) {
    Store.commit('data/SET_MAINTENANCEAPPLYDETAILS', {})
    Store.state.data.maintenanceApplyDetailsCustomList= []
    if (showMessage) {
        modal.msgSuccess('已清空')
    }
}

export function removeMaintenanceApplyDetails(item, showMessage = true) {
    delete Store.state.data.maintenanceApplyDetails[item.maintenanceProjectId]
    Store.commit('data/SET_MAINTENANCEAPPLYDETAILS', JSON.parse(JSON.stringify(Store.state.data.maintenanceApplyDetails)))
    if (showMessage) {
        modal.msgSuccess('已删除')
    }
}
