<?php

namespace app\Http\Models;

use Illuminate\Database\Eloquent\Model;
use App\Http\Libraries\Datagrid;

class UserKUA extends Model
{
    protected $table = 'user_kua';
    protected $fillable = ['id_user_kua','nama_kua','alamat','no_tlp','kecamatan_id','users_id'];
    protected $primaryKey = "id_user_kua";
    public $timestamps = false;

    public function kecamatan(){
        return $this->belongsTo('App\Http\Models\Kecamatan','kecamatan_id');        
    }

    public function users(){
        return $this->belongsTo('App\Http\Models\Users','users_id');        
    }

    public static function getJson($input)
    {
        $table  = 'user_kua';
        $select = 'user_kua.*, email, nik, active, level_user, nama_kecamatan';
        
        $replace_field  = [
            ['old_name' => 'actives', 'new_name' => 'active'],
        ];

        $param = [
            'input'         => $input->all(),
            'select'        => $select,
            'table'         => $table,
            'replace_field' => $replace_field
        ];
        $datagrid = new Datagrid;
        $data = $datagrid->datagrid_query($param, function($data){
            return $data->leftjoin('users','users.id','=','user_kua.users_id')
                        ->leftjoin('kecamatan','kecamatan.id_kecamatan','=','user_kua.kecamatan_id');
        });
        return $data;
    }
}
