<?php

namespace Romanah\Bagisto\Components;

use Cms\Classes\ComponentBase;
// use Illuminate\Support\Facades\Http;
use October\Rain\Network\Http;
use Redirect;
use Flash;
use Session as SessionP;
use RainLab\Translate\Models\Message;

class Session extends ComponentBase
{

    public function componentDetails()
    {
        return [
            'name'        => 'Sessions',
            'description' => 'Set Auth Session'
        ];
    }


    function onSetSessionNurgul()
    {
        $data = post();
        // dd($data);
        // return $data ?? 'asdasd';
        // setAttributeTranslated('name', 'Jean-Claude', 'fr');
        $success_msg = "logged.in.success";

        SessionP::put('nurgulToken', $data['token']);
        SessionP::put('name', $data['name']);
        SessionP::put('phone', $data['phone']);

        Flash::success(Message::trans($success_msg));
        return Redirect::to('/');
    }

    function onLogOutNurgul()
    {

        SessionP::forget('nurgulToken');
        SessionP::forget('name');
        SessionP::forget('phone');

        $getToken = SessionP::get('nurgulToken');
        if ($getToken == "") {
            Flash::success(Message::trans('logout.success'));
            return Redirect::to('/');
        } else {
            return Flash::error('error');
        }
    }

    function onAddCart()
    {
        $getToken = SessionP::get('nurgulToken');
        $dataInput = post();


        $response = Http::post('https://nurgul.com.tm/app/api/customer/cart/add/'.$dataInput["product_id"], function ($http) use($getToken, $dataInput) {
            // $http->header('Content-Type', 'application/json');
            $http->header('Authorization', 'Bearer '.$getToken);
            $http->header('Accept', 'application/json');

            // $data = $dataProd;
            $data = [
                $http->data("product_id", $dataInput["product_id"]),
                $http->data("quantity", $dataInput["quantity"]),
            ];

            $http->setOption(CURLOPT_POSTFIELDS, json_encode($data));
        });

        $dataqq = json_decode($response);

        if (!empty($dataqq)) {
            Flash::success(Message::trans('success.add.to.cart'));
            return redirect()->refresh();
        } elseif (empty($dataqq)) {
            Flash::error(Message::trans('error'));
            return redirect()->refresh();
        }
    }


    function onGetCart()
    {
        $getToken = SessionP::get('nurgulToken');

        $response = Http::get('https://nurgul.com.tm/app/api/customer/cart', function ($http) use($getToken) {
            $http->header('Authorization', 'Bearer '.$getToken);
        });

        $dataqq = json_decode($response);

        return $dataqq;
    }


    function onDeleteCartItem()
    {
        $getToken = SessionP::get('nurgulToken');
        $dataInput = post();

        $response = Http::delete('https://nurgul.com.tm/app/api/customer/cart/remove/'.$dataInput["item_id"], function ($http) use($getToken) {
            $http->header('Authorization', 'Bearer '.$getToken);
        });

        $dataqq = json_decode($response);

        return $dataqq;
    }


    function onDeleteCartItemPage()
    {
        $getToken = SessionP::get('nurgulToken');
        $dataInput = post();

        $response = Http::delete('https://nurgul.com.tm/app/api/customer/cart/remove/'.$dataInput["item_id"], function ($http) use($getToken) {
            $http->header('Authorization', 'Bearer '.$getToken);
        });

        $dataqq = json_decode($response);

        if (!empty($dataqq)) {
            Flash::success(Message::trans('remove.item.success.in.cart'));
            return redirect()->refresh();
        }
    }

    function onGetAddress()
    {
        $getToken = SessionP::get('nurgulToken');

        $response = Http::get('https://nurgul.com.tm/app/api/customer/addresses', function ($http) use($getToken) {
            $http->header('Authorization', 'Bearer '.$getToken);
        });

        $dataqq = json_decode($response);

        return $dataqq;
    }

