<?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();
//$deptid=  $s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'mst_dpt_heirarchy');
///$LowerDeptList=GETLowerDeptUser($_SESSION['UserID'],$deptid,$_SESSION['ACGCompanyId']);
//$lowerlist=  UserDeptList($LowerDeptList);
 if(count($lowerlist)>0)
$lowerlist=implode(",",$lowerlist).",".$_SESSION['UserID'];
else
$lowerlist="'".$_SESSION['UserID']."'";


$timezone = new DateTimeZone("Asia/Kolkata" );
$date = new DateTime();
$date->setTimezone($timezone);
$current_time=$date->format('H:i:s');


if($_REQUEST['action']=='AddRem' || $_REQUEST['action']=='SHRem' && $_REQUEST['id']!='')
{ 
	$sql=DB_query("Select * from tsk_taskassignment where id='".$_REQUEST['id']."'",$db);
	$myrow=DB_fetch_array($sql);
	extract($myrow);
}
else
{
	$start_date=date('Y/m/d');
	$end_date=date('Y/m/d');
}

if($_REQUEST['action']=='AddRem' && $_REQUEST['Remarks']!='' && $_REQUEST['add_reply']!='')
{
  
       /* $data['task_id']=$_REQUEST['id'];
	$data['remarks']=$_REQUEST['Remarks'];
	$data['remarks_by']=$_SESSION['UserID'];
	$data['remarks_date']=date('Y-m-d');
	$data['remarks_time']=$current_time;
	
	$sql2=$s->insertRecord('tsk_taskassignment_remarks', $data);
	$result2=DB_query($sql2,$db);
       $remark_count=DB_query("select * from tsk_taskassignment_remarks where task_id='".$_REQUEST['id']."' ",$db);
        if(DB_num_rows($remark_count)==1)
        {
            $close_task=DB_query("Update tsk_taskassignment set status='progress' where id='".$_REQUEST['id']."'",$db);
        }*/
if($_REQUEST['Close']!='' && count($_REQUEST['close'])>0)
{
            
        foreach($_REQUEST['close'] as $k=>$v)
        {    
         $close_task=DB_query("Update tsk_taskassignment_details set task_status='1' where id='".$v."'",$db);
        }
       $chld=DB_query("select* from tsk_taskassignment_details where task_id='".$_REQUEST['id']."' ",$db);
       $chld_app=DB_query("select* from tsk_taskassignment_details where task_id='".$_REQUEST['id']."' and task_status='1' ",$db);
       if(DB_num_rows($chld)==DB_num_rows($chld_app))
       {
        $close_task=DB_query("Update tsk_taskassignment set status='closed',closing_time='".date('Y-m-d H:i:s')."' where id='".$_REQUEST['id']."'",$db);
       }
echo "<script>location.href='index.php?Application=emp&modid=15'</script>";
}
	          
}


?>
<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>Task Assignment</h1>
<form action="" method="post">
<table width="100%" border="1" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata1">Branch</td>
    <td><select  style="width:150px;">
           
            <?php
	$Sitesql="select * from locations where  CompanyId='".$_SESSION['ACGCompanyId']."' and loccode='".$branch."' ";
	$SiteRes=DB_query($Sitesql,$db);
	while($SiteRow=DB_fetch_array($SiteRes))
	{?>
            <option value="<?php  echo $SiteRow['loccode'];?>" <?php  echo ($branch==$SiteRow['loccode']?'selected':'');?> >
            <?php  echo $SiteRow['locationname'];?>
            </option>
            <?php 
	}
	?>
          </select></td>
	<td class="tabledata1">Task No. </td>
	<td><input type="text" name="task_no" style="width:150px;" value="<?php echo $task_no ?>" readonly="true"></td>
</tr>
<tr class="row1">
          <td class="tabledata1">Starts</td>
          <td><input type="text" name="start_date" readonly style="width:100px;" class="" value="<?php echo ConvertSQLDate($start_date);?>">
            <input type="text" name="start_time" readonly style="width:45px;" value="<?php echo $start_time; ?>" ></td>
		<td class="tabledata1">Ends</td>
		<td><input type="text" name="end_date" style="width:100px;" class="" readonly value="<?php echo ConvertSQLDate($end_date);?>">
			<input type="text" name="end_time" style="width:45px;" readonly value="<?php echo $end_time; ?>"  id=""></td>
        </tr>
