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

$formname = 'Brand';
$maindb = 'brands';
//$trandb = 'proddetail';

$printfile = "branprint.php";
$file = 'brands.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 = $_POST["name"];
    $description = $_POST["description"];



    $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 ($a == 1) {

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


        $path1 = "MyImages/brand/";

        #####################IMAGE#####################

        $fileName = $mvouch . ".jpg";
//        browse($fileName);
        $img_file = $path1 . ($fileName);
//        browse($img_file);

        $fileTmpLoc = $_FILES["image1"]["tmp_name"]; // File in the PHP tmp folder
        $fileType = $_FILES["image1"]["type"]; // The type of file it is
        $fileSize = $_FILES["image1"]["size"]; // File size in bytes
        $fileErrorMsg = $_FILES["image1"]["error"]; // 0 for false... and 1 for true
//
//            echo "ERROR: Please browse for a file before clicking the upload button.";
//            exit();
//        }
//        browse($img_file);

        if (!empty($fileTmpLoc)) {
            if (move_uploaded_file($fileTmpLoc, "$img_file")) {
                browse("$fileName upload is complete");
            }
        }

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

        ############################## MAIN  ######################################################
        if ($imagepath == "") {
            mysql_query("INSERT INTO `$maindb`(code, `name`, `description`, `imgpath`)
                                        VALUES($qvouch, '$name', '$description', '$img_file')") 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', `description`='$description',`imgpath`='$img_file' 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);
}

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


if (isset($_GET["delete"])) {
    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-3 col-md-3 col-sm-3 col-xs-3 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-3 col-md-3 col-sm-3 col-xs-3 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 focusfield name"  name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" required>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                                <label class="label-control marg">Description</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-5 col-xs-5 pad">
                                <!--<input type="text" class="form-control form description" name="description" value="<?php Echo ($addflag != 1) ? $get["description"] : ""; ?>" required>-->
                                <textarea class="form-control form description" rows="5" col="6" name="description" id="description" maxlength="550" required><?php Echo ($addflag != 1) ? $get["description"] : ""; ?></textarea>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Image Largest</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>
                </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">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[imgpath] : "" ?>" style="padding: 10px; border: #B9408F solid 1px;height: 200px;width: 200px" alt="Image 1" />
                        </div>

                    </div>
                </div>

                <br>

            </div>
            <div class="col-lg-12 pad">
<?php include("../includes/buttons.php"); ?>
            </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>Unit</th>
                                    <th>Yarn</th>
                                    <th>Shade</th>
                                    <th>Quality</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'>$row[unit]</td>";
        echo "<td align='left'>$row[yarn]</td>";
        echo "<td align='left'>$row[shade]</td>";
        echo "<td align='left'>$row[quality]</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>
