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

$formname = 'Company';
$maindb = 'compmast';

$printfile = "company_mast_print.php";
$file = 'company_mast.php';

ini_set('display_errors', 1);
error_reporting(E_ERROR);

$trandbexist = 0;
$grid = '';
$keyfld = "code";
// $keycond = " AND `grpcode`='010'";
$keycond = "";
$focusfield = "name";
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");
$editdate = date("Y-m-d");
$edittime = date("h:i:sa");

$username = $_SESSION["username_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
// mysqli_select_db($conn, $zdb);
include("includes/code_generate.php");
include("includes/prev&next.php");


if (isset($_POST["submit"])) {

    $a = 1;
    $name = $_POST["name"];
    $add1 = mysqli_real_escape_string($conn, $_POST["add1"]);
    $add2 = mysqli_real_escape_string($conn, $_POST["add2"]);
    $add3 = mysqli_real_escape_string($conn, $_POST["add3"]);
    $city = $_POST["city"];
    $pin = $_POST["pin"];
    $coun = $_POST["country"];
    $tel = $_POST["tel"];
    $email = $_POST["email"];
    $web = $_POST["website"];
    $cpers1 = $_POST["contperson"];
    $insurance = $_POST["insurance"];
    $vat = $_POST["vatno"];
    $bankname = $_POST["bankname"];
    $acno = $_POST["acno"];
    $sortcode = $_POST["sortcode"];
    $ibanno = $_POST["ibanno"];
    $regnno = $_POST["regnno"];

    $mvouch = $qvouch;

    if ($name == "") {
        $a = 0;
        browse("Name Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?mode=last");
        }
    }

    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) {

        $path = "images/CompLogo/";
        $filename1 = $mvouch . ".jpg";
        $img_file1 = $path . ($filename1);

        $image_ftype = pathinfo($img_file1, PATHINFO_EXTENSION);
        $size = $_FILES['image']['size'];

        // Restrict file size to 2MB (2048000 bytes)
        if ($size <= 2048000) {
            move_uploaded_file($_FILES["image"]["tmp_name"], $img_file1);
        } else {
            browse("Image size should be less than 2MB. Please try again.");
            header("refresh: 2; $file?mode=last");
            exit();
        }

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`code`, `name`, `add1`, `add2`, `add3`, `city`, `pin`, `country`, `tel`,
                                                       `email`, `website`, `contperson`, `vatno`, `insurance`, `image`, `adduser`, `adddate`, `addtime`,
                                                       `bankname`, `acno`, `sortcode`, `ibanno`, `regnno`)
                                        VALUES('$qvouch','$name', '$add1', '$add2', '$add3', '$city', '$pin', '$coun', '$tel',
                                               '$email', '$web', '$cpers1', '$vat', '$insurance', '$img_file1', '$username', '$adddate', '$addtime',
                                               '$bankname', '$acno', '$sortcode', '$ibanno', '$regnno')") or die(mysqli_error($conn));
            //            insmess();
            $mvouch = $_GET["vouchno"];
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");

            //            header("refresh: 1; $file?mode=true");
        } else {
            $mvouch = $qvouch;
            $editdate = date("Y-m-d");
            $edittime = date("h:i:sa");
            mysqli_query($conn, "UPDATE `$maindb` SET `name`='$name', `add1`='$add1', `add2`='$add2', `add3`='$add3',
                                                      `city`='$city', `pin`='$pin', `country`='$coun',tel='$tel', `email`='$email', `website`='$web',
                                                      `contperson`='$cpers1', `vatno`='$vat', `insurance`='$insurance', image='$img_file1',
                                                      `bankname`='$bankname', `acno`='$acno', `sortcode`='$sortcode', `ibanno`='$ibanno', `regnno`='$regnno'
                                                      WHERE code = '$mvouch' ") or die(mysqli_error($conn));

            //            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
            //            header("refresh: 1; $grid");
        }
    }
}

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 = "";


// if (isset($_GET["delete"])) {
//     $getcustdtl = mysqli_query($conn, "SELECT * FROM `actran` WHERE `accode` = '$getvouch'") or die(mysqli_error($conn));
//     if (mysqli_num_rows($getcustdtl) > 0) {
//         browse("Entry Exist in Transaction");
//         header("refresh: 1; $file?mode=last");
//     } else {
//         delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
//     }
// }

include("includes/linkshead.php");
?>
<script src="MyScript/company_mast_search.js?a=<?= mt_rand() ?>" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#hadd").hide();
        $("#hdelete").hide();

        $(".image").change(function () {
            readURL1(this);
        });


        function readURL1(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $('.view1').attr('src', e.target.result);
                }

                reader.readAsDataURL(input.files[0]);
            }
        }

    });
