<?php namespace TPS\Birzha\Components;

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

class MyOffers extends ComponentBase
{
    /**
     * @var Collection A collection of user's posts
     */
    public $offers;
    
    /**
     * today's date
     */
    public $today;

    /**
     * @var String
     * A question for delete post confirmation
     */
    public $wantToDelete;
    
    public function componentDetails()
    {
        return [
            'name'        => 'MyOffers List',
            'description' => 'List of my offers'
        ];
    }

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

    public function onDeleteOffer() {
        $product = Product::find(Input::get('id'));
        $product->images()->delete();
        $product->translations()->delete();
        $product->delete();
        return \Redirect::back();
    }

    public function onRun() {
        $this->offers = $this->loadOffers();
        $this->today = \Carbon\Carbon::now();
        $this->wantToDelete = trans('validation.post_delete_confirm');
    }

    protected function loadOffers() {
        $perPage = $this->property('perPage');
        return \Auth::user()->products()
            ->orderBy('updated_at', 'desc')
            ->paginate(6);
    }

    
}
