// JavaScript Document

/*************************************validation********************************/
function jobcard_validation()
{
    var customer_name = document.getElementById("customer_name").value;
	if(customer_name=='')
	{
		document.getElementById("customer_name_validation_div").innerHTML="* This field is Required.";
		document.getElementById("customer_name").focus();
		return false;
	}
	
	var style_no = document.getElementById("style_no").value;
	if(style_no=='')
	{
		document.getElementById("style_no_validation_div").innerHTML="* This field is Required.";
		document.getElementById("style_no").focus();
		return false;
	}
	var created_by = document.getElementById("created_by").value;
	if(created_by=='')
	{
		document.getElementById("job_created_by_validation_div").innerHTML="* This field is Required.";
		document.getElementById("created_by").focus();
		return false;
	}
	var style_name = document.getElementById("style_name").value;
	if(style_name=='')
	{
		document.getElementById("style_name_validation_div").innerHTML="* This field is Required.";
		document.getElementById("style_name").focus();
		return false;
	}
	var cust_po_no = document.getElementById("cust_po_no").value;
	if(cust_po_no=='')
	{
		document.getElementById("cust_po_no_validation_div").innerHTML="* This field is Required.";
		document.getElementById("cust_po_no").focus();
		return false;
	}
	var combo = document.getElementById("combo").value;
	if(combo=='')
	{
		document.getElementById("combo_validation_div").innerHTML="* This field is Required.";
		document.getElementById("combo").focus();
		return false;
	}
}

/*************************************validation********************************/
function jobcard_sub_validation()
{
    var customer_name = document.getElementById("customer_name").value;
	if(customer_name=='')
	{
		document.getElementById("customer_name_validation_div").innerHTML="* This field is Required.";
		document.getElementById("customer_name").focus();
		return false;
	}
	 var particulars = document.getElementById("particulars").value;
	if(particulars=='')
	{
		document.getElementById("particulars_validation_div").innerHTML="* This field is Required.";
		document.getElementById("particulars").focus();
		return false;
	}
	
}
/*************************************validation********************************/


function jobcard_clear_validation(str)
{
	if(str!='')
	{
		document.getElementById("customer_name_validation_div").innerHTML="";
	}
	
	if(str!='')
	{
		document.getElementById("style_no_validation_div").innerHTML="";
	}
	if(str!='')
	{
		document.getElementById("job_created_by_validation_div").innerHTML="";
	}
	if(str!='')
	{
		document.getElementById("style_name_validation_div").innerHTML="";
	}
	if(str!='')
	{
		document.getElementById("cust_po_no_validation_div").innerHTML="";
	}

    if(str!='')
	{
		document.getElementById("combo_validation_div").innerHTML="";
	}
}
/************************************validation*********************************/

function jobcard_sub_clear_validation(str)
{
	if(str!='')
	{
		document.getElementById("customer_name_validation_div").innerHTML="";
	}
	
	if(str!='')
	{
		document.getElementById("particulars_validation_div").innerHTML="";
	}
	
	
}


/*************************************Main list add********************************/

function jobcard_main_list(jobcard_no,entry_time,customer_name,style_no,created_by,style_name,cust_po_no,combo,jobtype,fabric_type,overal_tot,overal_width,overal_height,overal_qty,tax,tax_amt,net_amt,edit_id,action)
{ 

		if((customer_name!='')&&(style_no!='')&&(created_by!='')&&(style_name!='')&&(cust_po_no!='')&&(combo!=''))
 {
	 
	  if(confirm('Are You Sure! You Want To Conform The JobCard..?'))
	{
        jQuery.ajax({
		type: "POST",
		url: "jobcard/jobcard_entry/jobcard_db.php",
		data: "jobcard_no="+jobcard_no+"&entry_time="+entry_time+"&customer_name="+customer_name+"&style_no="+style_no+"&created_by="+created_by+"&style_name="+style_name +"&cust_po_no="+cust_po_no+"&combo="+combo+"&jobtype="+jobtype+"&fabric_type="+fabric_type +"&overal_tot="+overal_tot+"&overal_width="+overal_width+"&overal_height="+overal_height+"&overal_qty="+overal_qty+"&tax="+tax+"&tax_amt="+tax_amt+"&net_amt="+net_amt+"&main_edit_id="+edit_id+"&action="+action,
		success: function(msg){jQuery("#jobcard_main_list_div").html(msg);	
		if(edit_id!='')
		{
			
		window.location.href="main_fram.php?file=jobcard_creation";
		}
		
			else
			{
			window.location.reload(true);
		    }
 }
		});
 }
 }
else
{
   jobcard_validation();
}
}

