<?php

namespace App\Repositories\Botanic;

use App\Exports\Botanic\FamiliesExport;
use App\Models\Botanic\Family;
use Maatwebsite\Excel\Facades\Excel;
use Yajra\DataTables\DataTables;

class Families
{
    public static function getDatatable()
    {
        $model = Family::orderBy('name');

        return Datatables::of($model)->make(true);
    }

    public static function getOptions()
    {
        return Family::get()->SortBy('name')->pluck('name', 'id')->toArray();
    }

    public static function getAll()
    {
        return Family::orderBy('name', 'asc')->get();
    }

    public static function get($id)
    {
        return Family::findOrFail($id);
    }

    public static function store($data)
    {
        $id = isset($data['id']) ? $data['id'] : false;
        $item = $id ? self::update($data, $id) : self::create($data);

        return $item->id;
    }

    public static function create($data)
    {
        return Family::create($data);
    }

    public static function update($data, $id = false)
    {
        $id = $id ? $id : $data['id'];
        $item = self::get($id);
        $item->update($data);

        return $item;
    }

    public static function destroy($id)
    {
        return Family::destroy($id);
    }

    public static function exportExcel()
    {
        return Excel::download(new FamiliesExport, 'families.xlsx');
    }
}
