<?php

namespace App;

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

/**
 * Class Section
 *
 * @package App
 * @property integer $number
 * @property string $name
 * @property string $section_type
 * @property integer $number_entrance_house
 * @property string $stage_of_construction
 * @property string $building_number
 * @property tinyInteger $is_pricing
 * @property tinyInteger $is_planning
 * @property string $date_start_building
 * @property string $date_finish_buildiing
 * @property string $residental_complex
 * @property string $quarterly_finish
 * @property string $quarterly_start
 * @property integer $count_of_floor
 * @property-write mixed $quarterly_finish_id
 * @property-write mixed $residental_complex_id
 * @property-write mixed $section_type_id
 * @property-write mixed $stage_of_construction_id
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Section newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Section newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Section onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Section query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\Section withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Section withoutTrashed()
 * @mixin \Eloquent
 */
class Section extends Model
{
    use SoftDeletes;
    protected $fillable = [
        'number',
        'name',
        'number_entrance_house',
        'date_start_building',
        'is_pricing',
        'is_planning',
        'date_finish_buildiing',
        'section_type_id',
        'stage_of_construction_id',
        'residental_complex_id',
        'quarterly_start_id',
        'quarterly_finish_id',
        'count_of_floor',
        'building_number'
    ];
    protected $hidden = [];

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

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

    /**
     * Set attribute to money format
     * @param $input
     */
    public function setNumberAttribute($input)
    {
        $this->attributes['number'] = $input ? $input : null;
    }

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

    /**
     * Set attribute to money format
     * @param $input
     */
    public function setNumberEntranceHouseAttribute($input)
    {
        $this->attributes['number_entrance_house'] = $input ? $input : null;
    }

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

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

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

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

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

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

    public function section_type()
    {
        return $this->belongsTo(SectionType::class, 'section_type_id')->withTrashed();
    }

    public function stage_of_construction()
    {
        return $this->belongsTo(StageOfConstruction::class, 'stage_of_construction_id')->withTrashed();
    }

    public function quarterly_start()
    {
        return $this->belongsTo(Quarterly::class, 'quarterly_start_id')->withTrashed();
    }

    public function quarterly_finish()
    {
        return $this->belongsTo(Quarterly::class, 'quarterly_finish_id')->withTrashed();
    }

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