// form/manager/collect-delivery/collect-delivery.js
/* global nc */

return {
	name: "CollectDelivery",
	created() {
		let id = nc.searchParameter("id")
		nc.callServer(this, "form/manager/project-material", { "parameter": { "session_id": id } }, this.loadGrid)
	},
	methods: {
		loadGrid(ret) {
			console.debug("form/manager/project-material callback ret: ", ret)
			// debugger
		},
		clickButton(action) {
			var data, deleteData, projectMaterialArr, saveData, saveRec
			if (action === "update") {
				nc.callServer(this, "form/manager/project-material", { parameter: { project: this.rec.project } })
			} else if (action === "add_order") {
				/* saveRec = function (rec) {
					var key, recSave, val
					recSave = {}
					recSave["ord.project_id"] = this.rec.project
					recSave["ordr"] = {}
					// structure arr generation nit ready
					recSave["ordr"][0] = {}
					for (key in rec) {
						val = rec[key]
						if (key === "pr.default_supplier_id") {
							recSave["ord.company_id"] = val
						} else if (key === "prst_product_id") {
							recSave["ordr"][0]["ord_product_id"] = val
						} else if (key === "pr.name") {
							recSave["ordr"][0]["ordr.description"] = val
						} else if (key === "order_amount") {
							recSave["ordr"][0]["ordr.order_amount"] = val
						}
					}
					// recSave[key] = val
					return recSave
				} */
				saveData = []
				projectMaterialArr = this.grid.project_material.data
				projectMaterialArr.forEach(function (rec) {
					if (rec.ordr.order_amount > 0 && rec.ord.company_id !== "") {
						saveData.push(rec)
					}
				})
				// saveData.push(saveRec(rec))
				debugger
				nc.saveJson(this,
					{
						preference_name: "form/manager/collect-delivery/save/save_purchase_order.json",
						save_data: saveData
					}, (ret) => {
						debugger
						this.clickButton("update")
					}
				)
				/*
				data =
					database: "4d"
					save: [{
						"table": "ord"
						"data": saveData
						"row_table": ["ordr"]
						"aggregate_field": "ord.company_id"
					}]

				nc.saveDeleteRecordArray(this, data)
				*/
			} else if (action === "save") {
				debugger
				saveRec = function (rec) {
					var key, recSave, val
					recSave = {}
					for (key in rec) {
						val = rec[key]
						if (key.substr(0, 5) === "prst_") {
							if (key === "prst_project_id") {
								recSave[key] = this.rec.project
							} else {
								recSave[key] = val
							}
						}
					}
					return recSave
				}
				saveData = []
				deleteData = []
				projectMaterialArr = this.grid.project_material.data
				projectMaterialArr.forEach(function (rec) {
					this.rec.checkmark = "x"
					if (this.rec.checkmark !== "") {
						if (!(this.rec.prst_original_amount === 0 && this.rec.calculated_amount === 0 && this.rec.prst_record_id !== "")) {
							// deleteData.push({prst_record_id: this.rec.prst_record_id})
							saveData.push(saveRec(rec))
						}
					}
				})
				data = {
					database: "4d",
					save: [
						{
							table: "prst",
							data: saveData
						}
					],
					delete: [
						{
							table: "prst",
							data: deleteData
						}
					]
				}
				nc.saveDeleteRecordArray(this, data)
			}
		}
	}
}