/******************************* Branch Sublist**********************************************/
// function jobcard_sub_list(
//     jobcard_no, entry_time, created_by, cust_po_no, customer_name, jobtype, 
//     fabric_type, hsn_code, particulars, width, height, rate, amount, 
//     conformed_amt, conform_rate, qty, values, edit_id, quotation_no, action
// ) {

// 	// alert(customer_name);
	
//     let ratetrrrr = parseFloat(sm("#customer_total_confirm_rate").val() || 0);
// //  alert("Confirm Rate: " + ratetrrrr);
//     // Ensure conform_rate is a number too
//     conform_rate = parseFloat(conform_rate);

       

//     // Compare as numbers
//     if (conform_rate >= ratetrrrr) {
//         if (particulars !== '' && amount !== '' && conformed_amt !== '' && values !== '') {
//             jQuery.ajax({
//                 type: "POST",
//                 url: "jobcard/jobcard_entry/jobcard_db.php",
//                 data: "jobcard_no=" + jobcard_no +
//                       "&entry_time=" + entry_time +
//                       "&created_by=" + created_by +
//                       "&cust_po_no=" + cust_po_no +
//                       "&customer_name=" + name +   // 👈 use clean name (without rate)
//                       "&jobtype=" + jobtype +
//                       "&fabric_type=" + fabric_type +
//                       "&hsn_code=" + hsn_code +
//                       "&particulars=" + particulars +
//                       "&jobcard_width=" + width +
//                       "&jobcard_height=" + height +
//                       "&rate=" + rate +
//                       "&amount=" + amount +
//                       "&conformed_amt=" + conformed_amt +
//                       "&conform_rate=" + conform_rate +
//                       "&qty=" + qty +
//                       "&values=" + values +
//                       "&edit_id=" + edit_id +
//                       "&quotation_no=" + quotation_no +
//                       "&action=" + action,
//                 success: function(msg) {
//                     jQuery("#jobcard_sublist_div").html(msg);    
                    
//                     // Reset form fields
//                     document.getElementById('particulars').value = "";
//                     document.getElementById('jobcard_width').value = "";
//                     document.getElementById('jobcard_height').value = "";
//                     document.getElementById('rate').value = "";
//                     document.getElementById('amount').value = "";
//                     document.getElementById('conformed_amt').value = "";
//                     document.getElementById('conform_rate').value = "";
//                     document.getElementById('qty').value = "";
//                     document.getElementById('values').value = "";
//                     document.getElementById('add').value = "ADD";
//                     document.getElementById('particulars').focus();
//                 }
//             });
//         }
//     } 
// 	// else {
//     //     jobcard_sub_validation();
// 	// 	  alert("Conform Rate (" + conform_rate.toFixed(3) + ") is less than Rate From Name (" + ratetrrrr.toFixed(3) + ")"); // Fail validation
//     // }

// 	else {
//     jobcard_sub_validation();

//     // Ensure values are numeric; if not, set to null
//     let conform = (conform_rate != null && !isNaN(conform_rate)) ? Number(conform_rate).toFixed(3) : 'null';
//     let rateFromName = (ratetrrrr != null && !isNaN(ratetrrrr)) ? Number(ratetrrrr).toFixed(3) : 'null';

//     alert("Conform Rate (" + conform + ") is less than Rate From Name (" + rateFromName + ")");
// }
// }

