<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;

class StatisticLog extends Model
{
    //
    use SoftDeletes;
  //  use LogsActivity;

    protected static $logAttributes = ['*'];

    protected $fillable = [
        'amount',
        'month',
        'year',
        'type',
        'hostel_id',
        'room_id',
        'contract_id',
        'statistic_month_id',
        'collect_spend_id',
        'owner_id',
        'note',
        'reserve_id',
        'money_info_id',
        'created_at',
        'updated_at',
        'deleted_at',
        'hostel_name',
        'room_name',
        'order'
    ];

    protected $dates = [
        'created_at',
        'updated_at',
        'deleted_at'
    ];

    public function statMonth()
    {
        return $this->belongsTo(StatisticMonth::class, 'statistic_month_id', 'id');
    }

    public function contract()
    {
        return $this->belongsTo(Contract::class)->withTrashed();
    }

    public function hostel()
    {
        return $this->belongsTo(Hostel::class);
    }

    public function room()
    {
        return $this->belongsTo(Room::class);
    }

    public function collectSpend()
    {
        return $this->belongsTo(CollectSpend::class);
    }

    public function moneyInfo()
    {
        return $this->belongsTo(MoneyInfo::class);
    }

    public static function boot()
    {
        parent::boot();
        static::creating(function ($item) {
            if ($item->collectSpend) {
                $collectSpend = $item->collectSpend;
                $moneyInfoId = $collectSpend->money_info_id;


                $check = StatisticMonth::query()->where('money_info_id', $moneyInfoId)->count();
                if($check == 1) {
                    $item->money_info_id = $moneyInfoId;
                    $moneyInfo = MoneyInfo::find($item->money_info_id);
                    if ($moneyInfo) {
                        if (!empty($moneyInfo->date_action)) {
                            $item->month = $moneyInfo->date_action->month;
                            $item->year = $moneyInfo->date_action->year;
                        }
                    }
                }
            }

            if($item->statMonth)
            {

                $item->month = $item->statMonth->month;
                $item->year = $item->statMonth->year;
            }

            if ($item->hostel) {
                $item->hostel_name = $item->hostel->name;
            }

            if ($item->room) {
                $item->room_name = $item->room->name;
                if (is_numeric($item->room->name)) {
                    $item->order = $item->room->name;
                } else {
                    $item->order = ord($item->room->name);

                }

            } else {
                $item->order = 9999;
            }
        });

//        static::created(function ($item) {
//            if ($item->collectSpend) {
//                $collectSpend = $item->collectSpend;
//                $moneyInfoId = $collectSpend->money_info_id;
//
//
//                $check = StatisticMonth::query()->where('money_info_id', $moneyInfoId)->count();
//                if($check == 1) {
//                    $item->money_info_id = $moneyInfoId;
//                    $moneyInfo = MoneyInfo::find($item->money_info_id);
//                    if ($moneyInfo) {
//                        if (!empty($moneyInfo->date_action)) {
//                            $item->month = $moneyInfo->date_action->month;
//                            $item->year = $moneyInfo->date_action->year;
//                        }
//                    }
//                }
//            }
//
//            if($item->statMonth)
//            {
//
//                $item->month = $item->statMonth->month;
//                $item->year = $item->statMonth->year;
//            }
//
//            if ($item->hostel) {
//                $item->hostel_name = $item->hostel->name;
//            }
//
//            if ($item->room) {
//                $item->room_name = $item->room->name;
//                if (is_numeric($item->room->name)) {
//                    $item->order = $item->room->name;
//                } else {
//                    $item->order = ord($item->room->name);
//
//                }
//
//            } else {
//                $item->order = 9999;
//            }
//        });
    }

    public function getTypeTextAttribute()
    {
        $type = $this->attributes['type'];

        if ($type == CollectSpend::COLLECT) {
            return '<label class="label label-success">Thu vào</label>';
        } else if ($type == CollectSpend::SPEND) {
            return '<label class="label label-danger">Chi ra</label>';
        }

        return '<label class="label label-primary">Chưa rõ trạng thái</label>';
    }


}
