<?php

namespace Romanah\Gokbakja\Components;

use Cms\Classes\ComponentBase;
use Romanah\Gokbakja\Models\Production as ProductionModel;
use Romanah\Gokbakja\Models\PivotProduction as PivotProductionModel;
use Romanah\Gokbakja\Models\SewerProduction as SewerModel;
use Redirect;
use Carbon\Carbon;
use Flash;
use DB;

class Sewer extends ComponentBase
{

    public $sewers;

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



    // public function onRun() {
    //     $this->sewers = $this->loadSewers();
    // }



    public function onRender(){

        $html_data = '';

        $sewerDatas = SewerModel::with("employee")->orderBy('id', 'DESC')->get();

        for ($x = 0; $x < count($sewerDatas); $x++) {
            $html_data .= '<tr>
                        <td style="font-weight: bold;">'.($x+1).'</td>
                        <td><a href="#" style="font-weight: bold;">'.$sewerDatas[$x]->employee->name.'</a></td>
                       <td>'.$sewerDatas[$x]->created_at->format('d.m.Y').'</td>
                        <td><span class="badge badge-soft-success"
                                style="font-size: 14px;">'.$sewerDatas[$x]->amount.' kg</span>
                        </td>
                        <td>'.$sewerDatas[$x]->produced_bag_qty.'</td>
                        <td>'.$sewerDatas[$x]->note.'</td>
                    </tr>';
        }

        return  $html_data;
    }

    public function onReportSewer()
    {

        $currentDate = Carbon::now()->timezone('UTC +05:00');
        $currentDateFormat =  $currentDate->format('Y-m-d');

        $data = post();


        $sewerProductions = SewerModel::with("employee")->orderBy('id', 'DESC');

        $start = Carbon::parse($data["start"])->format('Y-m-d');
        $end = Carbon::parse($data["end"])->format('Y-m-d');;
        $employee = $data["employee_id"];
        $min = $data["min"];
        $max = $data["max"];
        $amount = $data["amount"];


        if ($employee) {
            $sewerProductions->where("employee_id", $employee);
        }

        if ($min){
            $sewerProductions->whereRaw('produced_bag_qty >'.$min);
        }

        if ($max){
            $sewerProductions->whereRaw('produced_bag_qty <'.$max);
        }

        if ($amount){
            $sewerProductions->whereRaw('amount <='.$amount);
        }

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

        $sewerProductionsFiltered = $sewerProductions->get();
        $sewerProductionsSumBagQty = $sewerProductions->sum("produced_bag_qty");
        $sewerProductionsamount = $sewerProductions->sum("amount");

            $html_data = '';

            for ($x = 0; $x < count($sewerProductionsFiltered); $x++) {
                $html_data .= '<tr>
                        <td style="font-weight: bold;">'.($x+1).'</td>
                        <td><a href="#" style="font-weight: bold;">'.$sewerProductionsFiltered[$x]->employee->name.'</a></td>
                       <td>'.$sewerProductionsFiltered[$x]->created_at->format('d.m.Y').'</td>
                        <td><span class="badge badge-soft-success"
                                style="font-size: 14px;">'.$sewerProductionsFiltered[$x]->amount.' kg</span>
                        </td>
                        <td>'.$sewerProductionsFiltered[$x]->produced_bag_qty.'</td>
                        <td>'.$sewerProductionsFiltered[$x]->note.'</td>
                    </tr>';
            }

            if ($sewerProductionsFiltered) {
                return [
                    '#sewer_report_datas' => $html_data,
                    '#all_amount' => '<div class="col-md-6">
                                        <h3 class="card-title" style="font-size: 22px;color: #1e2038;">Tikinçiler Boýunça Umumy</h3>
                                        <p class="card-title-desc" style="color: #6c6ff5;">Hasabat</p>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="card bg-info 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> Jemi Dikilen Halta: '.number_format($sewerProductionsSumBagQty).' sany</h5>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <div class="card bg-info 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> Jemi Sarp Edilen Çig mal: '.number_format($sewerProductionsamount).' kg</h5>
                                            </div>
                                        </div>
                                    </div>',
                ];
            } else {
                Flash::error("Yalnyshlyk bar!!");
            }


    }

    public function onCreateSewerProduction()
    {
        $currentDate = Carbon::now()->timezone('UTC +05:00');

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

        $data = post();

        $createSewer = new SewerModel();
        $createSewer->employee_id = $data["employee_id"];
        $createSewer->amount = $data["amount"];
        $createSewer->produced_bag_qty = $data["produced_bag_qty"];
        $createSewer->note = $data["note"];
        $createSewer->save();


        $html_data = '';

        $sewerDatas = SewerModel::with("employee")->orderBy('id', 'DESC')->get();

        for ($x = 0; $x < count($sewerDatas); $x++) {
            $html_data .= '<tr>
                        <td style="font-weight: bold;">'.($x+1).'</td>
                        <td><a href="#" style="font-weight: bold;">'.$sewerDatas[$x]->employee->name.'</a></td>
                       <td>'.$sewerDatas[$x]->created_at->format('d.m.Y').'</td>
                        <td><span class="badge badge-soft-success"
                                style="font-size: 14px;">'.$sewerDatas[$x]->amount.' kg</span>
                        </td>
                        <td>'.$sewerDatas[$x]->produced_bag_qty.'</td>
                        <td>'.$sewerDatas[$x]->note.'</td>
                    </tr>';
        }

        if($createSewer){

            Flash::success("Hasabat Ustunlikli Goşuldy");
            return [
                '#sewer_datas' => $html_data,
            ];
        }else{
            return Flash::error("Yalnyshlyk bar!!");
        }


    }


}