function jobcard_sub_list(
    jobcard_no, entry_time, created_by, cust_po_no, customer_name, jobtype, 
    fabric_type, hsn_code, particulars, width, height, rate, amount, 
    conformed_amt, conform_rate, qty, customer_rate, values, edit_id, quotation_no, action
) {

	// let ratetrrrr = 0;

if (customer_rate !== '') {
    ratetrrrr = parseFloat(customer_rate);
} else {
    // Get customer total confirm rate
    ratetrrrr = parseFloat(sm("#customer_total_confirm_rate").val() || 0);
}

// alert(ratetrrrr);

// If customer rate is 0, alert and stop
if (ratetrrrr === 0) {
    alert("Customer rate must be set during party creation");
    return; // Stop further execution
}

// Ensure conform_rate is a number
conform_rate = parseFloat(conform_rate);

    // Compare as numbers
    if (conform_rate >= ratetrrrr) {
        if (particulars !== '' && amount !== '' && conformed_amt !== '' && values !== '') {
            jQuery.ajax({
                type: "POST",
                url: "jobcard/jobcard_entry/jobcard_db.php",
                data: "jobcard_no=" + jobcard_no +
                      "&entry_time=" + entry_time +
                      "&created_by=" + created_by +
                      "&cust_po_no=" + cust_po_no +
                      "&customer_name=" + customer_name +  // use correct customer name
                      "&jobtype=" + jobtype +
                      "&fabric_type=" + fabric_type +
                      "&hsn_code=" + hsn_code +
                      "&particulars=" + particulars +
                      "&jobcard_width=" + width +
                      "&jobcard_height=" + height +
                      "&rate=" + rate +
                      "&amount=" + amount +
                      "&conformed_amt=" + conformed_amt +
                      "&conform_rate=" + conform_rate +
                      "&qty=" + qty +
                      "&values=" + values +
                      "&edit_id=" + edit_id +
                      "&quotation_no=" + quotation_no +
					  "&ratetrrrr=" + ratetrrrr +					  
                      "&action=" + action,
                success: function(msg) {
                    jQuery("#jobcard_sublist_div").html(msg);    
                    
                    // Reset form fields
                    document.getElementById('particulars').value = "";
                    document.getElementById('jobcard_width').value = "";
                    document.getElementById('jobcard_height').value = "";
                    document.getElementById('rate').value = "";
                    document.getElementById('amount').value = "";
                    document.getElementById('conformed_amt').value = "";
                    document.getElementById('conform_rate').value = "";
                    document.getElementById('qty').value = "";
                    document.getElementById('values').value = "";
					document.getElementById('ratetrrrr').value = "";
                    document.getElementById('add').value = "ADD";
                    document.getElementById('particulars').focus();
                }
            });
        }
    } else {
        jobcard_sub_validation();

        // Ensure values are numeric; if not, set to null
        let conform = (conform_rate != null && !isNaN(conform_rate)) ? Number(conform_rate).toFixed(3) : 'null';
        let rateFromName = (ratetrrrr != null && !isNaN(ratetrrrr)) ? Number(ratetrrrr).toFixed(3) : 'null';

        alert("Conform Rate (" + conform + ") is less than Rate From Name (" + rateFromName + ")");
    }
}

/***********************************delete************************************/
function edit_jobcard_sub(edit_id,jobcard_no,quotation_no,source_frm)
{
	//alert("edit_id="+edit_id+"&jobcard_no="+jobcard_no+"&quotation_no="+quotation_no);
	
		jQuery.ajax({
		type: "POST",
		url: "jobcard/jobcard_entry/jobcard_sublist.php",
		data: "edit_id="+edit_id+"&jobcard_no="+jobcard_no+"&quotation_no="+quotation_no+"&source_frm="+source_frm,
		success: function(msg){jQuery("#jobcard_sublist_div").html(msg);
	}
	});

}
/********************************************************************************************/
function get_jobcard_amt(jobcard_width,jobcard_height,rate)
{
if(jobcard_width=='')
{
var jobcard_width=0;
}
if(jobcard_height=='')
{
var jobcard_height=0;
}
if(rate=='')
{
var rate=0;
}
var amt=(jobcard_width)*(jobcard_height)*(rate);
document.getElementById('amount').value=amt.toFixed(2);
}

function get_jobcard_amt_tot(jobcard_width,jobcard_height,conformed_amt)
{
	// alert(jobcard_width);
	// alert(jobcard_height);
	// alert(confirm_amnt);
if(jobcard_width=='')
{
var jobcard_width=0;
}
if(jobcard_height=='')
{
var jobcard_height=0;
}
if(conformed_amt=='')
{
var conformed_amt=0;
}
var conformed_rate=(conformed_amt)/(jobcard_width*jobcard_height);

document.getElementById('conform_rate').value=conformed_rate.toFixed(3);
}



