// form/manager/project-material/project-material.js
/* global nc */

const method = {}
const ret = {
	name: "ProjectMaterial",
	created() {
		// {"call": "dialog/project-material", "param": {"parameter": {"session_id": nc.searchParameter("id")}}, "callback": loadGrid}
		let id = nc.searchParameter("id")
		nc.callServer(this, "form/manager/project-material", { "parameter": { "session_id": id } })
	},
	methods: method
}

method.loadGrid = function () { debugger }

method.clickButton = function (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/project-material/save/save_purchase_order.json",
				save_data: saveData
			},
			(ret) => {
				debugger
				method.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)
	}
}
return ret
