title = "Production"
url = "/production/:excruiter"
layout = "platform_main"
is_hidden = 0

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

[production]
==
<?php
function onStart(){

    $currentDate = Carbon\Carbon::now()->timezone('UTC +05:00');

    $currentDateFormat = Carbon\Carbon::now()->format('Y-m-d');

    $this["currentDate"] = $currentDate;
    $this["currentMonth"] = $currentDate->format('m');
    $this["currentYear"] = $currentDate->format('Y');

    $excruiter = $this->param("excruiter");

    $this["productions"] = Romanah\Gokbakja\Models\Production::whereDate('created_at', date($currentDateFormat))->where('excruiter_id', $excruiter)->with(['pivot_production', 'excruiter', 'shift'])->orderBy('id', 'DESC')->get();

    $this["productCounts"] = Romanah\Gokbakja\Models\Product::where('report', 'simple')->count();

    $this["shifts"] = Romanah\Gokbakja\Models\Shift::get();

    $this["calcAll"] = Romanah\Gokbakja\Models\Production::whereDate('created_at', date($currentDateFormat))->sum('time');

}
?>
==
{% set records = builderList.records %}
{% set displayColumn = builderList.displayColumn %}
{% set noRecordsMessage = builderList.noRecordsMessage %}
{% set detailsPage = builderList.detailsPage %}
{% set detailsKeyColumn = builderList.detailsKeyColumn %}
{% set detailsUrlParameter = builderList.detailsUrlParameter %}




