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

// var_dump($_POST);

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

// echo "INSERT INTO `final_sorting_jobcard`(`date_jobcard_opened`, `product_id`, `checked_by_quality_control`, `checked_by_production`, `batch_no` ,`production_supervisor`, `quality_controll`) VALUES ('".$_POST['jobcard_date_opened']."','".$_POST['product_id']."','".$_POST['quality_control_checked_by']."','".$_POST['production_checked_by']."','".$_POST['batch_no']."','".$_POST['product_supervisor']."','".$_POST['quality_control']."')";

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

$final_sorting_jobcard = $db->query("final_sorting_jobcard","INSERT INTO `final_sorting_jobcard`(`date_jobcard_opened`, `product_id`, `checked_by_quality_control`, `checked_by_production`, `batch_no` ,`production_supervisor`, `quality_controll`) VALUES ('".$_POST['jobcard_date_opened']."','".$_POST['product_id']."','".$_POST['quality_control_checked_by']."','".$_POST['production_checked_by']."','".$_POST['batch_no']."','".$_POST['product_supervisor']."','".$_POST['quality_control']."')");

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

$db->query("equipment","INSERT INTO `equipment`(`type`, `jobcard_no`, `name`, `verified_settings`, `accurate`) VALUES ('FINAL_SORTING',$final_sorting_jobcard,'Conveyor Belt','{$_POST['belt_settings']}','{$_POST['belt_accurate']}')");

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

// echo "INSERT INTO `equipment`(`type`, `jobcard_no`, `name`, `verified_settings`, `accurate`) VALUES ('FINAL_SORTING',$final_sorting_jobcard,'Conveyor Belt','{$_POST['belt_settings']}','{$_POST['belt_accurate']}')";

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

// echo "INSERT INTO `equipment`(`type`, `jobcard_no`, `name`, `verified_settings`, `accurate`) VALUES ('FINAL_SORTING',$final_sorting_jobcard,'Conveyor Motors','{$_POST['motors_settings']}','{$_POST['motors_accurate']}')";

$db->query("equipment","INSERT INTO `equipment`(`type`, `jobcard_no`, `name`, `verified_settings`, `accurate`) VALUES ('FINAL_SORTING',$final_sorting_jobcard,'Conveyor Motors','{$_POST['motors_settings']}','{$_POST['motors_accurate']}')");

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

$counter = $_POST['equipment_counter'];
// echo "counter: " . $counter;
// echo "<br><br>";

for ($i = 0; $i < $counter; $i++) {
    $equipment = $_POST['equipment_' . $i];
    // echo "equipment: " . $equipment;
    // echo "<br><br>";

    if (strlen($equipment) > 0) {
        $verify_settings = $_POST['verify_settings_' . $i];
        $accurate = $_POST['accurate_' . $i];

        // echo "<br>Index: " . $i;
        // echo "<br>Equipment: " . $equipment;
        // echo "<br>Verify Settings: " . $verify_settings;
        // echo "<br>Accurate: " . $accurate;
        // echo "<br><br>";

        // echo "INSERT INTO `equipment`(`type`, `jobcard_no`, `name`, `verified_settings`, `accurate`) VALUES ('FINAL_SORTING',$final_sorting_jobcard,'$equipment','$verify_settings','$accurate')";

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

        $db->query("equipment", "INSERT INTO `equipment`(`type`, `jobcard_no`, `name`, `verified_settings`, `accurate`) VALUES ('FINAL_SORTING',$final_sorting_jobcard,'$equipment','$verify_settings','$accurate')");
    }
}

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

// echo "INSERT INTO `final_food_safety`(`jobcard_no`, `equipment_conveyors`, `conveyor_belt_integrity`, `visual_inspections`, `waste_containers`, `ppe_condition`, `air_dust_system`, `foreign_materials`, `crates_integrity`, `personal_hygiene`, `tools_sanitized`) VALUES ($final_sorting_jobcard,'{$_POST['equipment_conveyors']}','{$_POST['conveyor_belt_integrity']}','{$_POST['visual_inspections']}','{$_POST['waste_containers_available']}','{$_POST['ppe_condition']}','{$_POST['air_dust_system']}','{$_POST['foreign_materials']}','{$_POST['crates_integrity']}','{$_POST['personal_hygiene']}','{$_POST['tools_sanitized']}')";

