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

$formname = 'Item Details';
$formname2 = 'Repair History';

$maindb = 'item';
$trandb = 'itemtran';

$printfile = "item_print.php";
$file = 'item.php';

$keyfld = "code";
$keyfld2 = "itemcode";

$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;

    $code = $_POST["code"];
    $name = $_POST["name"];
    $openqty = $_POST["openqty"];
    $accode = $_POST["accode"];
    $add1 = $_POST["add1"];
    $add2 = $_POST["add2"];
    $add3 = $_POST["add3"];
    $city = $_POST["city"];
    $pin = $_POST["pin"];
    $mobile = $_POST["mobile"];
    $tel = $_POST["tel"];
    $fax = $_POST["fax"];
    $purchdt = $_POST["purchdt"];
    $cost = $_POST["cost"];
    $remark = $_POST["remark"];
    $count = $_POST["items"];

    $mvouch = $qvouch;


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


    if ($a == 1) {


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

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

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

            $tsrno = $_POST["srno_$i"];
            $tdate = $_POST["date_$i"];
            $taccode = $_POST["accode_$i"];
            $tcost = $_POST["repacost_$i"];
            $tremark = $_POST["remark_$i"];

            if (!empty($taccode)) {
                mysqli_query($conn,"INSERT INTO `$trandb`( `itemcode`,`srno`, `date`, `accode`, `repacost`, `remark`)
                                         VALUES( '$qvouch','$tsrno', '$tdate','$taccode', '$tcost','$tremark')") or die(mysqli_error($conn));
            }
        }





        if ($addflag == 1) {
            mysqli_query($conn,"INSERT INTO $maindb( `code`, `name`, `openqty`, `accode`, `add1`, `add2`, `add3`, `city`, `pin`, `mobile`, `tel`, `fax`, `purchdt`, `cost`, `remark`) 
                            VALUES ('$qvouch', '$name', '$openqty', '$accode', '$add1', '$add2', '$add3', '$city', '$pin', '$mobile', '$tel', '$fax', '$purchdt', '$cost', '$remark')") 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', `openqty`='$openqty', `accode`='$accode', `add1`='$add1', `add2`='$add2',
                    `add3`='$add3',`city`='$city',`pin`='$pin',`mobile`='$mobile',`tel`='$tel',`fax`='$fax',`purchdt`='$purchdt',`cost`='$cost',`remark`='$remark' 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);
//    print table($result);

    $gparty3 = mysqli_query($conn,"SELECT `name`,`accode` FROM `accmast` WHERE `accode`= '$get[accode]' order by name  ")or die(mysqli_error($conn));
    $sparty3 = mysqli_fetch_array($gparty3);
}

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



if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb", "$keyfld2", $_GET[$keyfld], "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script src="MyScript/item.js" type="text/javascript"></script>
<script src="MyScript/item_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">Item</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form focusfield" 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">Opening Qty.</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="number" class="form-control form text-right" name="openqty" value="<?php Echo ($addflag != 1) ? $get["openqty"] : 0; ?>" required>
                            </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">Customer Name</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-12 col-xs-12 pad">                                
                                <select name="accode" class=" form-control form select2 party custname">
                                    <option value=" <?php echo ($addflag != 1) ? $sparty3['accode'] : ""; ?>"><?php echo ($addflag != 1) ? $sparty3['name'] : ""; ?></option>
                                    <?php
                                    $gparty2 = mysqli_query($conn,"SELECT `name`,`accode` FROM `accmast` WHERE grpcode = '010' order by name  ")or die(mysqli_error($conn));
                                    while ($sparty2 = mysqli_fetch_array($gparty2)) {
                                        echo"<option value='$sparty2[accode]'>$sparty2[name]</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">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 add1" name="add1" value="<?php Echo ($addflag != 1) ? $get["add1"] : ""; ?>" readonly="">
                                <input type="text" class="form-control form add2" name="add2" value="<?php Echo ($addflag != 1) ? $get["add2"] : ""; ?>" readonly="">
                                <input type="text" class="form-control form add3" name="add3" value="<?php Echo ($addflag != 1) ? $get["add3"] : ""; ?>"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">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-8 col-md-8 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>
                        <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">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 class="col-lg-2 col-md-2 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Fax</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="fax" value="<?php Echo ($addflag != 1) ? $get["fax"] : ""; ?>" >
                            </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">Purchase Date</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="purchdt" value="<?php Echo ($addflag != 1) ? $get["purchdt"] : ""; ?>" >
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Purchase Cost</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="number" class="form-control form  text-right" name="cost" value="<?php Echo ($addflag != 1) ? $get["cost"] : ""; ?>" >
                            </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">Remark</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="remark" value="<?php Echo ($addflag != 1) ? $get["remark"] : ""; ?>" >
                            </div>
                        </div>
                    </div>

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

            <div class="col-lg-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">
                        <table class="table table-striped table-condensed table-bordered" style="width:100%">
                            <thead style="background-color:#64CCFC;" >
                                <tr>
                                    <th style="width:3%;"></th>
                                    <th style="width:5%;">Sr</th>
                                    <th style="width:5%;">Date</th>
                                    <th style="width:20%;">Party</th>
                                    <th style="width:10%;">Repair Cost</th>
                                    <th style="width:20%;">Remark</th>
                                </tr>
                            </thead>
                            <tbody class="tbody"  id="data">

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

                                        $getparty3 = mysqli_query($conn,"SELECT `name`,`accode` FROM `accmast` WHERE `accode`= '$tran[accode]' order by name ") or die(mysqli_error($conn));
                                        $showparty3 = mysqli_fetch_array($getparty3);

                                        Echo "<tr>";
                                        Echo "<td style='width:3%;'> <i class='fa fa-close remove' style='display: none;'></i> </td>";
                                        Echo "<td style='width:5%;'> <input type='text' class='form-control  form srno' name='srno_$ctr' value='$tran[srno]'> </td>";
                                        Echo "<td style='width:5%;'> <input type='date' class='form-control form date'  name='date_$ctr' value='$tran[date]'> </td>";
                                        Echo "<td style='width:20%;'>";
                                        Echo "<select class='form-control form party ' name='accode_$ctr'>";
                                        Echo "<option value='$showparty3[accode]'>$showparty3[name]</option>";
                                        $getparty = mysqli_query($conn,"SELECT `name`,`accode` FROM `accmast` WHERE `grpcode`= '010' order by name ") or die(mysqli_error($conn));
                                        while ($showparty = mysqli_fetch_array($getparty)) {
                                            Echo "<option value='$showparty[accode]'>$showparty[name]</option>";
                                        }
                                        Echo "</select>";
                                        Echo "</td>";
                                        Echo "<td style='width:10%;'><input type='number' class='form-control form repacost text-right'         name='repacost_$ctr'   value='$tran[repacost]'></td>";
                                        Echo "<td style='width:20%;'><input type='text' class='form-control form remark'          name='remark_$ctr'    value='$tran[remark]'></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="date_<?= $pctr ?>"     class="form-control form date" value=""></td>
                                        <td>
                                            <select name="accode_<?= $pctr ?>" class=" form-control form select2 party">
                                                <option value=""></option>
                                                <?php
                                                $gparty = mysqli_query($conn,"SELECT `name`,`accode` FROM `accmast` WHERE grpcode = '010' order by name  ")or die(mysqli_error($conn));
                                                while ($sparty = mysqli_fetch_array($gparty)) {
                                                    echo"<option value='$sparty[accode]'>$sparty[name]</option>";
                                                }
                                                ?>
                                            </select>
                                        </td>
                                        <td><input type="number" name="repacost_<?= $pctr ?>"    class="form-control text-right form repcost" value=""></td>
                                        <td><input type="text" name="remark_<?= $pctr ?>"     class="form-control form remark" value=""></td>
                                    </tr>
                                    <?php
                                    $pctr++;
                                }
                                ?>
                            </tbody>
                        </table>
                    </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; ?>" />  
                    </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">Item</option>
                                <option value="2">Opening Qty</option>
                                <option value="3">Address</option>
                                <option value="4">City</option>
                                <option value="5">Mobile</option>
                                <option value="6">Purchase Date</option>
                                <option value="7">Purchase Cost</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">Item</option>
                                <option value="2">Opening Qty</option>
                                <option value="3">Address</option>
                                <option value="4">City</option>
                                <option value="5">Mobile</option>
                                <option value="6">Purchase Date</option>
                                <option value="7">Purchase Cost</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:5%">Sr.no</th>
                                    <th style="width:15%">Item</th>
                                    <th style="width:10%">Opening Qty</th>
                                    <th style="width:25%">Address</th>
                                    <th style="width:10%">City</th>
                                    <th style="width:10%">Mobile</th>
                                    <th style="width:10%">Purchase Date</th>
                                    <th style="width:10%">Purchase Cost</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>
