text: // Copyright (c) 2019, Dirk van der Laarse and contributors
  // For license information, please see license.txt
  
  frappe.ui.form.on('Audit', {
  	before_submit: function(frm) {
  		//Check if audit Passed or Failed
  		var failures = "";
  		var child_table;
  		if (frm.doc.type == "Product Audit") {
  			child_table = frm.doc.product_characteristics;
  		} else if (frm.doc.type == "Process/Control Plan Audit") {
  			child_table = frm.doc.characteristic_controls;
  		}
  		$.each(child_table || [], function(i, d) {
  			if (frm.doc.type == "Product Audit") {
  				if (d.checked == "No"){
  					failures = failures + "The Yes/No Check failed for Customer Requirement " + d.code + " (" + d.description + ") |\n";
  				}
  			}
  			else if (frm.doc.type == "Process/Control Plan Audit") {
  				//Check if step is checked
  				if (d.step_checked == "No"){
  					failures = failures + "The Yes/No Check failed for Step at sequence " + d.idx + " at " + d.workstation + " (" + d.step + ": '" + d.step_description +"') |\n";
  				}
  				//Check if control is checked
  				if (d.control_checked == "No"){
  					failures = failures + "The Yes/No Check failed for Control at sequence " + d.idx + " at " + d.workstation + " (" + d.control_name + ": '" + d.control_description +"') |\n";
  				}
  			}
  		});
  
  		if (failures.length > 0){
  			frm.set_value("failure_reasons", failures);
  			cur_frm.toggle_display("failure_reasons", true);
  			frm.set_value("status", "Failed");
  		}
  		else {
  			frm.set_value("status", "Passed");
  		}
  
  	},
  	create_actions: function(frm) {
  		frappe.call({
        method: "create_task",
        doc: frm.doc,
        callback: function(r) {
  				frappe.set_route("Form", "Task",r.message);
  			}
  		})
  	}
  });
  
  
  // Button to navigate to next row in checklist table
  frappe.ui.form.on('Audit Step Characteristic Control', {
  	next: function(frm) {
  		var grid_row = frappe.ui.form.get_open_grid_form();
  		grid_row && grid_row.toggle_view(false, function() { grid_row.open_next() });
  	}
  });
  
  // Button to navigate to next row in checklist table
  frappe.ui.form.on('Audit Step Characteristic', {
  	next: function(frm) {
  		var grid_row = frappe.ui.form.get_open_grid_form();
  		grid_row && grid_row.toggle_view(false, function() { grid_row.open_next() });
  	}
  });
  
  
  cur_frm.cscript.custom_refresh = function(doc) {
  	// Hide checkbox in sequence column on child tables
  	$('input.grid-row-check.pull-left').css({'display': 'none'});
  
  	// Hide Add Row buttons
  	$('div.small.form-clickable-section.grid-footer').css({'display': 'none'});
  
  	if (doc.docstatus == 1) {
  		// Show failure reasons and create action button if doc is submitted and in Failed state
  		if (doc.status != "Passed" && doc.status != "In Progress"){
  			cur_frm.toggle_display("failure_reasons", true);
  			cur_frm.toggle_display("create_actions_section", true);
  		}
  	}
  	refresh_row_colours();
  }
  
  
  function refresh_row_colours() {
  	// Colour rows based on status
  	$.each(cur_frm.doc.characteristic_controls, function(j,row){
  		if(row.step_checked == "Not Audited"){
  			$('div.grid-row[data-idx="'+row.idx+'"]').css({'color': '#bfbfbf'});
  			$('div.grid-row[data-idx="'+row.idx+'"]').css({'background-color': '#f0f0f0'});
  		}
  		// else if (row.is_applicable == 1 && row.task === undefined){
  		// 	$('div.grid-row[data-idx="'+row.idx+'"]').css({'background-color': '#f55656'});
  		// }
  		// else if (row.is_applicable == 1 && row.completed == 0 && row.task != undefined){
  		// 	$('div.grid-row[data-idx="'+row.idx+'"]').css({'background-color': '#f79739'});
  		// }
  	});
  }
  