function get_total(overal_tot,tax)
{
	if(overal_tot=='')
{
var overal_tot=0;
}
	if(tax=='')
{
var tax=0;
}
var tax_amt=(overal_tot/100)*tax;
var tot_tax_amt2=tax_amt.toFixed(2);
document.getElementById('tax_amt').value=tot_tax_amt2;
var tot_tax_amt=parseFloat(overal_tot)+parseFloat(tax_amt);
var tot_tax_amt1=tot_tax_amt.toFixed(2);
document.getElementById('net_amt').value=tot_tax_amt1;
}
/******************************************************************************************/
function get_jobcard_tot(conformed_amt,qty)
{
if(conformed_amt=='')
{	
var	conformed_amt=0;
}
if(qty=='')
{	
var	qty=0;
}
var value_amt=parseFloat(conformed_amt)*parseFloat(qty);
document.getElementById('values').value=value_amt;

}
/**********************************************************************************************/
function get_fabric_type1(jobtype)
{
	if(jobtype=='TBilling')
	{
		jQuery.ajax({
			type: "POST",
			url: "jobcard/jobcard_entry/jobcard_entry_fabric_type_tbilling.php",
			success: function(data){
				jQuery("#fabric_type_main").html(data);
			}
		});
		
	}
	if(jobtype=='sample' || jobtype=='sticker' || jobtype=='T fusing work')
	{
		jQuery.ajax({
			type: "POST",
			url: "jobcard/jobcard_entry/jobcard_entry_fabric_type_empty.php",
			success: function(data){
				jQuery("#fabric_type_main").html(data);
			}
		});
		
	}

	if(jobtype=='production')
	{
		jQuery.ajax({
			type: "POST",
			url: "jobcard/jobcard_entry/jobcard_entry_fabric_type.php",
			success: function(data){
				jQuery("#fabric_type_main").html(data);
			}
		});

	}


	
}

function get_jobcard_type1(jobtype){
    if(jobtype === 'Re Production') {
        jQuery.ajax({
            type: "POST",
            url: "jobcard/jobcard_entry/jobcard_entry_jobcard_type.php",
            data: { action: 'get_old_jobcard', quotation_no: jQuery('#quotation_no').val(), edit_id: '<?php echo $edit_id; ?>' }, 
            success: function(data){
                jQuery("#old_jobcard_container").html(data);
                jQuery("#old_jobcard_main").show(); // show row
            }
        });
    } else {
        jQuery("#old_jobcard_container").html(''); // clear content
        jQuery("#old_jobcard_main").hide();       // hide row
    }
	
}


/***************************************************************************************/	
	function get_hsn_code(fabric_type,jobtype)
{

	
	if((fabric_type!='')&&(jobtype=='production'))
	{
		        document.getElementById('hsn_div').style.display='block'; 
				document.getElementById('hsn_div1').style.display='block';    
	}
	else
	{
		            
             document.getElementById('hsn_div').style.display='none'; 
			 document.getElementById('hsn_div1').style.display='none';
	}
	
	}
	
	function get_hsn_value(jobtype)
	{
		if(jobtype=='sticker')
		{
		   document.getElementById('hsn_code').value='998821'; 
		}
		else
		{
		   document.getElementById('hsn_code').value='998821'; 
		}
		
	}
	
/***********************************Delete Branch Sublist************************************/
function del_jobcard_sub(job_id,jobcard_no)
{
	if(confirm('Are You Sure! You Want To Delete?'))
	{
		jQuery.ajax({
		type: "POST",
		url: "jobcard/jobcard_entry/jobcard_db.php",
		data: "job_id="+job_id+"&jobcard_no="+jobcard_no+"&action=delete_job_sub",
		success: function(msg){jQuery("#jobcard_sublist_div").html(msg);
	}
	});
    }
}

function del_job_main(job_id,jobcard_no)
{
	if(confirm('Are You Sure! You Want To Delete?'))
	{
		jQuery.ajax({
		type: "POST",
		url: "jobcard/jobcard_entry/jobcard_db.php",
		data: "job_id="+job_id+"&jobcard_no="+jobcard_no+"&action=delete_job_main",
		success: function(msg){jQuery("#jobcard_main_list_div").html(msg);
	window.location.reload(true);
	}
	});
    }
}

function get_re_pro_jobcard(re_production_jobcard) {
    if(re_production_jobcard != '') {
        // Hide normal jobcard row
        jQuery("#jobcard_sublist_div").hide();

        // Show reproduction jobcard row
        jQuery("#reproduction_jobcard_row").show();

        // Load the reproduction sublist via AJAX
        jQuery.ajax({
            type: "POST",
            url: "jobcard/jobcard_entry/reproduction_jobcard_sublist.php",
            data: { re_production_jobcard: re_production_jobcard },
            success: function(msg) {
                jQuery("#jobcard_sublist_div_reproduction").html(msg);
            }
        });
    } else {
        // Show normal jobcard row if nothing is selected
        jQuery("#jobcard_sublist_div").show();
        jQuery("#reproduction_jobcard_row").hide();
        jQuery("#jobcard_sublist_div_reproduction").html('');
    }
}


