<?php

namespace app\Http\Models;

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

class Bidan extends Model
{
    protected $table = 'bidan';
    protected $fillable = ['id_bidan','nama_bidan','tgl_lahir','alamat','kabupaten_id','kecamatan_id','jenis_kelamin','no_tlp','lokasi_tugas','kecamatan_tugas_id','pusat_kesehatan_id','users_id'];
    protected $primaryKey = "id_bidan";
    public $timestamps = false;

    public function kabupaten(){
        return $this->belongsTo('App\Http\Models\Kabupaten','kabupaten_id');        
    }

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

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

    public function pusKes(){
        return $this->belongsTo('App\Http\Models\PusatKesehatan','pusat_kesehatan_id');        
    }

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

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

        $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','=','bidan.users_id')->leftjoin('kecamatan','kecamatan.id_kecamatan','=','bidan.kecamatan_tugas_id')->leftjoin('pusat_kesehatan','pusat_kesehatan.id_pusat_kesehatan','=','bidan.pusat_kesehatan_id');
        });
        return $data;
    }
}
