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

$formname = 'TO DO LIST';
$maindb = 'todo';
$trandb = 'todotran';

$printfile = "todoprn.php";
$file = 'todolist.php';
$trandbexist = 1;
$keyfld = "vouchno";
$zroot = $_SESSION["zdbroot"];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

$admin = $_SESSION["admin_" . $dbname];
$username = $_SESSION["username_" . $dbname];

$getus = mysqli_query($conn, "select code from $zroot.passmain where `user`='$username'");
$fetus = mysqli_fetch_array($getus);
$ucode = $fetus['code'];
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchdt = $_POST["vouchdt"];
    $task = $_POST["task"];
    $assby = $_POST["assby"];
    $done = $_POST["done"];
    $rem1 = $_POST["remark1"];
    $rem2 = $_POST["remark2"];

    if ($done == 1) {
        $doneval = "Y";
    } else {
        $doneval = "";
    }


    $count = $_POST["items"];

    $mvouch = $qvouch;

    $mgroup = $source . $qvouch;

    if (empty($vouchdt)) {
        browse("Voucher Date Cannot Be Empty");
        $a = 0;
    }

    /*  if ($billamt <= 0) {
      browse("Bill Amount Cannot Be Zero");
      $a = 0;
      }
     */
    if ($a == 1) {
        mysqli_query($conn, "Delete from `$trandb` where vouchno ='$mvouch'");
        $cnt = 0;
        for ($i = 0; $i < $count; $i++) {
            $srno = $_POST["srno_$i"];
            $staff = $_POST["staff_$i"];

            if (!empty($staff)) {
                $cnt++;
                mysqli_query($conn, "Insert into `$trandb` (vouchno, vouchdt, srno, staff) values('$mvouch', '$vouchdt','$cnt', '$staff')") or die(mysqli_error($conn));
            }
        }

        ############################## MAIN  ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(vouchno, `vouchdt`, `task`, `done`, `remark1`, `remark2`,assby, status)
                                        VALUES('$qvouch', '$vouchdt', '$task', '$doneval', '$rem1', '$rem2','$assby','Pending')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch; //1

            header("refresh: 1; $file?mode=edit&vouchno=$mvouch");
        } else {
            $mvouch = $qvouch; //1

            mysqli_query($conn, "UPDATE `$maindb` SET `vouchno`='$qvouch',`vouchdt`='$vouchdt', `task`='$task', `done`='$doneval', assby='$assby',
                        `remark1`='$rem1', `remark2`='$rem2' WHERE vouchno = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?mode=edit&vouchno=$mvouch");
        }
    }
}

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

