<?php
//print_r($_POST);
//$filter = ['id' => $_GET['econid']];
//print_r($user);
if (isset($_POST) && $_POST != []) {
    $data = $_POST;
    $filter = ['productName' => $data['productName']];
    //$test = $auth->getApiDataFilter("get/product",['companyName'=>2]);
    //print_r($test);
    $response = $auth->getApiDataFilter('get/product/exists', $filter);
    if (isset($response->status)) {
        $response = $auth->getApiData("get/product/lastid");
        $response = $response[0];
        $data['id'] = $response->id + 1;
        $data['postedBy'] = $user->id;
        //print_r(json_encode($data));
        $response = $auth->sendApiData("set/product/new", $data);
        //print_r($response);
    }
}
//$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-6 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="" 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"></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) :
                            ?>
                                <option value="<?php echo $category->id; ?>"><?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">
                        <select name="productUnit" class="select2 productUnit w-100">
                            <?php
                            $units = getUnits();
                            foreach ($units as $unit) :
                                $selected = (isset($product->productUnit) && $product->productUnit == $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>Típus növényvédőszer</h5>
                    </div>
                    <div class="col-lg-8 div-sm-6">
                        <input value="" 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>
</form>
<?php
addJS("edit.js");
?>