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


// export function addShoppingCart(item) {
//     if (Store.state.data.shoppingCart[item.itemId] == undefined) {
//         item._quantity = 1
//         Store.state.data.shoppingCart[item.itemId] = item
//     } else {
//         Store.state.data.shoppingCart[item.itemId]._quantity++
//     }
//     Store.commit('data/SET_SHOPPINGCART', JSON.parse(JSON.stringify(Store.state.data.shoppingCart)))
//     modal.msgSuccess('添加成功')
// }

// export function clearShoppingCart() {
//     Store.commit('data/SET_SHOPPINGCART', {})
// }

// export function removeShoppingCart(item) {
//     delete Store.state.data.shoppingCart[item.itemId]
//     Store.commit('data/SET_SHOPPINGCART', JSON.parse(JSON.stringify(Store.state.data.shoppingCart)))
// }



export function addApplyPurchaseDetails(item, showMessage = true) {
    if (item.impa) {
        item.materialWarehouseImpa = item.impa
    }
    if (item.impaCode) {
        item.materialWarehouseImpaCode = item.impaCode
    }
    if (item.description) {
        item.materialWarehouseImpaDescription = item.description
    }
    if (item.specifications) {
        item.materialWarehouseImpaSpecifications = item.specifications
    }
    if (item.unit) {
        item.materialWarehouseImpaUnit = item.unit
    }

    // if(item.){
    //     item. = item.
    // }

    // console.log(item.materialWarehouseImpa)
    // console.log(Store.state.data.applyPurchaseDetails[item.materialWarehouseImpa])
    if (Store.state.data.applyPurchaseDetails[item.materialWarehouseImpa] == undefined) {
        item.applyPurchaseNum = 1
        Store.state.data.applyPurchaseDetails[item.materialWarehouseImpa] = item
    } else {
        Store.state.data.applyPurchaseDetails[item.materialWarehouseImpa].applyPurchaseNum++
    }
    Store.commit('data/SET_APPLYPURCHASEDETAILS', JSON.parse(JSON.stringify(Store.state.data.applyPurchaseDetails)))
    if (showMessage) {
        modal.msgSuccess('添加成功')
    }
}

export function updateApplyPurchaseDetails(item, showMessage = true) {
    if (item.impa) {
        item.materialWarehouseImpa = item.impa
    }
    if (item.impaCode) {
        item.materialWarehouseImpaCode = item.impaCode
    }
    if (item.description) {
        item.materialWarehouseImpaDescription = item.description
    }
    if (item.specifications) {
        item.materialWarehouseImpaSpecifications = item.specifications
    }
    if (item.unit) {
        item.materialWarehouseImpaUnit = item.unit
    }

    // if(item.){
    //     item. = item.
    // }

    // console.log(item.materialWarehouseImpa)
    // console.log(Store.state.data.applyPurchaseDetails[item.materialWarehouseImpa])
    // if (Store.state.data.applyPurchaseDetails[item.materialWarehouseImpa] == undefined) {
    //     item.applyPurchaseNum = 1
    //     Store.state.data.applyPurchaseDetails[item.materialWarehouseImpa] = item
    // } else {
    //     Store.state.data.applyPurchaseDetails[item.materialWarehouseImpa].applyPurchaseNum = parseInt(item.applyPurchaseNum)
    // }
    Store.state.data.applyPurchaseDetails[item.materialWarehouseImpa] = item
    Store.state.data.applyPurchaseDetails[item.materialWarehouseImpa].applyPurchaseNum = parseInt(item.applyPurchaseNum) > 0 ? parseInt(item.applyPurchaseNum) : 1


    Store.commit('data/SET_APPLYPURCHASEDETAILS', JSON.parse(JSON.stringify(Store.state.data.applyPurchaseDetails)))
    if (showMessage) {
        modal.msgSuccess('修改成功')
    }
}

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

export function removeApplyPurchaseDetails(item, showMessage = true) {
    delete Store.state.data.applyPurchaseDetails[item.materialWarehouseImpa]
    Store.commit('data/SET_APPLYPURCHASEDETAILS', JSON.parse(JSON.stringify(Store.state.data.applyPurchaseDetails)))
    if (showMessage) {
        modal.msgSuccess('已删除')
    }
}