    function onAddAddress()
    {
        $getToken = SessionP::get('nurgulToken');
        $dataInput = post();


        $response = Http::post('https://nurgul.com.tm/app/api/customer/addresses', function ($http) use($getToken, $dataInput) {
            // $http->header('Content-Type', 'application/json');
            $http->header('Authorization', 'Bearer '.$getToken);
            $http->header('Accept', 'application/json');

            // $data = $dataProd;
            $data = [
                $http->data("locale", 'tm'),
                $http->data("address1", [$dataInput["new_addr"]]),
                $http->data("city", 'Ashgabat'),
            ];

            $http->setOption(CURLOPT_POSTFIELDS, json_encode($data));
        });

        $dataqq = json_decode($response);

        if (!empty($dataqq)) {
            Flash::success(Message::trans('success.add.addr'));
            return redirect()->refresh();
        } elseif (empty($dataqq)) {
            Flash::error(Message::trans('error'));
            return redirect()->refresh();
        }
    }


    function onSaveShipping()
    {
        $getToken = SessionP::get('nurgulToken');
        $dataInput = post();


        $response = Http::post('https://nurgul.com.tm/app/api/customer/checkout/save-shipping', function ($http) use($getToken, $dataInput) {
            // $http->header('Content-Type', 'application/json');
            $http->header('Authorization', 'Bearer '.$getToken);
            $http->header('Accept', 'application/json');

            // $data = $dataProd;
            $data = [
                $http->data("billing", ["address1" => [$dataInput["selected_address"]]]),
                $http->data("shipping", ["address1" => [$dataInput["selected_address"]]]),
                $http->data("shipping_method", 'courier_courier'),
            ];


            $http->setOption(CURLOPT_POSTFIELDS, json_encode($data));
        });

        $dataqq = json_decode($response);

        return $dataqq;
    }

    function onSavePayment()
    {
        $getToken = SessionP::get('nurgulToken');
        $dataInput = post();


        $response = Http::post('https://nurgul.com.tm/app/api/customer/checkout/save-payment', function ($http) use($getToken, $dataInput) {
            // $http->header('Content-Type', 'application/json');
            $http->header('Authorization', 'Bearer '.$getToken);
            $http->header('Accept', 'application/json');

            // $data = $dataProd;
            $data = [
                $http->data("payment", ["method" => $dataInput["payment"]]),
            ];

            $http->setOption(CURLOPT_POSTFIELDS, json_encode($data));
        });

        $dataqq = json_decode($response);

        return $dataqq;
    }


    function onSaveOrder()
    {
        $getToken = SessionP::get('nurgulToken');


        $response = Http::post('https://nurgul.com.tm/app/api/customer/checkout/save-order', function ($http) use($getToken) {
            // $http->header('Content-Type', 'application/json');
            $http->header('Authorization', 'Bearer '.$getToken);
            $http->header('Accept', 'application/json');


            $http->setOption(CURLOPT_POSTFIELDS);
        });

        $dataqq = json_decode($response);

        if (!empty($dataqq)) {
            Flash::success(Message::trans('success.make.order'));
            return Redirect::to('/');
        } elseif (empty($dataqq)) {
            Flash::error(Message::trans('error'));
            return redirect()->refresh();
        }
    }


    function onSaveWishlist()
    {
        $getToken = SessionP::get('nurgulToken');
        $dataInput = post();


        $response = Http::post('https://nurgul.com.tm/app/api/customer/wishlist/'.$dataInput["product_id"], function ($http) use($getToken, $dataInput) {
            // $http->header('Content-Type', 'application/json');
            $http->header('Authorization', 'Bearer '.$getToken);
            $http->header('Accept', 'application/json');

            $http->setOption(CURLOPT_POSTFIELDS);
        });

        $dataqq = json_decode($response);

        if (!empty($dataqq)) {
            Flash::success(Message::trans('success.add.wishlist'));
            return Redirect::to('/wishlist');
        } elseif (empty($dataqq)) {
            Flash::error(Message::trans('error'));
            return redirect()->refresh();
        }
    }

    function onGetWish()
    {
        $getToken = SessionP::get('nurgulToken');

        $response = Http::get('https://nurgul.com.tm/app/api/customer/wishlist', function ($http) use($getToken) {
            $http->header('Authorization', 'Bearer '.$getToken);
        });

        $dataqq = json_decode($response);

        return $dataqq;
    }
}



