<?php

namespace app\Http\Models;

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

class PernikahanPasangan extends Model
{
    protected $table = 'pernikahan_pasangan';
    protected $fillable = ['id_pernikahan_pasangan','syarat_administrasi','akad_sebelum_kua','tgl_akad','menerima_vitamin','ikut_konseling','tgl_pernikahan','no_reg_pernikahan','identitas_pasangan_id','user_kua_id'];
    protected $primaryKey = "id_pernikahan_pasangan";
    public $timestamps = false;

    public function kua(){
        return $this->belongsTo('App\Http\Models\UserKUA','user_kua_id');        
    }

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

    public static function getJson($input)
    {
        $table  = 'pernikahan_pasangan';
        $select = 'pernikahan_pasangan.*, no_registrasi, tgl_registrasi, identitas_pasangan.nama_suami, 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('identitas_pasangan','identitas_pasangan.id_identitas_pasangan','=','pernikahan_pasangan.identitas_pasangan_id')
                        ->leftjoin('ibu_hamil','ibu_hamil.id_bumil','=','identitas_pasangan.bumil_id');
        });
        return $data;
    }

    public static function getJsonCustom($input, $kolom, $isi)
    {
        $table  = 'pernikahan_pasangan';
        $select = 'pernikahan_pasangan.*, no_registrasi, tgl_registrasi, identitas_pasangan.nama_suami, 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('identitas_pasangan','identitas_pasangan.id_identitas_pasangan','=','pernikahan_pasangan.identitas_pasangan_id')
                        ->leftjoin('ibu_hamil','ibu_hamil.id_bumil','=','identitas_pasangan.bumil_id');
        }, $kolom, $isi);
        return $data;
    }
}
