<?php namespace TPS\Birzha\Components;

use Cms\Classes\ComponentBase;
use TPS\Birzha\Models\Product;
use RainLab\User\Models\User;
use Flash;
use ValidationException;
use Validator;

class ShopSettings extends ComponentBase
{

    public $user;


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



    public function onRun() {
        $this->user = $this->loadShopSettings(); 
    }


    protected function loadShopSettings() {
        $user = User::find(\Auth::user()->id);
        return $user;
    }


    public function onSave(){
        $data = post();
        $rules = [
            'vendorName' => 'required',
            'slogan' => 'required',
            'description' => 'required',
            'workTime' => 'required',
        ];

        $this->validateForm($data, $rules);

        $user = User::find(\Auth::user()->id);
        $user->shop_title = $data['vendorName'];
        $user->slogan = $data['slogan'];
        $user->description = $data['description'];
        $user->work_time = $data['workTime'];
        $user->save();

        Flash::success('Maglumatlar üýtgedildi');
    }

    protected function validateForm($data, $rules) {
        $validator = Validator::make($data, $rules);

        if($validator->fails()) {
            throw new ValidationException($validator);
        }
    }

}