<?php namespace TPS\Birzha\Components;

use Cms\Classes\ComponentBase;
use TPS\Birzha\Models\Product;
use TPS\Birzha\Models\VendorSales as VSales;
use Input;
use Flash;

class VendorSales extends ComponentBase
{
    /**
     * @var Collection A collection of user's posts
     */
    public $sales;
    
    
    public function componentDetails()
    {
        return [
            'name'        => 'Vendor sales List',
            'description' => 'List of vendor sales'
        ];
    }

    public function defineProperties()
    {
        return [
            'perPage' => [
                'title' => 'Number of vendor sales',
                'description' => 'How many vendor sales do you want to display',
                'default' => 12,
                'validationPattern' => '^[0-9]+$',
                'validationMessage' => 'Only numbers allowed'
            ],
        ];
    }


    public function onRun() {
        $this->sales = $this->loadVendorSales();
    }

    protected function loadVendorSales() {
        $perPage = $this->property('perPage');
        $sales = VSales::select('id', 'order_id', 'created_at', 'status')
                            ->where('vendor_id', \Auth::user()->id)
                            ->groupBy('order_id')
                            ->orderBy('id', 'DESC')->paginate($perPage);
        return $sales;
    }

    
}
