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

$formname = 'User Creation';
$maindb = 'passmain';
$trandb = 'usercomp';
$printfile = "";
$file = 'user.php';
$trandbexist = 0;
$grid = '';
$keyfld = "code";
$keyfld2 = "username";
$zroot = $_SESSION["zdir"];

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

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

if (isset($_POST["submit"])) {
    $a = 1;
    $user = $_POST["user"];
    $password = $_POST["password"];
    $usertype = $_POST["usertype"];
    $series = $_POST["series"];
    $count = $_POST["items"];
    $salevat = $_POST["salevat"];
    $salevatreg = $_POST["salevatreg"];
    $crednotevat = $_POST["crednotevat"];
    $crednotevatreg = $_POST["crednotevatreg"];
    ################## MASTERS ##################

    $customer = $_POST["customer"];
    $supplier = $_POST["supplier"];
    $prodgrp = $_POST["prodgrp"];
    $product = $_POST["product"];
    $transporter = $_POST["transporter"];
    $openbal = $_POST["openbal"];
    $chcustdtl = $_POST["chcustdtl"];
    $bank = $_POST["bank"];
    $cash = $_POST["cash"];
    $expense = $_POST["expense"];
    $group = $_POST["group"];
    $otherac = $_POST["otherac"];
    $paytype = $_POST["paytype"];

    ################## TRANSACTIONS ##################

    $purchase = $_POST["purchase"];
    $sale = $_POST["sale"];
    $debnote = $_POST["debnote"];
    $crednote = $_POST["crednote"];
    $inward = $_POST["inward"];
    $outward = $_POST["outward"];
    $damaged = $_POST["damaged"];
    $excess = $_POST["excess"];
    $transfer = $_POST["transfer"];
    $bankrecp = $_POST["bankrecp"];
    $bankpay = $_POST["bankpay"];
    $cashrecp = $_POST["cashrecp"];
    $cashpay = $_POST["cashpay"];
    $journal = $_POST["journal"];
    $contra = $_POST["contra"];
    $credoth = $_POST["credoth"];

    ################## REPORTS ##################


    $stock = $_POST["stock"];
    $inouthist = $_POST["inouthist"];
    $vat = $_POST["vat"];
    $ledger = $_POST["ledger"];
    $itemwisepl = $_POST["itemwisepl"];
    $itemwisesl = $_POST["itemwisesl"];
    $itemwisegrpsl = $_POST["itemwisegrpsl"];
    $stockmov = $_POST["stockmov"];
    $tipartysl = $_POST["tipartysl"];
    $leadparty = $_POST["leadparty"];
    $leadprod = $_POST["leadprod"];
    $dailyrep = $_POST["dailyrep"];
    $dailycash = $_POST["dailycash"];
    $bankbook = $_POST["bankbook"];
    $cashbook = $_POST["cashbook"];
    $exprep = $_POST["exprep"];
    $partywisesale = $_POST["partywisesale"];


    ################## REGISTERS ##################1

    $purreg = $_POST["purreg"];
    $salreg = $_POST["salreg"];
    $inwreg = $_POST["inwreg"];
    $outreg = $_POST["outreg"];
    $debreg = $_POST["debreg"];
    $credreg = $_POST["credreg"];
    $credothreg = $_POST["credothreg"];
    $damreg = $_POST["damreg"];
    $excreg = $_POST["excreg"];
    $tranreg = $_POST["tranreg"];
    $payablerep = $_POST["payablerep"];
    $bankrecon = $_POST["bankrecon"];


    ################## TOOLS ##################

    $tlcompany = $_POST["tlcompany"];
    $tlbackup = $_POST["tlbackup"];
    $tlprofile = $_POST["tlprofile"];


    if (isset($_POST["user"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `user`= '$user' AND code != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: 1; $file?code=$qvouch&mode=last");
        }
    }

    if ($a == 1) {
        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX($keyfld) AS maxcode FROM `$maindb` LIMIT 0,1 ") or die(mysqli_error($conn));

            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            $mvouch = $qvouch;
        }

        #################################TRAN PART #################################

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

        for ($i = 0; $i < $count; $i++) {
            //            $cono = $_POST["cono_$i"];
            $finyear = $_POST["finyear_$i"];
            $yearname = $_POST["yearname_$i"];
            $startdate = $_POST["startdate_$i"];
            $lastdate = $_POST["lastdate_$i"];

            if (!empty($finyear)) {
                mysqli_query($conn, "INSERT INTO `$trandb`(`username`,`cono`, `finyear`, `yearname`, `startdate`, `lastdate`) 
                    VALUES ('$qvouch', '1', '$finyear', '$yearname', '$startdate', '$lastdate')") or die(mysqli_error($conn));
            }
        }
        ############################## MAIN  ######################################################

        if ($addflag == 1) {
            $usecode = mysqli_query($conn, "SELECT code FROM `passmain` WHERE user='$username'") or die(mysqli_error($conn));
            $getuser = mysqli_fetch_object($usecode);
            $code = $getuser->code;

            mysqli_query($conn, "INSERT INTO `$maindb`(code, `user`, `password`, `usertype`, `customer`, `supplier`, `prodgrp`, `product`,`openbal`, `chcustdtl`,`transporter`,
                                                        `bank`, `cash`, `expense`, `group`, `otherac`,
                                                        `purchase`, `sale`, `debnote`, `crednote`, `inward`, `outward`, `damaged`, `excess`, 
                                                        `bankrecp`, `bankpay`, `cashrecp`, `cashpay`, `journal`,`contra`,
                                                        `stock`, `inouthist`, `vat`, `ledger`, `itemwisepl`, partywisesale,
                                                        `itemwisesl`, `itemwisegrpsl`, `stockmov`,`leadparty`, `leadprod`, `dailyrep`, `dailycash`,
                                                        `bankbook`, `cashbook`, `exprep`, `transfer`,
                                                        `purreg`, `salreg`, `inwreg`, `outreg`, `debreg`, `credreg`, `damreg`,  `credothreg`, `excreg`, `tranreg`, `payablerep`, `bankrecon`,
                                                        `closestk`, `trbal`, `profloss`, `profshar`, `balsheet`,`credoth`, salevat, salevatreg, crednotevat, crednotevatreg)
                                        VALUES($qvouch, '$user', '$password', '$usertype', '$customer', '$supplier', '$prodgrp', '$product',  '$openbal', '$chcustdtl','$transporter',
                                                         '$bank', '$cash', '$expense', '$group', '$otherac', 
                                                         '$purchase', '$sale', '$debnote', '$crednote', '$inward','$outward', '$damaged', '$excess', 
                                                         '$bankrecp', '$bankpay', '$cashrecp', '$cashpay', '$journal','$contra',
                                                         '$stock', '$inouthist', '$vat', '$ledger', '$itemwisepl', '$partywisesale',
                                                         '$itemwisesl', '$itemwisegrpsl', '$stockmov', '$leadparty', '$leadprod', '$dailyrep', '$dailycash',
                                                         '$bankbook', '$cashbook', '$exprep', '$transfer',
                                                         '$purreg', '$salreg', '$inwreg', '$outreg', '$debreg', '$credreg', '$damreg', '$credothreg', '$excreg', '$tranreg', '$payablerep', '$bankrecon',
                                                         '$closestk', '$trbal', '$profloss', '$profshar', '$balsheet','$credoth', '$salevat', '$salevatreg', '$crednotevat', '$crednotevatreg')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch;
            header("refresh: 1;$file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;

            $usecode = mysqli_query($conn, "SELECT code FROM `passmain` WHERE user = '$username'") or die(mysqli_error($conn));
            $getuser = mysqli_fetch_object($usecode);
            $code = $getuser->code;

            mysqli_query($conn, "UPDATE `$maindb` SET `user` = '$user', `password` = '$password', `usertype` = '$usertype', `customer` = '$customer', crednotevat='$crednotevat', crednotevatreg='$crednotevatreg',
                                                 `supplier` = '$supplier', `prodgrp` = '$prodgrp', `product`='$product',`openbal`='$openbal', `chcustdtl`='$chcustdtl', `transporter`='$transporter',
                                                 `bank` = '$bank', `cash` = '$cash', `expense` = '$expense', `group`='$group', `otherac`='$otherac', salevat='$salevat', salevatreg='$salevatreg', 
                                                 `purchase` = '$purchase', `sale` = '$sale', `debnote` = '$debnote', `crednote`='$crednote', `inward`='$inward', `outward`='$outward', `damaged`='$damaged',`excess`='$excess',
                                                 `bankrecp` = '$bankrecp', `bankpay` = '$bankpay', `cashrecp` = '$cashrecp',  `cashpay`='$cashpay', `journal`='$journal', `contra`='$contra',
                                                 `stock` = '$stock', `inouthist`='$inouthist', `vat` = '$vat',  `ledger`='$ledger', `itemwisepl`='$itemwisepl', `itemwisesl`='$itemwisesl', `itemwisegrpsl`='$itemwisegrpsl', `stockmov`='$stockmov',
                                                 `leadparty` = '$leadparty', `leadprod` = '$leadprod', `dailyrep` = '$dailyrep', `dailycash` = '$dailycash',  `bankbook`='$bankbook', `cashbook`='$cashbook', `exprep`='$exprep', `transfer`='$transfer',
                                                 `purreg` = '$purreg', `salreg` = '$salreg', `inwreg` = '$inwreg',  `outreg`='$outreg', `debreg`='$debreg', `credreg`='$credreg', `credothreg`='$credothreg', `damreg`='$damreg', `excreg`='$excreg',`tranreg`='$tranreg',
                                                 `payablerep`='$payablerep', `bankrecon`='$bankrecon',  `partywisesale`='$partywisesale',
                                                 `closestk` = '$closestk',`trbal`='$trbal',`profloss`='$profloss', `profshar`='$profshar', `balsheet`='$balsheet',`credoth`='$credoth' WHERE code = '$mvouch'") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1;$file?$keyfld=$mvouch&mode=edit");
        }
    }
}

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);
}

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

