<?php

namespace App\Models;

use App\Components\Functions;
use App\Events\LogAction;
use Illuminate\Database\Eloquent\Model;

class HostelFee extends Model
{
    //
    const ELECTRIC = 1;
    const WATER = 2;
    const VEHICLE = 3;
    const HYGIENIC = 4;
    const MANAGEMENT = 5;
    const OTHER = 6;
    const DYNAMIC = 7;

    const ELECTRIC_BY_PEOPLE = 8;
    const ELECTRIC_BY_CLOCK = 9;
    const ELECTRIC_DYNAMIC = 10;

    const WATER_BY_PEOPLE = 11;
    const WATER_BY_CLOCK = 12;
    const WATER_DYNAMIC = 13;

    const INTERNET = 14;

    const UNIT = [
        'Đồng/Kwh',
        'Đồng/Người',
        'Đồng/Khối',
        'Đồng/Chiếc/Tháng',
        'Đồng/Tháng'
    ];

    protected $fillable = [
        'fee',
        'unit',
        'type',
        'name',
        'created_at',
        'updated_at',
        'deleted_at',
        'rooms',
        'hostel_id'
    ];

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

    public function hostelItem()
    {
        return $this->belongsTo('App\Models\Hostel', 'hostel_id', 'id');
    }

    public static function boot()
    {
        parent::boot();
        static::deleted(function ($item) {
            $user = Functions::getCurrentUser();
            $desc = '{' . $user->name . '}  đã xóa dịch vụ {' . $item->name . '} của nhà {' . optional($item->hostelItem)->name . '}';
            event(new LogAction([
                'type' => 'delete-hostel-fee',
                'user_id' => optional($user)->id,
                'object_id' => $item->id,
                'hostel_id' => $item->hostel_id,
                'properties' => $item->toArray(),
                'desc' => $desc
            ]));
        });

        static::updated(function ($item) {
            $user = Functions::getCurrentUser();
            $desc = '{' . $user->name . '}  đã sửa dịch vụ {' . $item->name . '} của nhà {' . optional($item->hostelItem)->name . '}';
            event(new LogAction([
                'type' => 'update-hostel-fee',
                'user_id' => optional($user)->id,
                'object_id' => $item->id,
                'hostel_id' => $item->hostel_id,
                'properties' => $item->toArray(),
                'desc' => $desc
            ]));
        });
    }

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

        switch ($type) {
            case self::ELECTRIC:
                $message = 'Tiền điện';
                break;

            case self::ELECTRIC_BY_CLOCK:
                $message = 'Điện cố định theo đồng hồ';
                break;

            case self::ELECTRIC_BY_PEOPLE:
                $message = 'Điện cố định theo người';
                break;

            case self::ELECTRIC_DYNAMIC:
                $message = 'Điện biến động';
                break;

            case self::WATER:
                $message = 'Tiền nước';
                break;

            case self::WATER_BY_CLOCK:
                $message = 'Nước cố định theo đồng hồ';
                break;

            case self::WATER_BY_PEOPLE:
                $message = 'Nước cố định theo người';
                break;

            case self::WATER_DYNAMIC:
                $message = 'Nước biến động';
                break;

            case self::INTERNET:
                $message = 'Tiền internet';
                break;

            case self::VEHICLE:
                $message = 'Gửi xe';
                break;

            case self::MANAGEMENT:
                $message = 'Phí quản lý';
                break;

            case self::HYGIENIC:
                $message = 'Vệ sinh';
                break;

            case self::OTHER:
                $message = 'Khác';
                break;

            case self::DYNAMIC:
                $message = 'Biến động';
                break;

            default:
                $message = 'Dịch vụ khác';
                break;

        }

        return $message;
    }


}
