<?php namespace Tps\Birzha\Components;

use Cms\Classes\ComponentBase;
use TPS\Birzha\Models\Contactmessage;
use TPS\Birzha\Models\Settings;
use Flash;

class ContactForm extends ComponentBase
{
    public function componentDetails() {
        return [
            'name' => 'Contact form',
            'description' => 'Contact form'
        ];
    }

    public function onSend() {
        $data = post();
        $rules = [
            'name' => 'required|max:100',
            'surname' => 'required|max:100',
            'mobile' => 'required|min:6',
            'email' => 'required|email|max:100',
            'content' => 'required'
        ];
        $validator = \Validator::make($data, $rules);
        if($validator->fails()) {
            Flash::error("Doldurmaly ýerleri dolduryň");
            throw new \ValidationException($validator);
        } else {
            $contactMessage = new Contactmessage();
            $contactMessage->fill($data);
            $contactMessage->save();
            Flash::success('Hatyňyz ugradyldy');
        }
    }
}