{% 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" />
{% endput %}

<div class="container-fluid">
    <div class="row">
        <div class="col-lg">
            <a class="card bg-success text-white-50">
                <div class="card-body">
                    <h5 class="mb-4 text-white" id="percentage"><i class="mdi mdi-bullseye-arrow me-3"></i> 0 %</h5>
                    <p class="card-text" id="all_amount">JEMI: 0 kg</p>
                </div>
            </a>
        </div>
        {% for key, record in records %}
        <div class="col-lg col-sm">
            <a class="card bg-info text-white-50">
                <div class="card-body">
                    <h5 class="mb-4 text-white" id="avg_percentage_{{record.id}}"><i
                            class="mdi mdi-bullseye-arrow me-3"></i> 0 %</h5>
                    <p class="card-text" id="avg_amount_{{record.id}}">{{record.name}} : 0 kg</p>
                </div>
            </a>
        </div>
        {% endfor %}
    </div>
    <!-- end row -->
    <div class="row">

        <div class="col-md-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;">Çig Mallar Boýunça
                                ({{currentDate|date('d.m.Y | H:i')}})</h3>
                            <div id="back_btn">
                                <a href="#">HASABAT</a>
                            </div>
                        </div>
                        <div class="col-md-6" style="text-align: right;">
                            <a href="/excruiters" type="button" class="btn btn-danger waves-effect waves-light">
                                <i class="ri-arrow-left-line align-middle ms-2" style="font-size: 17px;"></i> Yza
                            </a>
                            <button data-request="onCalculateReportDaily" type="button" class="btn btn-primary waves-effect waves-light"
                                data-bs-toggle="modal" data-bs-target="#exampleModalFullscreen">
                                <i class="ri-pie-chart-line align-middle ms-2" style="font-size: 17px;"></i> Günüň
                                Jemini Jemle
                            </button>
                        </div>
                    </div>

                    <p id="product_count" style="color: transparent;position: absolute;">{{productCounts}}</p>
                    {% for key, record in records %}
                    <p id="product_id_{{key}}" style="color: transparent;font-size: 1;position: absolute;">{{record.id}}
                    </p>
                    {% endfor %}
                    <div class="table-responsive" style="margin-bottom: 20px;margin-top: 20px;">
                        <table class="table mb-0">

                            <thead class="table-light">

                                <form data-request="onCreateProduction">
                                    <tr id="step1">
                                        <th style="background: #daddff;">Saýla</th>
                                        <th style="background: #daddff;">
                                            <select class="form-control" name="shift_id" id="shift_id">
                                                <option value="0">Smen</option>
                                                {% for shift in shifts %}
                                                <option value="{{shift.id}}">{{shift.desc}} - ({{shift.start}} -
                                                    {{shift.end}})</option>
                                                {% endfor %}
                                            </select>
                                        </th>
                                        <th style="background: #daddff;">
                                            <input type="number" name="all_amount" id="all_amount_input"
                                                class="form-control" placeholder="JEMI"
                                                oninput="getAllAmount(this.value);">
                                        </th>
                                        <th style="background: #daddff;">
                                            <a onclick="goSecondStep();"
                                                class="btn btn-primary waves-effect waves-light">Dowam et</a>
                                        </th>
                                    </tr>
                                    <tr id="step2" style="display: none;">
                                        <th style="background: #daddff;">Täze maglumat</th>

                                        {% for key, record in records %}
                                        <th style="background: #daddff;">
                                            <input type="number" oninput="getValues();" step="0.01"
                                                pattern="/^-?\d+\.?\d*$/"
                                                onKeyPress="if(this.value.length==4) return false;"
                                                name="product_{{record.code}}" id="product_{{key}}" class="form-control"
                                                placeholder="{{record.name}} %" disabled>
                                        </th>
                                        {% endfor %}
                                        <th style="background: #daddff;">
                                            <input type="text" name="note" class="form-control" placeholder="Bellik">
                                        </th>
                                        <th style="background: #daddff;">
                                            <button type="submit"
                                                class="btn btn-primary waves-effect waves-light">Goş</button>
                                        </th>
                                    </tr>
                                </form>

                            </thead>
                        </table>
                    </div>

                    {% partial "production/modal" %}

                    <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>Senesi</th>
                                <th>Smen</th>
                                <th>Ekskruiter</th>
                                <th>Hemmesi</th>
                                {% for key, record in records %}
                                <th>{{record.name}}</th>
                                {% endfor %}
                                <th>Bellik</th>
                                <!--  <th>Настройки</th> -->
                            </tr>
                        </thead>
                        <tbody>

                            {% for key, report in productions %}
                            <tr>
                                <td style="font-weight: bold;width: 5%;">{{key + 1}}</td>
                                <td><a href="#" style="font-weight: bold;color: #0005c5;">{{report.created_at|date('d.m.Y |
                                        H:i')}}</a></td>
                                <td><a href="#" style="font-weight: bold;color: #1e2038;">{{report.shift.desc}}</a></td>
                                <td style="text-align: center;">
                                    <span class="badge badge-soft-info"
                                        style="font-size: 14px;">{{report.excruiter.name}}</span>
                                </td>
                                <td style="text-align: center;">
                                    <span class="badge badge-soft-success"
                                        style="font-size: 14px;">{{report.all_amount|number_format}} kg</span>
                                </td>

                                {% for product in report.pivot_production %}
                                <td> <a href="#" data-bs-toggle="modal"
                                        data-bs-target=".bs-example-modal-sm-{{product.id}}">{{product.amount_percentage|number_format(2)}}
                                        %</a></td>



                                <div class="modal fade bs-example-modal-sm-{{product.id}}" tabindex="-1" role="dialog"
                                    aria-labelledby="mySmallModalLabel" aria-hidden="true">
                                    <div class="modal-dialog modal-sm">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h5 class="modal-title" id="mySmallModalLabel">{{product.product_name}}
                                                    - {{report.created_at|date('d.m | H:i')}}</h5>
                                                <button type="button" class="btn-close" data-bs-dismiss="modal"
                                                    aria-label="Close"></button>
                                            </div>
                                            <div class="modal-body">
                                                <form data-request="onUpdatePivotProduction" method="POST">
                                                    <input type="number" step="0.01" pattern="/^-?\d+\.?\d*$/"
                                                        onKeyPress="if(this.value.length==4) return false;"
                                                        name="product_{{product.id}}" class="form-control"
                                                        placeholder="{{product.product_name}} %"
                                                        value="{{product.amount_percentage|number_format(2)}}">

                                                    <input type="hidden" value="{{product.id}}" name="product_id" />
                                                    <button type="submit"
                                                        class="btn btn-primary waves-effect waves-light"
                                                        style="margin-top: 15px;width: 100%;">Üýtget</button>
                                                </form>
                                            </div>
                                        </div><!-- /.modal-content -->
                                    </div><!-- /.modal-dialog -->
                                </div><!-- /.modal -->
                                {% endfor %}

                                <td style="font-weight: bold;color: #0005c5;">{{report.note}}</td>
                            </tr>


                            {% endfor %}



                        </tbody>
                        <tfoot>
                            <tr>
                                <th style="width: 5%;">№</th>
                                <th>Senesi</th>
                                <th>Smen</th>
                                <th>Ekskruiter</th>
                                <th>Hemmesi</th>
                                {% for key, record in records %}
                                <th>{{record.name}}</th>
                                {% endfor %}
                                <th>Bellik</th>
                            </tr>
                        </tfoot>
                    </table>
                </div>



            </div>
        </div>


    </div>

</div>

{% partial 'dataTableJs' %}

{% put scripts %}

<script>
    function backStep1() {
        document.getElementById("step1").setAttribute("style", "display:contents");
        document.getElementById("step2").setAttribute("style", "display:none");

        $("#back_btn").html(`<a href="#">HASABAT</a>`);
    }

    function goSecondStep() {
        var shift = document.getElementById("shift_id").value;
        var allAmount = document.getElementById("all_amount_input").value;


        if (shift == 0) {

            window.alert("SMEN SAÝLA!");
        } else if (allAmount == "") {
            window.alert("JEMI MUKDARY GIRIZ!");
        } else {
            document.getElementById("step1").setAttribute("style", "display:none");
            document.getElementById("step2").setAttribute("style", "display:contents");

            $("#back_btn").html(`<a href="#" onclick="backStep1();">YZA 1-nji ädime</a>`);
        }


    }

    function getAllAmount(amount) {
        var prodCount = document.getElementById('product_count').innerHTML;
        //var all_amount = document.getElementById("all_amount").value;
        $('#all_amount').html(`JEMI: ` + amount + ` kg`);

        if (amount) {
            for (let i = 0; i < prodCount; i++) {
                $("#product_" + i).prop('disabled', false);
            }
        } else {
            for (let i = 0; i < prodCount; i++) {
                $("#product_" + i).prop('disabled', true);
            }
        }
    }

    function getValues() {
        var prodCount = document.getElementById('product_count').innerHTML;

        var fieldValues = [];
        var all_amount_value = $("input[name='all_amount']").val();
        var allAmount = all_amount_value ? parseFloat(all_amount_value) : 0;
        // console.log(prodCount);
        for (let i = 0; i < prodCount; i++) {

            var prod = document.getElementById("product_" + i).value;
            var prodId = document.getElementById("product_id_" + i).innerHTML;

            fieldValues[i] = prod ? parseFloat(prod) : 0;

            calculate(prodId, prod, allAmount);
            //console.log("q" + i + ": " + prod);
        }

        var all = eval(fieldValues.join('+'));

        $('#percentage').html(`<i class="mdi mdi-bullseye-arrow me-3"></i> ` + all.toFixed(2) + ` %`);
        //  console.log(fieldValues);
        //  console.log(eval(fieldValues.join('+')));
        // return eval(fieldValues.join('+'));
    }

    function calculate(prodId, value, all_amount) {
        $.request('onCalculateAvg', {
            data: { 'prod_id': prodId, 'field_value': value, 'all_amount': all_amount },
            success: function (data) {

                $('#avg_percentage_' + prodId).html(`<i class="mdi mdi-bullseye-arrow me-3"></i> ` + data.avg_percentage + ` %`);
                $('#avg_amount_' + prodId).html(data.product_name + ` : ` + (data.avg_amount) + ` kg`);

            }
        });
    }

</script>

{% endput %}
