<?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\Order as OrderModel;
use Romanah\Gokbakja\Models\OrderItem as OrderItemModel;
use Romanah\Gokbakja\Models\Shipping as ShippingModel;
use Romanah\Gokbakja\Models\ShippingTransport as ShippingTransportModel;
use Romanah\Gokbakja\Models\Payment as PaymentModel;
use Redirect;
use Carbon\Carbon;
use Flash;
use DB;
use October\Rain\Support\Facades\Flash as FacadesFlash;

class Order extends ComponentBase
{



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

    public function onReportOrder()
    {

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

        $data = post();


        $orders = OrderModel::with(["client", "shipping"])
            ->withCount(['order_items as qty'])
            ->withCount(['order_items as order_all_price' => function ($query) {
                $query->select(DB::raw('sum(price) * sum(amount) / qty'));
            }])
            ->withCount(['payment as all_payments' => function ($query) {
                $query->select(DB::raw('sum(amount)'));
            }])
            ->orderBy('id', 'DESC');


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

        $client_id = $data["client_id"];
        $contract_no = $data["contract_no"];
        $note = $data["note"];


        if ($client_id) {
            $orders->where("client_id", $client_id);
        }

        if ($contract_no) {
            $orders->where("contract_no", "like", "%" . $contract_no . "%");
        }

        if ($note) {
            $orders->where("note", "like", "%" . $note . "%");
        }

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

        $ordersFiltered = $orders->get();
        $allPaid = 0;
        $allNotPaid = 0;

        $html_data = '';

        for ($x = 0; $x < count($ordersFiltered); $x++) {
            $status = '';

            $allPaid += $ordersFiltered[$x]->all_payments;
            $allNotPaid += ($ordersFiltered[$x]->order_all_price - $ordersFiltered[$x]->all_payments);

            $mustPay = '';
            $mustPayCalc = number_format(($ordersFiltered[$x]->order_all_price - $ordersFiltered[$x]->all_payments), 2);

            if($mustPayCalc < 0){
                $mustPay = 'artykmaç '.abs($mustPayCalc).' $';
            }else if($mustPayCalc == 0){
                $mustPay = '';
            } else{
                $mustPay = $mustPayCalc.' $';
            }

            $html_data .= '<tr>
                            <td style="font-weight: bold;">' . ($x + 1) . '</td>
                            <td><a href="/order-detail/' . $ordersFiltered[$x]->id . '" style="font-weight: bold;">Sargyt #' . $ordersFiltered[$x]->id . '</a></td>
                            <td><a href="/order-detail/' . $ordersFiltered[$x]->id . '" style="font-weight: bold;">' . $ordersFiltered[$x]->client->name . '</a></td>
                            <td><a href="/order-detail/' . $ordersFiltered[$x]->id . '" style="font-weight: bold;">' . $ordersFiltered[$x]->contract_no . '</a></td>
                            <td>' . $ordersFiltered[$x]->client->country . '</td>
                            <td><span class="badge badge-soft-primary"
                                    style="font-size: 14px;">' . number_format($ordersFiltered[$x]->order_all_price) . ' $</span>
                            </td>
                            <td><span class="badge badge-soft-success"
                                    style="font-size: 14px;">' . number_format($ordersFiltered[$x]->all_payments) . ' $</span>
                            </td>
                            <td><a href="#" class="badge badge-soft-danger" style="font-size: 14px;">' .  $mustPay . '</a>
                            </td>
                            <td>' . $ordersFiltered[$x]->created_at->format('d.m.Y') . '</td>
                            <td>' . $ordersFiltered[$x]->note . '</td>

                        </tr>';
        }

        if ($ordersFiltered) {
            return [
                '#orders_report_datas' => $html_data,
                '#all_amount_report' => '
                                <div class="col-md-4">
                                    <h3 class="card-title" style="font-size: 22px;color: #1e2038;">Sargytlar Boýunça Umumy</h3>
                                    <p class="card-title-desc" style="color: #6c6ff5;">Hasabat</p>
                                </div>
                                <div class="col-md-4">
                                    <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 Tölenen: '.number_format($allPaid).' $</h5>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="card bg-danger 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 Bergiler: '.number_format($allNotPaid).' $</h5>
                                        </div>
                                    </div>
                                </div>

                                ',
            ];
        } else {
            Flash::error("Yalnyshlyk bar!!");
        }
    }

