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

// initail quality inspection

$insect_infestation_coa = new input();
$insect_infestation_coa->name('insect_infestation_coa');
$insect_infestation_coa->id('insect_infestation_coa');
// $insect_infestation_coa->placeholder("COA");
$insect_infestation_coa->style("margin:1vw auto;");

$insect_infestation_evaluation = new input();
$insect_infestation_evaluation->name('insect_infestation_evaluation');
$insect_infestation_evaluation->id('insect_infestation_evaluation');
// $insect_infestation_evaluation->placeholder("Evaluation");
$insect_infestation_evaluation->style("margin:1vw auto;");

$insect_infestation_coa_comment = new input();
$insect_infestation_coa_comment->name('insect_infestation_coa_comment');
$insect_infestation_coa_comment->id('insect_infestation_coa_comment');
// $insect_infestation_coa_comment->placeholder("COMMENT");
$insect_infestation_coa_comment->style("width: 95%;margin:1vw auto;");

$mould_rot_coa = new input();
$mould_rot_coa->name('mould_rot_coa');
$mould_rot_coa->id('mould_rot_coa');
// $mould_rot_coa->placeholder(" COA");
$mould_rot_coa->style("width: 95%;margin:1vw auto;");

$mould_rot_evaluation = new input();
$mould_rot_evaluation->name('mould_rot_evaluation');
$mould_rot_evaluation->id('mould_rot_evaluation');
// $mould_rot_evaluation->placeholder("Evaluation");
$mould_rot_evaluation->style("width: 95%;margin:1vw auto;");

$mould_rot_comment = new input();
$mould_rot_comment->name('mould_rot_comment');
$mould_rot_comment->id('mould_rot_comment');
// $mould_rot_comment->placeholder("COMMENT");
$mould_rot_comment->style("width: 95%;margin:1vw auto;");

$foreign_matter_coa = new input();
$foreign_matter_coa->name('foreign_matter_coa');
$foreign_matter_coa->id('foreign_matter_coa');
// $foreign_matter_coa->placeholder(" COA");
$foreign_matter_coa->style("width: 95%;margin:1vw auto;");

$foreign_matter_evaluation = new input();
$foreign_matter_evaluation->name('foreign_matter_evaluation');
$foreign_matter_evaluation->id('foreign_matter_evaluation');
// $foreign_matter_evaluation->placeholder("Evaluation");
$foreign_matter_evaluation->style("width: 95%;margin:1vw auto;");

$foreign_matter_comment = new input();
$foreign_matter_comment->name('foreign_matter_comment');
$foreign_matter_comment->id('foreign_matter_comment');
// $foreign_matter_comment->placeholder("COMMENT");
$foreign_matter_comment->style("width: 95%;margin:1vw auto;");

$moisture_coa = new input();
$moisture_coa->name('moisture_coa');
$moisture_coa->id('moisture_coa');
// $moisture_coa->placeholder(" COA");
$moisture_coa->style("width: 95%;margin:1vw auto;");

$moisture_evaluation = new input();
$moisture_evaluation->name('moisture_evaluation');
$moisture_evaluation->id('moisture_evaluation');
// $moisture_evaluation->placeholder("Evaluation");
$moisture_evaluation->style("width: 95%;margin:1vw auto;");

$moisture_comment = new input();
$moisture_comment->name('moisture_comment');
$moisture_comment->id('moisture_comment');
// $moisture_comment->placeholder("COMMENT");
$moisture_comment->style("width: 95%;margin:1vw auto;");

$texture_specs = new input();
$texture_specs->name('texture_specifications');
$texture_specs->id('texture_specifications');
// $texture_specs->placeholder("Specifications");
$texture_specs->style("width: 100%;margin:1vw auto;");

$texture_coa = new input();
$texture_coa->name('texture_coa');
$texture_coa->id('texture_coa');
// $texture_coa->placeholder(" COA");
$texture_coa->style("width: 95%;margin:1vw auto;");

$texture_evaluation = new input();
$texture_evaluation->name('texture_evaluation');
$texture_evaluation->id('texture_evaluation');
// $texture_evaluation->placeholder("Evaluation");
$texture_evaluation->style("width: 95%;margin:1vw auto;");

$texture_comment = new input();
$texture_comment->name('_comment');
$texture_comment->id('_comment');
// $texture_comment->placeholder("COMMENT");
$texture_comment->style("width: 95%;margin:1vw auto;");

$physical_contamination_coa = new input();
$physical_contamination_coa->name('physical_contamination_coa');
$physical_contamination_coa->id('physical_contamination_coa');
// $physical_contamination_coa->placeholder(" COA");
$physical_contamination_coa->style("width: 95%;margin:1vw auto;");

$physical_contamination_evaluation = new input();
$physical_contamination_evaluation->name('physical_contamination_evaluation');
$physical_contamination_evaluation->id('physical_contamination_evaluation');
// $physical_contamination_evaluation->placeholder("Evaluation");
$physical_contamination_evaluation->style("width: 95%;margin:1vw auto;");

