<?php

namespace app\Http\Models;

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

class KesehatanPasangan extends Model
{
    protected $table = 'kesehatan_pasangan';
    protected $fillable = ['id_kesehatan_pasangan','tbc','diabetes','jantung','hipertensi','infeksi_kandungan','penyakit_lain','nama_penyakit','tekanan_darah','kesehatan_umum','imunisasi_tt','ket_imun_tt','kontrasepsi','jenis_kontrasepsi','ingin_anak_batuta','hb','test_kehamilan','test_lain','berat_badan','tinggi_badan','lingkar_lengan_atas','lingkar_pinggang','lingkar_panggul','vitamin_pengantin','buku_saku','tgl_periksa','identitas_pasangan_id','bumil_id','pusat_kesehatan_id'];
    protected $primaryKey = "id_kesehatan_pasangan";
    public $timestamps = false;

    public function idenPas(){
        return $this->belongsTo('App\Http\Models\IdentitasPasangan','identitas_pasangan_id');        
    }
    
    public function bumil(){
        return $this->belongsTo('App\Http\Models\IbuHamil','bumil_id');        
    }

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

    public static function getJson($input)
    {
        $table  = 'kesehatan_pasangan';
        $select = 'kesehatan_pasangan.*, nama_bumil, ibu_hamil.nik,  nama';
        
        $replace_field  = [
            ['old_name' => 'tglRegis', 'new_name' => 'tgl_registrasi'],
        ];

        $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('ibu_hamil','ibu_hamil.id_bumil','=','kesehatan_pasangan.bumil_id')
                        ->leftjoin('pusat_kesehatan','pusat_kesehatan.id_pusat_kesehatan','=','kesehatan_pasangan.pusat_kesehatan_id');
        });
        return $data;
    }
}
