<?php

namespace Modules\Store\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\Category\Models\Category;
use Modules\Product\Models\Product;
use Modules\Service\Uploads\UploadingService;
use Modules\User\Models\User;

class Store extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable = [
        'name',
        'phone',
        'email'	,
        'status_active',
        'min_order'	,
        'logo',
        'header',
        'expire_subscribe',
        'bank_account',
        'bank_num',
        'iban',
        'bank',
        'status_account',
        'user_id_front_image',
        'user_id_back_image',
        'iban_card',
        'license_work',
        'commercial_log',
        'manager_id',
        'contract_establish',
        'accepted_at',
        'owner_id'	,
        'category_id'	,
        'sub_category_id'
    ];

    public function days($day)
    {
        $work = StoreWork::where('day',$day)->where('store_id',$this->id)->first();
        return $work;
    }

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id')->withTrashed();
    }

    public function subCategory()
    {
        return $this->belongsTo(Category::class, 'sub_category_id')->withTrashed();
    }

    public function setUserIdFrontImageAttribute($value): void
    {
        if($value)
        {
            $file = UploadingService::uploadFile($value);
            $this->attributes['user_id_front_image'] = $file;
        }
        else
        {
            $this->attributes['user_id_front_image'] = $this->user_id_front_image;
        }
    }

    public function setUserIdBackImageAttribute($value): void
    {
        if($value)
        {
            $file = UploadingService::uploadFile($value);
            $this->attributes['user_id_back_image'] = $file;
        }
        else
        {
            $this->attributes['user_id_back_image'] = $this->user_id_back_image;
        }
    }

    public function setIbanCardAttribute($value): void
    {
        if($value)
        {
            $file = UploadingService::uploadFile($value);
            $this->attributes['iban_card'] = $file;
        }
        else
        {
            $this->attributes['iban_card'] = $this->iban_card;
        }
    }

    public function setLicenseWorkAttribute($value): void
    {
        if($value)
        {
            $file = UploadingService::uploadFile($value);
            $this->attributes['license_work'] = $file;
        }
        else
        {
            $this->attributes['license_work'] = $this->license_work;
        }
    }

    public function setCommercialLogAttribute($value): void
    {
        if($value)
        {
            $file = UploadingService::uploadFile($value);
            $this->attributes['commercial_log'] = $file;
        }
        else
        {
            $this->attributes['commercial_log'] = $this->commercial_log;
        }
    }

    public function setManagerIdAttribute($value): void
    {
        if($value)
        {
            $file = UploadingService::uploadFile($value);
            $this->attributes['manager_id'] = $file;
        }
        else
        {
            $this->attributes['manager_id'] = $this->manager_id;
        }
    }

    public function setContractEstablishAttribute($value): void
    {
        if($value)
        {
            $file = UploadingService::uploadFile($value);
            $this->attributes['contract_establish'] = $file;
        }
        else
        {
            $this->attributes['contract_establish'] = $this->contract_establish;
        }
    }

    public function setOwnerIdAttribute($value): void
    {
        if($value)
        {
            $file = UploadingService::uploadFile($value);
            $this->attributes['owner_id'] = $file;
        }
        else
        {
            $this->attributes['owner_id'] = $this->owner_id;
        }
    }

}
