<?php

namespace app\Http\Models;

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

class IdentitasPasangan extends Model
{
    protected $table = 'identitas_pasangan';
    protected $fillable = ['id_identitas_pasangan','no_registrasi','tgl_registrasi','nik_suami','nama_suami','tmp_lahir_suami','tgl_lahir_suami','agama_suami','suku_suami','pendidikan_suami','pekerjaan_suami','alamat_suami','desa_suami_id','kecamatan_suami_id','kabupaten_suami_id','provinsi_suami_id','pernah_menikah_suami','foto_suami','suku_istri','pernah_menikah_istri','bumil_id','user_desa_id'];
    protected $primaryKey = "id_identitas_pasangan";
    public $timestamps = false;

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

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

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

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

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

    public function usrDesa(){
        return $this->belongsTo('App\Http\Models\UserDesa','user_desa_id');        
    }

    public static function getJson($input)
    {
        $table  = 'identitas_pasangan';
        $select = 'identitas_pasangan.*, nama_bumil';
        
        $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','=','identitas_pasangan.bumil_id');
        });
        return $data;
    }

    public static function getJsonCustom($input, $kolom, $isi)
    {
        $table  = 'identitas_pasangan';
        $select = 'identitas_pasangan.*, nama_bumil';
        
        $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_custome1($param, function($data){
            return $data->leftjoin('ibu_hamil','ibu_hamil.id_bumil','=','identitas_pasangan.bumil_id');
        }, $kolom, $isi);
        return $data;
    }
}