</script>
<style>
    .upload-btn-wrapper {
        position: relative;
        overflow: hidden;
        display: inline-block;
    }

    .skbtn {
        border: 2px solid gray;
        color: gray;
        background-color: white;
        width: 150px;
        height: 40px;
        border-radius: 8px;
        font-size: 15px;
        font-weight: bold;
        margin-left: 30px;
    }

    .upload-btn-wrapper input[type=file] {
        font-size: 100px;
        position: absolute;
        left: 0;
        top: 0;
        opacity: 0;
    }

    .skimg {
        /* border: #E7B315 solid 1px; */
        width: 50%;
        height: auto;
        margin-left: 10px;
        margin-top: 30px;
        margin-bottom: 30px;
        border-radius: 6px;
        /* box-shadow: 4px 4px 5px 0 rgba(255, 255, 255, .5),
            -4px -4px 5px 0 rgba(116, 125, 136, .5),
            inset -4px -4px 5px 0 rgba(255, 255, 255, .2),
            inset 4px 4px 5px 0 rgba(0, 0, 0, .4); */
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    }
</style>
</head>

<body>
    <form action='' method='POST' enctype="multipart/form-data">
        <!-- Default box -->
        <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">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Name</label>
                        </div>
                        <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                            <input type="text" class="form-control form focusfield" 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 text-right pad">
                            <label class="label-control marg">Address</label>
                        </div>
                        <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                            <input type="text" class="form-control form" name="add1" value="<?php echo ($addflag != 1) ? $get["add1"] : ""; ?>">
                            <input type="text" class="form-control form" name="add2" value="<?php echo ($addflag != 1) ? $get["add2"] : ""; ?>">
                            <input type="text" class="form-control form" name="add3" value="<?php echo ($addflag != 1) ? $get["add3"] : ""; ?>">
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">City</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form" name="city" value="<?php echo ($addflag != 1) ? $get["city"] : ""; ?>">
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Pin</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="pin" value="<?php echo ($addflag != 1) ? $get["pin"] : ""; ?>">
                        </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">Country</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="country" value="<?php echo ($addflag != 1) ? $get["country"] : ""; ?>">
                        </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">Tel.</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="tel" value="<?php echo ($addflag != 1) ? $get["tel"] : ""; ?>">
                        </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">Email</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="email" value="<?php echo ($addflag != 1) ? $get["email"] : ""; ?>">
                        </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">Website</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="website" value="<?php echo ($addflag != 1) ? $get["website"] : ""; ?>">
                        </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">Contact Person</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="contperson"  value="<?php echo ($addflag != 1) ? $get["contperson"] : ""; ?>">
                        </div>
                    </div>

                    <hr>

                     
                    <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">Bank 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 " name="bankname" value="<?php Echo ($addflag != 1) ? $get["bankname"] : ""; ?>" >
                        </div> 
                    </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">Insurance No.</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form" name="insurance" value="<?php echo ($addflag != 1) ? $get["insurance"] : ""; ?>">
                        </div>
                 
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">VAT No.</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form " name="vatno" value="<?php echo ($addflag != 1) ? $get["vatno"] : ""; ?>">
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">Sort Code</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form" name="sortcode" value="<?php echo ($addflag != 1) ? $get["sortcode"] : ""; ?>">
                        </div>
                 
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">Account No.</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form " name="acno" value="<?php Echo ($addflag != 1) ? $get["acno"] : ""; ?>" >
                        </div>
                    </div>

                   
                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">IBAN No.</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form" name="ibanno" value="<?php echo ($addflag != 1) ? $get["ibanno"] : ""; ?>">
                        </div>
                 
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">Reg. No.</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form " name="regnno" value="<?php Echo ($addflag != 1) ? $get["regnno"] : ""; ?>" >
                        </div>
                    </div>

                </div>

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

        <div class="col-lg-5 pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Image Upload</h3>
                </div>

                <div class="panel-body">
                    <div class="row">
                        <div class="upload-btn-wrapper">
                            <button class="skbtn">Choose Image</button>
                            <input type="file" class="form image" name="image" accept="image/*" />
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-12">
                            <img class="view1 skimg"
                                src="<?php echo ($addflag != 1) ? $get['image'] . "?a=" . mt_rand() : "" ?>"
                                alt="Image" />
                            <!-- <img id="preview" src="<?php echo $imgpath; ?>" alt="your image" /> -->
                        </div>
                    </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 <? echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="">Sort By</option>
                                <option value="1">Name</option>
                                <option value="2">Address</option>
                                <option value="3">City</option>
                                <option value="4">Pin</option>
                                <option value="5">Telephone</option>
                                <option value="6">Insurance No</option>
                                <option value="7">VAT No</option>

                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Name</option>
                                <option value="2">Address</option>
                                <option value="3">City</option>
                                <option value="4">Pin</option>
                                <option value="5">Telephone</option>
                                <option value="6">Insurance No</option>
                                <option value="7">VAT No</option>

                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="asc">Asc</option>
                                <option value="desc">Desc</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">
                        <!--<img src="images/search.gif" alt="loading">-->
                    </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">
                                    <th width="20%">Name</th>
                                    <th width="30%">Address</th>
                                    <th width="13%">City</th>
                                    <th width="12%">Pin</th>
                                    <th width="10%">Telephone</th>
                                    <th width="12%">Insurance No</th>
                                    <th width="12%">VAT. No</th>
                                    <th 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>