<?php session_name('CAKEPHP'); session_start();	
$PageSecurity = 1;
$tabindex=0;
include('includes/DefinePaymentClass.php');
$title = _('Telephonic Order Memo');
include('includes/SQL_CommonFunctions.inc');
include('CRM/functions.php');
$s=new db_query();

if(isset($_REQUEST['save']))
{
   DB_query("delete from perf_approval_list",$db);
    foreach($_REQUEST['approve'] as $key=>$val)
    {
        $app_by=implode(',',$_REQUEST['approved_by'][$key]) ;
    
        DB_query("insert into perf_approval_list(approval,seq_no,approved_by) value('".$val."','".$_REQUEST['sequence'][$key]."','".$app_by."')",$db);
    }
    $s->pageLocation("index.php?route=TaskMgt/assign_task_approval".$rurl);  
}

if(isset($_REQUEST['aid']))
{
    $erow=DB_fetch_assoc(DB_query("select * from `perf_approval_list` where id='".$_REQUEST['aid']."' ",$db));
}

if(isset($_REQUEST['update']))
{
    $str1=implode(',',$_REQUEST['app_by']);
    DB_query("update perf_approval_list set approval='".$_REQUEST['app']."',seq_no='".$_REQUEST['seq']."',approved_by='".$str1."' where id='".$_REQUEST['aid']."' ",$db);
     $s->pageLocation("index.php?route=TaskMgt/assign_task_approval".$rurl);
}
?>
<script src="ui/jquery.ui.timepicker.js"></script>
<link rel="stylesheet" href="css/new_mxerp.css">
<style>
#showdata {
	display: block;
	background: url(Default.png) no-repeat;
	top: 0px;
	left: 0px;
	width: 300px;
	min-height: 60px;
	z-index:0;
}
#back {
	display: none;
	top: 0px;
	left: 0px;
	width: 300px;
	min-height: 60px;
	z-index:0;
}
</style>
<div class="body_contain">
<div class="tab_div">
<h1>Assign Task Approval</h1>
    <?php if(!isset($_REQUEST['aid'])) { ?><table >
        <tr>
            <td><strong> Number of Approvals</strong></td>
            <td><input type="text" name="app_num" id="app_num" value="" onkeypress="return isNumberKey(event)"  maxlength="2"></td>
            <td><button value="row" name="row" type="button" onclick="javascript:generate_row();" >Generate Rows</button></td>
        </tr>
    </table>
<div id="all_data"></div>
</div></div>
    <?php }?>

<?php if(isset($_REQUEST['aid'])){ ?>
<div>
    <form method="post" action="">
    <table><tr><th>Sequence Number</th><th>Approval Name</th><th>Approval By</th></tr>
<tr><td><input type="text" value="<?php echo $erow['seq_no'] ?>" name=seq></td>
       <td><input type="text" value="<?php echo $erow['approval'] ?>" name="app"></td><td>
      <?php $usersql=DB_query("select * from www_users where companyid='".$_SESSION['ACGCompanyId']."' ",$db);
            while($user1=DB_fetch_assoc($usersql)){ 
                $app=explode(",",$erow['approved_by']);
                if(in_array($user1['id'],$app))
                {
                   $p="checked";
                }
                else
                {
                    $p="";
                }
                ?>
            <input type="checkbox" name="app_by[]" <?php echo $p; ?> value="<?php echo $user1['id'] ?>"><?php echo $user1['realname'] ?>
          <?php   } ?>
        </td>
</tr>

<tr><td><button name="update" value="update">Update</button></td></table>
</form>
    
</div>
<?php } ?>
<div>
    <table width="80%" align="center" cellpadding="3" cellspacing="0" border="1" style="border-collapse:collapse;">
        <tr bgcolor="#CCCCCC">
            <td><strong>Sequence No.</strong></td>
            <td><strong>Approval Name</strong></td>
            <td><strong>Approved By</strong></td>
            <td><strong>Edit</strong></td>
        </tr>
        <?php $sql=DB_query("select * from perf_approval_list",$db); while($res=DB_fetch_assoc($sql)){ ?>
            <tr>
                <td><?php echo $res['seq_no']; ?></td>
                <td><?php echo $res['approval']; ?></td>
                <td><?php $app_by=explode(',',$res['approved_by']); $str=""; foreach($app_by as $val){ $name=DB_fetch_assoc(DB_query("select realname from www_users where id='".$val."' ",$db)); $str.=$name['realname'].","; } echo substr($str,0,-1) ?></td>
                <td><a href="index.php?route=TaskMgt/assign_task_approval&aid=<?php echo $res['id'].$rurl ?>"><img src="images/icon_edit.gif" width="10px" height="10"></a></td>
            </tr>
            <?php } ?>
    </table>
</div>

<script>
    function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

function generate_row()
{  
  var num= document.getElementById("app_num").value;
  if(num==0)
      {
          alert("Can not be zero");
          document.getElementById("app_num").value="";
          document.getElementById("app_num").focus();
      }
      else{
  var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("all_data").innerHTML=xmlhttp.responseText;
    }
  }

xmlhttp.open("POST","ajax_generate_row.php?num="+num,true);
xmlhttp.send();
}
}
</script>
