<link href="css/form.css" type="text/css" rel="stylesheet" />
<script language="javascript">
<!-- 
  function setKode(){
    
    var vket = document.getElementById("txtKeterangan").value;
    if(vket == "PENYEDIA"){
      document.getElementById("txtKode").value="1";
    }else if(vket == "SWAKELOLA"){
      document.getElementById("txtKode").value="2";
    }else{
      document.getElementById("txtKode").value="0";
    }
  }
-->
</script>

<?php
  
  //$tahun1=date("Y");
  //$tahun2=$tahun1+1;
  //$tahun_anggaran=$tahun1 . "/" . $tahun2;

  $id_desa = $_SESSION["ses_id_desa"];
	
  
  //mengambil data tahun anggaran aktif untuk user yang sedang aktif
  $username=$_SESSION["ses_admin_id"];
  $qryy=mysqli_query($mysqli,"SELECT * FROM settings WHERE username='$username'");

  if(mysqli_num_rows($qryy)>0){
    $datay=mysqli_fetch_array($qryy);
    $tahun_anggaran=$datay["tahun_anggaran"];
  }else{
    msgbox("Tahun Anggaran belum diatur di halaman pengaturan!");
    lempar("admin.php?page=settings&page_desc=Pengaturan Tahun Anggaran Aktif");
    die();
  }

  if(isset($_GET[kode_rekening])){
    //$kode1=substr($_GET[kode_rekening], 0,1);
    //$kode2=substr($_GET[kode_rekening], 1,1);
    //$kode3=substr($_GET[kode_rekening], 2,1);
    //$kode4=substr($_GET[kode_rekening], 3,1);
	
	$vkode=explode(".", $_GET[kode_rekening]);
    $kode1=$vkode[0];
    $kode2=$vkode[1];
    $kode3=$vkode[2];
    $kode4=$vkode[3];

  }

	echo "<h3>MENAMBAHKAN DATA ANGGARAN</h3>";
	echo "<h3 align=\"right\"><a href=\"?page=ls_anggaran_desa&page_desc=Daftar Mata Anggaran\" style=\"color:#FF0066\">Lihat Semua Mata Anggaran</a></h3>";
	
	
	//cek kode rekening terakhir untuk sub akun ini
	$q=mysqli_query($mysqli, "SELECT * FROM t_anggaran WHERE id_desa='$id_desa' AND tahun_anggaran='$tahun_anggaran' AND jenis_kode_rekening='H' AND induk_kode_rekening='$_GET[kode_rekening]' ORDER BY kode_rekening DESC");
	
	
	if(mysqli_num_rows($q)>0){
		$d=mysqli_fetch_array($q);
		$kode_terakhir=$d["kode_rekening"];
		
		$vkodex=explode(".", $kode_terakhir);
		$kodex1=$vkodex[0];
		$kodex2=$vkodex[1];
		$kodex3=$vkodex[2];
		$kodex4=$vkodex[3];
		
		$jumdigit=sizeof($vkodex);
		$digit_1="";
		$digit_2="";
		$digit_3="";
		$digit_4="";
		
		if($jumdigit=1){
			$digit_1=$kodex1;
			$digit_2=intval($kodex2)+1;
		}elseif($jumdigit=2){
			$digit_1=$kodex1;
			$digit_2=$kodex2;
			$digit_3=intval($kodex3)+1;
		}elseif($jumdigit=3){
			$digit_1=$kodex1;
			$digit_2=$kodex2;
			$digit_3=$kodex3;
			$digit_4=intval($kodex4)+1;
		}
		
		//echo "JUM DIGIT: " . $jumdigit . "<br/>";
		//echo "Digit 1: ". $digit_1 . "<br/>";
		//echo "Digit 2: ". $digit_2 . "<br/>";
		//echo "Digit 3: ". $digit_3 . "<br/>";
		//echo "Digit 4: ". $digit_4 . "<br/>";
		
		//echo "KODE TERAKHIR: " . $kode_terakhir . "<br/>";
		
		/* $digit_1=substr($kode_terakhir,0,1);
		$digit_2=substr($kode_terakhir,1,1);
		$digit_3=substr($kode_terakhir,2,1);
		$digit_4=substr($kode_terakhir,3,1);
		
		if(strlen($kode_terakhir)==1){
			$digit_1=intval($digit_1)+1;
		}elseif(strlen($kode_terakhir)==2){
			$digit_2=intval($digit_2)+1;
		}elseif(strlen($kode_terakhir)==3){
			$digit_3=intval($digit_3)+1;
		}elseif(strlen($kode_terakhir)==4){
			$digit_4=intval($digit_4)+1;
		} */
	}else{
		
		
		if($_GET["detail"]==true){
			$vkodex=explode(".", $_GET[kode_rekening]);
			$kodex1=$vkodex[0];
			$kodex2=$vkodex[1];
			$kodex3=$vkodex[2];
			$kodex4=$vkodex[3];
			
			$digit_1=$kodex1;
			$digit_2=$kodex2;
			$digit_3=$kodex3;
			$digit_4=$kodex4;
		}else{
			$vkodex=explode(".", $_GET[kode_rekening]);
			$kodex1=$vkodex[0];
			$kodex2=$vkodex[1];
			$kodex3=$vkodex[2];
			$kodex4=$vkodex[3];
			
			$digit_1=$kodex1;
			$digit_2=$kodex2;
			$digit_3=$kodex3;
			$digit_4=$kodex4;
			
			if(sizeof($vkodex)==1){
				$digit_1=intval($digit_1);
				$digit_2=2;
			}elseif(sizeof($vkodex)==2){
				$digit_1=intval($digit_1);
				$digit_2=intval($digit_2);
				$digit_3=2;
			}elseif(sizeof($vkodex)==3){
				$digit_1=intval($digit_1);
				$digit_2=intval($digit_2);
				$digit_3=intval($digit_3);
				$digit_4=2;
			}
		}
		
		/*
		if(strlen($_GET[kode_rekening])==1){
			$digit_1=intval($digit_1)+1;
		}elseif(strlen($_GET[kode_rekening])==2){
			$digit_2=intval($digit_2)+1;
		}elseif(strlen($_GET[kode_rekening])==3){
			$digit_3=intval($digit_3)+1;
		}elseif(strlen($_GET[kode_rekening])==4){
			$digit_4=intval($digit_4)+1;
		}
		*/
	}
	
	