    public function onReportLogistics()
    {

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

        $data = post();


        $shippingTransports = ShippingTransportModel::with("shipping.order")
                                // ->withCount(['loaded_items as loaded_item_amount' => function($query) {
                                //     $query->select(DB::raw('sum(loaded_amount)'));
                                // }])
                                ->orderBy('shipping_id', 'DESC');

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

        $transport = $data["transport_type"];
        $place = $data["place_now"];
        $status = $data["status"];
        $transport_no = $data["transport_no"];


        if ($transport) {
            $shippingTransports->where("transport_type", $transport);
        }

        if ($place) {
            $shippingTransports->where("place_now", "like", "%" . $place . "%");
        }

        if ($transport_no) {
            $shippingTransports->where("transport_no", "like", "%" . $transport_no . "%");
        }

        if ($status) {
            $shippingTransports->where("status", $status);
        }

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

        $shippingTransportsFiltered = $shippingTransports->get();

        // dd($shippingTransports->loaded_item_amount);

        $shippingTransportsAmountLoaded = $shippingTransports
        ->withCount(['loaded_items as loaded_item_amount' => function($query) {
            $query->select(DB::raw('sum(loaded_amount)'));
        }])->first();


        $html_data = '';

        for ($x = 0; $x < count($shippingTransportsFiltered); $x++) {
            $status = '';

            if ($shippingTransportsFiltered[$x]->status == "loaded") {
                $status = '<span class="badge badge-soft-info"
                style="font-size: 14px;">ÝÜKLENDI</span>';
            }

            if ($shippingTransportsFiltered[$x]->status == "loading") {
                $status = '<span class="badge badge-soft-primary"
                style="font-size: 14px;">ÝÜKLENÝÄR</span>';
            }

            if ($shippingTransportsFiltered[$x]->status == "proccess") {
                $status = '<span class="badge badge-soft-warning"
                style="font-size: 14px;">ÝOLDA</span>';
            }

            if ($shippingTransportsFiltered[$x]->status == "complated") {
                $status = '<span class="badge badge-soft-success"
                style="font-size: 14px;">TAMAMLANDY</span>';
            }

            $transportTitle = '';
            if($shippingTransportsFiltered[$x]->transport_type == 'truck')

            $transportTitle = 'TYR';

            else{
                $transportTitle = "WAGON";
            }

            $html_data .= '<tr>
                        <td style="font-weight: bold;">' . ($x + 1) . '</td>
                        <td><a href="/orders/transport/loaded/' . $shippingTransportsFiltered[$x]->shipping->order_id . '/'.$shippingTransportsFiltered[$x]->id.'" style="font-weight: bold;">' . $transportTitle . '</a></td>
                       <td>' . $shippingTransportsFiltered[$x]->transport_no . '</td>
                       <td><a href="/order-detail/' . $shippingTransportsFiltered[$x]->shipping->order_id . '" style="font-weight: bold;">Sargyt #' . $shippingTransportsFiltered[$x]->shipping->order_id . '</a></td>


                        <td>' . $shippingTransportsFiltered[$x]->place_now . '</td>
                        <td>' . $status . '</td>
                        <td>' . $shippingTransportsFiltered[$x]->note . '</td>
                    </tr>';
        }

        if ($shippingTransportsFiltered) {
            return [
                '#logistics_report_datas' => $html_data,
                '#all_amount' => ' <div class="col-md-6">
                                        <h3 class="card-title" style="font-size: 22px;color: #1e2038;">Logistika Boýunça Umumy</h3>
                                        <p class="card-title-desc" style="color: #6c6ff5;">Hasabat</p>
                                    </div>',
            ];
        } else {
            Flash::error("Yalnyshlyk bar!!");
        }
    }

    public function onRender()
    {
        $user = \Auth::user();
        $crudOrderq = $this->page["crudOrders"];

        $html_data = '';

        $orderDatas = OrderModel::with(["client", "shipping", "shippingTransport"])
            ->withCount(['order_items as qty'])
            ->withCount(['order_items as order_all_price' => function ($query) {
                $query->select(DB::raw('sum(price) * sum(amount) / qty'));
            }])
            ->withCount(['payment as all_payments' => function ($query) {
                $query->select(DB::raw('sum(amount)'));
            }])
            ->orderBy('id', 'DESC')
            ->get();


        for ($x = 0; $x < count($orderDatas); $x++) {
            $editBtn = '';

            if($orderDatas[$x]->user_id != $user->id){
                $editBtn = '<a href="#" style="color: darkorange;font-weight: bold;">Size degişli däl </a>';
            }else{
                $editBtn = $crudOrderq ? '<a href="#" data-request="onDeleteOrder" data-request-confirm="Sargyt #' . $orderDatas[$x]->id . ' pozmak isleýäňizmi?" data-request-data="orderId: ' . $orderDatas[$x]->id . '" style="color: darkred;font-weight: bold;">POZ</a>' : '';
            }

            $mustPay = '';
            $mustPayCalc = number_format(($orderDatas[$x]->order_all_price - $orderDatas[$x]->all_payments), 2);
            $mustPayCalcQQ = ($orderDatas[$x]->order_all_price - $orderDatas[$x]->all_payments);

            if($mustPayCalcQQ < 0){
                $mustPay = 'artykmaç '.abs((float)$mustPayCalc).' $';
            }else if($mustPayCalcQQ == 0){
                $mustPay = '';
            } else{
                $mustPay = $mustPayCalc.' $';
            }

            if($orderDatas[$x]->shippingTransport == null){

                $shippingDate = '';
                $status = '';

            }else{

                $shippingDate =  Carbon::parse($orderDatas[$x]->shippingTransport->date)->format('d.m.Y') ?? '';

                if ($orderDatas[$x]->shippingTransport->status == "loaded") {
                    $status = '<span class="badge badge-soft-info"
                    style="font-size: 14px;">ÝÜKLENDI</span>';
                }

                if ($orderDatas[$x]->shippingTransport->status == "loading") {
                    $status = '<span class="badge badge-soft-primary"
                    style="font-size: 14px;">ÝÜKLENÝÄR</span>';
                }

                if ($orderDatas[$x]->shippingTransport->status == "proccess") {
                    $status = '<span class="badge badge-soft-warning"
                    style="font-size: 14px;">ÝOLDA</span>';
                }

                if ($orderDatas[$x]->shippingTransport->status == "complated") {
                    $status = '<span class="badge badge-soft-success"
                    style="font-size: 14px;">TAMAMLANDY</span>';
                }


            }

            $html_data .= '<tr>
                                <td style="font-weight: bold;">' . ($x + 1) . '</td>
                                <td><a href="/order-detail/' . $orderDatas[$x]->id . '" style="font-weight: bold;">Sargyt #' . $orderDatas[$x]->id . '</a></td>
                                <td><a href="/order-detail/' . $orderDatas[$x]->id . '" style="font-weight: bold;">' . $orderDatas[$x]->client->name . '</a></td>
                                <td>' . ($orderDatas[$x]->contract_no ?? "") . '</td>
                                <td>' . $orderDatas[$x]->client->country . '</td>
                                <td><span class="badge badge-soft-primary"
                                        style="font-size: 14px;">' . number_format($orderDatas[$x]->order_all_price) . ' $</span>
                                </td>
                                <td><span class="badge badge-soft-success"
                                        style="font-size: 14px;">' . number_format($orderDatas[$x]->all_payments) . ' $</span>
                                </td>
                                <td><a href="#" class="badge badge-soft-danger" style="font-size: 14px;">' . $mustPay . '</a>
                                </td>
                                <td>' .$shippingDate . '</td>
                                <td>' .$status . '</td>
                                <td>' . $orderDatas[$x]->note . '</td>
                                <td>'.$editBtn.'</td>
                            </tr>';
        }


        return  $html_data;
    }