//    $getvouch = $_GET["vouchno"];
    $result = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb`
                           WHERE vouchno ='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
    $getas = mysqli_query($conn, "SELECT code,user FROM $zroot.`passmain`
                           WHERE code ='$get[assby]'") or die(mysqli_error($conn));
    $fetas = mysqli_fetch_array($getas);
    $asname = $fetas['user'];
}

$senable = "";
$cenable = "";

/* DELETE */
if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "vouchno", $_GET["vouchno"], "$file?mode=last");
    delete($zdb, "$trandb", "vouchno", $_GET["vouchno"], "$file?mode=last");

//    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
//    delnew($zdb, "$stkdb", "$delcond", "$file?mode=last");
}
include("includes/linkshead.php");
?>
<!--<script src="MyScript/todolist.js" type="text/javascript"></script>-->
<script src="MyScript/todolist_search.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var assby = "<?= $asname ?>".toUpperCase();
        var usern = "<?= $username ?>".toUpperCase();
        if (assby != usern) {
            $("#hedit").hide();
            $("#hdelete").hide();
        }
    });
</script>
</head>
<body>
    <form action='' method='POST'>

        <!-- Default box -->


        <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12  pad">
            <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-3 col-xs-3 text-right">
                            <label class="label-control marg">To Do No.</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                            <input type="text" style="width:70px" name="vouchno" value="<?php echo ($addflag != 1) ? $get['vouchno'] : "$qvouch"; ?>"  class="form-control vouchno" disabled /> 
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right">
                            <label class="label-control marg">Date</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                            <input type="date" style="width:178px" size="5" id="vouchdt" autofocus name="vouchdt" value="<?php echo ($addflag != 1) ? $get['vouchdt'] : date("Y-m-d"); ?>" class="form-control form" required/>   
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-1 col-xs-1 text-right">
                            <label class="label-control">Task</label>
                        </div>
                        <div class="col-lg-6 col-md-6 col-sm-3 col-xs-3 pad">
                            <textarea class="form-control form task"   name="task"><?php Echo ($addflag != 1) ? $get[task] : ""; ?></textarea>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-1 col-xs-1 text-right">
                            <label class="label-control">Assigned By</label>
                        </div>
                        <div class="col-lg-6 col-md-6 col-sm-3 col-xs-3 pad">
                            <select class='form-control form' name='assby'>";
                                <option value='<?php echo ($addflag != 1) ? $fetas['code'] : "$ucode" ?>'><?php echo ($addflag != 1) ? $fetas['user'] : "$username" ?></option>
                                <?php
//                                $getstaff2 = mysqli_query($conn, "Select * from $zroot.`passmain` order by user");
//                                while ($fetst2 = mysqli_fetch_array($getstaff2)) {
//                                    echo "<option value='$fetst2[code]'>$fetst2[user]</option>";
//                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-1 col-xs-1 text-right">
                            <label class="label-control marg">Done</label>
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                            <input type="checkbox" style="width:27px" name="done" class="form-control form" value="1" 
                            <?
                            if ($addflag != 1) {
                                if ($get[done] == 'Y') {
                                    Echo "checked";
                                }
                            }
                            ?> /> 
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-2"></div>
                        <div class="table-responsive col-lg-8" align='center' width='50%'>
                            <table class="table table-bordered">
                                <thead class="mySearchTr">
                                    <!--<tr>-->
                                <th style="width:15%">Sr.</th>
                                <th style="width:85%">Assigned To</th>
                                <!--</tr>-->
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $gettran = mysqli_query($conn, "Select * from `$trandb` where vouchno='$getvouch' order by srno");
                                        while ($row = mysqli_fetch_array($gettran)) {
                                            $getstaff2 = mysqli_query($conn, "Select * from $zroot.`passmain` where `code`='$row[staff]' order by user");
                                            $fetst2 = mysqli_fetch_array($getstaff2);
                                            echo "<tr>";
                                            echo "<td><input type='text' name='srno_$ctr' class='form-control form' value='$row[srno]'></td>";
                                            echo "<td>";
                                            echo "<select class='form-control form' name='staff_$ctr'>";
                                            echo "<option value='$row[staff]'>$fetst2[user]</option>";
                                            $getstaff = mysqli_query($conn, "Select * from $zroot.`passmain` order by user");
                                            while ($fetst = mysqli_fetch_array($getstaff)) {
                                                echo "<option value='$fetst[code]'>$fetst[user]</option>";
                                            }


                                            echo "</select>";
                                            echo "</td>";
                                            echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        echo "<tr>";
                                        echo "<td><input type='text' name='srno_0' value='1' class='form-control form'></td>";
                                        echo "<td>";
                                        echo "<select class='form-control form' name='staff_0'>";
                                        echo "<option value=''></option>";
                                        $getstaff = mysqli_query($conn, "Select * from $zroot.`passmain`");

                                        while ($fetst = mysqli_fetch_array($getstaff)) {
                                            echo "<option value='$fetst[code]'>$fetst[user]</option>";
                                        }
                                        echo "</select>";
                                        echo "</td>";
                                        echo "</tr>";
                                    }
                                    ?>
                                </tbody>

                            </table>

                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-2 pad">
                        </div>
                        <div class="col-lg-2 pad">
                            <button type="button" class="btn btn-primary btn-square form"  id="addnew">
                                <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>    
                            </button>
                            <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 1; ?>" />  
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-1 col-xs-1 pad text-right">
                            <label class="label-control marg ">Remark</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-4 col-xs-4 pad">
                            <input type="text" name="remark1" class="form-control form" value="<? echo ($addflag != 1) ? $get['remark1'] : ""; ?>" maxlength="60">
                        </div>
                        <!--                                    <div class="col-lg-3 col-xs-3 text-right">
                                                                <label class="label-control marg">Total Amount : </label>
                                                            </div>
                                                            <div class="col-lg-3 col-xs-4" >
                                                                <input type="text" name="tot" class="form-control form tot" style="text-align: right;" value="<? echo ($addflag != 1) ? $get['tot'] : ""; ?>" readonly>
                                                            </div>-->
                    </div> 
                </div>

                <div class="panel-footer">
                    <div class="col-lg-10">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                    <div class="col-lg-2">
                        <button id='smscust' type="button" class="btn btn-pad btn-lg smscust" style="background-color:grey;width: 100px ">
                            SMS
                        </button>
                    </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">ToDo No.</option>
                                <option value="2">Date</option>
                                <option value="3">Task</option>
                                <option value="4">Done/Pending</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="1">ToDo No.</option>
                                <option value="2">Date</option>
                                <option value="3">Task</option>
                                <option value="4">Done/Pending</option>
                            </select> 
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</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 id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr class="mySearchTr">
                                    <th style="color: white;"width="1%">To do No.</th>
                                    <th style="color: white;"width="5%"> Date</th>
                                    <th style="color: white;"width="8%">Task</th>
                                    <th style="color: white;"width="5%">Done/Pending</th>
                                    <th style="color: white;"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>