/* DELETE */
if (isset($_GET["delete"])) {
    delete("$zroot", "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete("$zroot", "$trandb", "$keyfld2", $_GET[$keyfld], "$file?mode=last");
}
?>
<?php include("includes/linkshead.php"); ?>
<style>
    input[type=checkbox] {
        width: 20px;
        height: 20px;
    }
</style>
<script src="MyScript/user.js" type="text/javascript"></script>

<body>
    <!-- Default box -->
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="">
            <div class="col-lg-6">
                <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">
                            <label class="col-sm-3 control-label marg">User Name</label>
                            <div class="col-sm-6">
                                <input type="text" class="form-control form" name="user" placeholder="User Name" autocomplete="off" value='<? echo ($addflag != 1) ? $get['user'] : ''; ?>' required="" autofocus>
                            </div>
                        </div>
                        <div class="row">
                            <label class="col-sm-3 control-label marg">Password</label>
                            <div class="col-sm-6">
                                <input type="password" class="form-control form" name="password" id="password" autocomplete="off" placeholder="Password" value='<? echo ($addflag != 1) ? $get['password'] : ''; ?>' required="">
                            </div>
                        </div>
                        <div class="row">
                            <label class="col-sm-3 control-label marg">Confirm Pass</label>
                            <div class="col-sm-6">
                                <input type="password" name="confpass" id="confpass" class="form-control form" placeholder="Confirm Password" required="" value='<? echo ($addflag != 1) ? $get['password'] : ''; ?>'>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-6">
                                <p id="alert"></p>
                            </div>
                        </div>
                        <div class="row">
                            <label class="col-sm-3 control-label marg">User Type</label>
                            <div class="col-sm-6">

                                <select class='form-control select2 form' name="usertype" id="usertype" required>
                                    <?php
                                    if ($addflag != 1) {
                                        $usertype = "";
                                        if ($get['usertype'] == 1) {
                                            $ucode = "1";
                                            $usertype = "Supervisor";
                                        }
                                        if ($get['usertype'] == 2) {
                                            $ucode = "2";
                                            $usertype = "Admin";
                                        }
                                        if ($get['usertype'] == 3) {
                                            $ucode = "3";
                                            $usertype = "Staff";
                                        }
                                        echo "<option value='$ucode'>$usertype</option>";
                                    }
                                    ?>
                                    <option value="1">Supervisor</option>
                                    <option value="2">Admin</option>
                                    <option value="3">Staff</option>
                                </select>
                            </div>
                        </div>
                        <div class="nav-tabs-custom">
                            <ul class="nav nav-tabs">
                                <li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="true">Masters</a></li>
                                <li class=""><a href="#tab_2" data-toggle="tab" aria-expanded="false">Transactions</a></li>
                                <li class=""><a href="#tab_3" data-toggle="tab" aria-expanded="false">Reports</a></li>
                                <li class=""><a href="#tab_4" data-toggle="tab" aria-expanded="false">Registers</a></li>
                            </ul>
                            <div class="tab-content">
                                <div class="tab-pane active" id="tab_1">
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg selectall">Select All/UnSelect All</label>
                                        <div class="col-sm-4" style="margin-top:6px">
                                            <input type="checkbox" class="form " id="Smasters" value="1">
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Customer</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  masters customer" id="customer" name="customer" value="1" <?
                                            if ($get['customer'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                        <label class="col-sm-4 control-label marg">Product</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  masters" id="product" name="product" value="1" <?
                                            if ($get["product"] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                    
                                </div><!-- /.tab-pane -->
                                    

                                <div class="tab-pane" id="tab_2">
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg selectall">Select All/UnSelect All</label>
                                        <div class="col-sm-4" style="margin-top:6px">
                                            <input type="checkbox" class="form transactions" id="Stransactions" value="1">
                                        </div>
                                    </div>
                                       
                                                                                                       
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Sale</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  transactions" id="sale" name="sale" value="1" <?
                                            if ($get['sale'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                        
                                        <label class="col-sm-4 control-label marg">Credit Note</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  transactions" id="crednote" name="crednote" value="1" <?
                                            if ($get['crednote'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                    
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Purchase</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  transactions" id="purchase" name="purchase" value="1" <?
                                            if ($get['purchase'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                        
                                        <label class="col-sm-4 control-label marg">Debit Note</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  transactions" id="debnote" name="debnote" value="1" <?
                                            if ($get['debnote'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                    
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Inward</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  transactions" id="inward" name="inward" value="1" <?
                                            if ($get['inward'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                        <label class="col-sm-4 control-label marg">Outward</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  transactions" id="outward" name="outward" value="1" <?
                                            if ($get['outward'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>                                                                      

                                </div><!-- /.tab-pane -->

                                <div class="tab-pane" id="tab_3">
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg selectall">Select All/UnSelect All</label>
                                        <div class="col-sm-4" style="margin-top:6px">
                                            <input type="checkbox" class="form " id="Sreports" value="1">
                                        </div>
                                    </div>
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Stock</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  reports" id="stock" name="stock" value="1" <?
                                            if ($get['stock'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                        <label class="col-sm-4 control-label marg">Product In/Out History</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  reports" id="inouthist" name="inouthist" value="1" <?
                                            if ($get['inouthist'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                    
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Negative Stock</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  reports" id="negstock" name="negstock" value="1" <?
                                            if ($get['negstock'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                        <label class="col-sm-4 control-label marg">Itemwise Sale</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  reports" id="itemwisesl" name="itemwisesl" value="1" <?
                                            if ($get['itemwisesl'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                    
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Partywise Sale</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  reports" id="partywisesale" name="partywisesale" value="1" <?
                                            if ($get['partywisesale'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>

                                    
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Leading Products</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  reports" id="leadprod" name="leadprod" value="1" <?
                                            if ($get['leadprod'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                        <label class="col-sm-4 control-label marg">Lead Parties</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  reports" id="leadparty" name="leadparty" value="1" <?
                                            if ($get['leadparty'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                   
                                  
                                </div><!-- /.tab-pane-->

                                <div class="tab-pane" id="tab_4">
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg selectall">Select All/UnSelect All</label>
                                        <div class="col-sm-4" style="margin-top:6px">
                                            <input type="checkbox" class="form " id="Sregisters" value="1">
                                        </div>
                                    </div>

                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Sale Register</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  registers" id="salreg" name="salreg" value="1" <?
                                            if ($get['salreg'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div> 
                                        
                                        <label class="col-sm-4 control-label marg">Credit Note Register</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  registers" id="credreg" name="credreg" value="1" <?
                                            if ($get['credreg'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                    
                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Purchase Register</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  registers" id="purreg" name="purreg" value="1" <?
                                            if ($get['purreg'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div> 
                                        
                                        <label class="col-sm-4 control-label marg">Debit Note Register</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  registers" id="debreg" name="debreg" value="1" <?
                                            if ($get['debreg'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>

                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Inward Register</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  registers" id="inwreg" name="inwreg" value="1" <?
                                            if ($get['inwreg'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                        <label class="col-sm-4 control-label marg">Outward Register</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  registers" id="outreg" name="outreg" value="1" <?
                                            if ($get['outreg'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>

                                    <div class="row">
                                        <label class="col-sm-4 control-label marg">Damaged Register</label>
                                        <div class="col-sm-2" style="margin-top:6px">
                                            <input type="checkbox" class="form  registers" id="damreg" name="damreg" value="1" <?
                                            if ($get['damreg'] == 1) {
                                                echo "checked";
                                            }
                                            ?>>
                                        </div>
                                    </div>
                                </div>

                            </div>
                        </div><!-- /.tab-content -->
                    </div>
                </div>
                <div class="panel-footer">
                    <?php include("includes/buttons.php"); ?>
                </div>
            </div>
        </div>
        <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12 pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Year Details</h3>

                </div>
                <div class="panel-body">
                    <div class="table-responsive">
                        <table class="table table-striped table-condensed table-bordered" width="100%">
                            <thead class="mySearchTr">
                                <tr>
                                    <th width="3%"></th>
                                    <th width="10%">Year</th>
                                    <th width="10%">Name</th>
                                    <th width="10%">From</th>
                                    <th width="10%">To</th>
                                </tr>
                            </thead>
                            <tbody class="tbody">
                                <?php
                                if ($addflag != 1) {
                                    $ctr = 0;
                                    $gettran = mysqli_query($conn, "SELECT `cono`,`finyear`,`startdate`,`lastdate`,`yearname` FROM `usercomp` WHERE `username`='$get[code]' ORDER BY `cono`") or die(mysqli_error($conn));
                                    while ($tran = mysqli_fetch_array($gettran)) {

                                        $fetyear = mysqli_query($conn, "SELECT `finyear` FROM `compyear` WHERE `finyear`='$tran[finyear]'") or die(mysqli_error($conn));
                                        $fyear = mysqli_fetch_array($fetyear);

                                        echo "<tr>";
                                        echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                        echo "<td>";
                                        echo "<select class='form-control form finyear select2' name='finyear_$ctr' style='width:100%'>";
                                        echo "<option value='$fyear[finyear]'>$fyear[finyear]</option>";
                                        $gyear = mysqli_query($conn, "SELECT `finyear` FROM `compyear`") or die(mysqli_error($conn));
                                        while ($year = mysqli_fetch_array($gyear)) {
                                            echo "<option value='$year[finyear]'>$year[finyear]</option>";
                                        }
                                        echo "</select>";
                                        echo "</td>";
                                        echo "<td><input type='text' class='form-control form yearname' name='yearname_$ctr' value='$tran[yearname]' readonly></td>";
                                        echo "<td><input type='text' class='form-control form startdate' name='startdate_$ctr' value='$tran[startdate]' readonly></td>";
                                        echo "<td><input type='text' class='form-control form lastdate' name='lastdate_$ctr' value='$tran[lastdate]' readonly></td>";
                                        echo "</tr >";
                                        $ctr++;
                                    }
                                } else {
                                    ?>
                                    <tr>
                                        <td><i class='fa fa-close remove'></i></td>
                                        <td>
                                            <select class="form-control form finyear select2" name="finyear_0" style='width:100%' required>
                                                <option value=""></option>
                                                <?php
                                                $gyear = mysqli_query($conn, "SELECT `finyear` FROM `compyear`") or die(mysqli_error($conn));
                                                while ($year = mysqli_fetch_array($gyear)) {
                                                    echo "<option value='$year[finyear]'>$year[finyear]</option>";
                                                }
                                                ?>
                                            </select>
                                        </td>
                                        <td><input type="text" class="form-control form yearname" name="yearname_0" value="" readonly></td>
                                        <td><input type="text" class="form-control form startdate" name="startdate_0" value="" readonly></td>
                                        <td><input type="text" class="form-control form lastdate" name="lastdate_0" value="" readonly></td>
                                    </tr>
                                    <?
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="col-lg-12 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>
        </div>
    </div>
</form>
<div class="modal fade bs-example-modal-lg" 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=" table-responsive">
                    <table id="example1" class="table table-striped">
                        <thead>
                            <tr class="mySearchTr">
                                <!--<th>Code</th>-->
                                <th style="width:90%">Name</th>
                                <th style="width:10%">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            if ($addflag != 1) {
                                $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb`") or die(mysqli_error($conn));
                                while ($row = mysqli_fetch_array($srchresult)) {
                                    echo "<tr>";
//                                    echo "<td align='left'>$row[code]</td>";
                                    echo "<td align='left'>$row[user]</td>";
                                    echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                    echo "</tr>";
                                }
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer"></div>
        </div>
    </div>
</div>
</body>

</html>
<?php include("includes/linksfoot.php"); ?>