<?php 
include 'header.php';
if(isset($_GET['id']))
{
    $id=$_GET['id'];
	$bdd=new DB();
	$row=$bdd->getOne('SELECT name,contact_no,date,chassis_no,status as stb FROM customer WHERE customer_id='.$id);
	$bdd->disconnect();

    $year = date('Y', strtotime($row['date']));
    $month = date('m', strtotime($row['date']));
    $path = 'uploads/' . $year . '/' . $month . '/' . $row['name'] . '-' . $row['contact_no'] . '-' . $row['chassis_no'] . '/';
    $files = glob($path . '/Ready.*');

    if (count($files) > 0) {   
        $bdd=new DB();
        $result=$bdd->execute("UPDATE customer SET status='RC' WHERE customer_id=".$id);
        $bdd->disconnect(); 
        echo "<script>alert('Customer file is already uploaded');location.href='customer.php?status=Ready';</script>";
    }
}
?>
<html>
    <head>
        
    </head>
    <body>
        
<div class="row">
	<div class="col-sm-12"><button class="btn btn-blue btn-block">Ready To Go</button></div>
	<div class="col-sm-12">&nbsp</div>
	<div class="col-sm-12"></div>
</div>
<table>
    <tr>
        <th>Customer Name: &nbsp</th><td><?=$row['name']?></td>
    </tr>
    <tr>
        <th>Phone No: &nbsp</th><td><?=$row['contact_no']?></td>
    </tr>
    <tr>
        <th>Chassis No: &nbsp</th><td><?=$row['chassis_no']?></td>
    </tr>
    <tr>
        <th><br></th>
    </tr>
    <!--<tr>
        <th>
            <input class="btn btn-primary" type="button" value="Send OTP">
        </th>
    </tr>-->
</table>
<br>
<br>
<div class="card">
  <div class="card-body center">
    <form method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label for="email">Select image:</label>
            <input class="form-control" type="file" accept="image/*" capture="camera" name="cam_image" Required>
        </div>
        <button type="submit" class="btn btn-default" name="submit">Submit</button>
    </form> 
  </div>
</div>

<?php
if(isset($_POST['submit'])){
    
	$filename="Ready";
	$file_ext=strtolower(pathinfo($_FILES['cam_image']['name'], PATHINFO_EXTENSION));
	$fullfilename=$filename.'.'.$file_ext;
	//echo "filename:".$fullfilename;
	if(move_uploaded_file($_FILES["cam_image"]["tmp_name"], $path . $fullfilename))
    {
        $bdd=new DB();
        $result=$bdd->execute("UPDATE customer SET status='RC' WHERE customer_id=".$id);
        $bdd->disconnect();

        $Uname=$_GET['Uname'];

        $bdd=new DB();
		$existingEntries = $bdd->getOne("SELECT * FROM modification WHERE CId =". $id);
		$bdd->disconnect();
		if ($existingEntries > 0) {
			$bdd=new DB();
			$resu=$bdd->execute("UPDATE modification SET Ready = $Uname,Ready_date='$current_datetime' WHERE CId=".$id);
			$bdd->disconnect();
		} else {
			$bdd=new DB();
			$insertResult = $bdd->execute("INSERT INTO modification (Ready, Ready_date, CId) VALUES ($Uname,'$current_datetime',$id)");
			$bdd->disconnect();
		}

        if($result)
        {
            echo "<script>alert('Customer has been updated')location.href='customer.php?status=Ready';</script>";
        }
    }
}
?>
<!--
<div class="card">
  <div class="card-body center">
    <div class="row">
        <div class="col">
            <form action="" method="post">
                <h2>Enter OTP</h2>
                <br>
                <input type="text" name="OTP" id="OTP" style="width:20%" placeholder="Enter OTP">
                <br>
                <br>
                <input class="btn btn-success" type="submit" value="Verify">
                <br>
                <br>
            </form>
        </div>
    </div>
    <div class="row">
        <div class="col">
            <h2>Take a Photo</h2>
    
            <video id="camera" style="display:none;" autoplay></video>
            <button class="btn" id="startCamera">Start Camera</button>
            <button id="capture" style="display:none;">Capture</button>
            <button id="recapture" style="display:none;">Recapture</button>
            <button id="upload" style="display:none;">Upload</button>
            <canvas id="canvas" style="display:none;"></canvas>
            <img id="capturedImage" style="width:50%; height:50%; display:none; cursor: pointer;" onclick="toggleImageSize()" />
            
        </div>
    </div>
  </div>