$final_food_safety = $db->query("final_food_safety","INSERT INTO `final_food_safety`(`jobcard_no`, `equipment_conveyors`, `conveyor_belt_integrity`, `visual_inspections`, `waste_containers`, `ppe_condition`, `air_dust_system`, `foreign_materials`, `crates_integrity`, `personal_hygiene`, `tools_sanitized`) VALUES ($final_sorting_jobcard,'{$_POST['equipment_conveyors']}','{$_POST['conveyor_belt_integrity']}','{$_POST['visual_inspections']}','{$_POST['waste_containers_available']}','{$_POST['ppe_condition']}','{$_POST['air_dust_system']}','{$_POST['foreign_materials']}','{$_POST['crate_integrity_clean']}','{$_POST['personal_hygiene_checked']}','{$_POST['tools_sanitized_labelled']}')");

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

// echo "INSERT INTO `final_food_safety_comments`(`food_safety_no`, `equipment_conveyors_comment`, `conveyor_belt_integrity_comment`, `visual_inspections_comment`, `waste_containers_available_comment`, `ppe_condition_comment`, `air_dust_system_comment`, `foreign_materials_comments`, `crate_integrity_comment`, `personal_hygiene_comment`, `tools_sanitized_comment`) VALUES ($final_food_safety,'{$_POST['equipment_conveyors_comment']}','{$_POST['conveyor_belt_integrity_comment']}','{$_POST['visual_inspections_comment']}','{$_POST['waste_containers_available_comment']}','{$_POST['ppe_condition_comment']}','{$_POST['air_dust_system_comment']}','{$_POST['contamination_present_comment']}','{$_POST['crate_integrity_clean_comment']}','{$_POST['personal_hygiene_checked_comment']}','{$_POST['tools_sanitized_labelled_comment']}')";

$db->query("final_food_safety_comments","INSERT INTO `final_food_safety_comments`(`food_safety_no`, `equipment_conveyors_comment`, `conveyor_belt_integrity_comment`, `visual_inspections_comment`, `waste_containers_available_comment`, `ppe_condition_comment`, `air_dust_system_comment`, `foreign_materials_comments`, `crate_integrity_comment`, `personal_hygiene_comment`, `tools_sanitized_comment`) VALUES ($final_food_safety,'{$_POST['equipment_conveyors_comment']}','{$_POST['conveyor_belt_integrity_comment']}','{$_POST['visual_inspections_comment']}','{$_POST['waste_containers_available_comment']}','{$_POST['ppe_condition_comment']}','{$_POST['air_dust_system_comment']}','{$_POST['contamination_present_comment']}','{$_POST['crate_integrity_clean_comment']}','{$_POST['personal_hygiene_checked_comment']}','{$_POST['tools_sanitized_labelled_comment']}')");

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

// echo "INSERT INTO `final_quality_process_control`(`date_time`, `shift`, `sample_size`,`product_sample_point`, `mouldy_kernal`, `germinated_kernal`, `insect_damaged`, `rancid_smell_kernal`, `foreign_matter`, `shell_in_kernal`, `uncracked_nuts`, `crushed_missing_kernal`, `miosture`, `env_temperature`, `env_humidity`, `magnet_inspection`, `person_inspecting`) VALUES ('{$_POST['date_time']}','{$_POST['shift']}',{$_POST['sample_size']},'{$_POST['product_sample_point']}',{$_POST['mouldy_kernal']},{$_POST['germinated_kernal']},{$_POST['insect_damaged']},{$_POST['rancid_smell_kernal']},{$_POST['foreign_matter']},{$_POST['shell_in_kernal']},{$_POST['uncracked_nuts']},{$_POST['crushed_missing_kernal']},{$_POST['moisture']},{$_POST['env_temperature']},{$_POST['env_humidity']},'{$_POST['magnet_inspection']}','{$_POST['person_inspection']}')";