    public function onUpdatePaymentItem()
    {
        $data = post();
        $orderId = $this->param("orderId");


        $createPayment = PaymentModel::where("id", $data["payment_id"])->first();
        $createPayment->amount = $data["amount"];
        $createPayment->note = $data["note"];
        $createPayment->order_id = $orderId;
        $createPayment->save();

        // $html_data = '';

        // $paymentDatas = PaymentModel::where("order_id", $orderId)->orderBy('id', 'DESC')->get();

        // for ($x = 0; $x < count($paymentDatas); $x++) {
        //     // dd($orderDatas[$x]->shipping->status);
        //     $html_data .= '<tr>
        //     <td style="font-weight: bold;">' . ($x + 1) . '</td>
        //     <td><a href="#" style="font-weight: bold;">' . $paymentDatas[$x]->amount . ' $</a></td>
        //     <td><a href="#" style="font-weight: bold;">' . ($paymentDatas[$x]->date == null ? "" : Carbon::parse($paymentDatas[$x]->date)->format('d.m.Y')) . '</a></td>
        //     <td>' . $paymentDatas[$x]->note . '</td>
        //     <td><a href="#" data-request="onModalSetPayment"
        //         data-request-data="paymentId: ' . $paymentDatas[$x]->id . '"
        //         data-bs-toggle="modal" data-bs-target=".bs-example-modal-sm-1" style="color: orange;"> Täzele </a></td>
        // </tr>';
        // }

        // $order = OrderModel::where("id", $orderId)->with(["client", "shipping"])
        //     ->withCount(['order_items as order_all_amount' => function ($query) {
        //         $query->select(DB::raw('sum(amount)'));
        //     }])
        //     ->withCount(['order_items as order_all_price' => function ($query) {
        //         $query->select(DB::raw('sum(price)'));
        //     }])
        //     ->first();

        // $all = ($order->order_all_amount * $order->order_all_price);

        // $allPaid = PaymentModel::where("order_id", $orderId)->sum("amount");
        // $allNot = ($all - $allPaid);

        if ($createPayment) {

            Flash::success("Töleg Ustunlikli Goşuldy");
            return Redirect::refresh();
        } else {
            Flash::error("Yalnyshlyk bar!!");
            return Redirect::refresh();
        }
        // }
    }