$physical_contamination_comment = new input();
$physical_contamination_comment->name('physical_contamination_comment');
$physical_contamination_comment->id('physical_contamination_comment');
// $physical_contamination_comment->placeholder("COMMENT");
$physical_contamination_comment->style("width: 95%;margin:1vw auto;");

$infestation_present = new select();
$infestation_present->name("infestation_present");
$infestation_present->id("infestation_present");
$infestation_present->class("inputs");
$infestation_present->add_option("", "Infestation Present");
$infestation_present->add_option("Yes", "Yes");
$infestation_present->add_option("No", "No");
$infestation_present->style("width: 95%;margin:1vw auto;");

$infestation_comment = new input();
$infestation_comment->name('infestation_comment');
$infestation_comment->id('infestation_comment');
// $infestation_comment->placeholder("COMMENT");
$infestation_comment->style("width: 95%;margin:1vw auto;");

$foreign_matter_present = new select();
$foreign_matter_present->name("foreign_matter_present");
$foreign_matter_present->id("foreign_matter_present");
$foreign_matter_present->class("inputs");
$foreign_matter_present->add_option("", "Foreign Matter Present");
$foreign_matter_present->add_option("Yes", "Yes");
$foreign_matter_present->add_option("No", "No");
$foreign_matter_present->style("width: 95%;margin:1vw auto;");

$matter_comment = new input();
$matter_comment->name('matter_comment');
$matter_comment->id('matter_comment');
// $matter_comment->placeholder("COMMENT");
$matter_comment->style("width: 95%;margin:1vw auto;");

$inspector_qc_name = new input();
$inspector_qc_name->name('inspector_qc_name');
$inspector_qc_name->id('inspector_qc_name');
$inspector_qc_name->placeholder("Intake Inspector/ QC Name & Surname");
$inspector_qc_name->style("width: 95%;margin:1vw auto;");

$driver_name = new input();
$driver_name->name('driver_name');
$driver_name->id('driver_name');
$driver_name->placeholder("Delivery Vehicle Driver Name");
$driver_name->style("width: 95%;margin:1vw auto;");

$submit_btn = new button();
$submit_btn->class("submit_btn");
$submit_btn->value("Submit");
$submit_btn->onclick("quality_inspection()");
$submit_btn->id("submit_btn");

$save_signature_btn = new button();
$save_signature_btn->class("submit_btn");
$save_signature_btn->value("Save Signature");
$save_signature_btn->id("save_signature");

?>

<div class="form_down">

    <h1>INITIAL QUALITY INSPECTION</h1><br>
    <style>
        table,
        th,
        td {
            border: 2px solid black;
            border-collapse: collapse;
            padding: 5px;
        }
    </style>
    <table id="quality_inspection_table" style="width: 90%;">
        <!-- <tr>
            <th colspan="5" style="background-color: #004aad80;">
                <h3>INITIAL QUALITY INSPECTION BEFORE OFF-LOAD</h3>
            </th>
        </tr> -->
        <tr style="background-color: #004aad80;">
            <td></td>
            <td>Specification</td>
            <td>COA</td>
            <td>Intake Evaluation</td>
            <td>Comment</td>
        </tr>
        <tr>
            <td>Visible Insect Infestation</td>
            <td>Non live insects; minimal webbing</td>
            <td>
                <?php
                $insect_infestation_coa->add();
                ?>
            </td>
            <td>
                <?php
                $insect_infestation_evaluation->add();
                ?>
            </td>
            <td>
                <?php
                $insect_infestation_coa_comment->add();
                ?>
            </td>
        </tr>
        <tr>
            <td>Mould/ Rot</td>
            <td>≤0.5% by volume</td>
            <td>
                <?php
                $mould_rot_coa->add();
                ?>
            </td>
            <td>
                <?php
                $mould_rot_evaluation->add();
                ?>
            </td>
            <td>
                <?php
                $mould_rot_comment->add();
                ?>
            </td>
        </tr>
        <tr>
            <td>Foreign Matter (leaves, sticks, stones)</td>
            <td>≤0.5% by volume</td>
            <td>
                <?php
                $foreign_matter_coa->add();
                ?>
            </td>
            <td>
                <?php
                $foreign_matter_evaluation->add();
                ?>
            </td>
            <td>
                <?php
                $foreign_matter_comment->add();
                ?>
            </td>
        </tr>
        <tr>
            <td>Moisture (shell feel)</td>
            <td>Dry to touch</td>
            <td>
                <?php
                $moisture_coa->add();
                ?>
            </td>
            <td>
                <?php
                $moisture_evaluation->add();
                ?>
            </td>
            <td>
                <?php
                $moisture_comment->add();
                ?>
            </td>
        </tr>
        <!-- <tr>
            <td>Texture</td>
            <td>
                <?php
                $texture_specs->add();
                ?>
            </td>
            <td>
                <?php
                $texture_coa->add();
                ?>
            </td>
            <td>
                <?php
                $texture_evaluation->add();
                ?>
            </td>
            <td>
                <?php
                $texture_comment->add();
                ?>
            </td>
        </tr> -->
        <tr>
            <td>Physical Contamination </td>
            <td>Non-Visible</td>
            <td>
                <?php
                $physical_contamination_coa->add();
                ?>
            </td>
            <td>
                <?php
                $physical_contamination_evaluation->add();
                ?>
            </td>
            <td>
                <?php
                $physical_contamination_comment->add();
                ?>
            </td>
        </tr>
        <tr>
            <td>
                Infestation
            </td>
            <td colspan="2">
                <?php
                $infestation_present->add();
                ?>
            </td>
            <td colspan="2">
                <?php
                $infestation_comment->add();
                ?>
            </td>
        </tr>
        <!-- <tr>
            <td>
                Foreign matter
            </td>
            <td colspan="2">
                <?php
                $foreign_matter_present->add();
                ?>
            </td>
            <td colspan="2">
                <?php
                $matter_comment->add();
                ?>
            </td>
        </tr> -->
        <tr>
            <td>
                Intake inspector/ QC Name & Signature
            </td>
            <td colspan="2">
                <?php
                $inspector_qc_name->add();
                ?>
            </td>
            <td colspan="2">
                <canvas id="inspector_qc_signature" name="inspector_qc_signature" class="signature-pad" width=400
                    height=100 style='background-color:lightgrey;border-radius:20px;'></canvas>
            </td>
        </tr>
        <tr>
            <td>
                Delivery Vehicle Driver Name & Signature
            </td>
            <td colspan="2">
                <?php
                $driver_name->add();
                ?>
            </td>
            <td colspan="2">
                <canvas id="driver_signature" name="driver_signature" class="signature-pad" width=400 height=100
                    style='background-color:lightgrey;border-radius:20px;'></canvas>
            </td>
        </tr>
    </table><br>
    <?php
    $save_signature_btn->add();
    $submit_btn->add();
    ?>
