<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DB;

class ExtandWarranty extends Model{
    use HasFactory;
    protected $table = 'extand_warranties';
    
    protected $fillable = [
        'years',
        'car_model',
        'amount',
        'profit',
        'gst_percentage',
        'gst',
        'total_profit',
        'status',
        'created_by',
        'created_at',
        'updated_by',
        'updated_at'
    ];

    public function export($slug)
    {
        $collection = DB::table('extand_warranties')
                        ->select('car_model.name as car_model', 'fule_type.title as fuel_type', 'extand_warranties.years', 'extand_warranties.amount', 'extand_warranties.profit', 'extand_warranties.gst_percentage', 'extand_warranties.gst', 'extand_warranties.total_profit', 'extand_warranties.status')
                        ->leftjoin('car_model', 'car_model.id', '=', 'extand_warranties.car_model')
                        ->leftjoin('fule_type', 'fule_type.id', '=', 'extand_warranties.fule_type')
                        ->where('extand_warranties.status', 'active');
        if($slug != 'all'){
            $collection->where('extand_warranties.status', $slug);
        }
        
        $data = $collection->get();
        
        return $data->isNotEmpty() ? $data : null;
    }
}
