<?php
include("../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}

if (isset($_POST) && !empty($_GET['delete'])) {
    $dataP['deleted'] = 1;
    ProductSetup::UpdateProductSetup($_GET['delete'],$dataP);
    header("Location: " . PROJECT_ADMIN_URL . "product_setup.php?view");
    exit();
}

$name = "";
$code = "";
$weight = "";
$unit = "";
$availability = "";
$status = "";
$tp_rate = "";
$carton_pieces = "";
$product_order = "";
$barcode = "";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('includes/inc_head.php'); ?>

</head>
<body>
<!-- Start Page Loading -->
<div id="loader-wrapper">
    <div id="loader"></div>
    <div class="loader-section section-left"></div>
    <div class="loader-section section-right"></div>
</div>
<!-- End Page Loading -->
<div id="container">
    <?php include('includes/inc_top.php'); ?>
    <div class="row">
        <div class="col s12 m12">
            <div class="">
                <h3>Product Setuppp</h3>
            </div>
            <hr>
            <?php if ($_SESSION['sess_role'] == 'VEC') {
                if (isset($_GET['view'])) { ?>
                    <a class="btn waves-effect waves-light" style="float:right;margin-left:5px;" href="?add">Add <i
                            class="mdi-content-add-box" style="font-size:12px;"></i></a>
                    <div class="clearfix"></div>
                <?php }
            } ?>
            <br/>
            <?php
            if (isset($_GET['add'])) {
            if (isset($_POST) && !empty($_POST)) {
                extract($_REQUEST);
                $data['code'] = __PRODUCT__;
                $data['name'] = $name;
                $data['weight'] = $weight;
                $data['unit'] = $unit;
                $data['barcode'] = $barcode;
                $data['tp_rate'] = $tp_rate;
                $data['carton_pieces'] = $carton_pieces;
                $data['product_order'] = $product_order;
                $data['availability'] = $availability;
                $data['status'] = $status;
                $data['datetime'] = 'NOW()';
                $usercheck = ProductSetup::CheckProduct($name, 0);

                $errors = array();
                // Validate the input

                if ((strlen($name) == 0) || (strlen($name) > 255))
                    array_push($errors, "Please enter product name");
                if ((strlen($weight) == 0))
                    array_push($errors, "Please enter value");
                if ((strlen($unit) == 0))
                    array_push($errors, "Please select unit");
                if ((strlen($availability) == 0))
                    array_push($errors, "Please enter available stock");
                if ($usercheck)
                    array_push($errors, "product name already exist");
            if (count($errors) == 0) {
                $q = ProductSetup::AddProductSetup($data);

                //Ledger Add Functionality
                $dataLedger['scheem_name'] = "product";
                $dataLedger['head'] = $q;
                $dataLedger['figure'] = 0;
                $dataLedger['datetime'] = 'NOW()';

                $led = Ledger::AddLedger($dataLedger);
                //Ledger Add Functionality

                if ($availability != 0) {
                    $dataStock['product_id'] = $q;
                    $dataStock['qty'] = $availability;
                    $dataStock['user_id'] = $_SESSION['sess_userid'];
                    $dataStock['datetime'] = 'NOW()';
                    Stock::AddStockSetup($dataStock);
                }
            if ($q) {
                $output = '';
                //header("Location: ?view&sucess"); ?>
                <script>window.location.href = '?view&sucess'</script>
            <?php
            echo "<div id='card-alert' class='card green lighten-5'>
                      <div class='card-content green-text'>
                        <p>SUCCESS : <strong>Attention! </strong>Submitted!</p>
                      </div>
                      <button type='button' class='close green-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
            } else {
                $output = '';
                echo "<div id='card-alert' class='card orange lighten-5'>
                      <div class='card-content orange-text'>
                        <p>WARNING : <strong>Attention! </strong>Some error occurred!</p>
                      </div>
                      <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
            }
            } else {
                //Prepare errors for output
                foreach ($errors as $val) {
                    $output .= "<div id='card-alert' class='card orange lighten-5'>
                      <div class='card-content orange-text'>
                        <p>WARNING : <strong>Attention! </strong>$val</p>
                      </div>
                      <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
                }
            }
            }
            ?>
                <form action="" method="post" id="product_setup_form" novalidate="novalidate">
                    <div class="row">
                        <div class="col s12 form-group">
                            <?php echo $output; ?>
                            <div class="col s8">
                                <div class="row margin">
                                    <span>(*) Mark Fields are mandatory</span><br/><br/>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="name" maxlength="250"
                                               name="name"
                                               value='<?php echo $name; ?>'/>
                                        <label for="name" class="control-label">Product Name *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s6">
                                        <input class="form-control" type="text"
                                               id="weight" maxlength="20"
                                               name="weight"
                                               value='<?php echo $weight; ?>'/>
                                        <label for="weight" class="control-label">Weight *</label>
                                    </div>
                                    <div class="input-field col s6">
                                        <select style="width: 55%; float: left; margin-left: 5%;"
                                                class="form-control" id="unit" name="unit">
                                            <option></option>
                                            <option value="gm" <?php if ($unit == 'gm') {
                                                echo 'selected';
                                            } ?> >gram
                                            </option>
                                            <option value="kg" <?php if ($unit == 'kg') {
                                                echo 'selected';
                                            } ?> >Kilogram
                                            </option>
                                            <option value="pc" <?php if ($unit == 'pc') {
                                                echo 'selected';
                                            } ?> >Piece
                                            </option>
                                        </select>
                                        <label for="unit" class="control-label">Unit</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text"
                                               id="tp_rate" maxlength="20"
                                               name="tp_rate"
                                               value='<?php echo $tp_rate; ?>'/>
                                        <label for="tp_rate" class="control-label">TP Rate</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text"
                                               id="carton_pieces" maxlength="20"
                                               name="carton_pieces"
                                               value='<?php echo $carton_pieces; ?>'/>
                                        <label for="carton_pieces" class="control-label">Pieces per unit carton</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text"
                                               id="product_order" maxlength="20"
                                               name="product_order"
                                               value='<?php echo $product_order; ?>'/>
                                        <label for="product_order" class="control-label">Product Order</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <label class="control-label">Barcode</label>
                                        <input class="form-control" type="text"
                                               id="barcode" maxlength="20"
                                               name="barcode"
                                               value='<?php echo $barcode; ?>'/>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="availability"
                                               name="availability"
                                               value='<?php echo $availability; ?>'/>
                                        <label for="availability" class="control-label">Availability *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div id="status" class="input-field col s12">
                                        <select class="form-control" name="status">
                                            <option value="active" <?php if ($status == 'active') {
                                                echo "selected='selected'";
                                            } ?>>Active
                                            </option>
                                            <option value="inactive" <?php if ($status == 'inactive') {
                                                echo "selected='selected'";
                                            } ?>>Inactive
                                            </option>
                                        </select>
                                        <label for="status" class="control-label">Status</label><br>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row margin">
                        <div id="status" class="input-field col s12">
                            <button id="btnsubmit" type="submit" class="btn waves-effect waves-light">Submit</button>
                            <button type="button" class="btn waves-effect waves-light"
                                    onclick="location.href='product_setup.php?view'">Cancel
                            </button>
                        </div>
                    </div>
                </form>

                <?php
            } // add close

            if (isset($_GET['view'])) {

                ?>
                <?php if (isset($_GET['sucess'])) { ?>
                    <div id='card-alert' class='card green lighten-5'>
                        <div class='card-content green-text'>
                            <p>Successfully updated!</p>
                        </div>
                        <button type='button' class='close green-text' data-dismiss='alert' aria-label='Close'>
                            <span aria-hidden='true'>x</span>
                        </button>
                    </div>
                <?php } ?>
                <?php if (isset($_GET['error'])) { ?>
                    <div id='card-alert' class='card orange lighten-5'>
                        <div class='card-content orange-text'>
                            <p>WARNING : <strong>Attention! </strong>Error occurred!</p>
                        </div>
                        <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                            <span aria-hidden='true'>x</span>
                        </button>
                    </div>
                <?php } ?>
                <div class="table-datatables">
                    <table id="data-table-simple" class="responsive-table display">
                        <thead>
                        <tr>
                            <th>Action</th>
                            <th>S.No</th>
                            <th>Product Name</th>
                            <th>Weight</th>
                            <th>TP Rate</th>
                            <th>Barcode</th>
                            <th>Availibility</th>
                            <th>Product order</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th>Action</th>
                            <th>S.No</th>
                            <th>Product Name</th>
                            <th>Weight</th>
                            <th>TP Rate</th>
                            <th>Barcode</th>
                            <th>Availibility</th>
                            <th>Product order</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                        </tfoot>
                        <tbody>
                        <?php
                        $filterP = " WHERE deleted = 0 ";
                        $qry = ProductSetup::GetProductSetupList($filterP);
                        if ($qry) {
                            $countingSr = 1;
                            foreach ($qry as $qery) { ?>
                                <tr>
                                    <td>
                                        <?php
                                        if ($_SESSION['sess_role'] == 'VEC' || $_SESSION['sess_role'] == 'VE') {
                                            if($qery->status != 'active'){
                                            ?>

                                            <a style="cursor:pointer;font-size: 10px;padding: 1px;" title="Delete">
                                                <span
                                                    style="width: 60px; height: 20px; border-radius: 5%; font-size: 12px; line-height: 20px;"
                                                    class="btn-warning-confirm<?= $qery->id ?> mini-stat-icon deep-orange darken-2"><i class="fa fa-arrows-alt">Delete</i></span>
                                            </a>
                                            <script>
                                                $('.btn-warning-confirm<?= $qery->id ?>').click(function () {
                                                    swal({
                                                            title: "Are you sure?",
                                                            text: "Are you sure you want to delete?",
                                                            type: "warning",
                                                            showCancelButton: true,
                                                            confirmButtonColor: '#DD6B55',
                                                            confirmButtonText: 'Yes',
                                                            closeOnConfirm: false
                                                        },
                                                        function () {
                                                            swal("Successful!", "Your product has been deleted!", "success");
                                                            window.location.href = 'product_setup.php?view&delete=<?php echo $qery->id; ?>';
                                                        });
                                                });
                                            </script>
                                            <?php } ?>

                                            <a href='?edit&id=<?php echo $qery->id; ?>&v' title="View">
                                    <span style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                          class="mini-stat-icon pink"><i class="mdi-action-description"></i></span>
                                            </a>
                                            <a href="?edit&id=<?php echo $qery->id; ?>" title="Edit">
                                    <span style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                          class="mini-stat-icon orange"><i class="mdi-image-edit"></i></span>
                                            </a>
                                        <?php } else { ?>
                                            <a href="?edit&id=<?php echo $qery->id; ?>&v" title="View">
                                    <span style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                          class="mini-stat-icon pink"><i class="mdi-action-description"></i></span>
                                            </a>
                                        <?php } ?>
                                    </td>
                                    <td><?php echo $countingSr; ?></td>
                                    <td><?php echo ucwords($qery->name); ?></td>
                                    <td><?php echo $qery->weight . " " . $qery->unit; ?></td>
                                    <td><?php echo $qery->tp_rate; ?></td>
                                    <td><?php echo $qery->barcode; ?></td>
                                    <td><?php echo $qery->availability; ?></td>
                                    <td><?php echo $qery->product_order; ?></td>
                                    <td><?= ($qery->status == 'active') ? ucwords($qery->status) : "Inactive"; ?></td>
                                    <td><?php echo $qery->datetime; ?></td>
                                </tr>
                                <?php
                                $countingSr++;
                            }
                        } ?>
                        </tbody>
                    </table>
                </div>
                <?php
            } // close view


            if (isset($_GET['edit']) && !empty($_POST)) {
                extract($_REQUEST);
                $id = $_GET['id'];
                $data['name'] = $name;
                $data['weight'] = $weight;
                $data['unit'] = $unit;
                $data['tp_rate'] = $tp_rate;
                $data['carton_pieces'] = $carton_pieces;
                $data['product_order'] = $product_order;
                $data['barcode'] = $barcode;
                $data['status'] = $status;
                $data['datetime'] = 'NOW()';
                $usercheck = ProductSetup::CheckProduct($name, $id);

                $errors = array();
                // Validate the input

                if ((strlen($name) == 0) || (strlen($name) > 255))
                    array_push($errors, "Please enter product name");
                if ((strlen($weight) == 0))
                    array_push($errors, "Please enter weight");
                if ((strlen($unit) == 0))
                    array_push($errors, "Please select unit");
                if ($usercheck)
                    array_push($errors, "product name already exist");

                if (count($errors) == 0) {

                    $led = Ledger::UpdateLedgerSetup($ledgreFigure->id, $dataLedger);
                    // Ledger Edit Functionality

                    $q = ProductSetup::UpdateProductSetup($id, $data);
                    if ($q) {
                        $output = '';
                        //header("Location: ?view&sucess"); ?>
                        <script>window.location.href = '?view&sucess'</script>
                        <?php
                        echo "<div id='card-alert' class='card green lighten-5'>
                      <div class='card-content green-text'>
                        <p>SUCCESS : <strong>Attention! </strong>Submitted!</p>
                      </div>
                      <button type='button' class='close green-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
                    } else {
                        $output = '';
                        echo "<div id='card-alert' class='card orange lighten-5'>
                      <div class='card-content orange-text'>
                        <p>WARNING : <strong>Attention! </strong>Some error occurred!</p>
                      </div>
                      <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
                    }
                } else {
                    //Prepare errors for output
                    foreach ($errors as $val) {
                        $output .= "<div id='card-alert' class='card orange lighten-5'>
                      <div class='card-content orange-text'>
                        <p>WARNING : <strong>Attention! </strong>$val</p>
                      </div>
                      <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
                    }
                }
            }
            if (isset($_GET['edit'])) {
                $id = $_GET['id'];
                if (empty($_POST)) {
                    $qry = ProductSetup::GetProductSetupById($id);
                    $name = $qry->name;
                    $code = $qry->code;
                    $weight = $qry->weight;
                    $unit = $qry->unit;
                    $barcode = $qry->barcode;
                    $tp_rate = $qry->tp_rate;
                    $carton_pieces = $qry->carton_pieces;
                    $product_order = $qry->product_order;
                    $availability = $qry->availability;
                    $status = $qry->status;
                }
                if ($_REQUEST['id'] && isset($_REQUEST['v'])) {
                    ?>
                    <style>
                        h4 {
                            float: left;
                            margin: 0;
                            width: 30%;
                        }

                        p {
                            float: left;
                        }

                        .activity-icon {
                            margin: 0;
                        }
                    </style>
                    <div class="row">
                    <!--  The form that will be parsed by jQuery before submit  -->
                    <div class="col s12 form-group">
                        <div class="col s8">
                            <div class="row">
                                <div class="col s6">Product Name:</div>
                                <div class="col s6"><?php echo $name; ?></div>
                            </div>
                            <div class="row">
                                <div class="col s6">Product Code:</div>
                                <div class="col s6"><?php echo $code; ?></div>
                            </div>
                            <div class="row">
                                <div class="col s6">Weight:</div>
                                <div class="col s6"><?php echo $weight . " " . $unit; ?></div>
                            </div>
                            <div class="row">
                                <div class="col s6">Availability:</div>
                                <div class="col s6"><?php echo $availability; ?></div>
                            </div>
                            <div class="row">
                                <h4>Status</h4>

                                <p>
                                    <?php
                                    if ($status == 'active') {
                                        echo "Active";
                                    } else if ($status == 'inactive') {
                                        echo "Inactive";
                                    } ?>
                                </p>
                            </div>
                        </div>
                    </div>
                <?php } else {
                    ?>
                    <div class="row">
                        <!--  The form that will be parsed by jQuery before submit  -->
                        <form action="?edit&id=<?php echo $id ?>" method="post" id="product_setup_form"
                              novalidate="novalidate">
                            <div class="col s12 form-group">
                                <div class="col s8">
                                    <div class="row">
                                        <?php echo $output; ?>
                                    </div>
                                    <div class="row">
                                        <span>(*) Mark Fields are mandatory</span><br/><br/>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="name" maxlength="250"
                                                   name="name"
                                                   value="<?php echo $name; ?>"/>
                                            <label for="name" class="control-label">Product Name *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s6">
                                            <input style="width: 40%; float: left;" class="form-control" type="text"
                                                   id="weight" maxlength="20"
                                                   name="weight"
                                                   value="<?php echo $weight; ?>"/>
                                            <label for="weight" class="control-label">Weight </label>
                                        </div>
                                        <div class="input-field col s6">
                                            <select style="width: 55%; float: left; margin-left: 5%;"
                                                    class="form-control" id="unit" name="unit">
                                                <option></option>
                                                <option value="gm" <?php if ($unit == 'gm') {
                                                    echo 'selected';
                                                } ?> >gram
                                                </option>
                                                <option value="kg" <?php if ($unit == 'kg') {
                                                    echo 'selected';
                                                } ?> >Kilogram
                                                </option>
                                                <option value="pc" <?php if ($unit == 'pc') {
                                                    echo 'selected';
                                                } ?> >Piece
                                                </option>
                                            </select>
                                            <label for="unit" class="control-label">Unit</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text"
                                                   id="tp_rate" maxlength="20"
                                                   name="tp_rate"
                                                   value='<?php echo $tp_rate; ?>'/>
                                            <label for="tp_rate" class="control-label">TP Rate</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text"
                                                   id="carton_pieces" maxlength="20"
                                                   name="carton_pieces"
                                                   value='<?php echo $carton_pieces; ?>'/>
                                            <label for="carton_pieces" class="control-label">Pieces per unit carton</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text"
                                                   id="product_order" maxlength="20"
                                                   name="product_order"
                                                   value='<?php echo $product_order; ?>'/>
                                            <label for="product_order" class="control-label">Product Order</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text"
                                                   id="barcode" maxlength="20"
                                                   name="barcode"
                                                   value='<?php echo $barcode; ?>'/>
                                            <label for="barcode" class="control-label">Barcode</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div id="status" class="input-field col s12">
                                            <select class="form-control" name="status">
                                                <option value="active" <?php if ($status == 'active') {
                                                    echo "selected='selected'";
                                                } ?>>Active
                                                </option>
                                                <option value="inactive" <?php if ($status == 'inactive') {
                                                    echo "selected='selected'";
                                                } ?>>Inactive
                                                </option>
                                            </select>
                                            <label for="status" class="control-label">Status</label>
                                        </div>
                                        <br/>
                                    </div>
                                    <div class="row margin">
                                        <div id="status" class="input-field col s12">
                                            <?php if ($_SESSION['sess_role'] == 'VE' || $_SESSION['sess_role'] == 'VEC') { ?>
                                                <button id="btnsubmit" type="submit"
                                                        class="btn waves-effect waves-light">
                                                    Submit
                                                </button>
                                            <?php } ?>
                                            <button type="button" class="btn waves-effect waves-light"
                                                    onclick="location.href='product_setup.php?view'">
                                                Cancel
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                <?php } ?>
                </div>
                <?php
            }
            ?>

        </div>
    </div>
</div>
</div>
<?php include('includes/inc_footer.php'); ?>
<?php include('includes/inc_js.php'); ?>
<script type="application/javascript">
    $(document).ready(function () {
        var fewSeconds = 2;
        $('#btnsubmit').click(function () {
            // Ajax request
            var btn = $(this);
            btn.hide();
            setTimeout(function () {
                btn.show();
            }, fewSeconds * 1000);
        });
    });
</script>
</body>
</html>