<?php 
include 'header.php';
if(isset($_GET['id']))
{
    // echo $_GET['id'];
    $id=$_GET['id'];
    $Uname = $_GET['Uname'];
	$bdd=new DB();
	$row=$bdd->getOne('SELECT name,contact_no,date,chassis_no,documents,status as stb FROM customer WHERE customer_id='.$id);
    // print_r($row); // aa formate ma showw karvanuc chhe
	$bdd->disconnect();
	$year=date('Y',strtotime($row['date']));
    $month=date('m',strtotime($row['date']));
    $status = $row['stb'];
	$pathA="uploads/".$year."/".$month."/".$row['name']."-".$row['contact_no'].'-'.$row['chassis_no'];
    $documents = $row['documents'];
	// $pathA="scannedImageResult";
	// echo $path;exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>View Image</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.10.377/pdf.min.js"></script>
<!--Popup Lightbox Js-->
<script src="js/jquery.popup.lightbox.js"></script>

<!--Popup Lightbox CSS-->
<link href="css/popup-lightbox.css" rel="stylesheet" />
   

    <style>
        img.scanned {
            height: 200px; /** Sets the display size */
            margin-right: 12px;
        }

        div#images {
            margin-top: 20px;
        }

        .table thead tr {
    color: #707070;
    font-weight: normal;
    }
    select.form-control {
        background-color: #FFFFFF;
        border: 1px solid #D5D5D5;
        border-radius: 0 0 0 0;
        color: #858585;
    }

    .form-control {
        display: block;
        width: 100%;
        height: 34px;
        padding: 6px 12px;
        font-size: 14px;
        line-height: 1.428571429;
        background-image: none;
        box-shadow: inset 0 1px 1px rgb(0 0 0 / 8%);
        transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
    }
    .xcrud-th th {
        background: #efefef;
        white-space: nowrap;
    }
    
    </style>
</head> 
<?php

$j = 0;
//echo "<pre>"; print_r($scan);exit;
$flag = 'yes';
function SearchArray( $m, $file1) 
{

    foreach($file1 as $f)
    {
      
	  if ( strstr(trim($f),trim($m["name"]) ) ) {
		  return "yes";
		} 
    }
  //  echo $GLOBAL['flag'];
    if(isset($GLOBALS['scan'][$GLOBALS['j']]))
    {
        $ret1=array();
        for($i = 0 ; $i < $m["nop"] ; $i++)
        { 
            if(isset($GLOBALS['scan'][$GLOBALS['j']]))
            {
                $ret1[]= $GLOBALS['scan'][$GLOBALS['j']++];
            }
        }
        //print_r($ret1);
        return $ret1;
    }
    return "no";
}
   ?>
<div class="row">
	<div class="col-sm-12"><button class="btn btn-blue btn-block">View image</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>

   </table>
   <br>

   <style>

 table tr td .btnhide { display:none;}
  table tr:hover td .btnhide { display:inline-block;}
   </style>
  <?php  
  include "connect.php";
  
        $cid = $_GET['id'];

        $stat = "SELECT status FROM customer where customer_id = $cid";
        $run = $con->query($stat);
        
        $res = $run->fetch_assoc();
        $res['status'] = $row['stb'];
  ?>
   <br><br>
    <form action="converted.php" method="post" enctype="multipart/form-data">
    <table class="xcrud-list table table-striped table-hover table-bordered">
        <thead>
            <tr class="xcrud-th">
                <th class="xcrud-column xcrud-action">Menu</th>
        
                <th>saved Files</th>
                
            </tr>
        </thead>
    <?php
    $i=0;
     $file1 = array();//('TaxtInvoice', 'Form20', 'Form21', 'Form22');
	
    foreach (array_filter(glob($pathA.'/*'), 'is_file') as $file)
    {       
        $sentence = str_replace($pathA.'/', ' ', $file);
		//echo $sentence;
         //$fileName = trim($sentence,'.pdf || .jpg');
        $fileName = pathinfo($sentence, PATHINFO_FILENAME);
		
        $file1[]=$fileName;
    }
    if(count($file1)<=0)
    {
        $flag="no";
    }

$file = $file1;


   
    $sql = "SELECT menu_id,menu_name as name, file_format as format, no_of_page as nop,compulsory FROM menu";
    $result = $con->query($sql);
	
    if ($result->num_rows > 0) {

        while($row = $result->fetch_assoc()) {	
                $menu[]=$row;
        }
    }