<tr class="row1">
	<td class="tabledata1">Repeat</td>
	<td><select  style="width:150px;">
			<option value="<?php echo $repeat_status ?>"><?php if($repeat_status=='N')echo 'No';else echo 'Yes'; ?></option>
			  </select></td>
			  <td class="tabledata1">Reminder</td>
          <td>
		  <?php if($reminder_status=='Y'){$ch="checked";}else{$ch='';}?>
		  <input type="checkbox" name="" disabled id="reminder_status" value="" onClick="activateTime()" <?php echo $ch;?>>
		  <input type="hidden" name="reminder_status" value="<?php if($_REQUEST['action']=='AddRem') {echo $reminder_status;} else {echo "N";} ?>" id="reminder" />
          <input type="text" name="reminder_time" id="reminder_time" style="width:45px;" disabled="disabled" value="<?php echo $reminder_time;?>">
          <select name="time_mode" style="width:100px;" disabled="disabled" id="time_mode">
		  <option value="H" <?php if($time_mode=='H') echo "selected";?>>Hours</option>
		  <option value="M" <?php if($time_mode=='M') echo "selected";?>>Minutes</option>
          </select>          </td>
</tr>
<tr class="row1">
	<td class="tabledata1" colspan="2">Assign To </td>
		  <?php 
		  if($_REQUEST['action']=='AddRem'){if($assign_to=='user'){$user_ch="checked"; $team_ch=''; $user_disable="";}else{$user_ch=""; $team_ch='checked'; $user_disable="disabled";}}else{$user_ch="checked"; $team_ch=''; $user_disable="";}
		  ?>
          <td style="width:250px;" colspan="2"><input name="assign_to" type="radio" value="user" onClick="checkAssignTo(this.value)" <?php echo $user_ch; ?>> 
            User 
           <!--   <input name="assign_to" type="radio" value="team" onClick="checkAssignTo(this.value)" <?php echo $team_ch; ?>> 
          Team </td>-->
        </tr>
<tr class="row1">
          <td class="tabledata1" colspan="2">User</td>
          <td colspan="2">
            <select name="" id=""  style="width:150px;">
		  <?php 
		  $users=DB_query("Select * from www_users where companyid='".$_SESSION['ACGCompanyId']."' and id='".$user_id."' ",$db);
		  while($users_row=DB_fetch_array($users))
		  {?>
		  	<option value="<?php echo $users_row['id']?>" <?php if($user_id!=''){if($user_id==$users_row['id']) echo "selected";}?>><?php echo $users_row['realname'];?></option>
		  <?php 	
		  }
		  ?>
          </select>
          </td>
</tr>
<tr class="row1">
          <td class="tabledata1" colspan="2">Task Description</td>
          <td colspan="2"><textarea readonly name="" style="width:100%; height:50px;"><?php echo $task_description;?></textarea></td>
</tr>
<tr class="row1">
          <td class="tabledata1" colspan="2">Milestone</td>
          <td colspan="2"><select name="milestone">
              
                <?php $sql=DB_query("select * from tsk_milestone_master where id='".$milestone."'",$db); while($res=DB_fetch_assoc($sql)) {  ?>
                <option value="<?php echo $res['id']; ?>" <?php if($res['id']==$milestone){echo "selected";} ?>><?php echo $res['mile_name']?></option>
                <?php } ?> </select></td>
</tr>
<tr class="row1">
    <td class="tabledata1" colspan="4"><strong>Sub Task Details</strong></td>
</tr>
<tr class="row1">
    <td class="tabledata1"></td>
    <td colspan="3" class="tabledata1">
        <table width="100%">
            
            <tr>
                 <?php if($_REQUEST['Close']=="true") { echo '<th>Select</th>'; } ?>
                <th>Task Description</th>
                <th>Start Date</th>
                <th>End Date</th>
            </tr>
            <?php $det_sql=DB_query("select id,task_desc,DATE(start_time) sd,TIME(start_time) st,DATE(end_time) ed,TIME(end_time) et,task_status  from tsk_taskassignment_details where task_id='".$_REQUEST['id']."'  ",$db); while($det_res=DB_fetch_assoc($det_sql)) { ?>
            <tr>
                <?php if($_REQUEST['Close']=="true") { ?>
                <td>
                   <?php if($det_res['task_status']=='0'){ ?>
                    <input type="checkbox" name="close[]" value="<?php echo $det_res['id'] ?>"></td>
                   <?php } else{ echo "Closed"; }  } ?>
                <td><textarea readonly style="width:99%; height:50px;"><?php echo $det_res['task_desc']; ?></textarea></td>
                <td><?php echo ConvertSQLDate($det_res['sd']).' '.$det_res['st'] ?></td>
                <td><?php echo ConvertSQLDate($det_res['ed']).' '.$det_res['et'] ?></td>
            </tr>
             <?php } ?>
        </table>
    </td>
