<?php include "../../root.class.php";
$db = new db_safeguard();


if (!empty($_POST['ps_signature'])) {
    $ps_data = $_POST['ps_signature'];
    $ps_data = str_replace('data:image/png;base64,', '', $ps_data);
    $ps_data = str_replace(' ', '+', $ps_data);
    $ps_decoded = base64_decode($ps_data);

    $ps_file = "signatures/supervisor_signature_" . date("Y_m_d_H_i_s") . ".png";
    file_put_contents($ps_file, $ps_decoded);
    $ps_data = ",`supervisor_signature` = '$ps_file'";
} else {
    $ps_data = "";
}

if (!empty($_POST['qc_signature'])) {
    $qc_data = $_POST['qc_signature'];
    $qc_data = str_replace('data:image/png;base64,', '', $qc_data);
    $qc_data = str_replace(' ', '+', $qc_data);
    $qc_decoded = base64_decode($qc_data);

    $qc_file = "signatures/quality_control_signature_" . date("Y_m_d_H_i_s") . ".png";
    file_put_contents($qc_file, $qc_decoded);
    $qc_data = ",`quality_controller_signature` = '$qc_file'";
} else {
    $qc_data = "";
}


$record_id = $_POST['record_id'];


$final_sorting_jobcard_id = $db->query("final_sorting_jobcard", "UPDATE `final_sorting_jobcard` SET `product_no` = '{$_POST['product_id']}', `batch_no` = '{$_POST['batch_no']}', `date_jobcard_opened` = '{$_POST['jobcard_date_opened']}', `quality_control_checked_by` = '{$_POST['production_checked_by']}', `production_checked_by` = '{$_POST['quality_control_checked_by']}', `production_supervisor` = '{$_POST['production_supervisor']}', `quality_controll` = '{$_POST['quality_control']}',`date_jobcard_closed` = '{$_POST['jobcard_closed']}' $ps_data $qc_data WHERE `record_id` = '{$_POST['record_id']}'");


$db->query("final_sorting_food_safety_inspections", "DELETE FROM `final_sorting_food_safety_inspections` WHERE `final_sorting_jobcard_no` = $record_id");
$final_sorting_food_safety_inspection_id = $db->query("final_sorting_food_safety_inspections", "INSERT INTO `final_sorting_food_safety_inspections`(`final_sorting_jobcard_no`, `equipment_cleaned`, `final_sorting_room_cleaned`, `pest_activity_noted`, `smells_taints_signs`, `air_dust_operational`, `visual_contamination_risks`, `cheese_crates_clean`, `trolley_traceability_available`,`personal_hygiene`,`tools_sanitized`) VALUES ($record_id,'{$_POST['equipment_cleaned']}','{$_POST['final_sorting_room_cleaned']}','{$_POST['pest_activity_noted']}','{$_POST['smells_taints_signs']}','{$_POST['air_dust_operational']}','{$_POST['visual_contamination_risks']}','{$_POST['cheese_crates_clean']}','{$_POST['trolley_traceability_available']}','{$_POST['personal_hygiene']}','{$_POST['tools_sanitized']}')");

$db->query("final_sorting_food_comments", "DELETE FROM `final_sorting_food_comments` WHERE `final_sorting_jobcard_no` = $record_id");
$db->query("final_sorting_food_comments", "INSERT INTO `final_sorting_food_comments`(`final_sorting_jobcard_no`, `equipment_cleaned_comment`, `final_sorting_room_comment`, `pest_activity_comment`, `smells_taints_signs_comment`, `air_dust_operational_comment`, `visual_contamination_risks_comment`, `cheese_crates_clean_comment`, `trolley_traceability_available_comment`,`personal_hygiene_comment`,`tools_sanitized_comment`) VALUES ($record_id,'{$_POST['equipment_cleaned_comment']}','{$_POST['final_sorting_room_comment']}','{$_POST['pest_activity_comment']}','{$_POST['smells_taints_signs_comment']}','{$_POST['air_dust_operational_comment']}','{$_POST['visual_contamination_risks_comment']}','{$_POST['cheese_crates_clean_comment']}','{$_POST['trolley_traceability_available_comment']}','{$_POST['personal_hygiene_comment']}','{$_POST['tools_sanitized_comment']}')");


$db->query("final_sorting_batch_summary", "DELETE FROM `final_sorting_batch_summary` WHERE `final_sorting_jobcard_no` = $record_id");
$db->query("final_sorting_batch_summary", "INSERT INTO `final_sorting_batch_summary`(`final_sorting_jobcard_no`, `kernal_size_accuracy`, `broken_kernal`, `shell_fragment_contamination`, `visual_defect_rate`, `colour_final_sorting_accuracy`, `whole_kernal`, `halves`, `chips_broken`, `reject`, `kernal_breakage_rate`, `moisture`, `whole_kernal_recovery`, `shell_kernal_contamination`, `kernal_stuck_shell`, `actions_taken`) VALUES ('$record_id','{$_POST['kernal_size_accuracy']}','{$_POST['broken_kernal']}','{$_POST['shell_fragment_contamination']}','{$_POST['visual_defect_rate']}','{$_POST['colour_final_sorting_accuracy']}','{$_POST['whole_kernal']}','{$_POST['halves']}','{$_POST['chips_broken']}','{$_POST['reject']}','{$_POST['kernal_breakage_rate']}','{$_POST['moisture']}','{$_POST['whole_kernal_recovery']}','{$_POST['shell_kernal_contamination']}','{$_POST['kernal_stuck_shell']}','{$_POST['actions_taken']}')");

// echo "<br><br>";
if (isset($_POST['save_halfway'])) {
    echo "<script>window.location.href = '../final_sorting/final_sorting.php?record_id={$_POST['record_id']}';</script>";
} else {
    echo "<script>window.location.href = '../final_sorting/final_sorting_home.php';</script>";
}
