<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');
//include('includes/spdfeImage.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);
$pdfdb = "visatran";

mysqli_select_db($conn, $zdb);
$success = "";
// $series = $_POST["series"];
$vouchno = $_POST["voucher"];
$srno = $_POST["msrno"];
$type = $_POST["type"];
$path = "images/$zyear/$vouchno/";
$imgfld = $_POST["imgfld"];
switch ($type) {
    case "user";
        $fileTmpLoc = $_FILES["mfile"]["tmp_name"]; // File in the PHP tmp folder
        $fileType = $_FILES["mfile"]["type"]; // The type of file it is
        $fileSize = $_FILES["mfile"]["size"]; // File size in bytes
        $fileErrorMsg = $_FILES["mfile"]["error"]; // 0 for false... and 1 for true
        break;
    case "visa":
        $fileTmpLoc = $_FILES["mfile2"]["tmp_name"]; // File in the PHP tmp folder
        $fileType = $_FILES["mfile2"]["type"]; // The type of file it is
        $fileSize = $_FILES["mfile2"]["size"]; // File size in bytes
        $fileErrorMsg = $_FILES["mfile2"]["error"]; // 0 for false... and 1 for true
        break;
    case "pass":
        $fileTmpLoc = $_FILES["mfile3"]["tmp_name"]; // File in the PHP tmp folder
        $fileType = $_FILES["mfile3"]["type"]; // The type of file it is
        $fileSize = $_FILES["mfile3"]["size"]; // File size in bytes
        $fileErrorMsg = $_FILES["mfile3"]["error"]; // 0 for false... and 1 for true
        break;
    case "contract":
        $fileTmpLoc = $_FILES["mfile4"]["tmp_name"]; // File in the PHP tmp folder
        $fileType = $_FILES["mfile4"]["type"]; // The type of file it is
        $fileSize = $_FILES["mfile4"]["size"]; // File size in bytes
        $fileErrorMsg = $_FILES["mfile4"]["error"]; // 0 for false... and 1 for true
        break;
    default:
        break;
}

$fileName = $vouchno . "_" . $srno . "_" . $type . ".jpg";
$pdf_file = $path . ($fileName);

//$fileName       = $_FILES["file2"]["name"]; // The file name


if (!$fileTmpLoc) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
}
if (move_uploaded_file($fileTmpLoc, "$pdf_file")) {

    echo "$fileName upload is complete";

    ###########################SAVE PART################################################   
//    $pdfp = mysqli_query($conn, "SELECT * FROM `$main`")or die(mysqli_error($conn));
//    $gpdf = mysqli_fetch_array($pdfp);
//    mysqli_query($conn, "DELETE FROM `$pdfdb` WHERE `vouchno`='$vouchno' AND `series`='$series'") or die(mysqli_error($conn));
//    if ($gpdf[vouchno] == $vouchno) {
    mysqli_query($conn, "UPDATE `visatran` SET $imgfld='$pdf_file' WHERE `myvouch`='$vouchno' and srno = '$srno'") or die(mysqli_error($conn));
//    }
} else {
    echo "move_uploaded_file function failed";
}
?>