<?php
include_once('dashboard_desk.php');
require_once('convert_marathi.php');
################################## Displaying All Accepted Letters to Return By Department ##########################
$username = $_SESSION['User_Name'];
?>
<!DOCTYPE html>
<html>
<head>  
	
		<?php convert_marathi();	//display data in marathi
			$rs1=mysql_query("SELECT * FROM tapal_to_dpt_transaction,desk_transaction,incoming_letters_transaction WHERE tapal_to_dpt_transaction.desk_approval='approve' AND tapal_to_dpt_transaction.desk_id=(select desk_id from login WHERE login_id='$username') and desk_transaction.ltr_category='' and desk_transaction.ltr_approval='Approve' AND desk_transaction.dpt_ltr_id=tapal_to_dpt_transaction.dpt_ltr_id AND tapal_to_dpt_transaction.ltr_id=incoming_letters_transaction.ltr_id ") or die("".mysql_error());
			while($row1=mysql_fetch_array($rs1))
			{
				?>
				<script type="text/javascript">
				$(document).ready(function(){
					var ltr="<?php echo $row1['desk_ltr_id'];?>";
					$('#dptlist'+ltr).fadeOut('fast');
					$('#otherdesk'+ltr).change(function(){
						if(this.checked)
							{
								$('#dptlist'+ltr).fadeIn('slow');
							}
						else
							{
								$('#dptlist'+ltr).fadeIn('slow');
							}
					});
					$('#owndpt'+ltr).change(function(){
						if(this.checked)
							{
								$('#dptlist'+ltr).fadeOut('slow');
							}
						else
							{
								$('#dptlist'+ltr).fadeOut('slow');
							}
					});

				})
				</script>
	<?php 	} ?>  
