<?php

namespace Romanah\Gokbakja\Components;

use Cms\Classes\ComponentBase;
use Romanah\Gokbakja\Models\Product as ProductModel;
use Romanah\Gokbakja\Models\Production as ProductionModel;
use Romanah\Gokbakja\Models\ProductionMachine as ProductionMachineModel;
use Romanah\Gokbakja\Models\PivotProduction as PivotProductionModel;
use Redirect;
use Carbon\Carbon;
use Flash;
// use Illuminate\Validation\Validator;
use \Validator;
use DB;

class MachineProduction extends ComponentBase
{


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


    public function onRender()
    {

        $html_data = '';

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

        $machineProductions = ProductionMachineModel::with(['bag_type', 'bag_size', 'machine.building', 'employee', 'mechanic'])->orderBy('id', 'DESC')->whereDate('created_at', date($currentDateFormat))->get();

        $html_data = '';
        for ($x = 0; $x < count($machineProductions); $x++) {
            // dd($machineProductions[0]->bag_size);
            $html_data .= '<tr>
                        <td style="font-weight: bold;">' . ($x + 1) . '</td>
                        <td><a href="#" style="font-weight: bold;">' . $machineProductions[$x]->building_name . '</a></td>
                        <td>' . $machineProductions[$x]->machine->name . '</td>
                        <td>' . $machineProductions[$x]->bag_size->name . '</td>
                        <td>' . $machineProductions[$x]->bag_type->name . '</td>
                        <td><span class="badge badge-soft-success"
                                style="font-size: 14px;">' . number_format($machineProductions[$x]->produced_weight) . ' kg</span>
                        </td>
                        <td>' . $machineProductions[$x]->employee_name . '</td>
                        <td>' . $machineProductions[$x]->mechanic_name . '</td>
                        <td>' . $machineProductions[$x]->note . '</td>
                    </tr>';
        }

        return  $html_data;
    }

    public function onReport()
    {

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

        $data = post();


        $machineProductions = ProductionMachineModel::with(['bag_type', 'bag_size', 'machine.building', 'employee', 'mechanic'])->orderBy('id', 'DESC');

        $start = Carbon::parse($data["start"])->format('Y-m-d');
        $end = Carbon::parse($data["end"])->format('Y-m-d');;
        $machine = $data["machine_id"];
        $mechanic = $data["mechanic_id"];
        $size = $data["size_id"];
        $type = $data["type_id"];


        if ($machine) {
            $machineProductions->where("machine_id", $machine);
        }

        if ($mechanic) {
            $machineProductions->where("mechanic_id", $mechanic);
        }

        if ($size) {
            $machineProductions->where("size_id", $size);
        }

        if ($type) {
            $machineProductions->where("type_id", $type);
        }

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

        $machineProductionsFiltered = $machineProductions->get();
        $machineProductionsSum = $machineProductions->sum("produced_weight");

            $html_data = '';

            for ($x = 0; $x < count($machineProductionsFiltered); $x++) {
                $html_data .= '<tr>
                        <td style="font-weight: bold;">' . ($x + 1) . '</td>
                        <td><a href="#" style="font-weight: bold;">' . $machineProductionsFiltered[$x]->created_at->format("d.m.Y") . '</a></td>
                        <td><a href="#" style="font-weight: bold;">' . $machineProductionsFiltered[$x]->building_name . '</a></td>
                        <td>' . $machineProductionsFiltered[$x]->machine->name . '</td>
                        <td>' . $machineProductionsFiltered[$x]->bag_size->name . '</td>
                        <td>' . $machineProductionsFiltered[$x]->bag_type->name . '</td>
                        <td><span class="badge badge-soft-success"
                                style="font-size: 14px;">' . number_format($machineProductionsFiltered[$x]->produced_weight) . ' kg</span>
                        </td>
                        <td>' . $machineProductionsFiltered[$x]->employee_name . '</td>
                        <td>' . $machineProductionsFiltered[$x]->mechanic_name . '</td>
                        <td>' . $machineProductionsFiltered[$x]->note . '</td>
                    </tr>';
            }

            if ($machineProductionsFiltered) {
                return [
                    '#machine_report_datas' => $html_data,
                    '#all_amount' => '<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 Öndürlen Önüm: '.number_format($machineProductionsSum).' kg</h5>
                                            </div>
                                        </div>',
                ];
            } else {
                Flash::error("Yalnyshlyk bar!!");
            }


    }

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

        $data = post();

        if (empty($data["produced_weight"]) || $data["type_id"] == 0 || $data["size_id"] == 0 || $data["machine_id"] == 0) {
            Flash::error("Gutulary Dolduryn!");
            return [
                '#validationq' => '  Gutulary Dolduryn!',
            ];
        } else {
            $createProductionMachine = new ProductionMachineModel();

            $createProductionMachine->type_id = $data["type_id"];
            $createProductionMachine->size_id = $data["size_id"];
            $createProductionMachine->machine_id = $data["machine_id"];
            $createProductionMachine->produced_weight = $data["produced_weight"];
            $createProductionMachine->note = $data["note"];
            $createProductionMachine->user_id = $user->id;
            $createProductionMachine->save();

            $productionMachine = ProductionMachineModel::where('id', $createProductionMachine->id)->with(['machine.employee', 'machine.mechanic', 'machine.building'])->first();
            // dd($productionMachine->machine->name);
            if ($productionMachine) {
                $updateResult = ProductionMachineModel::where('id', $productionMachine->id)
                    ->update(array(
                        'employee_id' => $productionMachine->machine->employee->id,
                        'mechanic_id' => $productionMachine->machine->mechanic->id,
                        'employee_name' => $productionMachine->machine->employee->name,
                        'mechanic_name' => $productionMachine->machine->mechanic->name,
                        'building_name' => $productionMachine->machine->building->name,
                    ));
            }

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

            $machineProductions = ProductionMachineModel::with(['bag_type', 'bag_size', 'machine.building', 'employee', 'mechanic'])->whereDate('created_at', date($currentDateFormat))->orderBy('id', 'DESC')->get();

            $html_data = '';

            for ($x = 0; $x < count($machineProductions); $x++) {
                $html_data .= '<tr>
                        <td style="font-weight: bold;">' . ($x + 1) . '</td>
                        <td><a href="#" style="font-weight: bold;">' . $machineProductions[$x]->building_name . '</a></td>
                        <td>' . $machineProductions[$x]->machine->name . '</td>
                        <td>' . $machineProductions[$x]->bag_size->name . '</td>
                        <td>' . $machineProductions[$x]->bag_type->name . '</td>
                        <td><span class="badge badge-soft-success"
                                style="font-size: 14px;">' . number_format($machineProductions[$x]->produced_weight) . ' kg</span>
                        </td>
                        <td>' . $machineProductions[$x]->employee_name . '</td>
                        <td>' . $machineProductions[$x]->mechanic_name . '</td>
                        <td>' . $machineProductions[$x]->note . '</td>
                    </tr>';
            }

            if ($createProductionMachine &&  $updateResult) {

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