<?php
//**************************FUNCTION FOR GET UNDER USERS********************START HERE******************************
include('functions.php');
$s=new db_query();
global $db;
$deptid=  $s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'mst_dpt_heirarchy');		 
$LowerDeptList=GETLowerDeptUser($_SESSION['UserID'],$deptid,$_SESSION['ACGCompanyId']);
$lowerlist=  UserDeptList($LowerDeptList);
$lowerlist[]=$_SESSION['UserID'];

if(count($lowerlist)>0)
$lowerlist=implode(",",$lowerlist) ;
else
$lowerlist="'".$_SESSION['UserID']."'";

$user_ids1	=$lowerlist;

$id1=array();



//echo $user_ids;

$user_id = $_SESSION['ACG_DashBoard_User'];


//**************************FUNCTION FOR GET UNDER USERS********************END HERE******************************





//*************************INSERT THE DATA*************************START HERE************************************

if($_REQUEST['Save'])
{
	$data['curr_date']=FormatDateForSQL($_REQUEST['curr_date']);
	$data['todo_date']=FormatDateForSQL($_REQUEST['todo_date']);
	$data['todostart_time']=$_REQUEST['hours'].":".$_REQUEST['mins'].":".$_REQUEST['AMPM'];
	$data['subject']=$_REQUEST['subject'];
	$data['description']=$_REQUEST['description'];
	$data['priority']=$_REQUEST['priority'];
	$data['client_id']=$_REQUEST['client_id'];
	$data['status']=$_REQUEST['status'];
	$data['createdby']=$_SESSION['UserID'];
	$s->RecordInsert('crm_add_to_do', $data);// INSERT THE DATA
	$s->pageLocation("index.php?route=CRM/CrmfieldvisitreportTODO".$rurl);
}

//*************************INSERT THE DATA*************************END HERE************************************




//*************************UPDATE THE DATA*************************START HERE************************************


if($_REQUEST['action']=='Edit')
{
	$condition="id='".$_REQUEST['todoid']."'";
	$rec=$s->GetRecordById('crm_add_to_do', $condition);
	$recRow=DB_fetch_array($rec);
	$todostart_time=explode(":",$recRow['todostart_time']);
	$hours=$todostart_time[0];
	$mins=$todostart_time[1];
	$AMPM=$todostart_time[2];
	extract($recRow);//EXTRACT DATA
}

if($_REQUEST['Update'] && $_REQUEST['action']=='Edit')
{
	$dataArray['curr_date']=FormatDateForSQL($_REQUEST['curr_date']);
	$dataArray['todo_date']=FormatDateForSQL($_REQUEST['todo_date']);
	$dataArray['todostart_time']=$_REQUEST['hours'].":".$_REQUEST['mins']." ".$_REQUEST['AMPM'];
	$dataArray['subject']=$_REQUEST['subject'];
	$dataArray['description']=$_REQUEST['description'];
	$dataArray['priority']=$_REQUEST['priority'];
	$dataArray['client_id']=$_REQUEST['client_id'];
	$dataArray['status']=$_REQUEST['status'];
	$dataArray['createdby']=$_SESSION['UserID'];
	$cond="id='".$_REQUEST['todoid']."'"; // CONDITION
	$s->RecordEdit('crm_add_to_do', $dataArray, $cond);// UPDATE THE DATA
	$s->pageLocation("index.php?route=CRM/CrmfieldvisitreportTODO".$rurl);
}

//*************************UPDATE THE DATA*************************END HERE************************************



//*************************VIEW THE DATA*************************START HERE************************************

if($_REQUEST['action']=='View')
{
	$condition="id='".$_REQUEST['todoid']."'";
	$rec=$s->GetRecordById('crm_add_to_do', $condition);
	$recRow=DB_fetch_array($rec);
	extract($recRow);//EXTRACT DATA
}

//*************************VIEW THE DATA*************************END HERE************************************




//*************************VIEW THE DATA*************************START HERE************************************

if($_REQUEST['Delete'])
{
	for($i=0;$i<count($_REQUEST['todo_id']);$i++)
	{
	$cond="id='".$_REQUEST['todo_id'][$i]."'";
	$row= $s->DeleteRecord('crm_add_to_do', $cond);
	}
	$s->pageLocation("index.php?route=CRM/CrmfieldvisitreportTODO".$rurl);
}
//*************************VIEW THE DATA*************************END HERE************************************