$final_quality_process = $db->query("final_quality_process_control","INSERT INTO `final_quality_process_control`(`date_time`, `shift`, `sample_size`,`product_sample_point`, `mouldy_kernal`, `germinated_kernal`, `insect_damaged`, `rancid_smell_kernal`, `foreign_matter`, `shell_in_kernal`, `uncracked_nuts`, `crushed_missing_kernal`, `miosture`, `env_temperature`, `env_humidity`, `magnet_inspection`, `person_inspecting`) VALUES ('{$_POST['date_time']}','{$_POST['shift']}',{$_POST['sample_size']},'{$_POST['product_samples_point']}',{$_POST['mouldy_kernal']},{$_POST['germinated_kernal']},{$_POST['insect_damaged']},{$_POST['rancid_smell_kernal']},{$_POST['foreign_matter']},{$_POST['shell_in_kernal']},{$_POST['uncracked_nuts']},{$_POST['crushed_missing_kernal']},{$_POST['moisture']},{$_POST['env_temperature']},{$_POST['env_humidity']},'{$_POST['magnet_inspection']}','{$_POST['person_inspection']}')");

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

$dir = "../sorting/final_signature/"; // your folder path

// Get all files in the folder
$files = glob($dir . "*");

// Filter only actual files
$files = array_filter($files, 'is_file');

// If no files exist
if (empty($files)) {
    // echo "No files found.";
    exit;
}

// Sort files by last modified time (newest first)
usort($files, function ($a, $b) {
    return filemtime($b) - filemtime($a);
});

// Get the last saved file (most recent)
$lastFile = $files[0];

// echo "Last saved file: " . basename($lastFile);

$signature = basename($lastFile);

$db->query("final_quality_process_control", "UPDATE `final_quality_process_control` SET `signature` = '$signature' WHERE `record_id` = $final_quality_process");

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

// echo "UPDATE `final_quality_process_control` SET `signature` = '$signature' WHERE `record_id` = $final_quality_process";

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

// echo "INSERT INTO `final_batch_summary`(`final_jobcard_no`, `kernal_size`, `Reject_waste`, `broken_kernal`, `kernal_breakage_rate`, `shell_fragment`, `shell_in_kernal`, `moisture_in_shell`, `visual_defect_rate`, `whole_kernal_recovery`, `color_sorting_accuracy`, `shell_in_kernal_contamination`, `whole_kernal`, `kernal_stuck_in_shell`, `halves`, `chips_broken`, `actions_taken`) VALUES ('$final_sorting_jobcard','{$_POST['kernal_size']}','{$_POST['Reject_waste']}','{$_POST['broken_kernal']}','{$_POST['kernal_breakage_rate']}','{$_POST['shell_fragment']}','{$_POST['shell_in_kernal']}','{$_POST['moisture_in_shell']}','{$_POST['visual_defect_rate']}','{$_POST['whole_kernal_recovery']}','{$_POST['color_sorting_accuracy']}','{$_POST['shell_in_kernal_contamination']}','{$_POST['whole_kernal']}','{$_POST['kernal_stuck_in_shell']}','{$_POST['halves']}','{$_POST['chips_broken']}','{$_POST['actions_taken']}')";

$db->query("final_batch_summary","INSERT INTO `final_batch_summary`(`final_jobcard_no`, `kernal_size`, `Reject_waste`, `broken_kernal`, `kernal_breakage_rate`, `shell_fragment`, `shell_in_kernal`, `moisture_in_shell`, `visual_defect_rate`, `whole_kernal_recovery`, `color_sorting_accuracy`, `shell_in_kernal_contamination`, `whole_kernal`, `kernal_stuck_in_shell`, `halves`, `chips_broken`, `actions_taken`) VALUES ('$final_sorting_jobcard','{$_POST['kernal_size']}','{$_POST['Reject_waste']}','{$_POST['broken_kernal']}','{$_POST['kernal_breakage_rate']}','{$_POST['shell_fragment']}','{$_POST['shell_in_kernal']}','{$_POST['moisture_in_shell']}','{$_POST['visual_defect_rate']}','{$_POST['whole_kernal_recovery']}','{$_POST['color_sorting_accuracy']}','{$_POST['shell_in_kernal_contamination']}','{$_POST['whole_kernal']}','{$_POST['kernal_stuck_in_shell']}','{$_POST['halves']}','{$_POST['chips_broken']}','{$_POST['actions_taken']}')");

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

echo"<script>window.location.href = '../sorting/final_sorting.php';</script>";
