<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');
header("Content-Type: text/html;charset=UTF-8");

$formname = 'Move File';
$maindb = 'visamain';
$trandb = 'visatran';
$file = 'movefile.php';
//$printfile = 'crmlist.php';
$keyfld = "myvouch";
$myyear = $_SESSION["tit_" . $dbname];

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$username = $_SESSION["username_" . $dbname];
$zroot = $_SESSION["zdir"];
//$modet = $_GET["mode"];
$myyear = $_SESSION["tit_" . $dbname];

mysqli_select_db($conn, $zdb);

include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {

    $passport1 = $_POST["passport"];
    $fromfile = $_POST["fromfile"];
    $tofile = $_POST["tofile"];

    $qry = mysqli_query($conn, "SELECT a.myvouch AS mainvouch,b.srno,b.imagepath,b.imagevisa,b.imagepass,b.visapdf
                            FROM `$maindb` a , `$trandb` b where a.myvouch= b.myvouch and b.passport ='$passport1' and a.manualno = '$fromfile'") or die(mysqli_error($conn));
    $row = mysqli_fetch_array($qry);

    $oldvouch = $row['mainvouch'];
    $oldimgpth = $row['imagepath'];
    $oldcnt = $row['srno'];
    $oldtfileNameU = $oldvouch . "_" . $oldcnt . "_user.jpg";
    $oldtfileNameP = $oldvouch . "_" . $oldcnt . "_pass.jpg";
    $oldtfileNameV = $oldvouch . "_" . $oldcnt . "_visa.jpg";
    $oldtfileNameF = $oldvouch . "_" . $oldcnt . "_visa.pdf";


    $qry2 = mysqli_query($conn, "SELECT myvouch AS mainvouch FROM `$maindb` where manualno = '$tofile'") or die(mysqli_error($conn));
    $row2 = mysqli_fetch_array($qry2);
//print table (qry2);
//die;
//    browse($oldimgpth);
    $newvouch = $row2['mainvouch'];

    $qry2cnt = mysqli_query($conn, "SELECT max(srno) as maxcode FROM `$trandb` where myvouch = '$newvouch'") or die(mysqli_error($conn));
    $fe2cnt = mysqli_fetch_array($qry2cnt);
    $newcnt = $fe2cnt['maxcode'] + 1;


    $dir1 = "images/" . $myyear . "/" . $oldvouch;
    $dir2 = "images/" . $myyear . "/" . $newvouch;
    if (is_dir($dir1) === false) {
        mkdir($dir1);
    }
    if (is_dir($dir2) === false) {
        mkdir($dir2);
    }
    $tpath1 = $dir1 . "/";
    $tpath2 = $dir2 . "/";


    $tfileNameU = $newvouch . "_" . $newcnt . "_user.jpg";
    $tfileNameP = $newvouch . "_" . $newcnt . "_pass.jpg";
    $tfileNameV = $newvouch . "_" . $newcnt . "_visa.jpg";
    $tfileNameF = $newvouch . "_" . $newcnt . "_visa.pdf";


    $timg_fileU = $tpath2 . ($tfileNameU);
    $timg_fileP = $tpath2 . ($tfileNameP);
    $timg_fileV = $tpath2 . ($tfileNameV);
    $timg_fileF = $tpath2 . ($tfileNameF);


    rename("$tpath1" . $oldtfileNameU, "$tpath2" . $tfileNameU);
    rename("$tpath1" . $oldtfileNameP, "$tpath2" . $tfileNameP);
    rename("$tpath1" . $oldtfileNameV, "$tpath2" . $tfileNameV);
    rename("$tpath1" . $oldtfileNameF, "$tpath2" . $tfileNameF);
//
    mysqli_query($conn, "UPDATE $trandb SET myvouch = '$newvouch',srno='$newcnt',`imagepath`='$timg_fileU',
                                           `imagevisa`='$timg_fileV', `imagepass`='$timg_fileP', `visapdf`='$timg_fileF'  
                                            WHERE myvouch  = '$row[mainvouch]' and passport = '$passport1' ")or die(mysqli_error($conn));





    browse("MOVED SUCESSFULLY");
}



if ($addflag != 1) {

    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

    mysqli_query($conn, "SET NAMES 'utf8'");
    mysqli_query($conn, 'SET CHARACTER SET utf8');
    $result = mysqli_query($conn, "SELECT *, date_format(`vouchdt`,'%d-%m-%Y') as mydt FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $pp = mysqli_query($conn, "SELECT * FROM `visatran` WHERE `passport` = '$get[passport]'");
    $gtpp = mysqli_fetch_array($pp);
}

if (isset($_GET["delete"])) {

    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script>
    $(document).ready(function () {
        $("body").on("change", "#passport", getChange);
        function getChange() {
            var passport = $("#passport").val();

            $.post('get_file.php', {passport: passport}, function (data) {
                $("#fromfile").val("");
                $("#fromfile").html(data).trigger("change");
            });

        }

        $("#hprint").hide();
        $("#search").hide();
        $("#edit").hide();
        $("#hprevious").hide();
        $("#scancel").hide();
        $("#hdelete").hide();
    });
</script>
<!--<script src="MyScript/submission.js"  type="text/javascript"></script>-->   


</head>
<body>
    <form action='' method='POST'  enctype="multipart/form-data" accept-charset="utf-8">
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-12" >
                <div class="panel panel-default" >
                    <div class="panel-heading">
                        <h3 class="panel-title">
                            <?php Echo $formname; ?>
                        </h3>
                    </div>
                    <div class="panel-body">
                        <div class="row">    
                            <div class="col-lg-3 col-md-3 col-sm-1 col-xs-1 text-right pad">
                                <!--for dummy div-->
                            </div> 
                            <div class="col-lg-2 col-md-3 col-sm-1 col-xs-1 text-right pad">
                                <label class="control-label marg">Passport</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-2 pad">
                                <select id="passport" name="passport" class="form-control form select2">
                                    <option value="<?php echo ($addflag != 1) ? $gtpp['passport'] : ""; ?>"><?php echo ($addflag != 1) ? $gtpp['passport'] : ""; ?></option>
                                    <?php
                                    $pass = mysqli_query($conn, "SELECT * FROM `visatran` WHERE passport !='' ORDER BY passport") or die(mysqli_error($conn));
                                    while ($gpass = mysqli_fetch_array($pass)) {
                                        echo "<option value='$gpass[passport]'>$gpass[passport]</option>";
                                    }
                                    ?>   
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-1 col-xs-1 text-right pad">
                                <!--for dummy div-->
                            </div> 
                            <div class="col-lg-2 col-md-3 col-sm-1 col-xs-1 text-right pad">
                                <label class="control-label marg">From File</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-2 pad">
                                <select id="fromfile" name="fromfile" class="form-control form select2 fromfile">
                                    <option value=""></option>
                                    <?php
                                    $ffile = mysqli_query($conn, "SELECT * FROM `visamain` WHERE manualno !='' ORDER BY manualno") or die(mysqli_error($conn));
                                    while ($gffile = mysqli_fetch_array($ffile)) {
                                        echo "<option value='$gffile[manualno]'>$gffile[manualno]</option>";
                                    }
                                    ?>   
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-1 col-xs-1 text-right pad">
                                <!--for dummy div-->
                            </div> 
                            <div class="col-lg-2 col-md-3 col-sm-1 col-xs-1 text-right pad">
                                <label class="control-label marg">To File</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-2 pad">
                                <select id="tofile" name="tofile" class="form-control form select2">
                                    <option value="<?php echo ($addflag != 1) ? $gtpp['manualno'] : ""; ?>"><?php echo ($addflag != 1) ? $gtpp['name'] : ""; ?></option>
                                    <?php
                                    $tfile = mysqli_query($conn, "SELECT * FROM `visamain` WHERE manualno !='' ORDER BY manualno") or die(mysqli_error($conn));
                                    while ($gtfile = mysqli_fetch_array($tfile)) {
                                        echo "<option value='$gtfile[manualno]'>$gtfile[manualno]</option>";
                                    }
                                    ?>   
                                </select>
                            </div>
                        </div>

                    </div>
                    <div class="panel-footer">
                        <? include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <div class="modal fade bs-example-modal-lg searchModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <!--<option value="1">Code</option>-->
                                <option value="1">Date</option>
                                <option value="2">Name</option>
                                <option value="3">Email Id</option>
                                <option value="4">Mobile No.</option>
                                <option value="5">Passport No.</option>
                                <option value="6">Job Category</option>
                                <option value="7">Education</option>
                                <option value="8">Remarks</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Date</option>
                                <option value="2">Name</option>
                                <option value="3">Email Id</option>
                                <option value="4">Mobile No.</option>
                                <option value="5">Passport No.</option>
                                <option value="6">Job Category</option>
                                <option value="7">Education</option>
                                <option value="8">Remarks</option>
                            </select> 
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="asc">Asc</option>
                                <option value="desc">Desc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search" >
                        </div>
                    </div>
                    <div class="col-lg-12 sloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->       
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table  class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr class="mySearchTr">
                                    <th width="6%">Date</th>
                                    <th width="15%">Name</th>
                                    <th width="6%">Email Id</th>
                                    <th width="7%">Mobile No.</th>
                                    <th width="9%">Passport No.</th>
                                    <th width="10%">Job Category</th>
                                    <th width="10%">Education</th>
                                    <th width="10%">Remarks</th>
                                    <th width="1%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button"  class="btn btn-primary sprev" value="Previous">
                            <input type="button"  class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>

</html>
