<?php

namespace App;

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

/**
 * Class CustomerAppeal
 *
 * @package App
 * @property string $phone
 * @property string $customer_appeal_option
 * @property tinyInteger $call_now
 * @property string $call_time
 * @property string $developer
 * @property tinyInteger $request_processed
 * @property string $residential_complexes
 * @property string $planning
 * @property-write mixed $customer_appeal_option_id
 * @property-write mixed $developer_id
 * @property-write mixed $planning_id
 * @property-write mixed $residential_complexes_id
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CustomerAppeal newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CustomerAppeal newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\CustomerAppeal onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\CustomerAppeal query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\CustomerAppeal withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\CustomerAppeal withoutTrashed()
 * @mixin \Eloquent
 */
class CustomerAppeal extends Model
{
    use SoftDeletes;
    protected $fillable = [
        'phone',
        'question',
        'call_now',
        'call_time',
        'request_processed',
        'name',
        'customer_appeal_option_id',
        'developer_id',
        'residential_complexes_id',
        'planning_id'
    ];

    protected $hidden = [];

    /**
     * Set to null if empty
     * @param $input
     */
    public function setCustomerAppealOptionIdAttribute($input)
    {
        $this->attributes['customer_appeal_option_id'] = $input ? $input : null;
    }

    public function getCreatedAtModifyAttribute()
    {
        $zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format'));
        if ($this->created_at != $zeroDate && $this->created_at  != null) {
            return Carbon::createFromFormat('Y-m-d H:i:s', $this->created_at)
                ->addHours(3)
                ->format(config('app.date_time_format'));
        } else {
            return '';
        }
    }

    /**
     * Set attribute to date format
     * @param $input
     */
    public function setCallTimeAttribute($input)
    {
        if ($input != null && $input != '') {
            $this->attributes['call_time'] = Carbon::createFromFormat(config('app.date_format') . ' H:i:s',
                $input)->format('Y-m-d H:i:s');
        } else {
            $this->attributes['call_time'] = null;
        }
    }

    /**
     * Get attribute from date format
     * @param $input
     *
     * @return string
     */
    public function getCallTimeAttribute($input)
    {
        $zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format') . ' H:i:s');
        if ($input != $zeroDate && $input != null) {
            return Carbon::createFromFormat('Y-m-d H:i:s', $input)->format(config('app.date_format') . ' H:i:s');
        } else {
            return '';
        }
    }

    /**
     * Set to null if empty
     * @param $input
     */
    public function setDeveloperIdAttribute($input)
    {
        $this->attributes['developer_id'] = $input ? $input : null;
    }

    /**
     * Set to null if empty
     * @param $input
     */
    public function setResidentialComplexesIdAttribute($input)
    {
        $this->attributes['residential_complexes_id'] = $input ? $input : null;
    }

    /**
     * Set to null if empty
     * @param $input
     */
    public function setPlanningIdAttribute($input)
    {
        $this->attributes['planning_id'] = $input ? $input : null;
    }

    public function customer_appeal_option()
    {
        return $this->belongsTo(CustomerAppealOption::class, 'customer_appeal_option_id')->withTrashed();
    }

    public function developer()
    {
        return $this->belongsTo(Developer::class, 'developer_id')->withTrashed();
    }

    public function residential_complexes()
    {
        return $this->belongsTo(ResidentialComplex::class, 'residential_complexes_id')->withTrashed();
    }

    public function planning()
    {
        return $this->belongsTo(Planning::class, 'planning_id')->withTrashed();
    }
}
