<?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 Seller extends ComponentBase
{

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

    function onCreateSellerProd()
    {
        $dataInput = post();


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

            // $data = $dataProd;
            $data = [
                $http->data("type", "simple"),
                $http->data("marketplace_seller_id", $dataInput["seller_id"]),
                $http->data("user", 'romanah_'),
                $http->data("password", 'bt110226$$'),
                $http->data("sku", \Str::slug($dataInput["sku"])),
            ];


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

        $dataqq = json_decode($response);

        if (isset($dataqq->status) && $dataqq->status == 200) {
            Flash::success(Message::trans('success.add.prod'));
            return Redirect::to('/seller/product/update/'.$dataqq->data->id);
        } elseif (!isset($dataqq->status)) {
            Flash::error(Message::trans('error'));
            return redirect()->refresh();
        }
        return $dataqq;
    }


    function onUpdateSellerProd()
    {
        $dataInput = post();
        // dd($dataInput["status"]);

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

            // $data = $dataProd;
            $data = [
                $http->data("product_id", $this->param("id")),
                $http->data("user", 'romanah_'),
                $http->data("password", 'bt110226$$'),
                $http->data("sku", 'skuq'),
                $http->data("channels", "Nurgul"),
                $http->data("short_description", $dataInput["short_description"]), //
                $http->data("description", $dataInput["description"]), //
                $http->data("name", $dataInput["name"]), //
                $http->data("url_key", \Str::slug($dataInput["name"])),
                $http->data("new", 1),
                $http->data("featured", $dataInput["featured"]),//
                $http->data("visible_individually", 1),
                $http->data("status", $dataInput["stat"]),//
                $http->data("product_number", $dataInput["product_number"]), //
                $http->data("price", $dataInput["price"]),//
                $http->data("special_price", $dataInput["special_price"]),//
                $http->data("weight", 0),
                $http->data("qty", $dataInput["qty"]),//
            ];


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

        $dataqq = json_decode($response);

        if ($dataqq->status == 200) {
            Flash::success(Message::trans('success.add.prod'));
            return Redirect::to('/seller/products');
        } elseif (empty($dataqq)) {
            Flash::error(Message::trans('error'));
            return redirect()->refresh();
        }
        return $dataqq;
    }

}



