<?php

namespace app\models;

use common\models\User;
use yii\base\Model;
use Yii;
use yii\web\IdentityInterface;
use yii\base\InvalidParamException;

/**
 * Account form: This form serves multiples purposes all linked to the personal account of a user,
 * It is the model for:
 * -the form which serves to change the personal information of a user, e.g. name, institution, etc.
 * (the corresponding view is separated into three different modal-windows: personal.php, address.php, delivery.php)
 * -the form that serves to change the user settings, eg. hits_per_page,... (the corresponding) view is 'settings.php'
 * 
 * @author a.hartebrodt
 *
 */
class AccountForm extends Model {
	
	public $user;
	public $username;
	public $form_of_address;
	public $password;
	public $passwordSecond;
	public $email; //admin only
	
	public $title;
	public $name;
	public $first_name; //admin only
	
	public $institution;
	public $division;
	public $street_pobox;
	public $postal_code;
	public $location_city;
	public $country;
	
	public $delivery_institution;
	public $delivery_division;
	public $delivery_street;
	public $delivery_postal_code;
	public $delivery_location;
	public $delivery_country;
	
	public $cites_code;
	
	public $default_hits_per_page;
	public $default_sort;
	public $order;
	public $subscribe;
	
	//public $searchToSave;

public function __construct($user, $config= []){
	if (empty ( $user )|| is_a($user, 'User')) {
		throw new InvalidParamException ( 'User must be given.' );
	}
	$this->user=$user;
	parent::__construct ( $config );
}

public function rules() {
	return [
			[
					'form_of_address',
					'filter',
					'filter' => 'trim'
			],
			[
					'password',
					'string',
					'min' => 6
			],
			[
					'email',
					'email'
			],
			[
					['name', 'institution', 'street_pobox', 'postal_code', 'location_city', 'country'],
					'safe'
			],
			[
					'passwordSecond',
					'compare',
					'compareAttribute'=>'password',
					'message' => 'Verify the spelling of your password'
			],
			//In order to recieve user input, attributes must have at least one rule. "safe" is default rule.
			//Possibilty to store Cites-codes in database an perfrom inline validation???
			[
					['title', 'division', 'delivery_institution', 'delivery_division', 'delivery_street',
							'delivery_postal_code', 'delivery_location', 'delivery_country', 'cites_code'],
					'safe'

			]

	];
}

public function attributeLabels(){
	return[
			'form_of_address' => 'Form of address',
			'password' => 'Password',
			'passwordSecond' =>'Password (repeat)',
			'title'=> 'Title',
			'name' => 'Name',
			'institution' =>'Institution',
			'division' =>'Division',
			'street_pobox' =>'Street/P.O.Box',
			'postal_code' =>'Postal code',
			'location_city' =>'Location/City',
			'country' => 'Country',
			'delivery_institution' =>'Institution',
			'delivery_division' =>'Division',
			'delivery_street' => 'Street',
			'delivery_postal_code' =>'Postal code',
			'delivery_location' => 'Location/City',
			'delivery_country' => 'Country'
			
	];
}
}