<?php
if($this->session->userdata('is_logged_in')===FALSE){
    redirect('login');
}

$url = base_url("resources/template/relaas/perdata_agama/".$blangko_relaas_termohon.".rtf");

$Template = file_get_contents($url);

foreach($dataperkara->result() as $row) {
	$nomorperkara = $row->nomor_perkara;
	$tgl_daftar = $this->templatehelper->convertKeTglIndo($row->tanggal_pendaftaran);
}

$Template = str_replace("#NOMOR_PERKARA#",''.$nomorperkara.'',$Template);

$namapn = '';
$namakota = '';

foreach ($datapengadilan->result() as $row) {
	$namapn = $this->templatehelper->standradnaming($row->NamaPN);
	$namakota =str_replace("Pengadilan Agama ",'',$namapn);
	$zona_waktu = trim($row->ZonaWaktu);
    $alamatSatker = $row->AlamatPN;
    $KetuaPA = $row->KetuaPNNama;
}

$Template = str_replace('#NAMA_PA#',''.$namapn.'',$Template);
$Template = str_replace('#KOTA_PA#',''.$namakota.'',$Template);
$Template = str_replace('#ALAMAT_PA#',''.$alamatSatker.'',$Template);
$Template = str_replace('#KETUA_PA#',''.$KetuaPA.'',$Template);
$hakimketua = '';
$tanggalpenetapanhakim = '';

$majelis = true;
$tglsidang             = '..............';
$tglpenetapansidang    = '..............';
$jamsidang             = '..............';
$harisidang            = '..............';
if($datapenetapansidang->num_rows >0){
    foreach ($datapenetapansidang->result() as $row) {
    	$tglsidang             = $this->templatehelper->convertKeTglIndo($row->sidang_pertama);
    	$tglpenetapansidang    = $this->templatehelper->convertKeTglIndo($row->penetapan_hari_sidang);
    	$jamsidang             = $row->jam_sidang;
    	$harisidang            = $this->templatehelper->getHari($row->sidang_pertama);
    }
}

$Template = str_replace("#TGL_SIDANG#",''.$tglsidang.'',$Template);
$Template = str_replace("#TGL_PHS#",''.$tglpenetapansidang.'',$Template);
$Template = str_replace("#JAM_SIDANG#",''.substr($jamsidang,0,5).' '.$zona_waktu ,$Template);
$Template = str_replace("#HARI_SIDANG#",''.$harisidang.'',$Template);

$nama_js    = "...................";
$jabatan    = "Jurusita Pengganti";
if($datajurusita!=''){
    if($datajurusita->num_rows>0){
        foreach ($datajurusita->result() as $row) {
            $nama_js    = $this->templatehelper->standradnaming($row->nama_gelar);
            if($row->jabatan==1)
                $jabatan = "Jurusita";
            else
                $jabatan = "Jurusita Pengganti";
        }
    } 
}

$Template = str_replace("#JABATAN#",''.$jabatan.'',$Template);
$Template = str_replace("#NAMA_JURUSITA#",''.$nama_js.'',$Template);

$tgl_putusan = '..................';
if(!empty($dataputusan)){
    if($dataputusan->num_rows>0){
        foreach ($dataputusan->result() as $row) {
            $tgl_putusan = $this->templatehelper->convertKeTglIndo($row->tanggal_putusan);
            break;
        }
    }
}
$Template = str_replace("#TGL_PUTUSAN#",''.$tgl_putusan.'',$Template);

$counter = 1;
$pihak1 = '';
$pihak1_id='';
$arr_pihak1 = array();
$numbering = true;
if($dataPihak1->num_rows >1){
    for ($i=0; $i < $dataPihak1->num_rows; $i++) { 
        $text = 'penggugat'.($i+1);
        if($i==$dataPihak1->num_rows-1){
            $Template = str_replace('#loopingpenggugat#','\b#'.$text.'#\b0',$Template);
        }else{
            $Template = str_replace('#loopingpenggugat#','\b#'.$text.'#\b0\\par'.PHP_EOL.'#loopingpenggugat#',$Template);
        }
    }   
}else{
    $numbering = false;
    $Template = str_replace('#loopingpenggugat#','#penggugat#',$Template);
}

if (!empty($dataPihak1->num_rows)){   
    foreach ($dataPihak1->result() as $row){
        $pihak1_id=$row->pihak_id;
        $arr_pihak1[]=$row->id;
        if ($dataPihak1->num_rows==1) {
            $tab='';
            $penggugat = '';
        }else{
            $tab='\tab';
            $penggugat = '\b0\\par ';
        }
        
        $pihak1 = $penggugat;
        $pihak1 .= ''.' Nama '.$this->templatehelper->standradnaming($row->nama).', Umur' . ' tahun,' . ' agama '.
        $row->agama.
        ', pekerjaan '.$row->pekerjaan.', pendidikan  '.$row->pendidikan.', tempat kediaman di '.$row->alamat.', sebagai #jenis_pihak1# #penggugatke#';

        if($numbering==true){
            $Template = str_replace('##jenis_pihak1#'.$counter.'#',$pihak1,$Template);
            $Template = str_replace('#penggugat#',$counter,$Template);
            $Template = str_replace('#penggugat/parapenggugat#','Para #jenis_pihak1#',$Template);
        }else{
           $Template = str_replace('#penggugat#',$pihak1,$Template);
           $Template = str_replace('#penggugatke#','',$Template);
           $Template = str_replace('#penggugat/parapenggugat#','Untuk selanjutnya disebut sebagai #jenis_pihak1#',$Template);
        }
        $counter++;    
    }
}


