<?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;
use Romanah\Gokbakja\Models\Journal as ModelsJournal;
use Romanah\Gokbakja\Models\ProductionMachine;
use Romanah\Gokbakja\Models\PivotSewer;

class Journal extends ComponentBase
{


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


    public function onModalSetIsRead()
    {

        $data = post();

        $journal = ModelsJournal::where('id', $data["journalId"])
        ->first();

        $oldData =   json_decode(json_encode((object) $journal->data), FALSE);;

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

                                <div class="col-md-12 mt-3">
                                <table id="datatable-buttons2" class="table table-striped table-bordered dt-responsive nowrap"
                                        style="border-collapse: collapse; border-spacing: 0; width: 100%;">

                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>Ady</th>
                                                <th>Köne Maglumat</th>
                                                <th>Täze Maglumat</th>
                                            </tr>
                                        </thead>
                                            <tr>
                                                <td>1</td>
                                                <td>Görnüş Ady</td>
                                                <td>'.$oldData->old_data->bag_type->name.'</td>
                                                <td>'.$oldData->new_data->bag_type->name.'</td>
                                            </tr>
                                            <tr>
                                                <td>2</td>
                                                <td>Ölçegi</td>
                                                <td>'.$oldData->old_data->bag_size->name.'</td>
                                                <td>'.$oldData->new_data->bag_size->name.'</td>
                                            </tr>
                                            <tr>
                                                <td>3</td>
                                                <td>Enjam</td>
                                                <td>'.$oldData->old_data->machine->name.'</td>
                                                <td>'.$oldData->new_data->machine->name.'</td>
                                            </tr>
                                            <tr>
                                                <td>4</td>
                                                <td>Jogapkär</td>
                                                <td>'.$oldData->old_data->employee->name.'</td>
                                                <td>'.$oldData->new_data->employee->name.'</td>
                                            </tr>
                                            <tr>
                                                <td>5</td>
                                                <td>Mehanik</td>
                                                <td>'.$oldData->old_data->mechanic->name.'</td>
                                                <td>'.$oldData->new_data->mechanic->name.'</td>
                                            </tr>
                                            <tr>
                                                <td>6</td>
                                                <td>Reňki</td>
                                                <td>'.$oldData->old_data->color->name.'</td>
                                                <td>'.$oldData->new_data->color->name.'</td>
                                            </tr>
                                            <tr>
                                                <td>7</td>
                                                <td>Öndürlen önüm (kg)</td>
                                                <td>'.$oldData->old_data->produced_weight.' kg</td>
                                                <td>'.$oldData->new_data->produced_weight.' kg</td>
                                            </tr>

                                </table>

                                </div>

                                </div>';
                                $html_data .=' <input type="hidden" name="journal_id" value="' . $data["journalId"] . '">';

                                if(!$journal->is_read){
                                    $html_data .= '<button type="submit" data-bs-dismiss="modal"
                                    class="btn btn-primary waves-effect waves-light"
                                    style="margin-top: 15px;width: 100%;">Okaldy</button>';
                                }



                            $html_data .='</form>
                        </div>';

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

    public function onUpdateJournalItem(){
        $data = post();

        $journal = ModelsJournal::where('id', $data["journal_id"])->first();
        $journal->is_read = 1;
        $journal->save();

        if($journal){
            Flash::success("Hasabat Ustunlikli Okaldy");
            return Redirect::refresh();
        } else {
            Flash::error("Yalnyshlyk bar!!");
            return Redirect::refresh();
        }

    }

}
