<?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'];


$sorting_jobcard_id = $db->query("sorting_jobcard", "UPDATE `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("sorting_food_safety_inspections", "DELETE FROM `sorting_food_safety_inspections` WHERE `sorting_jobcard_no` = $record_id");
$sorting_food_safety_inspection_id = $db->query("sorting_food_safety_inspections", "INSERT INTO `sorting_food_safety_inspections`(`sorting_jobcard_no`, `equipment_cleaned`, `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['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("sorting_food_comments", "DELETE FROM `sorting_food_comments` WHERE `sorting_jobcard_no` = $record_id");
$db->query("sorting_food_comments", "INSERT INTO `sorting_food_comments`(`sorting_jobcard_no`, `equipment_cleaned_comment`, `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['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("sorting_batch_summary", "DELETE FROM `sorting_batch_summary` WHERE `sorting_jobcard_no` = $record_id");
$db->query("sorting_batch_summary", "INSERT INTO `sorting_batch_summary`(`sorting_jobcard_no`, `kernal_size_accuracy`, `broken_kernal`, `shell_fragment_contamination`, `visual_defect_rate`, `colour_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_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 = '../sorting/sorting.php?record_id={$_POST['record_id']}';</script>";
} else {
    echo "<script>window.location.href = '../sorting/sorting_home.php';</script>";
}
