<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn,$zdb);
$admin = $_SESSION["admin_" . $dbname];
$maindb = 'challan';
$imgkeyfld = "imggroup";
$file = "chalmain.php";

if (isset($_POST['imgsubmit'])) {
    $path = "images/challan/";
    $ctr = 0;

    foreach ($_POST['imgdata'] as $mvouch) {

        if ($ctr == 0) {

            $gen1 = mysqli_query($conn,"SELECT MAX($imgkeyfld) AS maxcode FROM `$maindb` LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $imgqvouch = $vvouch["maxcode"] + 1;
            } else {
                $imgqvouch = '1';
            }


            $image_ftype = pathinfo($img_file1, PATHINFO_EXTENSION);
            $filename = $mvouch . ".jpg";
            $img_file = $path . ($filename);
            $size = $_FILES["image1"]["size"];


            if ($size > 1500000) {
                $uploadOk = 0;
                $a = 0;
            } else {
                $uploadOk = 1;
                $a = 1;
            }


            if ($uploadOk == 1) {
                move_uploaded_file($_FILES["image1"]["tmp_name"], $img_file);
            } else {
                browse("Sorry, there was an error uploading your file.(Check Size)");
                header("refresh: 1; $file?mode=last");
            }
        }

        $ctr++;
        mysqli_query($conn,"UPDATE `$maindb` SET `imgpath`='$img_file', `imggroup`='$imgqvouch' WHERE `vouchno`='$mvouch'") or die(mysqli_error($conn));
    }
    if ($ctr > 0) {
        browse("Image Uploaded Successfully");
    } else {
        browse("Please Select Challan");
    }
    header("refresh: 1; $file?mode=last");
} else {
    browse("Something Went Wrong");
    header("refresh: 1; $file?mode=last");
}