<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Carbon\Carbon;


/**
 * App\Models\UserAccount
 *
 * @property-read \App\Models\Country $company_country
 * @property-read \App\Models\Country $company_pre_phone
 * @property-read \App\Models\Country $country
 * @property mixed $birthday
 * @property-read mixed $company
 * @property-read \App\Models\Country $pre_mobil
 * @property-read \App\Models\Country $pre_phone
 * @property-read \App\User $user
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\UserAccount onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\UserAccount withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\UserAccount withoutTrashed()
 * @mixin \Eloquent
 * @property int $id
 * @property string|null $salutation
 * @property string|null $first_name
 * @property string|null $last_name
 * @property string|null $address
 * @property string|null $address_2
 * @property string|null $zipcode
 * @property string|null $city
 * @property int $country_id
 * @property int|null $pre_phone_id
 * @property string|null $phone
 * @property int|null $pre_mobil_id
 * @property string|null $mobil
 * @property string|null $tax_number
 * @property string|null $tax_identification_number
 * @property int $same_as_billing
 * @property string|null $shipping_salutation
 * @property string|null $shipping_company
 * @property string|null $shipping_firstname
 * @property string|null $shipping_lastname
 * @property string|null $shipping_address
 * @property string|null $shipping_address_2
 * @property string|null $shipping_zipcode
 * @property string|null $shipping_city
 * @property int $shipping_country_id
 * @property int|null $shipping_pre_phone_id
 * @property string|null $shipping_phone
 * @property string|null $website
 * @property string|null $facebook
 * @property string|null $facebook_fanpage
 * @property string|null $instagram
 * @property string|null $data_protection
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property-read \App\Models\Country $shipping_country
 * @property-read \App\Models\Country|null $shipping_pre_phone
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereAddress2($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereBirthday($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereCompany($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereCountryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereDataProtection($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereFacebook($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereFacebookFanpage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereFirstName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereInstagram($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereLastName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereMobil($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount wherePreMobilId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount wherePrePhoneId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereSalutation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereSameAsBilling($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereShippingAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereShippingAddress2($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereShippingCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereShippingCompany($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereShippingCountryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereShippingFirstname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereShippingLastname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereShippingPhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereShippingPrePhoneId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereShippingSalutation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereShippingZipcode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereTaxIdentificationNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereTaxNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereWebsite($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereZipcode($value)
 * @property string|null $m_account
 * @property string|null $m_salutation
 * @property string|null $m_first_name
 * @property string|null $m_last_name
 * @property string|null $m_notes
 * @property int|null $taxable_sales
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereMAccount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereMFirstName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereMLastName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereMNotes($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereMSalutation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereTaxableSales($value)
 * @property array|null $payment_data
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount wherePaymentData($value)
 * @property string|null $accepted_contract
 * @property array|null $notice
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereAcceptedContract($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserAccount whereNotice($value)
 */
class UserAccount extends Model
{
    protected $table = 'user_accounts';
    protected $fillable = [
        'm_account', 'm_salutation', 'm_first_name', 'm_last_name', 'm_notes', 'company', 'salutation', 'first_name', 'last_name', 'address', 'address_2', 'zipcode', 'city', 'country_id', 'pre_phone_id', 'phone', 'pre_mobil_id', 'mobil',
        'tax_number', 'tax_identification_number', 'taxable_sales', 'same_as_billing',
        'shipping_salutation', 'shipping_company', 'shipping_firstname', 'shipping_lastname', 'shipping_address', 'shipping_address_2', 'shipping_zipcode', 'shipping_city', 'shipping_country_id', 'shipping_pre_phone_id', 'shipping_phone',
        'birthday', 'website', 'facebook', 'facebook_fanpage', 'instagram', 'notice'
    ];

    protected $casts = [
        'payment_data' => 'array',
        'notice' => 'array',
    ];

    use SoftDeletes;
    protected $dates = ['deleted_at'];


    public function user()
    {
        return $this->hasOne('App\User', 'account_id');
    }

    public function country()
    {
        return $this->belongsTo('App\Models\Country', 'country_id');
    }

    public function shipping_country()
    {
        return $this->belongsTo('App\Models\Country', 'shipping_country_id');
    }

    public function pre_phone()
    {
        return $this->belongsTo('App\Models\Country', 'pre_phone_id');
    }

    public function pre_mobil()
    {
        return $this->belongsTo('App\Models\Country', 'pre_mobil_id');
    }

    public function shipping_pre_phone()
    {
        return $this->belongsTo('App\Models\Country', 'shipping_pre_phone_id');
    }

    public function getBirthdayAttribute($value)
    {
        if(!$value){
            return "";
        }
        return Carbon::parse($value)->format(\Util::formatDateDB());
    }

    public function setBirthdayAttribute( $value ) {
        $this->attributes['birthday'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : NULL;
    }

    public function getDataProtectionFormat(){
        if(!$this->attributes['data_protection']){ return ""; }
        return Carbon::parse($this->attributes['data_protection'])->format(\Util::formatDateTimeDB());
    }

    public function getAcceptContractFormat(){
        if(!$this->attributes['accept_contract']){ return ""; }
        return Carbon::parse($this->attributes['accept_contract'])->format(\Util::formatDateTimeDB());
    }


    public function getCountryAttrAs($attr, $as = false){
        if($this->country){
            $val = $this->country->getAttrByKey($attr);

            if($val){
                if($as){
                    return $as;
                }
                return true;

            }
        }
        return "";
    }

    public function setNotice($key, $value){
        $notice = $this->notice;
        $notice[$key] = $value;
        $this->notice = $notice;
        $this->save();
    }

    public function getNotice($key){
        return isset($this->notice[$key]) ? $this->notice[$key] : false;
    }

}