</tr>
<tr class="row1">
	<td class="tabledata1" colspan="2"><strong>Reply</strong></td>
	<td colspan="3">
		<table width="100%" cellpadding="3" cellspacing="0" id="comm">
		 <?php 
	   $remarks_sql=DB_query("Select * from tsk_taskassignment_remarks where task_id='".$_REQUEST['id']."'",$db);
	   while($remarks_row=DB_fetch_array($remarks_sql)){?>
		<tr class="row1">
		   <td class="tabledata1">
		   <textarea name="" readonly style="width:100%; height:50px;"><?php echo $remarks_row['remarks'];?></textarea>
		   <br />
		   <strong><?php 
		   $username=$s->getField('www_users','id',$remarks_row['remarks_by'],'realname');
		   echo "By ".$username." - ".ConvertSQLDate($remarks_row['remarks_date'])." at ".$remarks_row['remarks_time'];?></strong>
		   </td>
		</tr>
                        <?php } ?>
                
		   <tr height="5px"><td>&nbsp;</td></tr>
                 
		</table>
            <textarea name="Remarks" id="Remarks"  style="width:100%; height:50px;"></textarea>
	</td>
	
</tr> 
	
</table>
<input type="hidden" value="<?php echo $_REQUEST['id'] ?>" id="task_id">
	  
    
    <p align="center">
        <?php if($_REQUEST['Close']=='true'){ ?>
        <input type="submit" name="add_reply" value="Close" onclick="javascript:return validate()" class="search" /> <?php } else { ?>
        <input type="button" name="add_reply" id="add_reply" value="Add Reply" onclick="javascript:return validate()" class="search" /> <?php } ?> <a href="index.php?Application=emp&modid=<?php echo $_REQUEST['modid'] ?>"><input type="button"  value="Cancel" class="search" /></a>
</p>	  
    </form>
  </div>
</div>
<script type="text/javascript" language="javascript">
$('#timepicker1,#timepicker2').timepicker();

function checkAssignTo(str)
{
	if(str=='user')
	{document.getElementById("user_id").disabled=false;}
	else{document.getElementById("user_id").disabled=true;}
}
function activateTime()
{
	var stat=document.getElementById("reminder_status").checked;
	if(stat==true){document.getElementById("reminder_time").disabled=false; document.getElementById("time_mode").disabled=false; document.getElementById("reminder").value='Y';}
	else{document.getElementById("reminder_time").disabled=true; document.getElementById("time_mode").disabled=true; document.getElementById("reminder").value='N';}
}
function validate(){
    <?php if($_REQUEST['Close']=='true') { ?>
                        var flag=0;
                   var av=document.getElementsByName("close[]");
                    
                       for (e=0;e<av.length;e++) 
        {
            if(av[e].checked==true)
            {
                flag=1;
                break;
            }
            else
                {
                    flag=0;
                    
                }
        }
        if(flag==0)
            {
                alert("Please check atleast one task");
                return false;
            }
            <?php } ?>
   if($('#Remarks').val()=='')
        {
            
            $('#Remarks').focus()
            alert("Please Enter Task Reply")
            return false;
            
        }
    else{

       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)
		{
                      $("#Remarks").val(null)  
            document.getElementById("comm").innerHTML=xmlhttp.responseText;			
		}
	}
	//var order=document.getElementById("order_id").value;
           
        var remark=$('#Remarks').val();
        var task_id=$('#task_id').val();
	xmlhttp.open("POST","ajax_store_comment.php?remark="+remark+"&task_id="+task_id,true);
	xmlhttp.send();
    }
}
</script>