    public function onModalSetPayment()
    {

        $data = post();

        $paymentItem = PaymentModel::where("id", $data["paymentId"])->first();

        $html_data = '<div class="modal-header">
                            <h5 class="modal-title" id="mySmallModalLabel">Töleg Maglumatlaryny Üytegt</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal"
                                aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <form data-request="onUpdatePaymentItem" method="POST" data-request-flash>
                                <div class="row">


                                <div class="col-md-12 mt-3">
                                    <div>
                                        <label class="form-label">Mukdar ($)</label>
                                        <input type="number" name="amount" step="0.01" class="form-control"
                                            placeholder="Mukdar ($)" value="' . $paymentItem->amount . '">
                                    </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="' . $paymentItem->note . '">
                                    </div>
                                </div>

                                </div>
                                <input type="hidden" name="payment_id" value="' . $data["paymentId"] . '">
                                <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 onCreatePaymentItem()
    {
        $data = post();
        $orderId = $this->param("orderId");


        $createPayment = new PaymentModel();
        $createPayment->date = Carbon::parse($data["date"])->format('Y-m-d');
        $createPayment->amount = $data["amount"];
        $createPayment->note = $data["note"];
        $createPayment->order_id = $orderId;
        $createPayment->save();

        // $html_data = '';

        // $paymentDatas = PaymentModel::where("order_id", $orderId)->orderBy('id', 'DESC')->get();

        // for ($x = 0; $x < count($paymentDatas); $x++) {
        //     // dd($orderDatas[$x]->shipping->status);
        //     $html_data .= '<tr>
        //                         <td style="font-weight: bold;">' . ($x + 1) . '</td>
        //                         <td><a href="#" style="font-weight: bold;">' . $paymentDatas[$x]->amount . ' $</a></td>
        //                         <td><a href="#" style="font-weight: bold;">' . ($paymentDatas[$x]->date == null ? "" : Carbon::parse($paymentDatas[$x]->date)->format('d.m.Y')) . '</a></td>
        //                         <td>' . $paymentDatas[$x]->note . '</td>
        //                         <td><a href="#" data-request="onModalSetPayment"
        //                             data-request-data="paymentId: ' . $paymentDatas[$x]->id . '"
        //                             data-bs-toggle="modal" data-bs-target=".bs-example-modal-sm-1" style="color: orange;"> Täzele </a></td>
        //                     </tr>';
        // }

        // $order = OrderModel::where("id", $orderId)->with(["client", "shipping"])
        //     ->withCount(['order_items as order_all_amount' => function ($query) {
        //         $query->select(DB::raw('sum(amount)'));
        //     }])
        //     ->withCount(['order_items as order_all_price' => function ($query) {
        //         $query->select(DB::raw('sum(price)'));
        //     }])
        //     ->first();

        // $all = ($order->order_all_amount * $order->order_all_price);

        // $allPaid = PaymentModel::where("order_id", $orderId)->sum("amount");
        // $allNot = ($all - $allPaid);

        if ($createPayment) {

            Flash::success("Töleg Ustunlikli Goşuldy");
            return Redirect::refresh();
            // return [
            //     '#payment_item_datas' => $html_data,
            //     '#allAmount' => $order->order_all_amount . " kg",
            //     '#allPrice' => $order->order_all_price . " $",
            //     '#all' => "Jemi Bahasy: " . number_format($all) . " $",
            //     '#all_payment' => "Jemi Tölenen Töleg: " . number_format($allPaid) . " $",
            //     '#all_not' => "Bergisi: " . number_format($allNot) . "$",
            // ];
        } else {
            Flash::error("Yalnyshlyk bar!!");
            return Redirect::refresh();
        }
        // }
    }

    public function onModalSetTransportChange()
    {

        $data = post();
        $transports = ShippingTransportModel::where("status", "!=", "complated")->where("id", "!=", $data["transportId"])->with(["shipping.order.client"])->orderBy("id", "DESC")->get();

        $transportItem = ShippingTransportModel::where("id", $data["transportId"])->first();

        $html_data = '<div class="modal-header">
                    <h5 class="modal-title" id="mySmallModalLabel">Ýük geçirmek </h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"
                            aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <form data-request="onUpdateTransportChange" method="POST" data-request-flash>
                            <div class="row">
                            <div class="col-md-12">
                                <label class="form-label">Ýüklenmeli Transport</label>
                                <select class="form-control select2" name="shipping_id">
                                    <option value="0">Saýla</option>';
        for ($x = 0; $x < count($transports); $x++) {

            $transportTitle = "";
            $transportType = $transports[$x]->transport_type;

            if ($transportType == "truck") {
                $transportTitle = "TYR";
            }

            if ($transportType == "train") {
                $transportTitle = "WAGON";
            }

            $html_data .= '<option value="' . $transports[$x]->id . '">Sargyt #' . $transports[$x]->shipping->order->id . ' - ' . $transports[$x]->shipping->order->client->name . ' (' . $transportTitle . ' - ' . $transports[$x]->transport_no . ')</option>';
        }
        $html_data .= '</select>
                            </div>

                            <div class="col-md-12 mt-3">
                                <div>
                                    <label class="form-label">Transport No</label>
                                    <input type="text" name="transport_no" class="form-control"
                                        placeholder="Transport No" value="' . $transportItem->transport_no . '">
                                </div>
                            </div>

                            <div class="col-md-12 mt-3">
                                <div>
                                    <label class="form-label">Beýleki Transporta Ýüklenmeli Mukdar (kg)</label>
                                    <input type="number" name="loaded_amount" step="0.01" class="form-control"
                                        placeholder="Ýüklenen Mukdar (kg)" value="' . $transportItem->loaded_amount . '">
                                </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="' . $transportItem->note . '">
                                </div>
                            </div>

                            </div>
                            <input type="hidden" name="transport_id" value="' . $data["transportId"] . '">
                            <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 onUpdateTransportChange()
    {
        $data = post();
        $orderId = $this->param("orderId");

        $orderq = OrderModel::where("id", $orderId)
            ->withCount(['order_items as order_all_amount' => function ($query) {
                $query->select(DB::raw('sum(amount)'));
            }])->first();

        $transportAmountSum = ShippingTransportModel::where("shipping_id", $orderq->shipping_id)->sum('loaded_amount');

        $allLoadedAmount = $transportAmountSum + $data["loaded_amount"];

        // dd($allLoadedAmount);
        // if ($orderq->order_all_amount < $allLoadedAmount) {
        //     Flash::error("Yuklenen mukdar jemi edilen sargytdan uly");
        // } else {
        if ($data["shipping_id"]) {
            $createShippingTransport = ShippingTransportModel::where("id", $data["transport_id"])->first();
            $createShippingTransport->loaded_amount = ((float) $createShippingTransport->loaded_amount - (float) $data["loaded_amount"]);



            $changedShippingTransport = ShippingTransportModel::where("id", $data["shipping_id"])->first();

            $changedShippingTransport->loaded_amount = ((float) $data["loaded_amount"] + (float) $changedShippingTransport->loaded_amount);

            $changedShippingTransport->note = $data["note"];
            $changedShippingTransport->transport_no = $data["transport_no"];

            $changedShippingTransport->save();

            $createShippingTransport->save();



            // $html_data = '';

            // $transportDatas = ShippingTransportModel::where("shipping_id", $orderq->shipping_id)->orderBy('id', 'DESC')->get();

            // for ($x = 0; $x < count($transportDatas); $x++) {
            //     // dd($orderDatas[$x]->shipping->status);
            //     $html_data .= '<tr>
            //                     <td style="font-weight: bold;">' . ($x + 1) . '</td>
            //                     <td><a href="#" style="font-weight: bold;">' . $transportDatas[$x]->transport_type . '</a></td>
            //                     <td>' . $transportDatas[$x]->place_now . '</td>
            //                     <td>' . $transportDatas[$x]->loaded_amount . ' kg</td>
            //                     <td>' . $transportDatas[$x]->transport_no . '</td>
            //                     <td>' . Carbon::parse($transportDatas[$x]->date)->format('d.m.Y') ?? " " . '</td>';

            //     $html_data .= '<td>' . $transportDatas[$x]->note . '</td>
            //                    <td>' . $transportDatas[$x]->status . '</td>';

            //     if ($transportDatas[$x]->status == "complated") {
            //         $html_data .= '<td><a href="#" style="color: darkgreen;"> ' . $transportDatas[$x]->status . ' </a></td>';
            //     } else {
            //         $html_data .= '<td><a href="#" data-request="onModalSetTransportChange"
            //                     data-request-data="transportId: ' . $transportDatas[$x]->id . '"
            //                     data-bs-toggle="modal" data-bs-target=".bs-example-modal-sm-1" style="color: orange;"> Çalyşmak </a></td>';
            //     }
            //     $html_data .= '<td><a href="#" data-request="onModalSetTransport"
            //                     data-request-data="transportId: ' . $transportDatas[$x]->id . '"
            //                     data-bs-toggle="modal" data-bs-target=".bs-example-modal-sm-1" style="color: orange;"> Täzele </a></td>
            //                 </tr>';
            // }

            // $order = OrderModel::where("id", $orderId)->with(["client", "shipping"])
            //     ->withCount(['order_items as order_all_amount' => function ($query) {
            //         $query->select(DB::raw('sum(amount)'));
            //     }])
            //     ->withCount(['order_items as order_all_price' => function ($query) {
            //         $query->select(DB::raw('sum(price)'));
            //     }])
            //     ->first();

            // $all = ($order->order_all_amount * $order->order_all_price);

            // $allLoaded = ShippingTransportModel::where("shipping_id", $orderq->shipping_id)->sum("loaded_amount");

            if ($createShippingTransport) {

                Flash::success("Transport Ustunlikli Goşuldy");
                return Redirect::refresh();
                // return [
                //     '#transport_item_datas' => $html_data,
                //     '#allAmount' => $order->order_all_amount . " kg",
                //     '#allPrice' => $order->order_all_price . " $",
                //     '#all' => "Jemi Bahasy: " . number_format($all, 2) . " $",
                //     '#all_loaded' => "Jemi Ýüklenen Ýük: " . number_format($allLoaded, 2) . " kg",
                // ];
            } else {
                Flash::error("Yalnyshlyk bar!!");
                return Redirect::refresh();
            }
        } else {
            Flash::error("Ýüklenmeli Transport Saýlaň!");
            return Redirect::refresh();
        }
        // }
    }

    public function onModalSetTransport()
    {

        $data = post();

        $orders = OrderModel::where("status", "new")->get();

        $transportItem = ShippingTransportModel::where("id", $data["transportId"])->first();

        $html_data = '<div class="modal-header">
                            <h5 class="modal-title" id="mySmallModalLabel">Transport Maglumatlaryny Üytget</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal"
                                aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <form data-request="onUpdateTransportItem" method="POST" data-request-flash>
                                <div class="row">
                                <div class="col-md-12">
                                    <label class="form-label">Status Saýlaň</label>
                                    <select class="form-control select2" name="status">
                                        <option value="0">Saýla</option>

                                        <option value="loading" ' . ($transportItem->status == "loading" ? "selected" : "") . '>Ýüklenýär</option>
                                        <option value="loaded" ' . ($transportItem->status == "loaded" ? "selected" : "") . '>Ýüklendi</option>
                                        <option value="proccess" ' . ($transportItem->status == "proccess" ? "selected" : "") . '>Ýolda</option>
                                        <option value="complated" ' . ($transportItem->status == "complated" ? "selected" : "") . '>Ýerine Ýetirildi</option>

                                    </select>
                                </div>

                                <div class="col-md-12 mt-3">
                                    <label class="form-label">Transport Saýlaň</label>
                                    <select class="form-control select2" name="transport_type">
                                        <option value="0">Saýla</option>

                                        <option value="truck" ' . ($transportItem->transport_type == "truck" ? "selected" : "") . '>Tyr</option>
                                        <option value="train" ' . ($transportItem->transport_type == "train" ? "selected" : "") . '>Wagon</option>

                                    </select>
                                </div>

                                <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($transportItem->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">Şu wagty ýeri</label>
                                        <input type="text" name="place_now" class="form-control"
                                            placeholder="Şu wagty ýeri" value="' . $transportItem->place_now . '">
                                    </div>
                                </div>
                                <div class="col-md-12 mt-3">
                                    <div>
                                        <label class="form-label">Transport No</label>
                                        <input type="text" name="transport_no" class="form-control"
                                            placeholder="Transport No" value="' . $transportItem->transport_no . '">
                                    </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="' . $transportItem->note . '">
                                    </div>
                                </div>

                                </div>
                                <input type="hidden" name="transport_id" value="' . $data["transportId"] . '">
                                <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 onUpdateTransportItem()
    {
        $data = post();
        $orderId = $this->param("orderId");

        $orderq = OrderModel::where("id", $orderId)
            ->withCount(['order_items as order_all_amount' => function ($query) {
                $query->select(DB::raw('sum(amount)'));
            }])->first();


        // dd($allLoadedAmount);
        // if ($orderq->order_all_amount < $allLoadedAmount) {
        //     Flash::error("Yuklenen mukdar jemi edilen sargytdan uly");
        // } else {
        $createShippingTransport = ShippingTransportModel::where("id", $data["transport_id"])->first();

        $createShippingTransport->transport_type = $data["transport_type"];
        $createShippingTransport->place_now = $data["place_now"];
        $createShippingTransport->transport_no = $data["transport_no"];
        $createShippingTransport->date = Carbon::parse($data["date"])->format('Y-m-d');
        $createShippingTransport->status = $data["status"];
        $createShippingTransport->note = $data["note"];
        $createShippingTransport->shipping_id = $orderq->shipping_id;
        $createShippingTransport->save();

        // $html_data = '';

        // $transportDatas = ShippingTransportModel::where("shipping_id", $orderq->shipping_id)->orderBy('id', 'DESC')->get();

        // for ($x = 0; $x < count($transportDatas); $x++) {
        //     // dd($orderDatas[$x]->shipping->status);
        //     $html_data .= '<tr>
        //                         <td style="font-weight: bold;">' . ($x + 1) . '</td>
        //                         <td><a href="#" style="font-weight: bold;">' . $transportDatas[$x]->transport_type . '</a></td>
        //                         <td>' . $transportDatas[$x]->place_now . '</td>
        //                         <td>' . $transportDatas[$x]->loaded_amount . ' kg</td>
        //                         <td>' . $transportDatas[$x]->transport_no . '</td>
        //                         <td>' . Carbon::parse($transportDatas[$x]->date)->format('d.m.Y') ?? " " . '</td>';

        //         $html_data .= '<td>' . $transportDatas[$x]->note . '</td>
        //                        <td>' . $transportDatas[$x]->status . '</td>';

        //         if ($transportDatas[$x]->status == "complated") {
        //             $html_data .= '<td><a href="#" style="color: darkgreen;"> ' . $transportDatas[$x]->status . ' </a></td>';
        //         } else {
        //             $html_data .= '<td><a href="#" data-request="onModalSetTransportChange"
        //                         data-request-data="transportId: ' . $transportDatas[$x]->id . '"
        //                         data-bs-toggle="modal" data-bs-target=".bs-example-modal-sm-1" style="color: orange;"> Çalyşmak </a></td>';
        //         }
        //         $html_data .= '<td><a href="#" data-request="onModalSetTransport"
        //                         data-request-data="transportId: ' . $transportDatas[$x]->id . '"
        //                         data-bs-toggle="modal" data-bs-target=".bs-example-modal-sm-1" style="color: orange;"> Täzele </a></td>
        //                     </tr>';
        // }

        // $order = OrderModel::where("id", $orderId)->with(["client", "shipping"])
        //     ->withCount(['order_items as order_all_amount' => function ($query) {
        //         $query->select(DB::raw('sum(amount)'));
        //     }])
        //     ->withCount(['order_items as order_all_price' => function ($query) {
        //         $query->select(DB::raw('sum(price)'));
        //     }])
        //     ->first();

        // $all = ($order->order_all_amount * $order->order_all_price);

        // $allLoaded = ShippingTransportModel::where("shipping_id", $orderq->shipping_id)->sum("loaded_amount");

        if ($createShippingTransport) {

            Flash::success("Transport Ustunlikli Goşuldy");
            return Redirect::refresh();
            // return [
            //     '#transport_item_datas' => $html_data,
            //     '#allAmount' => $order->order_all_amount . " kg",
            //     '#allPrice' => $order->order_all_price . " $",
            //     '#all' => "Jemi Bahasy: " . number_format($all, 2) . " $",
            //     '#all_loaded' => "Jemi Ýüklenen Ýük: " . number_format($allLoaded, 2) . " kg",
            // ];
        } else {
            Flash::error("Yalnyshlyk bar!!");
            return Redirect::refresh();
        }
        // }
    }


    public function onCreateTransportItem()
    {
        $data = post();
        $orderId = $this->param("orderId");

        $orderq = OrderModel::where("id", $orderId)
            ->withCount(['order_items as order_all_amount' => function ($query) {
                $query->select(DB::raw('sum(amount)'));
            }])->first();

        // dd($allLoadedAmount);
        // if ($orderq->order_all_amount < $allLoadedAmount) {
        //     Flash::error("Yuklenen mukdar jemi edilen sargytdan uly");
        // } else {
        $createShippingTransport = new ShippingTransportModel();
        $createShippingTransport->transport_type = $data["transport_type"];
        $createShippingTransport->place_now = "Aşgabat";
        $createShippingTransport->transport_no = $data["transport_no"];
        $createShippingTransport->note = $data["note"];
        $createShippingTransport->date = Carbon::parse($data["date"])->format('Y-m-d');
        $createShippingTransport->shipping_id = $orderq->shipping_id;
        $createShippingTransport->order_id = $orderId;
        $createShippingTransport->save();

        // $html_data = '';

        // $transportDatas = ShippingTransportModel::where("shipping_id", $orderq->shipping_id)->orderBy('id', 'DESC')->get();

        // for ($x = 0; $x < count($transportDatas); $x++) {
        //     // dd($orderDatas[$x]->shipping->status);
        //     $html_data .= '<tr>
        //                         <td style="font-weight: bold;">' . ($x + 1) . '</td>
        //                         <td><a href="#" style="font-weight: bold;">' . $transportDatas[$x]->transport_type . '</a></td>
        //                         <td>' . $transportDatas[$x]->place_now . '</td>
        //                         <td>' . $transportDatas[$x]->loaded_amount . ' kg</td>
        //                         <td>' . $transportDatas[$x]->transport_no . '</td>
        //                         <td>' . Carbon::parse($transportDatas[$x]->date)->format('d.m.Y') ?? " " . '</td>';

        //         $html_data .= '<td>' . $transportDatas[$x]->note . '</td>
        //                        <td>' . $transportDatas[$x]->status . '</td>';

        //         if ($transportDatas[$x]->status == "complated") {
        //             $html_data .= '<td><a href="#" style="color: darkgreen;"> ' . $transportDatas[$x]->status . ' </a></td>';
        //         } else {
        //             $html_data .= '<td><a href="#" data-request="onModalSetTransportChange"
        //                         data-request-data="transportId: ' . $transportDatas[$x]->id . '"
        //                         data-bs-toggle="modal" data-bs-target=".bs-example-modal-sm-1" style="color: orange;"> Çalyşmak </a></td>';
        //         }
        //         $html_data .= '<td><a href="#" data-request="onModalSetTransport"
        //                         data-request-data="transportId: ' . $transportDatas[$x]->id . '"
        //                         data-bs-toggle="modal" data-bs-target=".bs-example-modal-sm-1" style="color: orange;"> Täzele </a></td>
        //                     </tr>';
        // }

        // $order = OrderModel::where("id", $orderId)->with(["client", "shipping"])
        //     ->withCount(['order_items as order_all_amount' => function ($query) {
        //         $query->select(DB::raw('sum(amount)'));
        //     }])
        //     ->withCount(['order_items as order_all_price' => function ($query) {
        //         $query->select(DB::raw('sum(price)'));
        //     }])
        //     ->first();

        // $all = ($order->order_all_amount * $order->order_all_price);

        // $allLoaded = ShippingTransportModel::where("shipping_id", $orderq->shipping_id)->sum("loaded_amount");

        if ($createShippingTransport) {

            Flash::success("Transport Ustunlikli Goşuldy");
            return Redirect::refresh();
            // return [
            //     '#transport_item_datas' => $html_data,
            //     '#allAmount' => $order->order_all_amount . " kg",
            //     '#allPrice' => $order->order_all_price . " $",
            //     '#all' => "Jemi Bahasy: " . number_format($all, 2) . " $",
            //     '#all_loaded' => "Jemi Ýüklenen Ýük: " . number_format($allLoaded, 2) . " kg",
            // ];
        } else {
            Flash::error("Yalnyshlyk bar!!");
            return Redirect::refresh();
        }
        // }
    }

    // data-request="onModalSet" data-request-data="orderId: '.$orderDatas[$x]->id.', header: \''.$orderDatas[$x]->client->name.'\'" data-bs-toggle="modal" data-bs-target=".bs-example-modal-sm-1"

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

        $order = OrderModel::where("id", $data["orderId"])->delete();

        // $html_data = '';

        // $orderDatas = OrderModel::with(["client", "shipping"])
        //     ->withCount(['order_items as order_all_amount' => function ($query) {
        //         $query->select(DB::raw('sum(amount)'));
        //     }])
        //     ->withCount(['order_items as order_all_price' => function ($query) {
        //         $query->select(DB::raw('sum(price)'));
        //     }])
        //     ->orderBy('id', 'DESC')
        //     ->get();


        // for ($x = 0; $x < count($orderDatas); $x++) {
        //     $html_data .= '<tr>
        //                         <td style="font-weight: bold;">' . ($x + 1) . '</td>
        //                         <td><a href="/order-detail/' . $orderDatas[$x]->id . '" style="font-weight: bold;">Sargyt #' . $orderDatas[$x]->id . '</a></td>
        //                         <td><a href="/order-detail/' . $orderDatas[$x]->id . '" style="font-weight: bold;">' . $orderDatas[$x]->client->name . '</a></td>
        //                         <td>' . $orderDatas[$x]->client->country . '</td>
        //                         <td>' . number_format($orderDatas[$x]->order_all_amount) . ' kg</td>
        //                         <td><span class="badge badge-soft-primary"
        //                                 style="font-size: 14px;">' . number_format($orderDatas[$x]->order_all_price) . ' $</span>
        //                         </td>
        //                         <td><span class="badge badge-soft-success"
        //                                 style="font-size: 14px;">' . number_format($orderDatas[$x]->all_payments) . ' $</span>
        //                         </td>
        //                         <td><a href="#" class="badge badge-soft-danger" style="font-size: 14px;">' .  number_format($orderDatas[$x]->order_all_price - $orderDatas[$x]->all_payments) . ' $</a>
        //                         </td>
        //                         <td>' . $orderDatas[$x]->created_at->format('d.m.Y') . '</td>
        //                         <td>' . $orderDatas[$x]->note . '</td>
        //                         <td><a href="#" data-request="onDeleteOrder" data-request-confirm="Sargyt #' . $orderDatas[$x]->id . ' pozmak isleýäňizmi?" data-request-data="orderId: ' . $orderDatas[$x]->id . '" style="color: darkred;font-weight: bold;">POZ</a></td>
        //                     </tr>';
        // }

        if ($order) {

            Flash::success("Sargyt Ustunlikli Pozuldy");
            return Redirect::refresh();
            // return [
            //     '#order_datas' => $html_data,
            // ];
        } else {
            Flash::error("Yalnyshlyk bar!!");
            return Redirect::refresh();
        }
    }

    public function onModalSet()
    {

        $data = post();

        $orderItem = OrderItemModel::where("id", $data["orderItemId"])->first();

        $html_data = '<div class="modal-header">
                            <h5 class="modal-title" id="mySmallModalLabel">Sargyt Haryt Bahasy</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal"
                                aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <form data-request="onUpdateOrderItemPrice" method="POST" data-request-flash>
                                <input type="number" step="0.01" pattern="/^-?\d+\.?\d*$/"
                                    onKeyPress="if(this.value.length==4) return false;"
                                    name="price" class="form-control"
                                    placeholder="%"
                                    value="' . $orderItem->price . '">
                                <input type="hidden" name="id" value="' . $data["orderItemId"] . '">
                                <button type="submit"
                                    class="btn btn-primary waves-effect waves-light"
                                    data-bs-dismiss="modal"
                                    style="margin-top: 15px;width: 100%;">Üýtget</button>
                            </form>
                        </div>';

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

    public function onModalSetAmount()
    {

        $data = post();

        $orderItem = OrderItemModel::where("id", $data["orderItemId"])->first();

        $html_data = '<div class="modal-header">
                            <h5 class="modal-title" id="mySmallModalLabel">Sargyt Mukdary</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal"
                                aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <form data-request="onUpdateOrderItemAmount" method="POST" data-request-flash>
                                <input type="number" step="0.01" pattern="/^-?\d+\.?\d*$/"
                                    onKeyPress="if(this.value.length==4) return false;"
                                    name="amount" class="form-control"
                                    placeholder=""
                                    value="' . $orderItem->amount . '">
                                <input type="hidden" name="id" value="' . $data["orderItemId"] . '">
                                <button type="submit"
                                    class="btn btn-primary waves-effect waves-light"
                                    data-bs-dismiss="modal"
                                    style="margin-top: 15px;width: 100%;">Üýtget</button>
                            </form>
                        </div>';

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

    public function onUpdateOrderItemAmount()
    {
        // $this["currentMonth"] = $currentDate->format('m');
        $data = post();

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

        $orderItem->amount = (float) $data["amount"];
        $orderItem->save();



        // $html_data = '';

        // $orderDatas = OrderItemModel::with(["type", "size"])->where("order_id", $orderItem->order_id)->orderBy('id', 'DESC')->get();

        // for ($x = 0; $x < count($orderDatas); $x++) {
        //     // dd($orderDatas[$x]->shipping->status);
        //     $html_data .= '<tr>
        //                         <td style="font-weight: bold;">' . ($x + 1) . '</td>
        //                         <td><a href="#" style="font-weight: bold;">' . $orderDatas[$x]->type->name . '</a></td>
        //                         <td>' . $orderDatas[$x]->size->name . '</td>
        //                         <td><a href="#" data-request="onModalSet"
        //                         data-request-data="orderItemId: ' . $orderDatas[$x]->id . '"
        //                         data-bs-toggle="modal" data-bs-target=".bs-example-modal-sm-1"
        //                         class="badge badge-soft-success"
        //                         style="font-size: 14px;">' . number_format($orderDatas[$x]->price) . ' $</a>
        //                         </td>
        //                         <td><a href="#" data-request="onModalSetAmount"
        //                         data-request-data="orderItemId: ' . $orderDatas[$x]->id . '"
        //                         data-bs-toggle="modal" data-bs-target=".bs-example-modal-sm-1">' . $orderDatas[$x]->amount . '</a></td>
        //                         <td>' . $orderDatas[$x]->note . '</td>
        //                     </tr>';
        // }

        // $order = OrderModel::where("id", $orderItem->order_id)->with(["client", "shipping"])
        //     ->withCount(['order_items as order_all_amount' => function ($query) {
        //         $query->select(DB::raw('sum(amount)'));
        //     }])
        //     ->withCount(['order_items as order_all_price' => function ($query) {
        //         $query->select(DB::raw('sum(price)'));
        //     }])
        //     ->first();

        // $all = ($order->order_all_amount * $order->order_all_price);

        if ($orderItem) {

            Flash::success("Sargyt Harydy Ustunlikli Goşuldy");
            return Redirect::refresh();
            // return [
            //     '#order_item_datas' => $html_data,
            //     '#allAmount' => $order->order_all_amount . " kg",
            //     '#allPrice' => $order->order_all_price . " $",
            //     '#all' => "Jemi Bahasy: " . number_format($all) . " $",
            // ];
        } else {
            Flash::error("Yalnyshlyk bar!!");
            return Redirect::refresh();
        }
    }

    public function onUpdateOrderItemPrice()
    {
        // $this["currentMonth"] = $currentDate->format('m');
        $data = post();

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

        $orderItem->price = (float) $data["price"];
        $orderItem->save();



        // $html_data = '';

        // $orderDatas = OrderItemModel::with(["type", "size"])->where("order_id", $orderItem->order_id)->orderBy('id', 'DESC')->get();

        // for ($x = 0; $x < count($orderDatas); $x++) {
        //     // dd($orderDatas[$x]->shipping->status);
        //     $html_data .= '<tr>
        //                         <td style="font-weight: bold;">' . ($x + 1) . '</td>
        //                         <td><a href="#" style="font-weight: bold;">' . $orderDatas[$x]->type->name . '</a></td>
        //                         <td>' . $orderDatas[$x]->size->name . '</td>
        //                         <td><a href="#" data-request="onModalSet"
        //                         data-request-data="orderItemId: ' . $orderDatas[$x]->id . '"
        //                         data-bs-toggle="modal" data-bs-target=".bs-example-modal-sm-1"
        //                         class="badge badge-soft-success"
        //                         style="font-size: 14px;">' . number_format($orderDatas[$x]->price) . ' $</a>
        //                         </td>
        //                         <td><a href="#" data-request="onModalSetAmount"
        //                         data-request-data="orderItemId: ' . $orderDatas[$x]->id . '"
        //                         data-bs-toggle="modal" data-bs-target=".bs-example-modal-sm-1">' . $orderDatas[$x]->amount . '</a></td>
        //                         <td>' . $orderDatas[$x]->note . '</td>
        //                     </tr>';
        // }

        // $order = OrderModel::where("id", $orderItem->order_id)->with(["client", "shipping"])
        //     ->withCount(['order_items as order_all_amount' => function ($query) {
        //         $query->select(DB::raw('sum(amount)'));
        //     }])
        //     ->withCount(['order_items as order_all_price' => function ($query) {
        //         $query->select(DB::raw('sum(price)'));
        //     }])
        //     ->first();

        // $all = ($order->order_all_amount * $order->order_all_price);

        if ($orderItem) {

            Flash::success("Sargyt Harydy Ustunlikli Goşuldy");
            return Redirect::refresh();
            // return [
            //     '#order_item_datas' => $html_data,
            //     '#allAmount' => $order->order_all_amount . " kg",
            //     '#allPrice' => $order->order_all_price . " $",
            //     '#all' => "Jemi Bahasy: " . number_format($all) . " $",
            // ];
        } else {
            Flash::error("Yalnyshlyk bar!!");
            return Redirect::refresh();
        }
    }

    public function onCreateOrderItem()
    {
        $data = post();
        $orderId = $this->param("orderId");


        $createOrderItem = new OrderItemModel();
        $createOrderItem->amount = $data["amount"];
        $createOrderItem->order_item_type = $data["order_item_type"];
        $createOrderItem->order_id = $orderId;
        $createOrderItem->type_id = $data["type_id"];
        $createOrderItem->size_id = $data["size_id"];
        $createOrderItem->color_id = $data["color_id"];
        $createOrderItem->bag_width = $data["bag_width"];
        $createOrderItem->bag_height = $data["bag_height"];
        $createOrderItem->price = $data["price"];
        $createOrderItem->note = $data["note"];
        $createOrderItem->save();

        if ($createOrderItem) {
            Flash::success("Sargyt Harydy Ustunlikli Goşuldy");
            return Redirect::refresh();
        }
    }


    public function onCreateOrder()
    {
        $user = \Auth::user();
        $currentDate = Carbon::now();

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

        $data = post();




        $createOrder = new OrderModel();
        $createOrder->client_id = $data["client_id"];
        $createOrder->note = $data["note"];
        $createOrder->contract_no = $data["contract_no"];
        $createOrder->shipping_id = 0;
        $createOrder->user_id = $user->id;
        $createOrder->save();

        $createShipping = new ShippingModel();
        $createShipping->order_id = $createOrder->id;
        $createShipping->status = 'not_loaded';
        $createShipping->employee_id = 0;
        $createShipping->place_now = "Aşgabat";
        $createShipping->loaded_amount = 0;
        $createShipping->note = "";
        $createShipping->save();


        $createOrder->shipping_id = $createShipping->id;
        $createOrder->save();

        if ($createOrder) {

            Flash::success("Sargyt Ustunlikli Goşuldy");
            return Redirect::refresh();
        } else {
            Flash::error("Yalnyshlyk bar!!");
            return Redirect::refresh();
        }
    }
}
