<?php
include("../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$status = "";
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
?>
<!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>Invoice Rate 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"></i></a>
                    <div class="clearfix"></div>
                <?php }
            } ?>
            <br/>
            <?php
            if (isset($_GET['add'])) {
            if (isset($_POST) && !empty($_POST)) {
                extract($_REQUEST);
                $data['customer_category_id'] = $customer_category_id;
                $data['status'] = $status == 'active' ? 1 : 0;
                $data['create_date'] = date('Y-m-d H:i:s');
                $checkInvoiceRate = InvoiceRate::CheckInvoiceRate($customer_category_id, 0);

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

                if ((strlen($customer_category_id) == 0))
                    array_push($errors, "Please select customer category");

                if ($checkInvoiceRate)
                    array_push($errors, "Invoice rate already exist");
            if (count($errors) == 0) {
                $q = InvoiceRate::AddInvoiceRate($data);

            if ($q) {
                if ($product_id) {
                    $dataProductRate['invoice_rate_id'] = $q;
                    foreach ($product_id AS $productValue) {
 //                       $dataProductRate['unit_cost_price'] = $unitcost[$productValue];
//                        $dataProductRate['unit_distributor_price'] = $unitdistributor[$productValue];
                        $dataProductRate['unit_sale_price'] = $unitsale[$productValue];
                        $dataProductRate['product_id'] = $productValue;
                        //print_r($dataProductRate);
                        InvoiceRate::AddInvoiceRateDetail($dataProductRate);
                    }
                }

                $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="invoice_rate_setup_form" novalidate="novalidate">
                    <div class="col s12 form-group">
                        <?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">
                                    <select class="form-control" required id="customer_category_id"
                                            name="customer_category_id">
                                        <option></option>
                                        <?php
										$filterCat = " ORDER BY name";
                                        $customersCat = CustomerCategorySetup::GetCustomerCategorySetupList($filterCat);
                                        foreach ($customersCat AS $customersCatRow) {
                                            $selected = $customersCatRow->id == $customer_category_id ? "selected" : "";
                                            echo '<option ' . $selected . ' value="' . $customersCatRow->id . '" data-mart="' . $customersCatRow->is_mart . '" >' . $customersCatRow->name . '</option>';
                                        }
                                        ?>
                                    </select>
                                    <label for="customer_category_id" class="control-label">Customer Category *</label>
                                </div>
                            </div>
                            <div class="row margin">
                                <div class="input-field col s12" id="status">
                                    <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>
                            </div>
                        </div>

                        <?php
                        $filterProduct = " WHERE status = 'active' order by product_order";
                        $qry = ProductSetup::GetProductSetupList($filterProduct);
                        if ($qry) {
                            echo '<div id="floatThead" class="section">';
                            echo '<table class="striped demo1">';
                            $counting = 1;
                            echo '<thead>';
                            echo '<tr>';
                            echo '<th>S.No</th>';
                            echo '<th>Product Name</th>';
                //            echo '<th>Cost Price</th>';
//                            echo '<th>Distributor Price</th>';
                            echo '<th>Sale Price</th>';
                            echo '</tr>';
                            echo '</thead>';
                            echo '<tbody>';
                            foreach ($qry AS $qryRow) {
                                echo '<tr>';
                                echo '<td>' . $counting . '</td>';
                                echo '<td>' . $qryRow->name . '</td>';
                                echo '<input class="form-control" type="hidden" name="product_id[]" id="product_id[]" value="' . $qryRow->id . '"></td>';
                          //      echo '<td><input class="form-control" type="text" name="unitcost[' . $qryRow->id . ']" readonly id="unitcost" value="' . $qryRow->cost_price . '"></td>';
//                                echo '<td><input class="form-control" type="text" name="unitdistributor[' . $qryRow->id . ']" id="unitdistributor" value=""></td>';
                                echo '<td><input class="form-control" type="text" name="unitsale[' . $qryRow->id . ']" id="unitsale" value=""></td>';
                                echo '</tr>';
                                $counting++;
                            }
                            echo '</tbody>';
                            echo '</table>';
                            echo '</div>';
                        }
                        ?>
                        <div class="row margin">
                            <div class="input-field col s12" id="status">
                                <div class="clearfix"></div>
                                <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='invoice_rate.php?view'">Cancel
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
                <?php
            } // add close

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

                ?>
                <?php if (isset($_GET['sucess'])) { ?>
                    <div class="alert alert-success  fade in">
                        <button type="button" class="close" data-dismiss="alert">&times;</button>
                        <strong>*</strong> Successfully Update.
                    </div>
                <?php } ?>
                <?php if (isset($_GET['error'])) { ?>
                    <div class="alert alert-error  fade in">
                        <button type="button" class="close" data-dismiss="alert">&times;</button>
                        Error Ocurred. Try Again Later.
                    </div>
                <?php } ?>
                <div class="table-datatables">
                    <table id="data-table-simple" class="responsive-table display">
                        <thead>
                        <tr>
                            <th>Action</th>
                            <th>Customer Name</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th>Action</th>
                            <th>Customer Name</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                        </tfoot>
                        <tbody>
                        <?php
                        $qry = InvoiceRate::GetInvoiceRateList();
                        if ($qry) {
                            foreach ($qry as $qery) {
                                $customerCategory = CustomerCategorySetup::GetCustomerCategorySetupById($qery->customer_category_id);
                                ?>
                                <tr>
                                    <td>
                                        <?php
                                        if ($_SESSION['sess_role'] == 'VEC' || $_SESSION['sess_role'] == 'VE') {
                                            ?>
                                  <!--          <a href="report/print/invoice_rate.php?id=<?php echo $qery->id; ?>"
                                               target="_blank" title="Print Invoice">
                                                                    <span
                                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                                        class="mini-stat-icon orange darken-2"><i
                                                                            class="mdi-action-print"></i></span>
                                            </a>-->
<!--                                            <a href='?edit&id=--><?php //echo $qery->id; ?><!--&v' title="View">-->
<!--                                                    <span-->
<!--                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"-->
<!--                                                        class="mini-stat-icon pink"><i-->
<!--                                                            class="mdi-action-description"></i></span>-->
<!--                                            </a>-->
                                            <a href="?edit&id=<?php echo $qery->id; ?>" title="Edit">
                                                    <span
                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                        class="mini-stat-icon orange"><i
                                                            class="mdi-image-edit"></i></span>
                                            </a>
                                        <?php } else { ?>
                                            <a href="?edit&id=<?php echo $qery->id; ?>&v" title="View">
                                                    <span
                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                        class="mini-stat-icon pink"><i
                                                            class="mdi-action-description"></i></span>
                                            </a>
                                        <?php } ?>
                                    </td>
                                    <td><?php echo ucwords($customerCategory->name); ?></td>
                                    <td><?php echo $qery->status == 1 ? 'Active' : 'In active'; ?></td>
                                    <td><?php echo date('Y-m-d', strtotime($qery->create_date)); ?></td>
                                </tr>
                                <?php
                            }
                        } ?>
                        </tbody>
                    </table>
                </div>
                <?php

            } // close view


            if (isset($_GET['edit']) && !empty($_POST)) {
                extract($_REQUEST);
                $id = $_GET['id'];

                $data['customer_category_id'] = $customer_category_id;
                $data['status'] = $status == 'active' ? 1 : 0;
                $data['create_date'] = date('Y-m-d H:i:s');
                $checkInvoiceRate = InvoiceRate::CheckInvoiceRate($customer_category_id, $id);

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

                if ((strlen($customer_category_id) == 0))
                    array_push($errors, "Please select customer category");

                if ($checkInvoiceRate)
                    array_push($errors, "Invoice rate already exist");
                if (count($errors) == 0) {
                    $q = InvoiceRate::UpdateInvoiceRateSetup($id, $data);

                    if ($q) {
                        InvoiceRate::DeleteInvoiceRateDetails($id);
                        if ($product_id) {
                            $dataProductRate['invoice_rate_id'] = $id;
                            foreach ($product_id AS $productValue) {
   //                             $dataProductRate['unit_cost_price'] = $unitcost[$productValue];
//                                $dataProductRate['unit_distributor_price'] = $unitdistributor[$productValue];
                                $dataProductRate['unit_sale_price'] = $unitsale[$productValue];
                                $dataProductRate['product_id'] = $productValue;
                                //print_r($dataProductRate);
                                InvoiceRate::AddInvoiceRateDetail($dataProductRate);
                            }
                        }
                        $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 = InvoiceRate::GetInvoiceRateSetupById($id);
                    $customer_category_id = $qry->customer_category_id;
                    $customerCat = CustomerCategorySetup::GetCustomerCategorySetupById($customer_category_id);
                    $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">Customer Category:</div>
                                <span class="col s6"><?php echo $customerCat->name; ?></span>
                            </div>
                            <div class="row">
                                <div class="col s6">Status:</div>
                                <span class="col s6"><?php $status == '1' ? 'Active' : 'In-active'; ?></span>
                            </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="invoice_rate_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">
                                            <select class="form-control" required id="customer_category_id"
                                                    name="customer_category_id">
                                                <option></option>
                                                <?php
												$filterCat = " ORDER BY name";
                                                $customersCat = CustomerCategorySetup::GetCustomerCategorySetupList($filterCat);
                                                foreach ($customersCat AS $customersCatRow) {
                                                    $selected = $customersCatRow->id == $customer_category_id ? "selected" : "";
                                                    echo '<option ' . $selected . ' value="' . $customersCatRow->id . '" data-mart="' . $customersCatRow->is_mart . '" >' . $customersCatRow->name . '</option>';
                                                }
                                                ?>
                                            </select>
                                            <label for="customer_category_id" class="control-label">Customer Category
                                                *</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 == '1') {
                                                    echo "selected='selected'";
                                                } ?>>Active
                                                </option>
                                                <option value="inactive" <?php if ($status == '0') {
                                                    echo "selected='selected'";
                                                } ?>>Inactive
                                                </option>
                                            </select>
                                            <label for="status" class="control-label">Status</label>
                                        </div>
                                    </div>
                                </div>
                                <?php
                                $filterProduct = " WHERE status = 'active' order by product_order";
                                $qry = ProductSetup::GetProductSetupList($filterProduct);
                                $filterInvoiceRateDetail = " WHERE invoice_rate_id = '$id' ";
                                $invoice_rate_detail = InvoiceRate::GetInvoiceRateDetails($filterInvoiceRateDetail);
                                if ($qry) {
                                    echo '<div id="floatThead" class="section">';
                                    echo '<table class="striped demo1">';
                                    $counting = 1;
                                    echo '<thead>';
                                    echo '<tr>';
                                    echo '<th>S.No</th>';
                                    echo '<th>Product Name</th>';
//                                    echo '<th>Cost Price</th>';
//                                    echo '<th>Distributor Price</th>';
                                    echo '<th>Sale Price</th>';
                                    echo '</tr>';
                                    echo '</thead>';
                                    echo '<tbody>';
                                    foreach ($qry AS $qryRow) {
                                        if ($invoice_rate_detail) {
                                            $print = false;
                                            foreach ($invoice_rate_detail AS $invoice_rate_detailRow) {
                                                if ($invoice_rate_detailRow->product_id == $qryRow->id) {
                                                    echo '<tr>';
                                                    echo '<td>' . $counting . '</td>';
                                                    echo '<td>' . $qryRow->name . '</td>';
                                                    echo '<input class="form-control" type="hidden" name="product_id[]" id="product_id[]" value="' . $qryRow->id . '"></td>';
  //                                                  echo '<td><input class="form-control" type="text" name="unitcost[' . $qryRow->id . ']" id="unitcost" readonly value="' . $qryRow->cost_price . '"></td>';
//                                                    echo '<td><input class="form-control" type="text" name="unitdistributor[' . $qryRow->id . ']" id="unitdistributor" value="' . $invoice_rate_detailRow->unit_distributor_price . '"></td>';
                                                    echo '<td><input class="form-control" type="text" name="unitsale[' . $qryRow->id . ']" id="unitsale" value="' . $invoice_rate_detailRow->unit_sale_price . '"></td>';
                                                    echo '</tr>';
                                                    $print = true;
                                                }
                                            }
                                            if ($print == false) {
                                                echo '<tr>';
                                                echo '<td>' . $counting . '</td>';
                                                echo '<td>' . $qryRow->name . '</td>';
                                                echo '<input class="form-control" type="hidden" name="product_id[]" id="product_id[]" value="' . $qryRow->id . '"></td>';
 //                                               echo '<td><input class="form-control" type="text" name="unitcost[' . $qryRow->id . ']" id="unitcost" readonly value="' . $qryRow->cost_price . //'"></td>';
//                                                echo '<td><input class="form-control" type="text" name="unitdistributor[' . $qryRow->id . ']" id="unitdistributor" value=""></td>';
                                                echo '<td><input class="form-control" type="text" name="unitsale[' . $qryRow->id . ']" id="unitsale" value=""></td>';
                                                echo '</tr>';
                                            }
                                        } else {
                                            echo '<tr>';
                                            echo '<td>' . $counting . '</td>';
                                            echo '<td>' . $qryRow->name . '</td>';
                                            echo '<input class="form-control" type="hidden" name="product_id[]" id="product_id[]" value="' . $qryRow->id . '"></td>';
 //                                           echo '<td><input class="form-control" type="text" name="unitcost[' . $qryRow->id . ']" readonly id="unitcost" value="' . $qryRow->cost_price . '">//</td>';
//                                            echo '<td><input class="form-control" type="text" name="unitdistributor[' . $qryRow->id . ']" id="unitdistributor" value=""></td>';
                                            echo '<td><input class="form-control" type="text" name="unitsale[' . $qryRow->id . ']" id="unitsale" value=""></td>';
                                            echo '</tr>';
                                        }
                                        $counting++;
                                    }
                                    echo '</body>';
                                    echo '</table>';
                                    echo '</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='invoice_rate.php?view'">
                                            Cancel
                                        </button>
                                    </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="text/javascript" src="<?= PROJECT_URL ?>js/plugins/floatThead/jquery.floatThead.min.js"></script>
<script type="text/javascript"
        src="<?= PROJECT_URL ?>js/plugins/floatThead/jquery.floatThead-slim.min.js"></script>
<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);
        });
        $('table.demo1').floatThead({
            position: 'fixed',
            scrollingTop: 65
        });
        $('table.demo1 thead').css('background', '#ECEFF1');
    });
</script>
</body>
</html>