</head>
    <div class="rightpanel">
        <ul class="breadcrumbs">
            <li><a href="dashboard_desk.php"><i class="iconfa-home"></i></a> <span class="separator"></span></li>
            <li><a href="dashboard_desk.php">Tables</a> <span class="separator"></span></li>
            <li>स्वीकृत पत्र परत करा </li>
        </ul>
        <div class="pageheader">
            <div class="pageicon"><span class="iconfa-table"></span></div>
            <div class="pagetitle">
                <h5>Tables</h5>
                <h1>स्वीकृत पत्र परत करा </h1>
            </div>
        </div><!--pageheader-->
        <div class="maincontent">
            <div class="maincontentinner">
			 	<?php		convert_marathi();
							$qrye=("SELECT * FROM tapal_to_dpt_transaction,desk_transaction,incoming_letters_transaction WHERE tapal_to_dpt_transaction.desk_approval='approve' AND tapal_to_dpt_transaction.desk_id=(select desk_id from login WHERE login_id='$username') and desk_transaction.ltr_category='' and desk_transaction.dpt_ltr_id=tapal_to_dpt_transaction.dpt_ltr_id AND tapal_to_dpt_transaction.ltr_id=incoming_letters_transaction.ltr_id ");
							$rsult=mysql_query($qrye) or die("".mysql_error());
							while($rw=mysql_fetch_array($rsult)) 
							{  
								$dptltrid=$rw['dpt_ltr_id'];
								$dt=$rw['sent_dt'];
								$deskltrid=$rw['desk_ltr_id'];
								$dt=$rw['sent_to_desk_dt'];
								$deskid=$rw['desk_id'];
								if(isset($_POST['allocate_status' . $rw['desk_ltr_id']]))
									{
										$selectedoption=$_POST['allocate_status' . $rw['desk_ltr_id']];
										$ltrid=$rw['ltr_id'];
										$ltrinwardno=$rw['ltr_inword_no'];
										$sentdate=$rw['sent_to_desk_dt'];	
										$rtntodsk = $_POST['desknm' . $rw['desk_ltr_id']];
										//echo $rtntodsk."--".$selectedoption;
										$sndt = date('Y-m-d', strtotime($sentdate));
										$us=$rw['dept_id'];
										$dptin=$rw['dpt_inword_no'];
										########################################################################
																	// If Returning To Own Dept
										######################################################################
										if($selectedoption=='owndpt')
											{
												convert_marathi();
												$qry1=mysql_query("INSERT INTO returned_from_desk(ltr_inword_no, dpt_inword_no, rec_dt, returned_dt, rtn_choice, returned_from, rtn_to_dsk) VALUES('$ltrinwardno', '$dptin', '$sndt', curdate(), '$selectedoption', '$deskid', '$rtntodsk')") or die("1111111".mysql_error());
												
												$qry2=mysql_query("UPDATE tapal_to_dpt_transaction SET desk_id='$rtntodsk', status='pending', desk_ltr_inword_no='0', desk_approval='returned' WHERE dpt_ltr_id='$dptltrid'") or die("22222222".mysql_error());	
												
												$qry23=mysql_query("UPDATE desk_transaction SET ltr_status='closed' WHERE desk_ltr_id='$deskltrid'");		
											}
										########################################################################
																	// If Returning To Other Desk
										######################################################################
										else if($selectedoption=='otherdesk')
											{	
												if($rtntodsk!='')
												{
													convert_marathi();
													$qry1=mysql_query("INSERT INTO returned_from_desk(ltr_inword_no, dpt_inword_no, rec_dt,returned_dt, rtn_choice, returned_from, rtn_to_dsk) VALUES('$ltrinwardno','$dptin','$sndt',curdate(),'$selectedoption','$deskid','$rtntodsk')") or die("333333".mysql_error());		
												
													$qry3=("UPDATE tapal_to_dpt_transaction SET desk_id='$rtntodsk', desk_approval='pending', desk_ltr_inword_no='0' WHERE dpt_ltr_id='$dptltrid'");	
													mysql_query(($qry3)) or die("bbb".mysql_error());
												
													$qry23=mysql_query("UPDATE desk_transaction SET desk_transaction.ltr_status='closed' WHERE desk_ltr_id='$deskltrid'");
												}
												else
												{
													echo "<script type='text/javascript'>alert('कृपया पत्र परत करण्यासाठीची माहिती भरा!..')</script>";
													echo '<script language="JavaScript"> window.location.href ="desk_accepted_rtn_table.php" </script>';
												}
											}
									}
							}	?>
                <h4 class="widgettitle">पत्र स्वीकार करा </h4>
                <form action="desk_accepted_rtn_table.php" method="post">
					<table id="dyntable" class="table table-bordered">
						<thead>
							<tr>
								<th class="head0 center">आवक क्र. <br> आवक दिं.</th>
								<th class="head1 center">संदर्भ क्र. <br>संदर्भ दिं.</th>
								<th class="head0 center">विभागीय आवक क्र.<br>विभागीय प्राप्त दिं.</th>
								<th class="head1 center">डेस्कला <br> प्राप्त दि. </th>
								<th class="head0 center">कोणाकडून <br>प्राप्त झाले  </th>
								<th class="head1 center">थोडक्यात <br>विषय</th>
								<th class="head0 center">संदर्भाचा प्रकार</th>
								<th class="head0 center">परत करा</th>
							</tr>
						</thead>
						<tbody>
							<?php 
							convert_marathi();
							$qry1=("SELECT * FROM tapal_to_dpt_transaction, desk_transaction, incoming_letters_transaction WHERE tapal_to_dpt_transaction.desk_approval='approve' AND tapal_to_dpt_transaction.desk_id=(select desk_id from login WHERE login_id='$username') and desk_transaction.ltr_category='' and desk_transaction.ltr_approval='Approve' AND desk_transaction.ltr_status!='closed' AND desk_transaction.dpt_ltr_id=tapal_to_dpt_transaction.dpt_ltr_id AND tapal_to_dpt_transaction.ltr_id=incoming_letters_transaction.ltr_id ");
							$rs1=mysql_query($qry1)or die ("aaa".mysql_error());
							while($rw=mysql_fetch_array($rs1))
							{	
								$recdate = strtotime($rw['receive_dt']);
								$rec= date('d-m-Y', $recdate);
								$refdate = strtotime($rw['ref_dt']);
								$ref= date('d-m-Y', $refdate);
								//sent_to_desk_dt
								$sentdate = strtotime($rw['sent_to_desk_dt']);
								$sdt= date('d-m-Y', $sentdate);
								//sent_dt
								$sentdt = strtotime($rw['sent_dt']);
								$sndt= date('d-m-Y', $sentdt);
								$id=$rw['ltr_inword_no'];
								$deptid=$rw['dept_id'];
								?>
								<tr class="gradeX unicode" lang="mr">
								<td class="center"><?php echo$rw['ltr_inword_no'];?><br><?php echo $rec;?></td>
								<td class="center"><?php echo $rw['ref_no'];?><br><?php echo $ref;?></td>
								<td class="center"><?php echo $rw['dpt_inword_no'];?><br><?php echo $sndt;?></td>
								<td class="center"><?php echo $sdt;?></td>
								<td class="center" id="wrapping"><?php echo $rw['sender_name'];?></td>
								<td class="center" id="wrapping"><?php echo $rw['subject'];?></td>
								<?php
								convert_marathi();
								$qry5=("Select * from sandharbh_master,incoming_letters_transaction WHERE sandharbh_master.sandhrbh_id=incoming_letters_transaction.ltr_type AND incoming_letters_transaction.ltr_inword_no='$id'");
								$rs5=mysql_query($qry5) or die ("mmmm". mysql_error());
								while($rw5=mysql_fetch_array($rs5))
									{
										$ltr=$rw5['sandharbh_nm'];
									}
								?>
								<td class="center"><?php echo $ltr; ?></td>
								<td class="center">
								<?php  
									echo "
									<label style='width:100px;'><input type='radio' name='allocate_status". $rw['desk_ltr_id']."' value='owndpt' id='owndpt". $rw['desk_ltr_id']."'>स्व-विभागास </label>
									<label style='width:100px;'><input type='radio' name='allocate_status". $rw['desk_ltr_id']."'  value='otherdesk' id='otherdesk". $rw['desk_ltr_id']."'> इतर डेस्कला </label>";							
									echo "<br><div id='dptlist". $rw['desk_ltr_id']."'><select name='desknm".$rw['desk_ltr_id']."'>Dropdown</option><option value=>डेस्क निवडा </option>";
									$qr=mysql_query("Select * from desk_master WHERE desk_id!=(select desk_id From login WHERE login_id='$username') AND dept_id='$deptid'");
										while($row = mysql_fetch_array($qr))
											{
												echo"	
												{
												('<option id='".$row['desk_id']."' value='".$row['desk_id']."'>".$row['desk_nm']."</option>')
												}";
											}
                                echo" </select></div></td></tr>";
							}?>
						</tbody>
					</table>
					<br>
				<p class="stdformbutton">
                    <center> <button class="btn btn-primary">बदल करा </button>
					<button type="reset" class="btn">रद्द करा </button></center>
                </p>
                <br /><br />
			</form> 
            <?php   
            include_once('footer.php');
            ?>
        </div><!--maincontentinner-->
    </div><!--maincontent-->   
</div><!--rightpanel-->  
</div><!--mainwrapper-->
</body>
</html>