$counter = 1;
$pihak2 = '';
$pihak2_id='';
$arr_pihak2 = array();
$numbering = true;
if($dataPihak2->num_rows >1){
    for ($i=0; $i < $dataPihak2->num_rows; $i++) { 
        $text = 'tergugat'.($i+1);
        if($i==$dataPihak2->num_rows-1){
            $Template = str_replace('#loopingtergugat#','#'.$text.'#',$Template);
        }else{
            $Template = str_replace('#loopingtergugat#','#'.$text.'#\\par'.PHP_EOL.'#loopingtergugat#',$Template);
        }
    }   
}else{
    $numbering = false;
    $Template = str_replace('#loopingtergugat#','#tergugat#',$Template);
}

if (!empty($dataPihak2->num_rows)){   
    foreach ($dataPihak2->result() as $row){
        $pihak2_id=$row->pihak_id;
        $arr_pihak2[]=$row->id;
        if ($dataPihak2->num_rows==1) {
            $tab='';
            $tergugat = '';
        }else{
            $tab='\tab';
            $tergugat = '\b0\\par ';
        }
        
        $pihak2 = $tergugat;
        $pihak2 .= ''.' Nama '.$this->templatehelper->standradnaming($row->nama).', Umur' . ' tahun,' . ' agama '.
        $row->agama.
        ', pekerjaan '.$row->pekerjaan.', pendidikan  '.$row->pendidikan.', tempat kediaman di '.$row->alamat.', sebagai #jenis_pihak2# #tergugatke#';

        if($numbering==true){
            $Template = str_replace('#tergugat'.$counter.'#',$pihak2,$Template);
            $Template = str_replace('#tergugatke#',$counter,$Template);
            $Template = str_replace('#tergugat/paratergugat#','Para \b#jenis_pihak2#\b0',$Template);
        }else{
           $Template = str_replace('#tergugat#',$pihak2,$Template);
           $Template = str_replace('#tergugatke#','',$Template);
           $Template = str_replace('#tergugat/paratergugat#','Untuk selanjutnya disebut sebagai #jenis_pihak2#',$Template);
        }
        $counter++;    
    }
}

$data_t = $dataPihak2;


    foreach ($data_t->result() as $row) {
             $nama_t  =$row->nama;
             $pekerjaan_t = $row->pekerjaan;
             $alamat_t =$row->alamat;
             $umur_t   =$row->umur;
             $agama_t   =$row->agama;
             $pendidikan_t   =$row->pendidikan;
             

 }

$Template = str_replace("#nama_t#",''.$nama_t.'',$Template);
$Template = str_replace("#umur_t#",''.$umur_t.'',$Template);
$Template = str_replace("#agama_t#",''.$agama_t.'',$Template);
$Template = str_replace("#pendidikan_t#",''.$pendidikan_t.'',$Template);
$Template = str_replace("#pekerjaan_t#",''.$pekerjaan_t.'',$Template);
$Template = str_replace("#alamat_t#",''.$alamat_t.'',$Template);

$idperkara=$this->encrypt->decode(base64_decode($enc));
$idalurperkara = $this->tanggalhelper->getIDAlurPerkara($idperkara);
$jenisPerkaraID=$this->tanggalhelper->getIDJenisPerkara($idperkara);

if ($idalurperkara==15) {
    $Template = str_replace("#GUGATAN_PERMOHONAN#",''." Gugatan ".'',$Template);
    if ($jenisPerkaraID==346) {
        $Template = str_replace("#jenis_pihak1#",''." Pemohon ".'',$Template);
        $Template = str_replace("#jenis_pihak2#",''." Termohon ".'',$Template);
        $Template = str_replace("#SEBAGAI_P#","Pemohon",$Template);
        $Template = str_replace("#SEBAGAI_T#","Termohon",$Template);
    }else{
        $Template = str_replace("#jenis_pihak1#",''." Penggugat ".'',$Template);
        $Template = str_replace("#jenis_pihak2#",''." Tergugat ".'',$Template);
        $Template = str_replace("#SEBAGAI_P#","Penggugat",$Template);
        $Template = str_replace("#SEBAGAI_T#","Tergugat",$Template);
    }
}else{
    $Template = str_replace("#GUGATAN_PERMOHONAN# ",''." Permohonan ".'',$Template);   
    $Template = str_replace("#jenis_pihak1#",''." Pemohon ".'',$Template);
    $Template = str_replace("#jenis_pihak2#",''." Termohon ".'',$Template);
}

$Template = str_replace("#NAMA_P#",$dataPihak1->row(0)->nama,$Template);
$Template = str_replace("#NAMA_T#",$dataPihak2->row(0)->nama,$Template);

header("Content-Type: application/vnd.ms-word");
header("Expires: 0");
header("Cache-Control:  must-revalidate, post-check=0, pre-check=0");
header("Content-disposition: attachment; filename=\""."Relaas Ikrar Talak Termohon"."-".$nomorperkara.".rtf\"");
echo $Template;
?>