<?php namespace TPS\Birzha\Components;

use Cms\Classes\ComponentBase;
use TPS\Birzha\Models\Product;
use TPS\Birzha\Models\Comment;
use TPS\Birzha\Models\OrderItems;
use RainLab\User\Facades\Auth;
use Flash;

use TPS\Birzha\Models\Favourites;
use ValidationException;
use Validator;

class OrderDetail extends ComponentBase
{
    public $order;
    public $total = 0;
    
    public function componentDetails()
    {
        return [
            'name'        => 'Single order',
            'description' => 'Selected order'
        ];
    }
    
        public function defineProperties()
    {
        return [
            'id' => [
                'title' => 'Order id',
                'description' => 'Order id',
                'type' => 'string',
                'default' => '{{ :id }}'
            ]
        ];
    }


    public function onRun() {
        $this->order = $this->loadOrder();
    }




    protected function loadOrder() {
        $order = OrderItems::where('order_id', $this->property('id'))->first();
        return $order;
    }
    
    public function onCreateFav()
    {
        $data = input();
        $validator = Validator::make($data, [
            'product_id' => 'required'
        ]);
        if($validator->fails()) {
            Flash::error("Haryt maglumatyny nädogry");
        }
        $favourite = new Favourites;
        $favourite->user_id = \Auth::user()->id;
        $favourite->product_id = (int)$data['product_id'];
        $favourite->save();
        Flash::success("Haryt halanlaryma goşuldy");
    }
}