</div>
<input id="Okay" class="btn btn-success" type="button" value="All Okay" style="display:none;">
<br>
<br>
-->

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
/*    
$(document).ready(function() {
    const videoElement = document.getElementById('camera');
    const canvasElement = document.getElementById('canvas');
    const capturedImageElement = document.getElementById('capturedImage');
    const startCameraButton = document.getElementById('startCamera');
    const captureButton = document.getElementById('capture');
    const recaptureButton = document.getElementById('recapture');
    const uploadButton = document.getElementById('upload');
    const Okay = document.getElementById('Okay');

    let stream; 
    async function startCamera() {
        try {
            stream = await navigator.mediaDevices.getUserMedia({ video: true });
            videoElement.srcObject = stream;
        } catch (error) {
            console.error('Error accessing camera:', error);
        }
    }
    
    startCameraButton.addEventListener('click', () => {
        startCamera();
        videoElement.style.display = 'block';
        startCameraButton.style.display = 'none';
        captureButton.style.display = 'block';
    });

    captureButton.addEventListener('click', () => {
        const context = canvasElement.getContext('2d');
        canvasElement.width = videoElement.videoWidth;
        canvasElement.height = videoElement.videoHeight;
        context.drawImage(videoElement, 0, 0, canvasElement.width, canvasElement.height);
        
        // Show the captured image and buttons for recapture and upload
        capturedImageElement.src = canvasElement.toDataURL('image/jpeg');
        capturedImageElement.style.width = '50%';
        capturedImageElement.style.height = '50%';
        capturedImageElement.style.display = 'block';
        recaptureButton.style.display = 'inline-block';
        uploadButton.style.display = 'inline-block';
        
        // Hide the video stream and other buttons
        videoElement.style.display = 'none';
        canvasElement.style.display = 'none';
        captureButton.style.display = 'none';
        startCameraButton.style.display = 'none';
        
        // Stop the camera stream
        if (stream) {
            const tracks = stream.getTracks();
            tracks.forEach(track => track.stop());
        }
    });

    recaptureButton.addEventListener('click', () => {
        // Show the video stream and capture button, hide the captured image and other buttons
        videoElement.style.display = 'block';
        captureButton.style.display = 'block';
        canvasElement.style.display = 'none';
        capturedImageElement.style.display = 'none';
        recaptureButton.style.display = 'none';
        uploadButton.style.display = 'none';
        startCameraButton.style.display = 'none';
        
        // Restart the camera
        startCamera();
    });

    /*uploadButton.addEventListener('click', () => {
        // Get the captured image data as a data URL
        const imageDataURL = capturedImageElement.src;
        const chassisno = document.getElementById('field1').value;
        const jsVariable = name;
        const phpVariable = '<?php echo $truckId; ?>';
        // Create a FormData object to send data
        const formData = new FormData();
        // Add the PHP variable and the JavaScript variable to the FormData object
        formData.append('photo', imageDataURL);
        formData.append('phpVariable', phpVariable);
        formData.append('jsVariable', jsVariable);
        formData.append('chassisno', chassisno);
        // Send the image data to the server using XMLHttpRequest
        const xhr = new XMLHttpRequest();
        xhr.open('POST', 'uploadImage.php', true);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    alert('Image uploaded successfully.');
                    // Hide all buttons on successful upload
                    recaptureButton.style.display = 'none';
                    uploadButton.style.display = 'none';
                    videoElement.style.display = 'none';
                    canvasElement.style.display = 'none';
                    captureButton.style.display = 'none';
                    startCameraButton.style.display = 'none';
                    capturedImageElement.style.display = 'block';
                    Okay.style.display = 'block';
                } else {
                    alert('Error uploading image.');
                }
            }
        };
        xhr.send(formData);
    });

    // JavaScript function to toggle the image size
    function toggleImageSize() {
        if (capturedImageElement.style.width === '' || capturedImageElement.style.width === '100%') {
            capturedImageElement.style.width = '50%';
            capturedImageElement.style.height = '50%';
        } else {
            capturedImageElement.style.width = '100%';
            capturedImageElement.style.height = '100%';
        }
    }
});*/


</script>

</body>
</html>
<?php
include 'footer.php';
?>