// form/nc/project-material/project-material.js

onMounted(() => {
	clickButton('update') // first data load
})

function clickButton(action) {
	if (action === 'update') {
		nc.callServer(state, 'calc/project-material', {
			parameter: {
				project: state.rec.project
			}
		})
	} else if (action === 'add_order') {
		const saveData = []
		const projectMaterialArr = state.grid.project_material.data
		debugger
		projectMaterialArr.forEach(function (rec) {
			if (rec.order_amount > 0 && rec.ord && rec.ord.company_id) {
				saveData.push(rec)
			}
		})
		nc.callServer(
			state,
			'save',
			{
				preference_name: 'form/nc/project-material/save-purchase-order.json',
				save_data: saveData
			},
			() => {
				clickButton('update')
			}
		)
	} /*  else if (action === 'save') {
		callback = function (ret) {}
		saveRec = function (rec) {
			let key, recSave, val
			recSave = {}
			for (key in rec) {
				val = rec[key]
				if (key.substr(0, 5) === 'st.') {
					if (key === 'st.project_id') {
						recSave[key] = state.rec.project
					} else {
						recSave[key] = val
					}
				}
			}
			return recSave
		}
		saveData = []
		deleteData = []
		projectMaterialArr = state.grid.project_material.data
		projectMaterialArr.forEach(function (rec) {
			state.rec.checkmark = 'x'
			if (state.rec.checkmark !== '') {
				if (state.rec.st.original_amount === 0 && state.rec.calculated_amount === 0 && state.rec.st.record.id !== '') {
				} else {
					saveData.push(saveRec(rec))
				}
			}
		})
		data = {
			database: '4d',
			save: [
				{
					table: 'st',
					data: saveData
				}
			],
			delete: [
				{
					table: 'st',
					data: deleteData
				}
			]
		}
		nc.saveDeleteRecordArray(state, data, callback)
	} */
}
