title = "product/actions"
url = "/product/actions/:type?"
layout = "platform_main"
is_hidden = 0

[builderList]
modelClass = "Romanah\Gokbakja\Models\Product"
scope = "-"
scopeValue = "{{ :scope }}"
displayColumn = "id"
noRecordsMessage = "No records found"
detailsPage = "-"
detailsUrlParameter = "id"
pageNumber = "{{ :page }}"
sortColumn = "id"
sortDirection = "desc"

[builderList builderList2]
modelClass = "Romanah\Gokbakja\Models\Stock"
scope = "-"
scopeValue = "{{ :scope }}"
displayColumn = "id"
noRecordsMessage = "No records found"
detailsPage = "-"
detailsUrlParameter = "id"
pageNumber = "{{ :page }}"
sortColumn = "id"
sortDirection = "desc"

[action]
==
<?php
function onStart(){
    $type = $this->param("type");
    $this["type"] = $this->param("type");

    //$this["products"] = '';
    //dump($catId);

    if($type){
        $this["actions"] = Romanah\Gokbakja\Models\Action::orderBy("id", "desc")
                            ->with(['stock'])
                            ->where("type", $type)
                            ->get();
    }else{
        $this["actions"] = Romanah\Gokbakja\Models\Action::orderBy("id", "desc")
                            ->with(['stock'])
                            ->get();
    }
}
?>
==
{% put styles %}
<link href="{{'assets/libs/datatables.net-bs4/css/dataTables.bootstrap4.min.css'|theme}}" rel="stylesheet"
    type="text/css" />
<link href="{{'assets/libs/datatables.net-buttons-bs4/css/buttons.bootstrap4.min.css'|theme}}" rel="stylesheet"
    type="text/css" />
<link href="{{'assets/libs/datatables.net-select-bs4/css/select.bootstrap4.min.css'|theme}}" rel="stylesheet"
    type="text/css" />

<link href="{{'assets/libs/select2/css/select2.min.css'|theme}}" rel="stylesheet" type="text/css">

<link href="{{'assets/libs/bootstrap-datepicker/css/bootstrap-datepicker.min.css'|theme}}" rel="stylesheet">
<link href="{{'assets/libs/spectrum-colorpicker2/spectrum.min.css'|theme}}" rel="stylesheet" type="text/css">
<link href="{{'assets/libs/bootstrap-touchspin/jquery.bootstrap-touchspin.min.css'|theme}}" rel="stylesheet">
{% endput %}

{% set products = builderList.records %}
{% set stocks = builderList2.records %}