$currdate=date('d/m/Y');// CURRENT DATE

?>

<!-------------------------JS FILES------------------------------------START HERE-------------------------->

<script type="text/javascript" language="javascript">
function LeadData()
{	
	myCheckboxArray = document.myform.elements["todo_id[]"];
	var t=myCheckboxArray.length;
	if(document.getElementById('todo_id').checked==true)
	{	
	if(t==undefined)
	document.getElementById('todo_id_'+1).checked=true;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('todo_id_'+i).checked=true;	
	}	
	}
	else
	{
	if(t==undefined)		
	document.getElementById('todo_id_'+1).checked=false;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('todo_id_'+i).checked=false;	
	}
	}
}
</script>

<!-------------------------JS FILES------------------------------------END HERE-------------------------->


<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Task Information</h1>
		
<!--------------------------------IF ACTION IS VIEW---------------------------START HERE------------------------------>
	
<?php if($_REQUEST['action']=='View') {?>
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata1">Current Date</td>
	<td><?php echo $curr_date; ?></td>
	<td class="tabledata1">To Do Date</td>
	<td><?php echo $todo_date; ?></td>
	<td class="tabledata1">Time</td>
	<td><?php echo $todostart_time;?></td>
</tr>
<tr class="row1">
	<td class="tabledata1">Subject<label><font color="#FF0000">*</font></label></td>
	<td colspan="5"><?php echo $subject?></td>
</tr>
<tr  class="row1">
	<td class="tabledata1">Description</th>
	<td colspan="5"><?php echo $description?></td>
</tr>
<tr class="row1">
	<td class="tabledata1">Priority <label><font color="#FF0000">*</font></label></td>
	<td><?php echo $priority?></td>
	<td class="tabledata1">Link With<label><font color="#FF0000">*</font></label></td>
	<td><?php echo $s->GetFieldValue('clientmaster',"id='".$client_id."'",'c_name');?></td>
	<td class="tabledata1">Status<label><font color="#FF0000">*</font></label></td>
	<td><?php echo $status?></td>
</tr>
</table>
<?php } else { ?>
		
<!--------------------------------IF ACTION IS VIEW---------------------------END HERE-------------------------------->


<!--------------------------------IF ACTION IS ADD/EDIT---------------------------START HERE--------------------------->
		
<form name="form1" method="post" action="">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata1">Current Date</td>
	<td><input type="text" name="curr_date" id="curr_date" readonly="" value="<?php if($_REQUEST['action']!=''){echo ConvertSQLDate($curr_date);}else{echo $currdate;} ?>" class="datepicker" /></td>
	<td class="tabledata1" style="white-space:nowrap;">To Do Date</td>
	<td><input type="text" name="todo_date" id="todo_date" value="<?php if($_REQUEST['action']!=''){echo ConvertSQLDate($todo_date);}else{echo $currdate;} ?>" class="datepicker" /></td>
	<td class="tabledata1">Time</td>
	<td><select name="hours" style="width:40px;">
<?php for($i=1;$i<=12;$i++){
		$p='';
		if($i==$hours)
		$p='selected';
		echo "<option value='".$i."' $p>".$i."</option>";
		}?>
		  </select></td>
	<td><select name="mins" style="width:40px;">
<?php for($i=05;$i<=60;$i+=05){ 
		$p='';
		if($i==$mins)
		$p='selected';
		echo "<option value='".$i."'  $p>".$i."</option>";
		}?>
		  </select></td>
	<td><select name="AMPM" style="width:40px;">
			 <option  value="AM"<?php  echo ($AMPM=='AM'?'selected':'')?>>AM</option>
			 <option  value="PM"<?php  echo ($AMPM=='PM'?'selected':'')?>>PM</option>			 
		  </select></td>
</tr>
<tr class="row1">
	<td class="tabledata1">Subject<label><font color="#FF0000">*</font></label></td>
	<td colspan="7"><input type="text" name="subject" value="<?php echo $subject?>" style="width:425px;"></td>
</tr>
<tr class="row1">
	<td class="tabledata1">Description</td>
	<td  colspan="7"><textarea name="description" cols="50" rows="5"><?php echo $description?> </textarea> </td>
</tr>
<tr class="row1">
	<td class="tabledata1">Priority <label><font color="#FF0000">*</font></label></td>
	<td><select style="width:150px;" name="priority">
					<option value="0">--Select--</option>
					<option value="High" <?php  echo ($priority=='High'?'selected':'')?>>High</option>
					<option value="Low" <?php  echo ($priority=='Low'?'selected':'')?>>Low</option>
					<option value="Medium" <?php  echo ($priority=='Medium'?'selected':'')?>>Medium</option>
		</select></td>
	<td class="tabledata1">Link With<label><font color="#FF0000">*</font></label></td>
	<td><select name="client_id" id="">
			<option value="0">Select Client</option>
<?php $ClientSQL="Select * from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."' and  Site in('".$_SESSION['AssignedBranch']."') 
		and Zone in(".$_SESSION['AssignedSites'].")";
$ClientRes=DB_query($ClientSQL,$db);
while($ClientRow=DB_fetch_array($ClientRes)){
$cel='';
if($client_id==$ClientRow['id'])
$cel='selected';?>
		<option value="<?php echo $ClientRow['id']?>" <?php echo $cel;?>><?php echo $ClientRow['c_name']?></option>
<?php  } ?>		</select>
	</td>
	<td class="tabledata1">Status<label><font color="#FF0000">*</font></label></td>
	<td>
		<select name="status" id="status">
			<option value="pending" <?php echo($status=='pending'?'selected':''); ?>>Pending</option>
			<option value="completed"<?php echo($status=='completed'?'selected':'');?>>Completed</option>
		</select>
	</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
</tr>
<tr class="row1">
	<td class="8"><input type="submit" name="<?php if($_REQUEST['action']=='Edit'){echo 'Update';}else{echo 'Save';} ?>" value="<?php if($_REQUEST['action']=='Edit'){echo 'Update';}else{echo 'Save';} ?>" class="search">
	</td>
</tr>
</table>
</form>

<!--------------------------------IF ACTION IS ADD/EDIT---------------------------END HERE------------------------------>

<form action="" method="post" name="myform" >
<p align="left">
<input type="submit" name="Delete" value="Delete" class="search" />
</p>
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata"><input name="todo_id" type="checkbox"   id="todo_id" onclick="LeadData()"    />  </td>
	<td class="tabledata">To Do Date</td>
	<td class="tabledata">Subject</td>
	<td class="tabledata">Description</td>
	<td class="tabledata">Client</td>
	<td class="tabledata">Priority</td>
	<td class="tabledata">Time</td>
	<td class="tabledata">Status</td>
	<td class="tabledata">User</td>
</tr>
<?php $i=1;$getlead=DB_query( "select * from crm_add_to_do where createdby in($lowerlist)",$db);
while($todo_id=DB_fetch_array($getlead)){?>
<tr class="row1">
	<td>
	<input name="todo_id[]" type="checkbox" id="todo_id_<?php echo $i; ?>"   value="<?php  echo $todo_id['id']?>" />
	<a href="index.php?route=CRM/CrmfieldvisitreportTODO&action=Edit&todoid=<?php echo $todo_id['id'].$rurl;?>">
	<img src="images/icon_edit.gif" width="10px"  height="10" /></a>
	<a href="index.php?route=CRM/CrmfieldvisitreportTODO&action=View&todoid=<?php echo $todo_id['id'].$rurl;?>">
	<img src="images/search.gif" width="10px"  height="10" /></a>
	</td>
	<td> <?php echo ConvertSQLDate($todo_id['todo_date']);?></td>
	<td> <?php  echo $todo_id['subject'];?></td>	
	<td><?php  echo $todo_id['description'];?></td>
	<td><?php echo $s->GetFieldValue('clientmaster',"id='".$todo_id['client_id']."'",'c_name');?></td>
	<td><?php  echo $todo_id['priority'];?></td>
	<td><?php  echo $todo_id['todostart_time'];?></td>
	<td><?php  echo $todo_id['status'];?></td>
	<td><?php 
	$user=explode(',',$todo_id['createdby']);
	$User1=implode("','",$user);
	$Username="select * from www_users where id IN('".$User1."')";
	$UserSql=DB_query($Username,$db);
	while($UserRow=DB_fetch_array($UserSql))
	{
	echo $UserRow['userid']," ,";
	} 
	?></td>
</tr>
<?php $i++; } ?>
</table>		
</form>
<?php } ?>
</div></div>