<?php

//$filter = ['id' => $_GET['econid']];
//print_r($user);
//print_r(json_encode($_POST),JSON_NUMERIC_CHECK);
if ($_POST != []) {
    $data = $_POST;
    $data['id'] = $_GET['id'];
    //print_r(json_encode($data));
    //print_r($data);
    $product = $auth->sendApiData("update/product", $data);
}
if (isset($_GET['id'])) {
    $data = ['id' => $_GET['id']];
    $product = $auth->getApiDataFilter("get/product", $data);
    //print_r($product);
    $product = $product[0];
}
//print_r($product);
//$partners = $auth->getApiDataFilter("get/machine/full", $filter);
?>
<div class="row card">
    <div class="col-12 text-center">
        <button type="button" class="btn btn-block bg-gradient-success btn-lg savedata">Mentés</button>
    </div>
</div>
<form id="sendForm" class="row" method="POST">
    <div class="col-lg-12 col-sm-12">
        <div class="card">
            <div class="card-header">
                <h4>Új Termék</h4>
            </div>
            <div class="card-body">
                <div class="form-group row">
                    <div class="col-lg-4 div-sm-6">
                        <h5>Termék neve</h5>
                    </div>
                    <div class="col-lg-8 div-sm-6">
                        <input value="<?php echo (isset($product->productName)) ? $product->productName : ''; ?>" class="form-control" name="productName" />
                    </div>
                </div>

                <div class="form-group row">
                    <div class="col-lg-4 div-sm-6">
                        <h5>Termék leírás</h5>
                    </div>
                    <div class="col-lg-8 div-sm-6">
                        <textarea class="form-control" name="productDescription"><?php echo (isset($product->productDescription)) ? $product->productDescription : ''; ?></textarea>
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-lg-4 div-sm-6">
                        <h5>Termékcsoportok</h5>
                    </div>
                    <div class="col-lg-8 div-sm-6">
                        <select name="productCategory" class="select2 productCategory w-100">
                            <?php
                            $categories = $auth->getApiData("get/productcategory");
                            foreach ($categories as $category) :
                                $selected = (isset($product->productCategory) && $product->productCategory == $category->id) ? "selected" : '';
                            ?>
                                <option value="<?php echo $category->id; ?>" <?php echo $selected ?>><?php echo $category->productCategoryName; ?></option>
                            <?php
                            endforeach;
                            ?>
                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-lg-4 div-sm-6">
                        <h5>Ár</h5>
                    </div>
                    <div class="col-lg-8 div-sm-6">
                        <input value="<?php echo (isset($product->productPrice)) ? $product->productPrice : ''; ?>" class="form-control" name="productPrice" type="text" />
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-lg-4 div-sm-6">
                        <h5>Mértékegység</h5>
                    </div>
                    <div class="col-lg-4 div-sm-3">
                        <input value="<?php echo (isset($product->productQuantity)) ? $product->productQuantity : ''; ?>" class="form-control" name="productQuantity" type="number" />
                    </div>
                    <div class="col-lg-4 div-sm-3">
                        <?php
                            $units = getUnits();
                        ?>
                        <select name="productUnit" class="select2 productUnit w-100">
                            <?php
                            foreach ($units as $key => $unit) :
                                $selected = (isset($product->productUnit) && $product->productUnit == $key) ? "selected" : "";
                            ?>
                                <option value="<?php echo $key; ?>" <?php echo $selected ?>><?php echo $unit; ?></option>
                            <?php
                            endforeach;
                            ?>
                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-lg-4 div-sm-6">
                        <h5>Típus növényvédőszer</h5>
                    </div>
                    <div class="col-lg-8 div-sm-6">
                        <input value="<?php echo (isset($product->productType)) ? $product->productType : ''; ?>" class="form-control" name="productType" type="text" />
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-lg-4 div-sm-6">
                        <h5>Jutlék KATA</h5>
                    </div>
                    <div class="col-lg-4 div-sm-3">
                        <input value="<?php echo (isset($product->productComissionKata)) ? $product->productComissionKata : '0'; ?>" class="form-control" name="productComissionKata" type="number" />
                    </div>

                    <div class="col-lg-4 div-sm-3">
                        <select name="productJutalekKATA" class="select2 productJutalekKATA w-100">

                            <?php
                            $units = getJutalek();
                            foreach ($units as $unit) :
                                $selected = (isset($product->productJutalekKATA) && $product->productJutalekKATA == $unit) ? "selected" : '';
                            ?>
                                <option value="<?php echo $unit; ?>" <?php echo $selected ?>><?php echo $unit; ?></option>
                            <?php
                            endforeach;
                            ?>
                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-lg-4 div-sm-6">
                        <h5>Jutlék Normál</h5>
                    </div>
                    <div class="col-lg-4 div-sm-3">
                        <input value="<?php echo (isset($product->productComission)) ? $product->productComission : '0'; ?>" class="form-control" name="productComission" type="number" />
                    </div>
                    <div class="col-lg-4 div-sm-3">
                        <select name="productNormal" class="select2 productNormal w-100">

                            <?php
                            $units = getJutalek();
                            foreach ($units as $unit) :
                                $selected = (isset($product->productNormal) && $product->productNormal == $unit) ? "selected" : '';
                            ?>
                                <option value="<?php echo $unit; ?>" <?php echo $selected ?>><?php echo $unit; ?></option>
                            <?php
                            endforeach;
                            ?>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-12 col-sm-12">
        <div class="card">
            <div class="card-header">
                <h4>Ársávok</h4>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-6">Megnevezés</div>
                    <div class="col-6">Termékára</div>
                </div>
                <?php
                $priceranges = $auth->getApiData("get/priceranges");
                foreach ($priceranges as $pr) : ?>
                    <div class="row">
                        <div class="col-6"><?php echo $pr->name ?></div>
                        <div class="col-6"><input class="form-control" type="number" name="pricerange[<?php echo $pr->id ?>]" value="<?php echo (isset($product->pricerange->{$pr->id}))?$product->pricerange->{$pr->id}:''?>"></div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</form>
<?php
addJS("edit.js");
?>