<?php

namespace App;

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

/**
 * Class Planning
 *
 * @package App
 * @property string $name
 * @property string $image
 * @property string $amount_of_apartment
 * @property double $cost_per_square_meter
 * @property double $space
 * @property double $total_cost
 * @property string $room
 * @property string $room_type
 * @property string $section
 * @property tinyInteger $is_sold
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Floor[] $floor_id
 * @property-write mixed $room_id
 * @property-write mixed $room_type_id
 * @property-write mixed $section_id
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Planning newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Planning newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Planning onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Planning query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\Planning withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Planning withoutTrashed()
 * @mixin \Eloquent
 */
class Planning extends Model
{
    use SoftDeletes;
    protected $fillable = [
        'name',
        'image',
        'cost_per_square_meter',
        'space',
        'total_cost',
        'room_id',
        'room_type_id',
        'section_id',
        'count_of_floor',
        'is_sold',
        'amount_of_apartment'
    ];
    protected $hidden = [];

    public function getSectionNameAttribute($input)
    {
        $section = $this->section;
        $sectionName = $section->name;
        $rcName = '';
        if ($section) {
            $rc = $section->residental_complex;
            $rcName = $rc->name;
        }

        return !empty($rcName) ? $sectionName . ", " . $rcName : $sectionName;
    }

    /**
     * Set attribute to date format
     * @param $input
     */
    public function setCostPerSquareMeterAttribute($input)
    {
        if ($input != '') {
            $this->attributes['cost_per_square_meter'] = $input;
        } else {
            $this->attributes['cost_per_square_meter'] = null;
        }
    }

    /**
     * Set attribute to date format
     * @param $input
     */
    public function setSpaceAttribute($input)
    {
        if ($input != '') {
            $this->attributes['space'] = $input;
        } else {
            $this->attributes['space'] = null;
        }
    }

    /**
     * Set attribute to date format
     * @param $input
     */
    public function setTotalCostAttribute($input)
    {
        if ($input != '') {
            $this->attributes['total_cost'] = $input;
        } else {
            $this->attributes['total_cost'] = null;
        }
    }

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

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

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

    public function room()
    {
        return $this->belongsTo(RoomCount::class, 'room_id')->withTrashed();
    }

    public function room_type()
    {
        return $this->belongsTo(RoomType::class, 'room_type_id')->withTrashed();
    }

    public function floor_id()
    {
        return $this->belongsToMany(Floor::class, 'floor_planning')->withTrashed();
    }

    public function section()
    {
        return $this->belongsTo(Section::class, 'section_id')->withTrashed();
    }
}
