<?php

namespace app\Http\Models;

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

class IbuHamil extends Model
{
    protected $table = 'ibu_hamil';
    protected $fillable = ['id_bumil','nik','nama_bumil','tmp_lahir','tgl_lahir','agama','suku','pendidikan','pekerjaan','nama_suami','alamat_ktp','provinsi_ktp_id','kabupaten_ktp_id','kecamatan_ktp_id','desa_ktp_id','no_tlp','foto'];
    protected $primaryKey = "id_bumil";
    public $timestamps = false;

    public function provinsi(){
        return $this->belongsTo('App\Http\Models\Provinsi','provinsi_ktp_id');        
    }

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

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

    public function desa(){
        return $this->belongsTo('App\Http\Models\Desa','desa_ktp_id');        
    }

    public static function getJson($input)
    {
        $table  = 'ibu_hamil';
        $select = 'ibu_hamil.*, nama_kecamatan, nama_desa';
        
        $replace_field  = [
            ['old_name' => 'namaKecamatan', 'new_name' => 'nama_kecamatan'],
            ['old_name' => 'namaDesa', 'new_name' => 'nama_desa'],
        ];

        $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('kecamatan','kecamatan.id_kecamatan','=','ibu_hamil.kecamatan_ktp_id')->leftjoin('desa','desa.id_desa','=','ibu_hamil.desa_ktp_id');
        });
        return $data;
    }
}
