<?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();
}
$name = "";
$description = "";
$unit = "";
$availability = "";
$status = "";
?>
<!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>Raw Material Setup</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="ing_stock_setup.php?view">History <i-->
<!--                            class="mdi-content-add-box" style="font-size:12px;"></i></a>-->
                    <a class="btn waves-effect waves-light" style="float:right;margin-left:5px;" href="?add">Add <i
                            class="mdi-content-add-box"></i></a>
                    <div class="clearfix"></div>
                <?php }
            } ?>
            <br/>
            <?php
            if (isset($_GET['add'])) {
            if (isset($_POST) && !empty($_POST)) {
                extract($_REQUEST);
                $data['name'] = $name;
                $data['description'] = $description;
                $data['unit'] = $unit;
                $data['availability'] = $availability;
                $data['datetime'] = 'NOW()';
                $data['status'] = $status;
                $usercheck = IngSetup::CheckIng($name, 0);

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

                if ((strlen($name) == 0) || (strlen($name) > 255))
                    array_push($errors, "Please enter ingredient name");
                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, "ingredient name already exist");
            if (count($errors) == 0) {
                $q = IngSetup::AddIngSetup($data);

                if ($availability != 0) {
                    $dataStock['ingredient_id'] = $q;
                    $dataStock['qty'] = $availability;
                    $dataStock['user_id'] = $_SESSION['sess_userid'];
                    $dataStock['datetime'] = 'NOW()';
                    ProductionStock::AddProductionStockSetup($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="ing_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">Name *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <textarea class="materialize-textarea" id="description" name="description"><?php echo $description; ?></textarea>
                                        <label for="description" class="control-label">Description</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <select style="width: 55%; float: left; margin-left: 5%;"
                                                class="form-control" id="unit" name="unit">
                                            <option></option>
                                            <option value="Ltr" <?php if ($unit == 'Ltr') {
                                                echo 'selected';
                                            } ?> >Liter
                                            </option>
                                            <option value="kg" <?php if ($unit == 'kg') {
                                                echo 'selected';
                                            } ?> >Kilogram
                                            </option>
                                            <option value="Tin" <?php if ($unit == 'Tin') {
                                                echo 'selected';
                                            } ?> >Tin
                                            </option>
                                            <option value="Pcs" <?php if ($unit == 'Pcs') {
                                                echo 'selected';
                                            } ?> >Pieces
                                            </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="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='ing_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>Name</th>
        <th>Description</th>
        <th>Availability</th>
        <th>AVG Rate</th>
    </tr>
</thead>
<tfoot>
    <tr>
        <th>Name</th>
        <th>Description</th>
        <th>Availability</th>
        <th>AVG Rate</th>
    </tr>
</tfoot>
<tbody>
<?php
$qry = IngSetup::GetIngSetupList();
if ($qry) {
    $countingSr = 1;
    foreach ($qry as $qery) { ?>
        <tr>
            <!-- Remove commented-out code that was here before -->
            <td><?php echo ucwords($qery->name); ?></td>
            <td><?php echo $qery->description; ?></td> <!-- New column for description -->
            <td><?php echo $qery->availability; ?></td>
            <td><?php echo $qery->avg_rate; ?></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['description'] = $description;
                $data['unit'] = $unit;
                $data['status'] = $status;
                $data['datetime'] = 'NOW()';
                $usercheck = IngSetup::CheckIng($name, $id);

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

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

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

                    $q = IngSetup::UpdateIngSetup($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 = IngSetup::GetIngSetupById($id);
                    $name = $qry->name;
                    $description = $qry->description;
                    $unit = $qry->unit;
                    $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">Name:</div>
                                <div class="col s6"><?php echo $name; ?></div>
                            </div>
                            <div class="row">
                                <div class="col s6">Description:</div>
                                <div class="col s6"><?php echo $description; ?></div>
                            </div>
                            <div class="row">
                                <div class="col s6">Unit:</div>
                                <div class="col s6"><?php echo $unit; ?></div>
                            </div>
                            <div class="row">
                                <div class="col s6">Availability:</div>
                                <div class="col s6"><?php echo $availability; ?></div>
                            </div>
                            <div class="row">
                                <div class="col s6">Status:</div>
                                <div class="col s6">
                                    <?php
                                    if ($status == 'active') {
                                        echo "Active";
                                    } else if ($status == 'inactive') {
                                        echo "Inactive";
                                    } ?>
                                </div>
                            </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="ing_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">Name *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <textarea class="materialize-textarea" id="description" name="description"><?php echo $description; ?></textarea>
                                            <label for="weight" class="control-label">Description </label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <select style="width: 55%; float: left; margin-left: 5%;"
                                                    class="form-control" id="unit" name="unit">
                                                <option></option>
                                                <option value="Ltr" <?php if ($unit == 'Ltr') {
                                                    echo 'selected';
                                                } ?> >Liter
                                                </option>
                                                <option value="kg" <?php if ($unit == 'Kg') {
                                                    echo 'selected';
                                                } ?> >Kilogram
                                                </option>
                                                <option value="Tin" <?php if ($unit == 'Tin') {
                                                    echo 'selected';
                                                } ?> >Tin
                                                </option>
                                                <option value="Pcs" <?php if ($unit == 'Pcs') {
                                                    echo 'selected';
                                                } ?> >Pieces
                                                </option>
                                            </select>
                                            <label for="unit" class="control-label">Unit</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='ing_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>