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



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'];
$db->query("lop_jobcard", "UPDATE `lop_jobcard` SET `product_no` = '{$_POST['product_id']}', `batch_no` = '{$_POST['batch_no']}', `date_jobcard_opened` = '{$_POST['jobcard_date_opened']}',`jobcard_date_closed` = '{$_POST['jobcard_closed']}', `checked_by_quality_control` = '{$_POST['production_checked_by']}', `checked_by_production` = '{$_POST['quality_control_checked_by']}', `room_no` = '{$_POST['room_number']}', `mass_on_trolley` = {$_POST['trolley_mass']}, `number_of_trolleys` = {$_POST['trolley_no']}, `product_supervisor` = '{$_POST['product_supervisor']}', `quality_control` = '{$_POST['quality_control']}' $ps_data $qc_data  WHERE `record_id` = $record_id");

$db->query("lop_food_safety_inspections", "DELETE FROM `lop_food_safety_inspections` WHERE `lop_jobcard_no` = $record_id");
$lop_food_safety_inspection_id = $db->query("lop_food_safety_inspections", "INSERT INTO `lop_food_safety_inspections`(`lop_jobcard_no`, `equipment_cleaned`, `lop_room_cleaned`, `pest_activity_noted`, `smells_taints_signs`, `air_dust_operational`, `visual_contamination_risks`, `cheese_crates_clean`, `trolley_traceability_available`) VALUES ($record_id,'{$_POST['equipment_cleaned']}','{$_POST['lop_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']}')");

$db->query("lop_food_comments", "DELETE FROM `lop_food_comments` WHERE `lop_jobcard_no` = $record_id");
$db->query("lop_food_comments", "INSERT INTO `lop_food_comments`(`lop_jobcard_no`, `equipment_cleaned_comment`, `lop_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`) VALUES ($record_id,'{$_POST['equipment_cleaned_comment']}','{$_POST['lop_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']}')");


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