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

$formname = 'staff';
$formname2 = 'History';

$maindb = 'staff';
$trandb = 'staftran';

$printfile = "staff_print.php";
$file = 'staff.php';

$keyfld = "code";
$keyfld2 = "staffcode";

$zroot = $_SESSION["zdbroot"];
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn,$zdb);

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

if (isset($_POST["submit"])) {
    $a = 1;

    $name = $_POST["name"];
    $code = $_POST["code"];
    $add1 = $_POST["add1"];
    $add2 = $_POST["add2"];
    $add3 = $_POST["add3"];
    $city = $_POST["city"];
    $pin = $_POST["pin"];
    $mobile = $_POST["mobile"];
    $tel = $_POST["tel"];
    $joindt = $_POST["joindt"];
    $desig = $_POST["desig"];
    $salary = $_POST["salary"];
    $type = $_POST["type"];
    $day = $_POST["day"];
    $salaryph = $_POST["salaryph"];

    $count = $_POST["items"];

    $mvouch = $qvouch;




    if ($a == 1) {


//        mysqli_query($conn,"LOCK TABLES `$maindb` WRITE, LOCK TABLES `$trandb` WRITE") or die(mysqli_error($conn));

        mysqli_query($conn,"DELETE FROM `$trandb` WHERE `staffcode`='$mvouch'") or die(mysqli_error($conn));

        for ($i = 0; $i < $count; $i++) {

            $tsrno = $_POST["srno_$i"];
            $tjdt = $_POST["jdt_$i"];
            $tldt = $_POST["ldt_$i"];
            $tsalarytrn = $_POST["salarytrn_$i"];
            $tmd = $_POST["md_$i"];
            $thrsday = $_POST["hrsday_$i"];
            $tsalhour = $_POST["salhour_$i"];


            if (!empty($tjdt)) {
                mysqli_query($conn,"INSERT INTO `$trandb`(`staffcode`,`srl`, `trandate`, `leftdate`, `salary_h`, `saltype_h`, `salhrs_h`, `hrs_day_h`)
                                         VALUES('$qvouch','$tsrno','$tjdt','$tldt','$tsalarytrn', '$tmd','$thrsday','$tsalhour')") or die(mysqli_error($conn));
            }
        }




        if ($addflag == 1) {
            mysqli_query($conn,"INSERT INTO $maindb(`code`, `name`, `add1`, `add2`, `add3`, `city`, `pin`, `mobile`, `tel`,  
                                             `joindt`, `desig`, `salary`,`type`,`day`,`salaryph`) 
                                     VALUES('$qvouch', '$name', '$add1', '$add2', '$add3', '$city', '$pin', '$mobile', '$tel',
                                             '$joindt', '$desig', '$salary', '$type','$day','$salaryph')") or die(mysqli_error($conn));

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

            mysqli_query($conn,"UPDATE `$maindb` SET  `name`='$name',  `add1`='$add1', `add2`='$add2',`add3`='$add3',`city`='$city',`pin`='$pin',
                         `mobile`='$mobile',`tel`='$tel',`joindt`='$joindt',`desig`='$desig',`salary`='$salary',`type`='$type',`day`='$day',`salaryph`='$salaryph' WHERE  `code` = '$qvouch' ") or die(mysqli_error($conn));

            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
//        mysqli_query($conn,"UNLOCK TABLES");
    }
}

if ($addflag != 1) {

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

    $result = mysqli_query($conn,"SELECT * FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $gdesig = mysqli_query($conn,"SELECT `code`,`desig` FROM `$zroot`.`desig` WHERE `code`= '$get[desig]' order by desig  ")or die(mysqli_error($conn));
    $sdesig = mysqli_fetch_array($gdesig);
}


if (isset($_GET["delete"])) {
    $getparty = mysqli_query($conn,"SELECT * FROM actran where accode = '$getvouch'")or die(mysqli_error($conn));
    if (mysqli_num_rows($getparty) > 0) {
        browse("Entry Exist in Transaction");
        header("refresh: 1; $file?mode=last");
    } else {
        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
        elete($zdb, "$trandb", "$keyfld2", $_GET[$keyfld], "$file?mode=last");
    }
}
include("includes/linkshead.php");
?>
<script src="MyScript/staff.js" type="text/javascript"></script>
<script src="MyScript/staff_search.js" type="text/javascript"></script>
</head>
<body>

    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6 pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <!--<i class="fa fa-times"></i>-->
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="hidden" class="form-control form " name="code" value="<?php Echo ($addflag != 1) ? $get["code"] : "$qvouch"; ?>"  readonly="">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Name</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form " name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" required autofocus>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Address</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="add1" value="<?php Echo ($addflag != 1) ? $get["add1"] : ""; ?>" >
                                <input type="text" class="form-control form" name="add2" value="<?php Echo ($addflag != 1) ? $get["add2"] : ""; ?>" >
                                <input type="text" class="form-control form" name="add3" value="<?php Echo ($addflag != 1) ? $get["add3"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">City</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="city" value="<?php Echo ($addflag != 1) ? $get["city"] : ""; ?>" >
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Pin</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form " name="pin" value="<?php Echo ($addflag != 1) ? $get["pin"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Mobile</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="mobile" value="<?php Echo ($addflag != 1) ? $get["mobile"] : ""; ?>" >
                            </div>

                            <div class="col-lg-2 col-md-32 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Tel</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="tel" value="<?php Echo ($addflag != 1) ? $get["tel"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Date Of Joining</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form" name="joindt" value="<?php Echo ($addflag != 1) ? $get["joindt"] : ""; ?>" >
                            </div>

                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Designation</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <select name="desig" class=" form-control form select2" required>   
                                    <option value="<?php Echo ($addflag != 1) ? $sdesig["code"] : ""; ?>"><?php Echo ($addflag != 1) ? $sdesig["desig"] : ""; ?> </option>
                                    <?php
                                    $getdesig = mysqli_query($conn,"SELECT `code`,`desig` FROM `$zroot`. `desig` order by desig")or die(mysqli_error($conn));

                                    while ($showdesig = mysqli_fetch_array($getdesig)) {
                                        Echo"<option  value='$showdesig[code]'>$showdesig[desig]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Salary</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-right salary" name="salary" value="<?php Echo ($addflag != 1) ? $get["salary"] : ""; ?>" >
                            </div>

                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Type</label>
                            </div>
                            <div class="col-lg-3 col-md-2 col-sm-8 col-xs-8 pad">
                                <select name="type" class="form-control form select2 type">

                                    <option value=""></option>
                                    <option value="M" <?php
                                    if ($addflag != 1) {
                                        if ($get["type"] == "M") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Monthly</option>
                                    <option value="D" <?php
                                    if ($addflag != 1) {
                                        if ($get["type"] == "D") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Daily</option>
                                </select>                                
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Type</label>
                            </div>
                            <div class="col-lg-3 col-md-2 col-sm-8 col-xs-8 pad">
                                <select name="type" class="form-control form select2 type">

                                    <option value="0" <?php
                                    if ($addflag != 1) {
                                        if ($get["type"] == "0") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Active</option>
                                    <option value="1" <?php
                                    if ($addflag != 1) {
                                        if ($get["type"] == "1") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Not-Active</option>
                      
                                </select>                                
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Hours/Day</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form day" name="day" value="<?php Echo ($addflag != 1) ? $get["day"] : "8"; ?>" >
                            </div>
                        
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Salary Per Hour</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-right salaryph" name="salaryph" value="<?php Echo ($addflag != 1) ? $get["salaryph"] : ""; ?>" disabled>
                            </div>
                        </div>


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


            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php Echo $formname2; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-bordered" style="width:100%">
                                <thead  style="background: #64CCFC ;">
                                    <tr>
                                        <th style="width:3%;"></th>
                                        <th style="width:5%;">Sr</th>
                                        <th style="width:5%;">DOJ</th>
                                        <th style="width:8%;">Left Date</th>
                                        <th style="width:20%;">Salary</th>
                                        <th style="width:5%;">M/D</th>
                                        <th style="width:20%;">Hrs/Day</th>
                                        <th style="width:20%;">Sal/Hour</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">

                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $gettran = mysqli_query($conn,"SELECT * FROM `$trandb` WHERE `staffcode`='$get[code]' ORDER BY `srl`")or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {



                                            Echo "<tr>";
                                            Echo "<td> <i class='fa fa-close remove' style='display: none;'></i> </td>";
                                            Echo "<td> <input type='text' class='form-control form srno' name='srno_$ctr' value='$tran[srno]'> </td>";
                                            Echo "<td> <input type='date' class='form-control form jdt'  name='jdt_$ctr' value='$tran[trandate]'> </td>";
                                            Echo "<td> <input type='date' class='form-control form ldt'  name='ldt_$ctr' value='$tran[leftdate]'> </td>";
                                            Echo "<td> <input type='text' class='form-control form salaryt' name='salarytrn_$ctr' value='$tran[salary_h]'> </td>";
                                            Echo "<td> <input type='text' class='form-control form md'      name='md_$ctr' value='$tran[saltype_h]'> </td>";
                                            Echo "<td> <input type='text' class='form-control form hrsday'   name='hrsday_$ctr'   value='$tran[hrs_day_h]'></td>";
                                            Echo "<td> <input type='text' class='form-control form salhour'  name='salhour_$ctr'    value='$tran[salhrs_h]'></td>";
                                            Echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        $pctr = 0;
                                        ?>
                                        <tr>
                                            <td><i class='fa fa-close remove'></i></td>
                                            <td><input type="text" name="srno_<?= $pctr ?>"      class="form-control form srno" value="1"/></td>
                                            <td><input type="date" name="jdt_<?= $pctr ?>"       class="form-control form jdt" value=""></td>
                                            <td><input type="date" name="ldt_<?= $pctr ?>"       class="form-control form ldt" value=""></td>
                                            <td><input type="text" name="salarytrn_<?= $pctr ?>" class="form-control form salarytrn" value=""></td>
                                            <td><input type="text" name="md_<?= $pctr ?>"        class="form-control form md" value=""></td>
                                            <td><input type="text" name="hrsday_<?= $pctr ?>"    class="form-control form hrsday" value=""></td>
                                            <td><input type="text" name="salhour_<?= $pctr ?>"   class="form-control form salhour" value=""></td>
                                        </tr>
                                        <?php
                                        $pctr++;
                                    }
                                    ?>
                                </tbody>
                            </table>

                        </div>
                    </div>
                    <div class="row">
                        <button type="button" class="btn   form"  id="addnew" style="background-color: #1aa6e6">
                            <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                        </button>
                        <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 1; ?>" />  
                        <a type="button" style="margin-left: 75%; width: 12%; background-color:crimson;"  href="staff_lst.php?code=<?=$get[code]?>" class="btn"  id="print" target="_blank">
                            <span class="" style="color:#ffffff; font-weight: bold;" data-toggle='tooltip' title='Print'>LIST</span>   
                        </a>
                    </div>
                    <div class="panel-footer"></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 <?= $formname; ?></h4>
                </div>
                <div class="modal-body">
<!--                    <input type="hidden" value="<?php // Echo $zseries ?>" class="form-control series">
                    <input type="hidden" value="<?php // Echo $get[code]; ?>" class="form-control vouchno">
                    <input type="hidden" value="<?php // Echo $file ?>" class="form-control file">
                    <input type="hidden" value="<?php // Echo $para ?>" class="form-control para">-->
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Name</option>
                                <option value="2">Address</option>
                                <option value="3">City</option>
                                <option value="4">Mobile</option>
                                <option value="5">Join Date</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">Name</option>
                                <option value="2">Address</option>
                                <option value="3">City</option>
                                <option value="4">Mobile</option>
                                <option value="5">Join Date</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">
                    </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" style="background-color: #64CCFC">
                                    <th style="width:10%">Sr No</th>
                                    <th style="width:25%">Name</th>
                                    <th style="width:25%">Address</th>
                                    <th style="width:10%">City</th>
                                    <th style="width:10%">Mobile</th>
                                    <th style="width:15%">Join Date</th>
                                    <th style="width:5%">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>
