<?php

namespace Romanah\Gokbakja\Components;

use Cms\Classes\ComponentBase;
use Romanah\Gokbakja\Models\Action as ActionModel;
use Redirect;
use Flash;

class Action extends ComponentBase
{


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


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

        $data = post();
        $actionType = $this->param("type");

        if ($actionType == 'inbox') {

            $createResult = new ActionModel();
            $createResult->product_id = $data["product_id"];
            $createResult->type = 'inbox';
            $createResult->status_accountant = "new";
            $createResult->status_director = "new";
            $createResult->transport_no = $data["transport_no"];
            $createResult->driver = $data["driver"];
            $createResult->note = $data["note"];
            $createResult->amount = $data["quantity"];
            $createResult->stock_id = $data["stock_id"];
            $createResult->user_id = $user->id;

            $createResult->save();
            // $createResult = DollorModel::create([]);

            // dd($data);
            if ($createResult) {
                Flash::success("Action Inbox Ustunlikli Goshuldy");
                return Redirect::refresh();
            }
        } elseif ($actionType == 'outbox') {
            $qty = ActionModel::where("product_id", $data["product_id"])
                ->where("stock_id", $data["stock_id"])
                ->sum("amount");

            // $productOutbox = ActionModel::where("product_id", $data["product_id"])
            //                 ->where("stock_id", $data["stock_id"])
            //                 ->where("type", "outbox")
            //                 ->sum("quantity");

            // $productQuantity = $productInbox - $productOutbox;
            // dump($productQuantity);
            if ($qty >= $data["quantity"]) {
                $createResult = new ActionModel();
                $createResult->product_id = $data["product_id"];
                $createResult->type = 'outbox';
                $createResult->quantity = -$data["quantity"];
                $createResult->stock_id = $data["stock_id"];
                $createResult->user_id = $user->id;

                $createResult->save();
                // $createResult = DollorModel::create([]);

                // dd($data);
                if ($createResult) {
                    Flash::success("Action Outbox Ustunlikli Goshuldy");
                    return Redirect::refresh();
                }
            } else {
                Flash::error("Данного товара нет в наличии");
                return Redirect::refresh();
            }
        }
    }

    public function onUpdateAction()
    {
        $user = \Auth::user();
        $data = post();

        $updateResult = ActionModel::where('id', $data["id"])
            ->update(array(
                'product_id' => $data["product_id"],
                'amount' => $data["quantity"],
                'stock_id' => $data["stock_id"],
                'note' => $data["note"],
                'transport_no' => $data["transport_no"],
                'driver' => $data["driver"],
                'user_id' => $user->id,
            ));
        // $createResult = DollorModel::create([]);

        // dd($data);
        if ($updateResult) {
            Flash::success("Action Ustunlikli Uytgedildi");
            return Redirect::refresh();
        }
    }


    public function onGetAction()
    {
        $id = post('id');
        return ActionModel::where('id', $id)->first();
    }

    public function onDeleteAction()
    {
        $id = post('id');

        $item = ActionModel::Find($id);
        $item->delete();

        if ($item) {
            Flash::success("Action Ustunlikli Pozuldy");
            return Redirect::refresh();
        }
    }




    // public function onDeleteSellerAction()
    // {
    //     $id = post('id');

    //     $item = SellerActionModel::Find($id);
    //     $item->delete();

    //     if ($item) {
    //         Flash::success("Action Ustunlikli Pozuldy");
    //         return Redirect::refresh();
    //     }
    // }

    // public function onGetSellerAction()
    // {
    //     $id = post('id');
    //     return SellerActionModel::where('id', $id)->first();
    // }


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

    //     $data = post();


    //     $qty = ActionModel::where("product_id", $data["product_id"])
    //         ->where("stock_id", $data["stock_id"])
    //         ->sum("quantity");


    //     if ($qty >= $data["quantity"]) {
    //         $createResult = new ActionModel();
    //         $createResult->product_id = $data["product_id"];
    //         $createResult->type = 'outbox';
    //         $createResult->quantity = -$data["quantity"];
    //         $createResult->stock_id = $data["stock_id"];
    //         $createResult->user_id = $user->id;

    //         $createResult->save();


    //         $createSellerResult = new SellerActionModel();
    //         $createSellerResult->seller_id = $data["seller_id"];
    //         $createSellerResult->product_id = $data["product_id"];
    //         $createSellerResult->quantity = $data["quantity"];
    //         $createSellerResult->stock_id = $data["stock_id"];
    //         $createSellerResult->user_id = $user->id;

    //         $createSellerResult->save();
    //         // $createResult = DollorModel::create([]);

    //         // dd($data);
    //         if ($createResult && $createSellerResult) {
    //             Flash::success("Action Outbox Ustunlikli Goshuldy");
    //             return Redirect::refresh();
    //         }
    //     } else {
    //         Flash::error("Данного товара нет в наличии");
    //         return Redirect::refresh();
    //     }
    // }



    // public function onUpdateSellerAction()
    // {
    //     $user = \Auth::user();
    //     $data = post();


    //     $qty = ActionModel::where("product_id", $data["product_id"])
    //         ->where("stock_id", $data["stock_id"])
    //         ->sum("quantity");


    //     if ($qty >= $data["quantity"]) {

    //         $updateResult = SellerActionModel::where('id', $data["id"])
    //             ->update(array(
    //                 'product_id' => $data["product_id"],
    //                 'quantity' => $data["quantity"],
    //                 'stock_id' => $data["stock_id"],
    //                 'user_id' => $user->id,
    //             ));
    //         // $createResult = DollorModel::create([]);

    //         // dd($data);
    //         if ($updateResult) {
    //             Flash::success("Action Ustunlikli Uytgedildi");
    //             return Redirect::refresh();
    //         }
    //     } else {
    //         Flash::error("Данного товара нет в наличии");
    //         return Redirect::refresh();
    //     }
    // }
}
