<?php

namespace App\Models;

use GuzzleHttp\Psr7\Request;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DB;

class Product extends Model{
    use HasFactory;
    protected $table = 'products';
    
    protected $fillable = [
        'category_id', 
        'car_model', 
        'name', 
        'veriant', 
        'key_number', 
        'engine_number', 
        'chassis_number', 
        'vin_number',
        'interior_color', 
        'exterior_color',
        'is_applicable_for_mcp',
        'status',
        'created_by',
        'created_at',
        'updated_by',
        'updated_at'
    ];

    public function export($slug){

        ini_set('memory_limit', '100M');


        /*ini_set('display_errors', '1');
                        ini_set('display_startup_errors', '1');
                        error_reporting(E_ALL);*/

        $collection = DB::table('products')
                      ->select('car_model.name as car_name', 'car_varient.name as varient_name', 'interiorcolor.name as interior_color', 'exteriorcolor.name as exterior_color', 'products.status')
                      ->leftJoin('car_model','car_model.id','=','products.car_model')
                      ->leftJoin('exteriorcolor','exteriorcolor.id','=','products.exterior_color')
                      ->leftJoin('interiorcolor','interiorcolor.id','=','products.interior_color')
                      ->leftJoin('car_varient','car_varient.id','=','products.veriant');

        if($slug != 'all')
            $collection->where(['products.status' => $slug]);
        
        $data = $collection->get();
        
        if($data->isNotEmpty()){
            return $data;
        }else{
            return null;
        }
    }
     public function variant()
    {
        return $this->belongsTo(Carvarient::class,'veriant');
    }
}
