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

var_dump($_POST);

// echo "<br><br>";

// var_dump($_FILES);

// echo "<br><br>";


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


$cracking_jobcard_id = $db->query("cracking_jobcard", "UPDATE `cracking_jobcard` SET `product_id` = '{$_POST['product_id']}', `jobcard_date_opened` = '{$_POST['jobcard_date_opened']}', `quality_control_checked_by` = '{$_POST['quality_control_checked_by']}',`production_checked_by` = '{$_POST['production_checked_by']}', `batch_no` = '{$_POST['batch_no']}', `jobcard_closed` = '{$_POST['jobcard_closed']}', `production_supervisor` = '{$_POST['production_supervisor']}', `quality_control` = '{$_POST['quality_control_name']}',`jobcard_closed` = '{$_POST['jobcard_closed']}'  $ps_data $qc_data WHERE `record_id` = '{$_POST['record_id']}'");

$db->query("cracking_food_safety_inspections", "DELETE FROM `cracking_food_safety_inspections` WHERE `cracking_jobcard_id` = '{$_POST['record_id']}'");
$db->query("cracking_food_safety_comments", "INSERT INTO `cracking_food_safety_inspections`(`cracking_jobcard_id`, `equipment_cleaned`, `magnet_1_name`, `magnet_1_cleaned`, `magnet_2_name`, `magnet_2_cleaned`, `oxyacid_concentration_verification`, `cracking_equipment_checked`, `oil_rust_signs`, `shell_sieves_clean`, `filter_dust_control_working`, `personal_hygiene`, `tools_sanitized_labelled`) VALUES ('{$_POST['record_id']}','{$_POST['equipment_cleaned']}','{$_POST['magnet_1_name']}','{$_POST['magnet_1_cleaned']}','{$_POST['magnet_2_name']}','{$_POST['magnet_2_cleaned']}','{$_POST['oxyacid_concentration_verification']}','{$_POST['cracking_equipment_checked']}','{$_POST['oil_rust_signs']}','{$_POST['shell_sieves_clean']}','{$_POST['filter_dust_control_working']}','{$_POST['personal_hygiene']}','{$_POST['tools_sanitized_labelled']}')");

$db->query("cracking_food_safety_comments", "DELETE FROM `cracking_food_safety_comments` WHERE `food_safety_id` = '{$_POST['record_id']}'");
$db->query("cracking_food_safety_comments", "INSERT INTO `cracking_food_safety_comments`(`food_safety_id`, `equipment_cleaned_comment`, `magnet_1_comment`, `magnet_2_comment`, `creaking_equipment_comment`, `oil_rust_signs_comment`, `shell_sieves_clean_comment`, `filter_dust_comment`, `personal_hygiene_comment`, `tools_sanitized_comment`,`oxyacid_concentration_verification_comment`) VALUES ('{$_POST['record_id']}','{$_POST['equipment_cleaned_comment']}','{$_POST['magnet_1_comment']}','{$_POST['magnet_2_comment']}','{$_POST['creaking_equipment_comment']}','{$_POST['oil_rust_signs_comment']}','{$_POST['shell_sieves_clean_comment']}','{$_POST['filter_dust_comment']}','{$_POST['personal_hygiene_comment']}','{$_POST['tools_sanitized_comment']}','{$_POST['oxyacid_concentration_verification_comment']}')");


$db->query("cracking_batch_summary", "DELETE FROM cracking_batch_summary WHERE cracking_jobcard_id = '{$_POST['record_id']}'");
$db->query("cracking_batch_summary", "INSERT INTO `cracking_batch_summary`(`cracking_jobcard_id`, `whole_kernal`, `halves`, `dust_summary`, `shell_summary`, `chips_broken`, `shell_in_kernal_summary`, `uncracked_nuts_summary`, `crushed_missing_kernal_summary`, `waste`, `kernal_breakage_rate`, `moisture_in_shell`, `whole_kernal_recovery`, `shell_in_kernal_contamination`, `kernal_stuck_in_shell`, `actions_taken`) VALUES ('{$_POST['record_id']}','{$_POST['whole_kernal']}','{$_POST['halves']}','{$_POST['dust_summary']}','{$_POST['shell_summary']}','{$_POST['chips_broken']}','{$_POST['shell_in_kernal_summary']}','{$_POST['uncracked_nuts_summary']}','{$_POST['crushed_missing_kernal_summary']}','{$_POST['waste']}','{$_POST['kernal_breakage_rate']}','{$_POST['moisture_in_shell']}','{$_POST['whole_kernal_recovery']}','{$_POST['shell_in_kernal_contamination']}','{$_POST['kernal_stuck_in_shell']}','{$_POST['actions_taken']}')");





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