<?php
include_once("../../class/clsConnection.php");
include_once(
"../../class/clsDB.php");
include_once 
"../global.php";
include_once 
"../link/function.php";
include_once 
"../class/clsDepartment.php";
include_once 
"../class/clsdepttype.php";
include_once 
"../class/clsdeptref.php";

$oC = new clsConnection($GLOBALS['DBHOST'], $GLOBALS['DBNAME_EPERSON'], $GLOBALS['DBUSER_EPERSON'], $GLOBALS['DBPASS_EPERSON']);

$oDp = new Department($oC);
$oDpT = new Department($oC);
$oDp2 = new Department($oC);
$oDp3 = new Department($oC);
$oDp4 = new Department($oC);
$oDp5 = new Department($oC);
$oDp6 = new Department($oC);
$oDpt= new Departmenttype($oC);
$maxdate $oDp5->MaxDate();
$oDr= new deptref($oC);

?>
<script language="JavaScript1.2">
function checkFormat(){
    var xv = 0;
    if(document.ps.deptName.value==""){
        alert("กรุณากรอกชื่อส่วนราชการภายใน!");
        xv = 1;
        return false;
    }
    if(document.ps.depttypeId.value==""){
        alert("กรุณากรอกประเภทส่วนราชการภายใน!");
        xv = 1;
        return false;
    }
}
</script>
<? 
if($method=="add" && $xv != 1){ 
        
//    $oC->BeginTrans();
            
if($depttypeId == || $depttypeId == || $depttypeId == 3  || $depttypeId == || $depttypeId == 5  || $depttypeId == 6){   
                    echo 
"  if 1234";
                    if(
$depttypeId == || $depttypeId == 2){   
                            echo 
"  if 12";
                            
$oDp2->SearchByName();
                            if(
$oDp2->GetRecord() == 1)
                                    
$oDp->deptgroup $oDp2->deptId;
                            else
                                    
$oDp->deptgroup 1;  
                    }
                     if(
$depttypeId == 3  || $depttypeId == 4) {   
                             echo 
"  if 34";
                            
$oDp->deptgroup $oDp->deptId;
                     }
                     echo 
"-------".$oDp->deptgroup;
                     
$oDp->AddNew();
                    
$oDp->deptId=$oDp->GetNextCode();
                    
$oDp->deptName=$deptName;
                    
$oDp->deptDesc=$deptDesc;
                    
$oDp->deptCode=$deptCode;
                    
$oDp->deptParent=0;
                    
$oDp->deptYear=$deptYear;
                    
$oDp->deptDate=$deptDate;
                    
$oDp->deptRef=0;
                    
$oDp->pbriId=$pbriId;
                    
$oDp->depttypeId=$depttypeId;
                    
$oDp->deptParentId=$deptParentId;
                    
$oDp->deptLevel=$deptLevel;
                    
$oDp->deptgroup=$oDp->deptgroup;
                    
$oDp->Save();
                    
$oDp4->searchdeptDate($maxdate);
                    while(
$oDp4->GetRecord()){
                        if(
$deptmaxdate[$oDp4->deptId]==1){
                                
$oDr->AddNew();
                                
$oDr->drId=$oDr->GetNextCode();
                                
$oDr->deptId=$oDp->deptId;
                                
$oDr->deptrefId=$oDp4->deptId;
                                
$oDr->Save();
                        }
                    }
                     
            }else{    
                    while(
$count != 'END') {
                            
$oDp->SearchByKey($deptParentId);
                            
$oDp->GetRecord();
                            if(
$oDp->depttypeId == || $oDp->depttypeId == || $oDp->depttypeId == 3  || $oDp->depttypeId == 4){ //echo "  else 1234";
                                    
if($oDp->depttypeId == || $oDp->depttypeId == 2){//echo "  else 12";
                                            
$oDp2->SearchByName();
                                            if(
$oDp2->GetRecord() == 1)
                                                    
$oDp->deptgroup $oDp2->deptId;
                                            else
                                                    
$oDp->deptgroup 1;
                                    }else if(
$oDp->depttypeId == 3  || $oDp->depttypeId == 4){ //echo "  else 34";
                                            
$oDp->deptgroup $oDp->deptId;
                                            
$count 'END';
                                    }
                            }else{
                                    
$deptParentId $oDp->deptParentId;
                                    
$count 'NO';
                            }
                }
//while
                
$oDp->AddNew();
                
$oDp->deptId=$oDp->GetNextCode();
                
$oDp->deptName=$deptName;
                
$oDp->deptDesc=$deptDesc;
                
$oDp->deptCode=$deptCode;
                
$oDp->deptParent=0;
                
$oDp->deptYear=$deptYear;
                
$oDp->deptDate=$deptDate;
                
$oDp->deptRef=0;
                
$oDp->pbriId=$pbriId;
                
$oDp->depttypeId=$depttypeId;
                
$oDp->deptParentId=$deptParentId;
                
$oDp->deptLevel=$deptLevel;
                
$oDp->deptgroup=$deptG;
                 
$oDp->Save();
                    
$oDp4->searchdeptDate($maxdate);
                    while(
$oDp4->GetRecord()){
                        if(
$deptmaxdate[$oDp4->deptId]==1){
                                
$oDr->AddNew();
                                
$oDr->drId=$oDr->GetNextCode();
                                
$oDr->deptId=$oDp->deptId;
                                
$oDr->deptrefId=$oDp4->deptId;
                                
$oDr->Save();
                        }
                    }    
        }
//else
                
                
$oDp5->RSdeptSetgroupHeaddeptDate0();
                while(
$oDp5->GetRecord()){
                                
                                
$oDp3->SearchByName();
                                if(
$oDp3->GetRecord() == 1)
                                        
$deptG $oDp3->deptId;
                                else
                                        
$deptG 1;
                                        
                        
$oDp6->SearchByKey($oDp5->deptId);
                        
$oDp6->GetRecord();
                        
$oDp6->Edit();
                        
$oDp6->deptId=$oDp6->deptId;
                        
$oDp6->deptName=$oDp6->deptName;
                        
$oDp6->deptDesc=$oDp6->deptDesc;
                        
$oDp6->deptCode=$oDp6->deptCode;
                        
$oDp6->deptParent=$oDp6->deptParent;
                        
$oDp6->deptYear=$oDp6->deptYear;
                        
$oDp6->deptDate=$oDp6->deptDate;
                        
$oDp6->deptRef=$oDp6->deptRef;
                        
$oDp6->pbriId=$oDp6->pbriId;
                        
$oDp6->depttypeId=$oDp6->depttypeId;
                        
$oDp6->deptParentId=$oDp6->deptParentId;
                        
$oDp6->deptLevel=$oDp6->deptLevel;
                        
$oDp6->deptgroup=$deptG;
                        
$oDp6->Save();
            }
?>
<script language="javascript1.2">
window.opener.location.href="deptstructure.php?startyear=<?php echo $startyear;?>&endyear=<?php echo $endyear;?>";
window.close();
</script>
<?php    
}
?>
<html>
<head>
<title>เพิ่มส่วนราชการภายใน</title>
<meta http-equiv="Content-Type" content="text/html; charset=tis-620">
<link href="../source/style.css" rel="stylesheet" type="text/css">
<style>
<!--
A:link {text-decoration: none }
A:visited {text-decoration: none}
A:hover {text-decoration: none; font-weight: underline}
-->
</style>
</head>
<body bgcolor="#FFFFFF">
<form name="ps" action="<?php echo $PHP_SELF;?>" method="post" >
<table width="550" border="0" cellspacing="1" cellpadding="1" align="center">
    <tr > 
        <td align="center" colspan="4" height="22"><font size="3" color="<?php echo $GLOBALS['COLOR_FONT_3'];?>"><b>เพิ่มส่วนราชการภายใน</b></font></td>
    </tr>
                                <tr>
                                    <td colspan="4" height="22"><font size="2" color="<?php echo $GLOBALS['COLOR_FONT_3'];?>">&nbsp;</font></td>
                                </tr>
                                <tr height="22" >
                                    <td align="left" width="221" ><font size="2" color="<?php echo $GLOBALS['COLOR_FONT_4'];?>"><b>ชื่อส่วนราชการภายใน</b></font></td>
                                    <td align="left" width="4">&nbsp;</td>
                                    <td align="left" width="315"><input type="text" name="deptName" size="40" value="<? echo $deptName?>" onKeyPress="if(event.keyCode==39){  alert('ระบบไม่สามารถบันทึกตัวอักษรนี้ได้!!'); return false;}">
                                    <font size="2" color="<?php echo $GLOBALS['COLOR_FONT_3'];?>">*</font></td>
                                </tr>
                                <? if($maxdate!='0000-00-00' && $maxdate!=""){ ?>
                                <tr height="22" bgcolor="<?php echo $GLOBALS['COLOR_BG_TD_4'];?>">
                                    <td align="left"><font size="2" color="<?php echo $GLOBALS['COLOR_FONT_4'];?>"><b>ส่วนราชการภายใน<br>(โครงสร้างเพื่อการส่งหนังสือในระบบสารบรรณเก่า)</b></font></td>
                                    <td align="left" >&nbsp;</td>
                                    <td align="left">
                                    <?
                                            $oDp4
->searchdeptDate($maxdate);
                                            while(
$oDp4->GetRecord()){
                                    
?>
                                                    <input name="deptmaxdate[<? echo $oDp4->deptId?>]" type="checkbox" value="1">&nbsp;<? echo $oDp4->deptName?><br>
                                    <?  ?>        
                                    </td>
                                </tr>
                                <? ?>
                                <tr height="22" >
                                    <td align="left"><font size="2" color="<?php echo $GLOBALS['COLOR_FONT_4'];?>"><b>ประเภทของส่วนราชการภายใน</b></font></td>
                                    <td align="left" >&nbsp;</td>
                                    <td align="left">
                                <select name="depttypeId" >
                            <?     if($depttypeId == 0) {     ?>
                                  <option value="">----เลือกประเภทของส่วนราชการภายใน---- 
                            <?
                                            $oDpt
->RSDepartmenttype();
                                            while(
$oDpt->GetRecord())
                                                    echo
"<option value=\"    $oDpt->depttypeId\">$oDpt->depttypeName\n";
                                                    
                                    }
//end if depttypeId=0
                                    
else{
                                            
$oDpt->SearchByKey($depttypeId);
                                            
$oDpt->GetRecord();
                                            
                            
?>
                                  <option value="<? echo $oDpt->depttypeId;?>"><? echo $oDpt->depttypeName;?> 
                             <?
                                            $oDpt
->RSDepartmenttype();
                                            while(
$oDpt->GetRecord())
                                                    echo
"<option value=\"    $oDpt->depttypeId\">$oDpt->depttypeName\n";
                                                    
                                            }
//end else depttypeId=0
                            
?>
                                </select>

                                    <font size="2" color="<?php echo $GLOBALS['COLOR_FONT_3'];?>">*</font></td>
                                </tr>
                                <tr height="22" bgcolor="<?php echo $GLOBALS['COLOR_BG_TD_4'];?>">
                                    <td align="left" width="221" ><font size="2" color="<?php echo $GLOBALS['COLOR_FONT_4'];?>"><b>รายละเอียด</b></font></td>
                                    <td align="left" width="4">&nbsp;</td>
                                    <td align="left" width="315"><input type="text" name="deptDesc" size="40" value="<? echo $deptDesc?>" onKeyPress="if(event.keyCode==39){  alert('ระบบไม่สามารถบันทึกตัวอักษรนี้ได้!!'); return false;}">
                                    </td>
                                </tr>
                                <tr height="22">
                                    <td align="left" width="221" colspan="4" height="22"><font size="2" color="<?php echo $GLOBALS['COLOR_FONT_4'];?>"><b>&nbsp;</b></font></td>
                                </tr>
<tr height="40">
        <td colspan="4" align="center" bgcolor="#FFFFFF"><input type="submit" name="add" value="บันทึก" onClick="return checkFormat()">
          <input type="reset" name="clear" value="เคลียร์ข้อมูล">
        <input type="hidden" name="method" value="add">
        
        </td>
    </tr>
        <input type="hidden" name="deptParentId" value="<?php echo $deptParentId;?>">
        <input type="hidden" name="deptLevel" value="<?php echo $deptLevel;?>">
        <input type="hidden" name="startyear" value="<?php echo $startyear;?>">
        <input type="hidden" name="endyear" value="<?php echo $endyear;?>">
        <input type="hidden" name="dbeperson" value="<?php echo $GLOBALS['DBNAME_EPERSON'];?>">
        
        <br>
</table>
<table width="550" border="0" cellspacing="0" cellpadding="1" align="center" >
    <tr>
        <td align="center" colspan="4" bgcolor="#FFFFFF" height="22"><a href="#" onClick="window.close()"><font size="1" face="Microsoft Sans Serif">[ปิดหน้าต่าง]</font></a></td>
    </tr>
</table>
</form>
</body>
</html>