<?php
include("../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$name = '';
$status = '';
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('includes/inc_head.php'); ?>
    <style>
        table {
            font-size: 18px;
        }
    </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>Expense Mart Category 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
                        Category <i
                            class="mdi-content-add-box"></i></a>
                    <a class="btn waves-effect waves-light" style="float:right;margin-left:5px;" href="?addsub">Add Sub
                        Category <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['parent_id'] = 0;
                $data['name'] = $name;
                $usercheck = ExpenseMartCategorySetup::CheckExpenseMartCategory($name, 0);

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

                if ((strlen($name) == 0) || (strlen($name) > 255))
                    array_push($errors, "Please enter category name");
                if ($usercheck)
                    array_push($errors, "Expense name already exist");
            if (count($errors) == 0) {
                $q = ExpenseMartCategorySetup::AddExpenseMartCategorySetup($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="expense_mart_category_setup_form" novalidate="novalidate">
                        <div class="col-sm-12 form-group">
                            <?php echo $output; ?>
                            <div class="col-sm-8">
                                <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"
                                               required
                                               value='<?php echo $name; ?>'/>
                                        <label for="name" class="control-label">Category Name *</label>
                                    </div>
                                </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='expense_mart_category_setup.php?view'">Cancel
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            <?php
            } elseif (isset($_GET['addsub'])) {
            if (isset($_POST) && !empty($_POST)) {
            extract($_REQUEST);
            $data['parent_id'] = $cat_id;
            $data['name'] = $name;
            $data['status'] = $status;
            $usercheck = ExpenseMartCategorySetup::CheckExpenseMartCategory($name, 0);

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

            if ((strlen($name) == 0) || (strlen($name) > 255))
                array_push($errors, "Please enter sub category name");
            if ((strlen($cat_id) == 0) || (strlen($cat_id) > 255))
                array_push($errors, "Please select category name");
            if ($usercheck)
                array_push($errors, "Sub category name already exist");
            if (count($errors) == 0) {
            $q = ExpenseMartCategorySetup::AddExpenseMartCategorySetup($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="expense_mart_category_setup_form" novalidate="novalidate">
                        <div class="col-sm-12 form-group">
                            <?php echo $output; ?>
                            <div class="col-sm-8">
                                <div class="row">
                                    <span>(*) Mark Fields are mandatory</span><br/><br/>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <select class="form-control" name="cat_id" id="cat_id">
                                            <?php
                                            $expenseCatFilter = " WHERE parent_id = 0 ORDER BY name";
                                            $expenseCat = ExpenseMartCategorySetup::GetExpenseMartCategorySetupList($expenseCatFilter);
                                            if ($expenseCat) {
                                                foreach ($expenseCat AS $expenseCatRow) {
                                                    echo '<option value="' . $expenseCatRow->id;
                                                    if ($expenseCatRow->id == $cat_id) {
                                                        echo 'selected';
                                                    }
                                                    echo '">' . ucwords($expenseCatRow->name) . '</option>';
                                                }
                                            }
                                            ?>
                                        </select>
                                        <label for="cat_id" class="control-label">Category</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="name" maxlength="250"
                                               name="name"
                                               required
                                               value='<?php echo $name; ?>'/>
                                        <label for="name" class="control-label">Sub Category Name *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div id="status" class="input-field col s12">
                                        <select class="form-control" name="status" id="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>
                                </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='expense_mart_category_setup.php?view'">Cancel
                                        </button>
                                    </div>
                                </div>
                            </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>Category Name</th>
                            <th>Sub Category Name</th>
                            <th>Status</th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th>Action</th>
                            <th>Category Name</th>
                            <th>Sub Category Name</th>
                            <th>Status</th>
                        </tr>
                        </tfoot>
                        <tbody>
                        <?php
                        $qry = ExpenseMartCategorySetup::GetExpenseMartCategorySetupList();
                        if ($qry) {
                            foreach ($qry as $qery) {
                                $ExpenseCatParent = ExpenseMartCategorySetup::GetExpenseMartCategorySetupById($qery->parent_id);
                                if (empty($ExpenseCatParent)) {
                                    $addnew = 'add';
                                } else {
                                    $addnew = 'addsub';
                                }
                                ?>
                                <tr>
                                    <td>
                                        <?php
                                        if ($_SESSION['sess_role'] == 'VEC' || $_SESSION['sess_role'] == 'VE') {
                                            ?>
                                            <a href='?edit&id=<?php echo $qery->id; ?>&v&action=<?= $addnew ?>'
                                               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; ?>&action=<?= $addnew ?>"
                                               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&action=<?= $addnew ?>"
                                               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 !empty($ExpenseCatParent->name) ? ucwords($ExpenseCatParent->name) : ucwords($qery->name); ?></td>
                                    <td><?php echo !empty($ExpenseCatParent->name) ? ucwords($qery->name) : '-'; ?></td>
                                    <td><?php echo !empty($ExpenseCatParent->name) ? ucwords($qery->status) : '-'; ?></td>
                                </tr>
                                <?php
                            }
                        } ?>
                        </tbody>
                    </table>
                </div>
                <?php

            } // close view
            if (isset($_GET['edit']) && $_GET['action'] == 'add' && !empty($_POST)) {
                extract($_REQUEST);
                $id = $_GET['id'];

                $data['parent_id'] = 0;
                $data['name'] = $name;
                $usercheck = ExpenseMartCategorySetup::CheckExpenseMartCategory($name, $id);

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

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

                if ($usercheck)
                    array_push($errors, "Category name already exist");

            if (count($errors) == 0) {
                $q = ExpenseMartCategorySetup::UpdateExpenseMartCategorySetup($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>";
                }
            }
            } elseif (isset($_GET['edit']) && $_GET['action'] == 'addsub' && !empty($_POST)) {
            extract($_REQUEST);
            $id = $_GET['id'];
            $data['parent_id'] = !empty($cat_id) ? $cat_id : 0;
            $data['name'] = $name;
            $data['status'] = $status;
            $usercheck = ExpenseMartCategorySetup::CheckExpenseMartCategory($name, $id);

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

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

            if ($usercheck)
                array_push($errors, "Sub Category name already exist");

            if (count($errors) == 0) {
            $q = ExpenseMartCategorySetup::UpdateExpenseMartCategorySetup($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'];
            $action = $_GET['action'];
            if (empty($_POST)) {
                $qry = ExpenseMartCategorySetup::GetExpenseMartCategorySetupById($id);
                $name = $qry->name;
                $cat_id = $qry->parent_id;
                $status = $qry->status;
                $ExpCatParent = ExpenseMartCategorySetup::GetExpenseMartCategorySetupById($cat_id);
                $parentCat = $ExpCatParent->name;
            }
            if ($_REQUEST['id'] && isset($_REQUEST['v']) && $action == 'add') {
            ?>
            <div class="row">
                <!--  The form that will be parsed by jQuery before submit  -->
                <div class="col-sm-12 form-group">
                    <div class="col-sm-8">
                        <div class="row">
                            <div class="col s6">Category Name:</div>
                            <div class="col s6"><?php echo $name; ?></div>
                        </div>
                    </div>
                </div>
                <?php } elseif ($_REQUEST['id'] && isset($_REQUEST['v']) && $action == 'addsub') {
                    ?>
                    <div class="row">
                        <!--  The form that will be parsed by jQuery before submit  -->
                        <div class="col-sm-12 form-group">
                            <div class="col-sm-8">
                                <div class="row">
                                    <div class="col s6">Category Name:</div>
                                    <div class="col s6"><?php echo $parentCat; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col s6">Sub Category Name:</div>
                                    <div class="col s6"><?php echo $name; ?></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>
                    </div>
                <?php } else {
                    ?>
                    <div class="row">
                        <!--  The form that will be parsed by jQuery before submit  -->
                        <form action="?edit&id=<?php echo $id; ?>&action=<?php echo $action; ?>" method="post"
                              id="expense_mart_category_setup_form"
                              novalidate="novalidate">
                            <div class="col-sm-12 form-group">
                                <div class="col-sm-8">
                                    <div class="row">
                                        <?php echo $output; ?>
                                    </div>
                                    <div class="row">
                                        <span>(*) Mark Fields are mandatory</span><br/><br/>
                                    </div>
                                    <?php if ($action == 'addsub') { ?>
                                        <div class="row margin">
                                            <div class="input-field col s12">
                                                <select class="form-control" name="cat_id" id="cat_id">
                                                    <?php
                                                    $expenseCatFilter = " WHERE parent_id = 0 ORDER BY name";
                                                    $expenseCat = ExpenseMartCategorySetup::GetExpenseMartCategorySetupList($expenseCatFilter);
                                                    if ($expenseCat) {
                                                        foreach ($expenseCat AS $expenseCatRow) {
                                                            echo '<option value="' . $expenseCatRow->id;
                                                            if ($expenseCatRow->id == $id) {
                                                                echo 'selected';
                                                            }
                                                            echo '">' . ucwords($expenseCatRow->name) . '</option>';
                                                        }
                                                    }
                                                    ?>
                                                </select>
                                                <label for="cat_id" class="control-label">Category</label>
                                            </div>
                                        </div>
                                    <?php } ?>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="name" maxlength="250"
                                                   name="name"
                                                   required
                                                   value="<?php echo $name; ?>"/>
                                            <label for="name" class="control-label">Category Name *</label>
                                        </div>
                                    </div>
                                    <?php if ($action == 'addsub') { ?>
                                        <div class="row margin">
                                            <div id="status" class="input-field col s12">
                                                <select class="form-control" name="status" id="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>
                                        </div>
                                    <?php } ?>
                                    <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='expense_mart_category_setup.php?view'">
                                                Cancel
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </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>