$k =0;
function get_file_index($fname,$file)
{
	for($i=0;$i<count($file);$i++)
	{
		//echo strstr( trim($file[$i]),trim($fname) ).$fname.$file[$i]."<br/>";
		
		if(strstr(trim($file[$i]),trim($fname) ) ) {
		//	echo $i;
		  return $i;
		} 
	}
	return -1;
}
foreach($menu as $m)
{
    $s=SearchArray($m,$file); 
    //print_r($s);
    if($s=="no"){
        $GLOBALS['j']++;
    }
    echo '<tr class="hd1">
            <td>'.$m["name"].(($m["compulsory"]=="yes")?"*":"").'</td>';
            
    
    echo "<td class='hd'>";
    
    
    if($s=="yes"){
        
        $x=0;
        
        $folder = $pathA.'/';
        //echo $folder;
        $files = scandir($folder);
        $ext ="";

        foreach ($files as $f) 
        {
            if ($f !== '.' && $f !== '..') 
            {
                $fileName = pathinfo($f, PATHINFO_FILENAME);
                
                if(substr($fileName, 0, strlen($m['name'])) === $m['name'])
                {
                    $ext = pathinfo($f, PATHINFO_EXTENSION);
                    $path = $folder .''. $fileName . '.' . $ext ;
                    break;
                }
            }
            
        }

        

            if ($ext == 'jpg' || $ext == 'JPG') {
                echo '<a href="javascript:void(0);" onclick="newPopup(\''.$path.'\');">
                <img id="orgImg'.$k.'" style="width: 70px;" src="'.$path.'"></a>';
                //if($status == 'Pending')
                //{
                   if($type=="admin" && $res['status'] == 'Uploaded'){ 
				    echo '&nbsp<span id="btnMain'.$k.'"  class="btn btn-danger btnhide" ';?>onclick="delete_file('<?= $path?>',<?= $k++?>,<?= $id?>,'<?=$m['name']?>','<?=$documents?>','<?=$status?>')"style="float: right;padding: 0px;" ><i class="glyphicon glyphicon-remove"></i></span><?php 
				   }
                   elseif($type=="admin"){ 
				    echo '&nbsp<span id="btnMain'.$k.'"  class="btn btn-danger btnhide" ';?>onclick="delete_orignal('<?= $path?>',<?= $k++?>,<?= $id?>,<?= $m["menu_id"]?>,'<?=$documents?>','<?=$status?>')"style="float: right;padding: 0px;" ><i class="glyphicon glyphicon-remove"></i></span><?php 
				   }
                //}        
            }
            else if($ext == 'pdf'){
                echo '<a id="orgImg'.$k.'" href="'.$path.'">PDF</i></a>';
               // if($status == 'Pending')
                //{
                    if($type=="admin" && $res['status'] == 'Uploaded'){ 
                        echo '&nbsp<span id="btnMain'.$k.'"  class="btn btn-danger btnhide" ';?>onclick="delete_file('<?= $path?>',<?= $k++?>,<?= $id?>,'<?=$m['name']?>','<?=$documents?>')"style="float: right;padding: 0px;" ><i class="glyphicon glyphicon-remove"></i></span><?php 
                       }
                       
                    elseif($type=="admin"){
                 echo '&nbsp<span id="btnMain'.$k.'"  class="btn btn-danger btnhide" ';?>onclick="delete_orignal('<?= $path?>',<?= $k++?>,<?= $id?>,<?= $m["menu_id"]?>,'<?=$documents?>')" style="float: right;padding: 0px;"><i class="glyphicon glyphicon-remove"></i></span><?php 
					 }
               // }
            }
      
    }
    elseif($s=="no")
    {
        if($m["compulsory"]=="yes")
        {
            $flag = 'no';
           // echo $flag;exit;
        }

        
    }
    else 
    {
        
    }
    
    echo "</td>";

            $i++;
            echo "</tr>";



}
echo '</table>';
// echo $flag;
?>
<?php if($status == 'Pending' || $status == 'Completed')
{?>
<input type="hidden" name="flag" value='<?= $flag ?>'>
<input type="hidden" name='id' value='<?= $id ?>'>
<input type="hidden" name='pathA' value='<?= $pathA ?>'>
<?php } ?>
</form>
<?php
if($status == 'Pending'){
if($flag=="yes"){
	echo "Ready for Next Step....";
}
else
{
	echo "Please Upload all Complusory files.";
}
}
	
?>
</div>

<script>
    function delete_image(a,i,j,documents)
    {
        if (confirm("Are you sure you want to delete this image?")) {
            $("#imgv" + i+a).val("");
            // alert($("#imgv" + i+a).val());
                $.ajax({
                    type: "GET",
                    url: "ImageLap.php",
                    data: ({
                        id: a,
                        documents: documents
                         
				    }),
                    cache: false,
                    success: function(html) {
                   // alert(html);
				   
                        $("#img" + i +j).remove();
                        $("#btnTemp" + i +j).remove();
                        $("#imgv" + i +j).val("");
                        window.location.reload();
                    }
                });
            } else {
                return false;
            }

    }
function d_image(img,nm,st,documents)
    {
		if(st=="no")
			img="";
                $.ajax({
                    type: "GET",
                    url: "ImageLap.php",
                    data: ({
						nm:nm,
                       img:img,
                       documents: documents
                    }),
                    cache: false,
                    success: function(html) {
						//alert(html);
                    }
                });
           

    }
    function delete_orignal(path,i,id,mid,documents)
    {
        // alert(path);
        if (confirm("Are you sure you want to delete this file ?")) {
            // $("#orgImg" + i + path).val("");
            // alert($("#orgImg" + i).val());
                $.ajax({
                    type: "GET",
                    url: "ImageLap.php",
                    data: ({
                        orgId: path,
						cust_id: id,
						menu_id:mid,
                        documents: documents
                    }),
                    cache: false,
                    success: function(html) {
					//	alert(html);
                        $("#orgImg" + i).remove();
                        $("#btnMain" + i).remove();
                        window.location.reload();
                    }
                });
            } else {
                return false;
            }
    }
    function delete_file(path,i,id,name,documents,status)
    {
        // alert(path);
        if (confirm("Are you sure you want to delete this file ?")) {
            // $("#orgImg" + i + path).val("");
            // alert($("#orgImg" + i).val());
                $.ajax({
                    type: "GET",
                    url: "ImageLap.php",
                    data: ({
                        location: path,
                        name: name,
                        id: id,
                        documents: documents,
                        status: status
                    }),
                    cache: false,
                    success: function(html) {
					//	alert(html);
                        $("#orgImg" + i).remove();
                        $("#btnMain" + i).remove();
                        window.location.reload();
                    }
                });
            } else {
                return false;
            }
    }

    function newPopup(url) {
    popupWindow = window.open(
        url,'popUpWindow','height=700,width=800,left=10,top=10,resizable=yes,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=yes')
    }



</script>