</div>

<script src="https://cdn.jsdelivr.net/npm/signature_pad@2.3.2/dist/signature_pad.min.js"></script>

<script>
    var inspector_qc_canvas = document.getElementById('inspector_qc_signature');
    var inspector_qc_pad = new SignaturePad(inspector_qc_canvas);

    var driver_canvas = document.getElementById('driver_signature');
    var driver_pad = new SignaturePad(driver_canvas);

    document.getElementById('save_signature').addEventListener('click', function () {
        if (inspector_qc_pad.isEmpty()) {
            alert("Please provide signature");
        } else {
            var dataURL = inspector_qc_pad.toDataURL('image/png');
            var blob = dataURLToBlob(dataURL);
            var formData = new FormData();
            formData.append('file', blob, 'signature.png');
            formData.append('type', 'inspector_qc');

            var xhr = new XMLHttpRequest();
            xhr.open("POST", "../receive/upload_signature.php", true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    // alert(xhr.responseText); // Handle the server response
                    if (xhr.responseText.indexOf("OK") > 1) {
                        // repload page
                        // location.reload();
                        console.log("Signature saved successfully.");

                    } else {
                        alert(xhr.responseText);
                    }
                }
            };
            xhr.send(formData);
        }
    });

    document.getElementById('save_signature').addEventListener('click', function () {
        if (driver_pad.isEmpty()) {
            alert("Please provide signature");
        } else {
            var dataURL = driver_pad.toDataURL('image/png');
            var blob = dataURLToBlob(dataURL);
            var formData = new FormData();
            formData.append('file', blob, 'signature.png');
            formData.append('type', 'driver');

            var xhr = new XMLHttpRequest();
            xhr.open("POST", "../receive/upload_signature.php", true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    // alert(xhr.responseText); // Handle the server response
                    if (xhr.responseText.indexOf("OK") > 1) {
                        // repload page
                        // location.reload();
                        console.log("Signature saved successfully.");

                    } else {
                        alert(xhr.responseText);
                    }
                }
            };
            xhr.send(formData);
        }
    });

    function dataURLToBlob(dataURL) {
        var binary = atob(dataURL.split(',')[1]);
        var array = [];
        for (var i = 0; i < binary.length; i++) {
            array.push(binary.charCodeAt(i));
        }
        return new Blob([new Uint8Array(array)], {
            type: dataURL.split(',')[0].split(':')[1].split(';')[0]
        });
    }
</script>

<?php

$quality_inspection_ajax = new js_ajax();
$quality_inspection_ajax->function_name("quality_inspection");
$quality_inspection_ajax->insert("quality_inspection");
$quality_inspection_ajax->on_success("QUALITY INSPECTION FORM SAVED");
$quality_inspection_ajax->redirect("");