<?php
ob_start();
require("../includes/check_auth_user.php");
require("../includes/dbconnect.php");
include('../includes/function.php');
//include('MyImages/product/largest/image-resize/lib/ImageResize.php');

$formname = 'Product';
$maindb = 'product';
$trandb = 'prodtran';
//$trandb = 'proddetail';

$printfile = "prodprint.php";
$file = 'product.php';
$trandbexist = 0;
$keyfld = "code";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysql_select_db($zdb);

include("../includes/vouchno_generate.php");
include("../includes/prev&next.php");
if (isset($_POST["submit"])) {

    $a = 1;
    $name = mysql_real_escape_string(trim($_POST["name"]));
    $dname = $_POST["dname"];
    $type = $_POST["type"];
    $subtype = $_POST["subtype"];
    $cost = $_POST["cost"];
    $cost2 = $_POST["cost2"];
    $selling = $_POST["selling"];
    $descrip = $_POST["descrip"];
    $count = $_POST["items"];
    $newarrival = $_POST["newarrival"];


    $mvouch = $qvouch;

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

    if (isset($_POST["dname"])) {
        $result = mysql_query("SELECT * FROM `$maindb` WHERE `dname`= '$dname' AND code != '$qvouch'") or die(mysql_error());
        if (mysql_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: 1; $file?$keyfld=$qvouch&mode=last");
        }
    }

    if ($a == 1) {

        #################################TRAN PART #################################
//        mysql_query("DELETE FROM `$trandb` WHERE `code`='$mvouch'") or die(mysql_error());
//
//        for ($i = 0; $i < $count; $i++) {
//            $msrno = $_POST["srno_$i"];
//            $ttype = $_POST["ttype_$i"];
//
//            if (!empty($ttype)) {
//                $getname = mysql_query("SELECT * FROM `ttype` WHERE `code`='$ttype'")or die(mysql_error());
//                $typename = mysql_fetch_array($getname);
//                $tname = $typename[name];
//
//                mysql_query("INSERT INTO `$trandb`(`code`,`srno`, `ttype`, `tname`)
//                            VALUES('$mvouch', '$msrno','$ttype','$tname')") or die(mysql_error());
//            }
//        }

        $path1 = "MyImages/product/largest/";
        $path2 = "MyImages/product/large/";
        $path3 = "MyImages/product/small/";
        $path4 = "MyImages/product/sleft/";
        $path5 = "MyImages/product/sright/";

        #####################IMAGE#####################
        $filename1 = $mvouch . ".jpg";
        $filename2 = $mvouch . ".jpg";
        $filename3 = $mvouch . ".jpg";
        $filename4 = $mvouch . ".jpg";
        $filename5 = $mvouch . ".jpg";

        $img_file1 = $path1 . ($filename1);
        $img_file2 = $path2 . ($filename2);
        $img_file3 = $path3 . ($filename3);
        $img_file4 = $path4 . ($filename3);
        $img_file5 = $path5 . ($filename3);


        $image_ftype = pathinfo($img_file1, PATHINFO_EXTENSION);

        $size = $_FILES["image1"]["size"];
        $size2 = $_FILES["image2"]["size"];
        $size3 = $_FILES["image3"]["size"];
        $size4 = $_FILES["image4"]["size"];
        $size5 = $_FILES["image5"]["size"];

//        $image1 = new ImageResize($_FILES["image1"]["tmp_name"]);
//        $image1->crop(400, 400);
//        $image1->save($filename1);
//
//
//        $image2 = new ImageResize($_FILES["image2"]["tmp_name"]);
//        $image2->crop(300, 300);
//        $image2->save($filename3);
//
//
//        $image3 = new ImageResize($_FILES["image3"]["tmp_name"]);
//        $image3->crop(400, 400);
//        $image3->save($filename3);





        if ($size > 2048000 || $size2 > 1500000 || $size3 > 1500000 || $size4 > 1500000 || $size5 > 1500000) {
            $uploadOk = 0;
            $a = 0;
        } else {
            $uploadOk = 1;
            $a = 1;
        }



        if ($uploadOk == 1) {
            move_uploaded_file($_FILES["image1"]["tmp_name"], $img_file1);
            move_uploaded_file($_FILES["image2"]["tmp_name"], $img_file2);
            move_uploaded_file($_FILES["image3"]["tmp_name"], $img_file3);
            move_uploaded_file($_FILES["image4"]["tmp_name"], $img_file4);
            move_uploaded_file($_FILES["image5"]["tmp_name"], $img_file5);
        } else {
            browse("Sorry, there was an error uploading your file.(Check Size)");
            header("refresh: 1; $file?mode=last");
        }

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

        mysql_query("DELETE FROM `$trandb` WHERE `prodcode`='$mvouch'") or die(mysql_error());

        $srl = 0;
        for ($i = 0; $i < $count; $i++) {
            $srno = $_POST["srno_$i"];
            $size = $_POST["size_$i"];

            if (!empty($size)) {
                $srl++;
                mysql_query("INSERT INTO `$trandb`(`srno`, `prodcode`, `size`)
                                        VALUES( '$srl', '$qvouch', '$size')") or die(mysql_error());
            }
        }

        ############################## MAIN  ######################################################
        $getimage = mysql_query("SELECT * FROM `$maindb` WHERE `code`='$mvouch'")or die(mysql_error());
        $image = mysql_fetch_array($getimage);
        $imagepath = $image['largestimg'];

        ############################## MAIN  ######################################################
        if ($imagepath == "" && $a == 1) {
            mysql_query("INSERT INTO `$maindb`(code, `name`, `dname`, `type`,`subtype`, `cost`, `cost2`, `selling`, `descrip`,
                                              `largestimg`,`largeimg`,`smallimg`,`sleftimg`, `srightimg`, `newarrival`)
                                        VALUES($qvouch, '$name', '$dname', '$type','$subtype', '$cost', '$cost2', '$selling', '$descrip',
                                               '$img_file1', '$img_file2', '$img_file3','$img_file4','$img_file5','$newarrival')") or die(mysql_error());
            insmess();
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
            mysql_query("UPDATE `$maindb` SET `name`='$name', `dname`='$dname', `type`='$type',`subtype`='$subtype', `cost`='$cost', `cost2`='$cost2', `selling`='$selling',
                                              `largestimg`='$img_file1', `largeimg`='$img_file2', `smallimg`='$img_file3',`sleftimg`='$img_file4',`srightimg`='$img_file5',
                                              `newarrival`='$newarrival', `descrip`='$descrip' WHERE code = '$mvouch'") or die(mysql_error());
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

if ($addflag != 1) {

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

    $result = mysql_query("SELECT * FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysql_error());
    $get = mysql_fetch_array($result);

    $findtype = mysql_query("SELECT * FROM `type` WHERE `code`='$get[type]'") or die(mysql_error());
    $ftype = mysql_fetch_array($findtype);

    $findsubtype = mysql_query("SELECT * FROM `subtype` WHERE `code`='$get[subtype]'") or die(mysql_error());
    $fstype = mysql_fetch_array($findsubtype);

    $findbrand = mysql_query("SELECT * FROM `brand` WHERE `code`='$get[brand]'") or die(mysql_error());
    $fbrand = mysql_fetch_array($findbrand);

    $findttype = mysql_query("SELECT * FROM `ttype` WHERE `code`='$get[ttype]'") or die(mysql_error());
    $fttype = mysql_fetch_array($findttype);
}

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


if (isset($_GET["delete"])) {
    $getimg = mysql_query("SELECT largestimg,largeimg,smallimg, sleftimg, srightimg FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysql_error());
    $img = mysql_fetch_array($getimg);
    $largestimg = $img['largestimg'];
    $largeimg = $img['largeimg'];
    $smallimg = $img['smallimg'];
    unlink($largestimg);
    unlink($largeimg);
    unlink($smallimg);
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
//    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}

include("../includes/linkshead.php");
?>
<script src="MyScript/item.js" type="text/javascript"></script>
</head>
<body>
    <form action='' method='POST' enctype="multipart/form-data">
        <!-- Default box -->
        <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"><?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-4 col-md-4 col-sm-4 col-xs-4 numeric pad">
                                <label class="label-control marg">Code</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form" name="code" value="<?php Echo ($addflag != 1) ? $get["code"] : " $qvouch"; ?>" required readonly>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 numeric pad">
                                <label class="label-control marg">Name</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                <input type="text" class="form-control form item focusfield" name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" required >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 numeric pad">
                                <label class="label-control marg" style="color: #B9408F">Display Name</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                <input type="text" class="form-control form dname" name="dname" value="<?php Echo ($addflag != 1) ? $get["dname"] : ""; ?>" required>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 numeric pad">
                                <label class="label-control marg">Category</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <select class="form-control form " name="type" style='width:100%'>
                                    <option value="<?php echo ($addflag != 1) ? $ftype['code'] : ""; ?>"><? echo ($addflag != 1) ? $ftype['name'] : ""; ?></option>
                                    <?php
                                    $gtype = mysql_query("SELECT * FROM `type`")or die(mysql_error());
                                    while ($type = mysql_fetch_object($gtype)) {
                                        ?>
                                        <option value="<? Echo $type->code; ?>"><? Echo $type->name; ?></option>
                                    <? } ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 numeric pad">
                                <label class="label-control marg">Sub-Category</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <select class="form-control form " name="subtype" style='width:100%'>
                                    <option value="<?php echo ($addflag != 1) ? $fstype['code'] : ""; ?>"><? echo ($addflag != 1) ? $fstype['name'] : ""; ?></option>
                                    <?php
                                    $gstype = mysql_query("SELECT * FROM `subtype`")or die(mysql_error());
                                    while ($stype = mysql_fetch_object($gstype)) {
                                        ?>
                                        <option value="<? Echo $stype->code; ?>"><? Echo $stype->name; ?></option>
                                    <? } ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 numeric pad">
                                <label class="label-control marg">MRP 1</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="text" class="form-control form cost numeric" name="cost" value="<? Echo ($addflag != 1 ) ? $get['cost'] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 numeric pad">
                                <label class="label-control marg">MRP 2</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="text" class="form-control form cost numeric" name="cost2" value="<? Echo ($addflag != 1 ) ? $get['cost2'] : ""; ?>">
                            </div>
                        </div>
                        <!--                        <div class="row">
                                                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 numeric pad">
                                                        <label class="label-control marg">Selling</label>
                                                    </div>
                                                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                                        <input type="text" class="form-control form selling numeric" name="selling" value="<? // Echo ($addflag != 1 ) ? $get['selling'] : "";              ?>">                            </div>
                                                </div>-->
                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 numeric pad">
                                <label class="label-control marg">Description</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <textarea class="form-control form " name="descrip">
                                    <? Echo ($addflag != 1 ) ? $get['descrip'] : ""; ?>
                                </textarea>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Front Image<b>(1.5MB)</b></label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="file" class="btn btn-primary form image3" name="image3" id="image3">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Back Image<b>(1.5MB)</b></label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="file" class="btn btn-primary form image2" name="image2" id="image2">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Image Large<b>(2MB)</b></label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="file" class="btn btn-primary form image1" name="image1" id="image1">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg"><b>S Left (1.5MB)</b></label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="file" class="btn btn-primary form image4" name="image4" id="image4">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg"><b>S Right (1.5MB)</b></label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="file" class="btn btn-primary form image5" name="image5" id="image5">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">New Arrival</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad" >
                                <input type="checkbox"  class="form-control form newarrival" <?php
                                if ($get['newarrival'] == "1") {
                                    Echo "checked";
                                }
                                ?> value="1" name="newarrival" style="width: 50%">                            
                            </div>
                        </div>
                    </div>
                </div>
                <div class="panel-footer">
                    <?php include("../includes/buttons.php"); ?>
                </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">Size</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 table-padd" width="100%">
                                <thead  style="background-color: #B9408F">
                                    <tr>
                                        <th width="5%"></th>
                                        <th width="10%" class='text-right'>Sr.</th>
                                        <th width="85%">Size</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $tqty = 0;
                                        $tamt = 0;
                                        $gettran = mysql_query("SELECT * FROM `$trandb` WHERE `prodcode`='$get[code]' order by code")or die(mysql_error());
                                        while ($tran = mysql_fetch_array($gettran)) {

                                            $getitem1 = mysql_query("SELECT * FROM `size` WHERE `code`='$tran[size]' ")or die(mysql_error());
                                            $fitem = mysql_fetch_array($getitem1);


                                            Echo "<tr>";
                                            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            Echo "<td><input type='text' class='form-control form text-right srno'  name='srno_$ctr' value='$tran[srno]'></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form size select2' name='size_$ctr' style='width:100%' required>";
                                            Echo "<option value='$fitem[code]'>$fitem[name]</option>";
                                            $getsize = mysql_query("SELECT * FROM `size` ORDER BY name")or die(mysql_error());
                                            while ($size = mysql_fetch_array($getsize)) {
                                                Echo "<option value='$size[code]'>$size[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            Echo "</tr >";
                                            $ctr++;
                                        }
                                    } else {
                                        $pctr = 0;
//                                        $yctr = 1;
                                        ?>
                                        <tr>
                                            <td><i class="fa fa-close remove" style="display:none"></i></td> 
                                            <td><input type="text" class="form-control form text-right srno"      name="srno_<?= $pctr ?>"        value="1"></td> 
                                            <td>
                                                <select name="size_<?= $pctr ?>" class="form-control form select2 size">
                                                    <option value=""></option>
                                                    <?
                                                    $getsize = mysql_query("SELECT * FROM `size` ORDER BY name")or die(mysql_error());
                                                    while ($size = mysql_fetch_array($getsize)) {
                                                        Echo "<option value='$size[code]'>$size[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                        </tr>  
                                        <?php
                                    }
                                    $pctr++;
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="row">
                            <div class="col-lg-1" >
                                <button type="button" class="btn btn-square form addgen" style="background-color: #B9408F"  id="addnew">
                                    <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 : $pctr; ?>" />  
                            </div> 
                        </div> 
                    </div>
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h3 class="panel-title">Image</h3>
                            <div class="actions pull-right">
                                <i class="fa fa-chevron-down"></i>
                            </div>
                        </div>
                        <div class="panel-body">
                            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 pad">
                                <img class="view1" src="<?php Echo ($addflag != 1) ? $get['largestimg'] . "?a=" . mt_rand() : "" ?>" style="padding: 10px; border: #B9408F solid 1px;height: 200px;width: 200px" alt="Image 1" />
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 pad">
                                <img class="view2" src="<?php Echo ($addflag != 1) ? $get['largeimg'] . "?a=" . mt_rand() : "" ?>" style="padding: 10px; border: #B9408F solid 1px;height: 200px;width: 200px" alt="Image 2" />
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 pad">
                                <img class="view3" src="<?php Echo ($addflag != 1) ? $get['smallimg'] . "?a=" . mt_rand() : "" ?>" style="padding: 10px; border: #B9408F solid 1px;height: 200px;width: 200px" alt="Image 3" />
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 pad">
                                <img class="view3" src="<?php Echo ($addflag != 1) ? $get['sleftimg'] . "?a=" . mt_rand() : "" ?>" style="padding: 10px; border: #B9408F solid 1px;height: 200px;width: 200px" alt="Image 4" />
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 pad">
                                <img class="view3" src="<?php Echo ($addflag != 1) ? $get['srightimg'] . "?a=" . mt_rand() : "" ?>" style="padding: 10px; border: #B9408F solid 1px;height: 200px;width: 200px" alt="Image 5" />
                            </div>
                        </div>
                    </div>
                    <br>
                </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>Name</th>
                                    <th>Type</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysql_query("SELECT * FROM `$maindb` ORDER BY `code`")or die(mysql_error());
                                    while ($row = mysql_fetch_array($srchresult)) {
                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td align='center'>$row[code]</td>";
                                        echo "<td align='left'>$row[name]</td>";
                                        echo "<td align='left'>$row[type]</td>";
                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[code]\"><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>
    <?php include("../includes/linksfoot.php"); ?>
</body>
</html>
