<?php

namespace App\Models;

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

class ImportExport extends Model
{
    //
    use SoftDeletes;

    protected $table = 'import_export';

    protected $fillable = [
        'item_id',
        'warehouse_id',
        'import',
        'export',
        'type',
        'created_at',
        'updated_at',
        'deleted_at',
        'user_id',
        'warehouse_name',
        'item_name',
        'user_name',
        'owner_id',
        'hostel_id',
        'room_id',
        'hostel_name',
        'room_name',
        'note',
        'item_type_id',
        'item_type_name',
	    'receiver',
	    'mover'
    ];

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

    public static function boot()
    {
        parent::boot();
        static::created(function ($item) {

            $userId = $item->user_id;
            //$warehouseId = $item->warehouse_id;
            $itemId = $item->item_id;
            $hostelId = $item->hostel_id;
            $roomId = $item->room_id;
            $itemTypeId = $item->item_type_id;

            $userName = null;
            $warehouseName = null;
            $itemName = null;
            $ownerId = null;
            $hostelName = null;
            $roomName = null;
            $itemTypeName = null;

            $user = User::find($userId);
           // $warehouseModel = Warehouse::find($warehouseId);
            $itemModel = Item::find($itemId);
            $hostel = Hostel::find($hostelId);
            $room = Room::find($roomId);
            $itemType = ItemType::find($itemTypeId);

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

            if($room)
            {
                $roomName = $room->name;
            }

            if ($user) {
                $userName = $user->name_text;

                if ($user->type == User::OWNER) {
                    $ownerId = $user->id;
                } else if ($user->type == User::STAFF) {
                    $ownerId = $user->staff_owner_id;
                }
            }
//            if ($warehouseModel) {
//
//                $warehouseName = $warehouseModel->name;
//            }

            if ($itemModel) {
                $itemName = $itemModel->name;
            }

            if($itemType)
            {
                $itemTypeName = $itemType->name;
            }

           // $item->warehouse_name = $warehouseName;
            $item->user_name = $userName;
            $item->item_name = $itemName;
            $item->owner_id = $ownerId;
            $item->hostel_name = $hostelName;
            $item->room_name = $roomName;
            $item->item_type_name = $itemTypeName;
            $item->save();
        });
    }


    public function itemType()
    {
        return $this->belongsTo(ItemType::class);
    }
    public function warehouse()
    {
        return $this->belongsTo(Warehouse::class);
    }
    public function hostel()
    {
        return $this->belongsTo(Hostel::class);
    }
    public function room()
    {
        return $this->belongsTo(Room::class);
    }
    public function getTypeTextAttribute()
    {
        $type = $this->attributes['type'];

        if ($type == Warehouse::IMPORT) {
            return '<label class="label label-success">Nhập</label>';
        }

        return '<label class="label label-danger">Xuất</label>';
    }

}