<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <h3 class="card-title" style="font-size: 22px;color: #1e2038;">Harytlar</h3>
                        <p class="card-title-desc" style="color: #6c6ff5;">{{type}}</p>
                    </div>
                    <div class="col-md-6" style="text-align: right;">
                        {% if type == 'inbox' %}
                        <button onClick="createData();" type="button" class="btn btn-primary waves-effect waves-light"
                            data-bs-toggle="offcanvas" data-bs-target="#offcanvasBottom"
                            aria-controls="offcanvasBottom">
                            <i class="ri-add-line align-middle ms-2" style="font-size: 17px;"></i> Täze Hereket
                        </button>
                        {% endif %}
                    </div>
                </div>
                <table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap"
                    style="border-collapse: collapse; border-spacing: 0; width: 100%;">
                    <thead>
                        <tr>
                            <th style="width: 5%;">№</th>
                            <th>Haryt</th>
                            <th>Görnüşi</th>
                            <th>Mukdary (KG)</th>
                            <th>Senesi</th>
                            <th>Sklad</th>
                            <th>Bellik</th>
                            <th>Sazlamalar</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for key, record in actions %}
                        <tr>
                            <td style="font-weight: bold;width: 5%;">{{key + 1}}</td>
                            <td><a style="font-weight: bold;color: #0005c5;">{{record.product.name}}</a></td>
                            <td style="text-align: center;">
                                <span
                                    class="badge badge-soft-{% if record.type == 'inbox' %}success {% else %}danger {% endif %}"
                                    style="font-size: 14px;">{{record.type}}</span>
                            </td>
                            <td style="color: #500101;font-weight: bold;">{{record.amount|number_format}} kg</td>
                            <td>{{record.created_at|date('d.m.Y')}}</td>
                            <td>{{record.stock.name}}</td>
                            <td>{{record.note}}</td>
                            <td>
                                <div style="display: flex;justify-content: center;">
                                    <div style="margin-right: 10px;">
                                        <a onClick="getData({{record.id}});" type="button"
                                            class="btn btn-warning waves-effect waves-light" data-bs-toggle="offcanvas"
                                            data-bs-target="#offcanvasBottom" aria-controls="offcanvasBottom"
                                            style="padding-top: 5px;padding-bottom: 5px;">
                                            <i class="ri-pencil-line align-middle me-2"></i> Обновить
                                        </a>
                                    </div>
                                    <div>
                                        <a type="button" data-request-confirm="Вы уверены? Удалить этот продукт"
                                            data-request-data="id:{{record.id}}" data-request="onDeleteAction"
                                            class="btn btn-danger waves-effect waves-light"
                                            style="padding-top: 5px;padding-bottom: 5px;">
                                            <i class="ri-close-line align-middle me-2"></i> Удалить
                                        </a>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                    <tfoot>
                        <tr>
                            <th style="width: 5%;">№</th>
                            <th>Haryt</th>
                            <th>Görnüşi</th>
                            <th>Mukdary (KG)</th>
                            <th>Senesi</th>
                            <th>Sklad</th>
                            <th>Bellik</th>
                            <th>Sazlamalar</th>
                        </tr>
                    </tfoot>
                </table>
            </div>

            <div class="offcanvas offcanvas-top" id="offcanvasBottom" aria-labelledby="offcanvasBottomLabel"
                style="height: 40%;">
                <div class="offcanvas-header">
                    <h5 id="offcanvasBottomLabel">Täze Hereket</h5>
                    <button onClick="createData();" type="button" class="btn-close text-reset"
                        data-bs-dismiss="offcanvas" aria-label="Close"></button>
                </div>
                <div class="offcanvas-body">
                    <form class="needs-validation" novalidate data-request="onCreateAction" id="prodForm">
                        <div class="row">
                            <div class="col-md-4">
                                <div class="mb-3">
                                    <label for="validationCustom01" class="form-label">Haryt Saýla</label>
                                    <select class="form-control select2" name="product_id" id="productId">
                                        <option value="0">Saýla</option>

                                        {% for record in products %}
                                        <option value="{{record.id}}" style="text-transform: capitalize;">
                                            {{record.name}}</option>
                                        {% endfor %}
                                    </select>

                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="mb-3">
                                    <label for="validationCustom02" class="form-label">Mukdary</label>
                                    <input type="number" class="form-control" id="validationCustom02"
                                        placeholder="Mukdary" name="quantity" required>
                                    <div class="valid-feedback">
                                        dogry
                                    </div>
                                </div>
                            </div>

                            <div class="col-md-4">
                                <label for="validationCustom03" class="form-label">Sklad</label>
                                <select class="form-control select2" name="stock_id" id="stockId">
                                    <option value="0">Saýla</option>

                                    {% for record in stocks %}
                                    <option value="{{record.id}}" style="text-transform: capitalize;">{{record.name}}
                                    </option>
                                    {% endfor %}
                                </select>
                            </div>

                            <div class="col-md-4">
                                <div class="mb-3">
                                    <label for="validationCustom02" class="form-label">Maşyn nomer</label>
                                    <input type="text" class="form-control" id="validationCustom02"
                                        placeholder="Maşyn Nomer" name="transport_no">
                                </div>
                            </div>

                            <div class="col-md-4">
                                <div class="mb-3">
                                    <label for="validationCustom02" class="form-label">Sürüji</label>
                                    <input type="text" class="form-control" id="validationCustom02" placeholder="Sürüji"
                                        name="driver">
                                </div>
                            </div>

                            <div class="col-md-4">
                                <div class="mb-3">
                                    <label for="validationCustom02" class="form-label">Bellik</label>
                                    <input type="text" class="form-control" id="validationCustom02" placeholder="Bellik"
                                        name="note">
                                </div>
                            </div>
                        </div>
                        <input name="id" type="hidden" value="">
                        <div>
                            <button class="btn btn-primary" type="submit" id="formBtn">Goş</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>


{% put scripts %}
<script>
    function createData() {
        $('#prodForm').attr('data-request', 'onCreateAction');
        $('#formBtn').html('Goş');

        $('input[name=id]').val('');
        $("#productId").select2().val(0).trigger("change");
        $('input[name=quantity]').val('');
        $('input[name=driver]').val('');
        $('input[name=transport_no]').val('');
        $('input[name=note]').val('');
        $("#stockId").select2().val(0).trigger("change");
    }

    function getData(id) {
        $(this).request('onGetAction', {
            data: { id: id },
            success: function (data) {

                $('#prodForm').attr('data-request', 'onUpdateAction');
                $('#formBtn').html('Täzele');

                $('input[name=id]').val(data.id);
                $("#productId").select2().val(data.product_id).trigger("change");
                $('input[name=quantity]').val(data.amount);
                $('input[name=driver]').val(data.driver);
                $('input[name=transport_no]').val(data.transport_no);
                $('input[name=note]').val(data.note);
                $("#stockId").select2().val(data.stock_id).trigger("change");
            }
        });
    };
</script>

<script src="{{'assets/libs/select2/js/select2.min.js'|theme}}"></script>
<script src="{{'assets/libs/bootstrap-datepicker/js/bootstrap-datepicker.min.js'|theme}}"></script>
<script src="{{'assets/libs/spectrum-colorpicker2/spectrum.min.js'|theme}}"></script>
<script src="{{'assets/libs/bootstrap-touchspin/jquery.bootstrap-touchspin.min.js'|theme}}"></script>
<script src="{{'assets/libs/admin-resources/bootstrap-filestyle/bootstrap-filestyle.min.js'|theme}}"></script>
<script src="{{'assets/libs/bootstrap-maxlength/bootstrap-maxlength.min.js'|theme}}"></script>

<script src="{{'assets/js/pages/form-advanced.init.js'|theme}}"></script>
{% endput %}

{% partial 'dataTableJs' %}
