<?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 = "";
$per_unit_oil = "";
$per_unit_mung = "";
$per_unit_mash = "";
$per_unit_rice = "";
$per_unit_packets = "";
$status = "";
$batch_date = "";
if ($batch_date == "") {
    $batch_date = date('d-m-Y');
} else {
    $batch_date = date("d-m-Y", strtotime($batch_date));
}
if (isset($_POST) && !empty($_POST['production']) && $_POST['production'] == 1) {
    extract($_REQUEST);
    $batch = BatchSetup::GetBatchSetupById($batch_id);

    $data['batch_id'] = $batch_id;
    $data['batch_qty'] = $batch_qty;
    $data['batch_date'] = date('Y-m-d', strtotime($batch_date));
    $data['datetime'] = 'NOW()';
    $data['batch_name'] = $batch->name;

    $data['per_unit_oil'] = $batch->per_unit_oil;
    $data['per_unit_mung'] = $batch->per_unit_mung;
    $data['per_unit_mash'] = $batch->per_unit_mash;
    $data['per_unit_rice'] = $batch->per_unit_rice;
    $data['per_unit_packets'] = $batch->per_unit_packets;
    $data['status'] = "active";
    $q = ProductionSetup::AddProductionSetup($data);
    if ($q) {
        ?> <script>window.location.href = '?view&sucess'</script>
    <?php
    } else {
        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>";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
    <style>
        .modal .modal-footer{
            height: auto!important;
            width: auto!important;
        }
        .modal {
            height: 80%!important;
            max-height: 80%!important;
        }
    </style>
</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>Batch 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="?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['name'] = $name;
                $data['description'] = $description;
                $data['per_unit_oil'] = $per_unit_oil;
                $data['per_unit_mung'] = $per_unit_mung;
                $data['per_unit_mash'] = $per_unit_mash;
                $data['per_unit_rice'] = $per_unit_rice;
                $data['per_unit_packets'] = $per_unit_packets;
                $data['datetime'] = 'NOW()';
                $data['status'] = $status;
                $batchcheck = BatchSetup::CheckBatchAccount($name, 0);

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

                if ((strlen($name) == 0) || (strlen($name) > 255))
                    array_push($errors, "Please enter batch name");

                if ($batchcheck)
                    array_push($errors, "Batch already exist");
            if (count($errors) == 0) {

                $q = BatchSetup::AddBatchSetup($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>";
                }
            }
            }
            ?>
                <div class="row">
                    <form action="" method="post" id="batch_setup_form" novalidate="novalidate">
                        <div class="col s12">
                            <?php echo $output; ?>
                            <div class="col s8">
                                <div class="row">
                                    <span>(*) Mark Fields are mandatory</span><br/><br/>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input type="text" name="name" id="name" maxlength="250" class="form-control"
                                               value='<?php echo $name; ?>'>
                                        <label for="name" class="center-align">Batch Name *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="description"
                                               maxlength="30"
                                               name="description"
                                               value='<?php echo $description; ?>'/>
                                        <label for="description" class="center-align">Description</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="per_unit_oil"
                                               maxlength="30"
                                               name="per_unit_oil"
                                               value='<?php echo $per_unit_oil; ?>'/>
                                        <label for="per_unit_oil" class="center-align">Per Unit Oil
                                            *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="per_unit_mung"
                                               maxlength="30"
                                               name="per_unit_mung"
                                               value='<?php echo $per_unit_mung; ?>'/>
                                        <label for="per_unit_mung" class="center-align">Per Unit Mung Daal
                                            *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="per_unit_mash"
                                               maxlength="30"
                                               name="per_unit_mash"
                                               value='<?php echo $per_unit_mash; ?>'/>
                                        <label for="per_unit_mash" class="center-align">Per Unit Mash Daal
                                            *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="per_unit_rice"
                                               maxlength="30"
                                               name="per_unit_rice"
                                               value='<?php echo $per_unit_rice; ?>'/>
                                        <label for="per_unit_rice" class="center-align">Per Unit Rice
                                            *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="per_unit_packets"
                                               maxlength="30"
                                               name="per_unit_packets"
                                               value='<?php echo $per_unit_packets; ?>'/>
                                        <label for="per_unit_packets" class="center-align">Per Unit Packets
                                            *</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="center-align">Status</label>
                                    </div>
                                    <br/>
                                </div>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <div class="row margin">
                            <div 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='batch_setup.php?view'">Cancel
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <?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>Batch Name</th>
                            <th>Per Unit Oil</th>
                            <th>Per Unit Mung</th>
                            <th>Per Unit Mash</th>
                            <th>Per Unit Rice</th>
                            <th>Per Unit Packets</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th>Action</th>
                            <th>Batch Name</th>
                            <th>Per Unit Oil</th>
                            <th>Per Unit Mung</th>
                            <th>Per Unit Mash</th>
                            <th>Per Unit Rice</th>
                            <th>Per Unit Packets</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                        </tfoot>
                        <tbody>
                        <?php
                        $qry = BatchSetup::GetBatchSetupList();
                        if ($qry) {
                            foreach ($qry as $qery) { ?>
                                <tr>
                                    <td>
                                        <?php
                                        if ($_SESSION['sess_role'] == 'VEC' || $_SESSION['sess_role'] == 'VE') {
                                            ?>
                                            <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 } ?>
                                        <a style="color: #fff; padding: 5px; border-radius: 5px; font-size: 12px;" class="waves-effect waves-light btn-sm modal-trigger light-blue" href="#modal<?php echo $qery->id; ?>">Create</a>
                                        <script>
                                            $(function () {
                                                $('#datepicker<?php echo $qery->id; ?>').pickadate({
                                                    format: 'dd-mm-yyyy',
                                                    selectMonths: true,
                                                });
                                            });
                                        </script>
                                        <form action="" method="post" id="create_batch_production">
                                        <div id="modal<?php echo $qery->id; ?>" class="modal">
                                            <div class="modal-content">
                                                <p>
                                                <h3><?=ucwords($qery->name)?></h3>
                                                <div class="row margin">
                                                    <div class="input-field col s12">
                                                        <input type="text" name="batch_qty" required="required" id="batch_qty" class="form-control" />
                                                        <label for="batch_qty" class="center-align">Batch Quantity *</label>
                                                    </div>
                                                </div>
                                                <div class="row margin">
                                                    <div class="input-field col s12">
                                                        <input type="text" name="batch_date" id="datepicker<?php echo $qery->id; ?>" value="<?= $batch_date ?>" class="form-control"/>
                                                        <label for="datepicker" class="center-align">Batch Date *</label>
                                                    </div>
                                                </div>
                                                    <input type="hidden" name="batch_id" id="batch_id" value='<?php echo $qery->id; ?>'>
                                                    <input type="hidden" name="production" value='1'>
                                                </p>
                                            </div>
                                            <div class="modal-footer">
                                                <button id="btnsubmit" type="submit" class="waves-effect waves-red btn-flat">
                                                    Submit
                                                </button>
                                                <a href="#" class="waves-effect waves-green btn-flat modal-action modal-close">Close</a>
                                            </div>
                                        </div>
                                        </form>
                                    </td>
                                    <td><?php echo ucwords($qery->name); ?></td>
                                    <td><?php echo $qery->per_unit_oil; ?></td>
                                    <td><?php echo $qery->per_unit_mung; ?></td>
                                    <td><?php echo $qery->per_unit_mash; ?></td>
                                    <td><?php echo $qery->per_unit_rice; ?></td>
                                    <td><?php echo $qery->per_unit_packets; ?></td>
                                    <td><?= ($qery->status == 'active') ? ucwords($qery->status) : "Inactive"; ?></td>
                                    <td><?php echo $qery->datetime; ?></td>
                                </tr>
                                <?php
                            }
                        } ?>
                        </tbody>
                    </table>
                </div>
                <?php

            } // close view


            if (isset($_GET['edit']) && !empty($_POST)) {
                extract($_REQUEST);
                $id = $_GET['id'];
                $data['name'] = $name;
                $data['description'] = $description;
                $data['per_unit_oil'] = $per_unit_oil;
                $data['per_unit_mung'] = $per_unit_mung;
                $data['per_unit_mash'] = $per_unit_mash;
                $data['per_unit_rice'] = $per_unit_rice;
                $data['per_unit_packets'] = $per_unit_packets;
                $data['status'] = $status;
                $data['datetime'] = 'NOW()';
                //test
                $batchcheck = BatchSetup::CheckBatchAccount($name, $id);

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

                if ((strlen($name) == 0) || (strlen($name) > 255))
                    array_push($errors, "Please enter batch name");

                if ($batchcheck)
                    array_push($errors, "Batch already exist");

                if (count($errors) == 0) {
                    $q = BatchSetup::UpdateBatchSetup($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 = BatchSetup::GetBatchSetupById($id);
                $name = $qry->name;
                $description = $qry->description;
                $per_unit_oil = $qry->per_unit_oil;
                $per_unit_mung = $qry->per_unit_mung;
                $per_unit_mash = $qry->per_unit_mash;
                $per_unit_rice = $qry->per_unit_rice;
                $per_unit_packets = $qry->per_unit_packets;
                $status = $qry->status;
            }
            if ($_REQUEST['id'] && isset($_REQUEST['v'])) {
                ?>
                <div class="row">
                    <!--  The form that will be parsed by jQuery before submit  -->
                    <div class="col s12">
                        <div class="col s8">
                            <div class="row">
                                <div class="col s6">Batch Name:</div>
                                <span class="col s6"><?php echo $name; ?></span>
                            </div>
                            <div class="row">
                                <div class="col s6">Description:</div>
                                <span class="col s6"><?php echo $description; ?></span>
                            </div>
                            <div class="row">
                                <div class="col s6">Per Unit Oil:</div>
                                <span class="col s6"><?php echo $per_unit_oil; ?></span>
                            </div>
                            <div class="row">
                                <div class="col s6">Per Unit Mung:</div>
                                <span class="col s6"><?php echo $per_unit_mung; ?></span>
                            </div>
                            <div class="row">
                                <div class="col s6">Per Unit Mash:</div>
                                <span class="col s6"><?php echo $per_unit_mash; ?></span>
                            </div>
                            <div class="row">
                                <div class="col s6">Per Unit Rice:</div>
                                <span class="col s6"><?php echo $per_unit_rice; ?></span>
                            </div>
                            <div class="row">
                                <div class="col s6">Per Unit Packets:</div>
                                <span class="col s6"><?php echo $per_unit_packets; ?></span>
                            </div>
                            <div class="row">
                                <div class="col s6">Status</div>

                                <span class="col s6">
                                    <?php
                                    if ($status == 'active') {
                                        echo "Active";
                                    } else if ($status == 'inactive') {
                                        echo "Inactive";
                                    } ?>
                                </span>
                            </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="batch_setup_form" name="batch_setup_form"
                          novalidate="novalidate">
                        <div class="col s12">
                            <div class="col s8">
                                <div class="row">
                                    <?php echo $output; ?>
                                </div>
                                <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; ?>"/><br/>
                                        <label for="name" class="control-label">Batch Name *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="description"
                                               maxlength="30"
                                               name="description"
                                               value="<?php echo $description; ?>"/><br/>
                                        <label for="description" class="control-label">Description</label><br/>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="per_unit_oil"
                                               maxlength="30"
                                               name="per_unit_oil"
                                               value='<?php echo $per_unit_oil; ?>'/>
                                        <label for="per_unit_oil" class="center-align">Per Unit Oil
                                            *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="per_unit_mung"
                                               maxlength="30"
                                               name="per_unit_mung"
                                               value='<?php echo $per_unit_mung; ?>'/>
                                        <label for="per_unit_mung" class="center-align">Per Unit Mung
                                            *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="per_unit_mash"
                                               maxlength="30"
                                               name="per_unit_mash"
                                               value='<?php echo $per_unit_mash; ?>'/>
                                        <label for="per_unit_mash" class="center-align">Per Unit Mash
                                            *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="per_unit_rice"
                                               maxlength="30"
                                               name="per_unit_rice"
                                               value='<?php echo $per_unit_rice; ?>'/>
                                        <label for="per_unit_rice" class="center-align">Per Unit Rice
                                            *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="per_unit_packets"
                                               maxlength="30"
                                               name="per_unit_packets"
                                               value='<?php echo $per_unit_packets; ?>'/>
                                        <label for="per_unit_packets" class="center-align">Per Unit Packets
                                            *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div id="status" class="input-field col s12">
                                        <label class="control-label">Status</label><br>
                                        <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>
                                    </div>
                                    <br/>
                                </div>
                                <div class="clearfix"></div>
                                <div class="row margin">
                                    <div 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='batch_setup.php?view'">
                                            Cancel
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            <?php } ?>
        </div>
        <?php
        } ?>
    </div>
</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>
<script type="text/javascript">
    /*Show entries on click hide*/
    $(document).ready(function(){
        $(".dropdown-content.select-dropdown li").on( "click", function() {
            var that = this;
            setTimeout(function(){
                if($(that).parent().hasClass('active')){
                    $(that).parent().removeClass('active');
                    $(that).parent().hide();
                }
            },100);
        });
    });
</script>
</body>
</html>