<?php

namespace Romanah\Gokbakja\Components;

use Cms\Classes\ComponentBase;
use Redirect;
use Flash;
use Romanah\Gokbakja\Models\Drobika as ModelsDrobika;
use Romanah\Gokbakja\Models\SewerProduction;
use Carbon\Carbon;

class Drobika extends ComponentBase
{


    public function componentDetails()
    {
        return [
            'name'        => 'Drobika',
            'description' => 'Drobika settings'
        ];
    }

    public function onReportDrobika()
    {

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

        $data = post();


        $drobikaProductions = ModelsDrobika::with(["shift", "product"])->orderBy('id', 'DESC');

        $product = $data["product_id"];
        $shift_id = $data["shift_id"];
        $start = Carbon::parse($data["start"])->format('Y-m-d');
        $end = Carbon::parse($data["end"])->format('Y-m-d');


        if ($product) {
            $drobikaProductions->where("product_id", $product);
        }

        if ($shift_id) {
            $drobikaProductions->where("shift_id", $shift_id);
        }

        if ($start != $currentDateFormat) {
            $drobikaProductions->whereBetween('date', [$start, $end]);
        }

        $drobikaDatas = $drobikaProductions->get();


        $sumProduced = $drobikaDatas->sum("produced_weight");
        $sumAmount = $drobikaDatas->sum("amount");


        $html_data = '';

        for ($x = 0; $x < count($drobikaDatas); $x++) {
            $statusTitle = '';

            $html_data .= '<tr>
                                <td>'.($x+1).'</td>
                                <td style="font-weight: bold;color: #0005c5;">'.$drobikaDatas[$x]->product->code.'</td>
                                <td>'.$drobikaDatas[$x]->shift->desc.'</td>
                                <td>'.(Carbon::parse($drobikaDatas[$x]->date)->format('d.m.Y') ?? '').'</td>
                                <td><span class="badge badge-soft-primary"
                                    style="font-size: 14px;">'.number_format($drobikaDatas[$x]->amount).' kg</span></td>
                                <td> <span class="badge badge-soft-success"
                                    style="font-size: 14px;">'.number_format($drobikaDatas[$x]->produced_weight).' kg</span></td>
                                <td>'.$drobikaDatas[$x]->note.'</td>
                            </tr>';
        }

        if ($drobikaDatas) {
            return [
                '#drobika_datas' => $html_data,
                '#all_amount' => '
                                    <div class="col-md-3">
                                        <div class="card bg-warning text-white-50">
                                            <div class="card-body">
                                                <h5 class="text-white" style="text-transform: uppercase;margin-bottom: 0;"><i class="mdi mdi-bullseye-arrow me-3"></i> Işlenen Çig mal: ' . number_format($sumAmount) . ' kg</h5>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="card bg-success text-white-50">
                                            <div class="card-body">
                                                <h5 class="text-white" style="text-transform: uppercase;margin-bottom: 0;"><i class="mdi mdi-bullseye-arrow me-3"></i> Önen çig mal : ' . number_format($sumProduced) . ' kg</h5>
                                            </div>
                                        </div>
                                    </div>',
            ];
        } else {
            Flash::error("Yalnyshlyk bar!!");
        }
    }

    public function onModalSetDrobika()
    {

        $data = post();

        $drobika = ModelsDrobika::where("id", $data["id"])->first();

        $html_data = '<div class="modal-header">
                            <h5 class="modal-title" id="mySmallModalLabel">Drobika No #' . $drobika->id . '</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal"
                                aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <form data-request="onUpdateDrobika" method="POST" data-request-flash>
                                <div class="row">



                                <div class="col-md-12 mt-3">
                                    <div>
                                    <label class="form-label">Ugramaly Senesi</label>
                                        <div class="input-group" id="datepicker2">
                                            <input type="text" class="form-control" placeholder="d.m.Y"
                                                data-date-format="d.m.yyyy" data-date-container="#datepicker2" data-provide="datepicker"
                                                data-date-autoclose="true" name="date" value="' . (Carbon::parse($drobika->date)->format('d.m.Y')) . '">

                                            <span class="input-group-text"><i class="mdi mdi-calendar"></i></span>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-12 mt-3">
                                    <div>
                                        <label class="form-label">Enjama goýulan mukdar (kg)</label>

                                        <input type="number" name="amount" step="0.01" class="form-control"
                                            placeholder="Enjama goýulan mukdar" value="' . ($drobika->amount) . '">
                                    </div>
                                </div>

                                <div class="col-md-12 mt-3">
                                    <div>
                                        <label class="form-label">Önen mukdar (kg)</label>

                                        <input type="number" name="produced_weight" step="0.01" class="form-control"
                                            placeholder="Önen mukdar" value="' . ($drobika->produced_weight) . '">
                                    </div>
                                </div>

                                <div class="col-md-12 mt-3">
                                    <div>
                                        <label class="form-label">Bellik</label>

                                        <input type="text" name="note" class="form-control" placeholder="Bellik" value="' . $drobika->note . '">
                                    </div>
                                </div>

                                </div>
                                <input type="hidden" name="id" value="' . $data["id"] . '">
                                <button type="submit" data-bs-dismiss="modal"
                                    class="btn btn-primary waves-effect waves-light"
                                    style="margin-top: 15px;width: 100%;">Üýtget</button>
                            </form>
                        </div>';

        return [
            '#modal-form' => $html_data,
        ];
    }


    public function onCreateDrobika()
    {
        $user = \Auth::user();

        $data = post();

        $drobika = new ModelsDrobika ();
        $drobika->product_id = $data["product_id"];
        $drobika->shift_id = $data["shift_id"];
        $drobika->user_id = $user->id;
        $drobika->amount = $data["amount"];
        $drobika->date = Carbon::parse($data["date"])->format('Y-m-d');;
        $drobika->note = $data["note"];
        $drobika->produced_weight = $data["produced_weight"];
        $drobika->save();

        if ($drobika) {
            Flash::success("Maglumatlar Ustunlikli Girizildi");
            return Redirect::refresh();
        } else {
            Flash::error("Yalnyshlyk bar!!");
            return Redirect::refresh();
        }

    }

    public function onUpdateDrobika()
    {

        $data = post();

        $drobika = ModelsDrobika::where('id', $data["id"])->first();
        $drobika->amount = $data["amount"];
        $drobika->date = Carbon::parse($data["date"])->format('Y-m-d');;
        $drobika->note = $data["note"];
        $drobika->produced_weight = $data["produced_weight"];
        $drobika->save();

        if ($drobika) {
            Flash::success("Maglumatlar Ustunlikli Girizildi");
            return Redirect::refresh();
        } else {
            Flash::error("Yalnyshlyk bar!!");
            return Redirect::refresh();
        }

    }

    public function onDeleteDrobika()
    {

        $data = post();

        $drobika = ModelsDrobika::where('id', $data["id"])->first();
        $drobika->delete();

        if ($drobika) {
            Flash::success("Maglumatlar Ustunlikli Pozuldy");
            return Redirect::refresh();
        } else {
            Flash::error("Yalnyshlyk bar!!");
            return Redirect::refresh();
        }

    }


}
