<?php

namespace $NAMESPACE_REPOSITORY$;

use $NAMESPACE_MODEL$\$MODEL_NAME$;
use $NAMESPACE_APP$\Repositories\BaseRepository;
use Illuminate\Support\Facades\DB;

$DOCS$
class $MODEL_NAME$Repository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [
        $FIELDS$
    ];

    /**
     * Return searchable fields
     *
     * @return array
     */
    public function getFieldsSearchable()
    {
        return $this->fieldSearchable;
    }

    /**
     * Configure the Model
     **/
    public function model()
    {
        return $MODEL_NAME$::class;
    }

    public function getAll($request)
    {
        $filter = \json_decode($request->get('filter'));
        $query = $this->model->newQuery();

        if (count((array)$filter)) {
            foreach ($filter as $key => $value) {
                if (in_array($key, $this->getFieldsSearchable())) {
                    $query->where($key, $value);
                }
            }
        }

        $out['total'] = $query->count();

        $query->skip(0);
        $query->limit(100);

        $out['data'] = $query->get(['*']);
        return $out['data'];
    }

    public  function getAllPagination($request)
    {
        $filter = \json_decode($request->get('filter'));
        $limit = $filter->pageSize;
        $index = $filter->pageIndex;

        $order_by = "";
        $order_by = " order by " . $this->getFieldsSearchable()[0] . " desc ";
        if (isset($filter->sortKey)) {
            $order_by = " order by " . $filter->sortKey . " " . ($filter->sortValue == 'descend' ? 'desc' : 'asc') . " ";
        }

        $filter_sql = "";
        if (isset($search)) {
            /*---------------------- pencarian ----------*/
            $filter_sql .= " and ( ";
            $filter_sql .= " " . $this->getFieldsSearchable()[1] . " like '%" . $search  . "%' ";
            //$filter_sql .= " or $TABLE_NAME$.PARAM like '%" . $search  . "%' ";
            $filter_sql .= " )";
            /*----------------------end  ---------------*/
        }

        //if (isset($filter->id_lokasi)) {
        //    $id_lokasi = $filter->id_lokasi;
        //    $filter_sql .= " and $TABLE_NAME$.id_lokasi = '" . $id_lokasi . "' ";
        //}

        if (count((array)$filter)) {
            foreach ($filter as $key => $value) {
                if (in_array($key, $this->getFieldsSearchable())) {
                    $filter_sql .= " and $TABLE_NAME$.$key = '" . $value . "' ";
                }
            }
        }

        $sql_total = "select
                count(*) as total
                from $RAW_ROUTE_PREFIX$.$TABLE_NAME$
                    where $TABLE_NAME$.$PRIMARY_KEY_NAME$ is not null
                " . $filter_sql;

        $total = DB::connection('$RAW_ROUTE_PREFIX$')->select($sql_total);
        $out['total'] = 0;
        if (count($total) > 0) {
            $out['total'] = $total[0]->total;
        }

        $sql_data = "Select
                    *
                    from $RAW_ROUTE_PREFIX$.$TABLE_NAME$
                    where $TABLE_NAME$.$PRIMARY_KEY_NAME$ is not null
                    " . $filter_sql . $order_by . "
                    limit " . (($index - 1) * $limit) . "," . ($limit)  . " ";

        $out['data'] = DB::connection('$RAW_ROUTE_PREFIX$')->select($sql_data);

        return $out;
    }
}