?>

<form name="form1" method="post" action="_save_anggaran_desa.php">
  <table width="600" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="204">&nbsp;</td>
      <td width="396">&nbsp;</td>
    </tr>
    <tr>
      <td valign="top">Tahun Anggaran</td>
      <td valign="top"><input type="text" name="txtTahunAnggaran" id="txtTahunAnggaran" value="<?php echo $tahun_anggaran ?>" class="kotak" size="20" maxlength="10" required></td>
    </tr>
    <tr>      
      <td valign="top">Kode Rekening</td>
      <td valign="top">
        <select name="txtKodeRekening01" id="txtKodeRekening01" class="kotak">
          <?php
              for($i=0;$i<=99;$i++){
                $s="";
				if($i==$digit_1){
                 
                  $s="selected";
                }
                
				
                echo "<option value='$i' $s>$i</option>";
              }

              if(!empty($kode1)){
                echo "<option value='' >&nbsp;</option>";
              }else{
                echo "<option value='' selected>&nbsp;</option>"; 
              }

          ?>
        </select>&nbsp;
        <select name="txtKodeRekening02" id="txtKodeRekening02" class="kotak">
          <?php
              
              
              for($i=0;$i<=99;$i++){
                $s="";
                if($i==$kode2){
                  $s="selected";
                }
				
				
                echo "<option value='$i' $s>$i</option>";
              }

              if(!empty($kode2)){
                echo "<option value='' >&nbsp;</option>";
              }else{
                echo "<option value='' selected>&nbsp;</option>"; 
              }
          ?>
        </select>&nbsp;
        <select name="txtKodeRekening03" id="txtKodeRekening03" class="kotak">
          <?php
              //echo "<option value='' selected>&nbsp;</option>";
              for($i=0;$i<=99;$i++){
                $s="";
                if($i==$kode3){
                  $s="selected";
                }
				
				
                echo "<option value='$i' $s>$i</option>";
              }

              if(!empty($kode3)){
                echo "<option value='' >&nbsp;</option>";
              }else{
                echo "<option value='' selected>&nbsp;</option>"; 
              }
          ?>
        </select>&nbsp;

        <select name="txtKodeRekening04" id="txtKodeRekening04" class="kotak">
          <?php
              //echo "<option value='' selected>&nbsp;</option>";
              for($i=0;$i<=99;$i++){
                $s="";
                if($i==$kode4){
                  $s="selected";
                }
				
                echo "<option value='$i' $s>$i</option>";
              }
              if(!empty($kode4)){
                echo "<option value='' >&nbsp;</option>";
              }else{
                echo "<option value='' selected>&nbsp;</option>"; 
              }
          ?>
        </select>&nbsp;
		<?php
			if(!isset($_GET["detail"])){
				echo "&nbsp;&nbsp;<font color='red' size='1'>INFO KODE REK TERAKHIR: " . $kode_terakhir . "</font><br/>";
			}
		?>
      </td>
    </tr>

    <tr>
      <td valign="top">Jenis Kode</td>
      <td valign="top">
			<?php
				if(isset($_GET["detail"])){
					if($_GET["detail"]==true){
						$s_d="selected";
						$s_h="";
					}else{
						$s_d="";
						$s_h="selected";
					}
				}else{
					$s_d="";
					$s_h="selected";
				}
			?>
          <select name="txtJenisKode" id="txtJenisKode" class="kotak" >
            <option value="" >&nbsp;</option>
            <option value="H" <?php echo $s_h ?> >Heading/Sub Header</option>
            <option value="D" <?php echo $s_d ?>>Detail/Rincian</option>
          </select>
    </tr>



    <tr>
      <td valign="top">Uraian</td>
      <td valign="top"><input type="text" name="txtUraian" id="txtUraian" class="kotak" size="60" required></td>
    </tr>

    <tr>
      <td valign="top">Anggaran</td>
      <td valign="top"><input type="text" name="txtAnggaran" id="txtAnggaran" class="kotak" size="30" ></td>
    </tr>

    <tr>
      <td valign="top">Keterangan</td>
      <td valign="top">
          <select name="txtKeterangan" id="txtKeterangan" class="kotak" onchange="setKode()">
            <option value="" selected="">&nbsp;</option>
            <option value="PENYEDIA">1. PENYEDIA</option>
            <option value="SWAKELOLA">2. SWAKELOLA</option>

          </select>
    </tr>
    
    <tr>
      <td valign="top">Kode</td>
      <td valign="top">
          <select name="txtKode" id="txtKode" class="kotak">
            <option value="" selected>&nbsp;</option>
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>

          </select>
    </tr>

    



    <tr>
      <td width="204">&nbsp;</td>
      <td width="396">&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="hidden" name="txtKodeRekeningAbs" id="txtKodeRekeningAbs" value="<?php echo $_GET[kode_rekening] ?>"/><input type="submit" name="button" id="button" value="Simpan" class="tombol"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>

