<? if($_SESSION['page'] == '2' and $_SESSION['section'] == '3') { ?>
<link href="../admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
			window.addEvent('domready', function() { 
				new vlaDatePicker('From', { style: 'adobe_cs3',separator: '-', format: 'y/m/d'});
				new vlaDatePicker('To', { style: 'adobe_cs3',separator: '-', format: 'y/m/d'});
			});	
		</script>
        
        
        
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
    <table width="95%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#F7D782">
    <tr>
      <td>
        <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#EFB412">
          <tr>
            <td class="Main_Text">Plates Damage Report</td>
            <td width="10%" class="Main_Text"><div align="right"><img src="image/more_info.gif" width="32" height="32" border="0" /></a></div></td>
          </tr>
          <tr>
            <td colspan="2">
              <? if ($_GET['stat'] == '' ) {  ?>

              <form action="index.php?stat=show" method="post" name="form1" id="form1">
                <br />
                <table width="95%" border="0" align="center">
                <tr>
                  <td width="15%" class="Main_Text1_case">Date From</td>
                  <td class="text_row1"><div align="center"><span id="sprytextfield1">
                    <input name="From" type="text" id="From" />
                    <span class="textfieldInvalidFormatMsg">*</span>
                    <span class="textfieldRequiredMsg">*</span>
                  </span></div></td>
                  <td width="15%" class="Main_Text1_case">Date To</td>
                  <td class="text_row1"><div align="center"><span id="sprytextfield2">
                    <input type="text" name="To" id="To" />
                  <span class="textfieldRequiredMsg">*</span><span class="textfieldInvalidFormatMsg">*</span></span></div></td>
                  <td width="150" rowspan="3"><div align="center">
                    <input name="Submit" type="submit" class="Button1" id="Submit" value="   Create   " />
                  </div></td>
                </tr>
                <tr>
                  <td class="Main_Text1_case">Time From</td>
                  <td class="text_row1"><div align="center"><span id="sprytextfield3">
                    <input name="time_from" type="text" id="time_from" value="00:00" />
                  <span class="textfieldRequiredMsg">*</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></div></td>
                  <td class="Main_Text1_case">Time To</td>
                  <td class="text_row1"><div align="center"><span id="sprytextfield4">
                    <input name="time_to" type="text" id="time_to" value="23:59" />
                  <span class="textfieldRequiredMsg">*</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></div></td>
                  </tr>
                <tr>
                  <td class="Main_Text1_case">Detailed</td>
                  <td class="text_row1"><div align="center">
                    <input name="detailed" type="checkbox" id="detailed" value="1" />
                  </div></td>
                  <td class="Main_Text1_case">Mode</td>
                  <td class="text_row1"><input name="mode_print" type="checkbox" id="mode_print" value="1" />
                    Print</td>
                </tr>
              </table>
            
                <br />
              </form>
           
           
    <? } if ($_GET['stat'] == 'show' ) { 
								$From = $_POST["From"];
						  		$To = $_POST["To"];
						  		$time_from = $_POST["time_from"];
						  		$time_to = $_POST["time_to"];
		?>
                                      <br />
                        <table width="60%" border="0" align="center">
                          <tr>
                            <td class="Main_Text1">Date</td>
                            <td class="text_row1"><div align="center">From </div></td>
                            <td class="text_box_textarea"><div align="center"><? echo $From;?></div></td>
                            <td class="text_row1"><div align="center">to</div></td>
                            <td class="text_box_textarea"><div align="center"><? echo $To;?></div></td>
                          </tr>
                          <tr>
                            <td class="Main_Text1">Time</td>
                            <td class="text_row1"><div align="center">From </div></td>
                            <td class="text_box_textarea"><div align="center"><? echo $time_from;?></div></td>
                            <td class="text_row1"> <div align="center">to</div></td>
                            <td class="text_box_textarea"><div align="center"><? echo $time_to;?></div></td>
                          </tr>
                        </table>
    <br />
              <?
								
								
				// list of staff
					 	$number_of_staff = 0 ;
						$result1 = mysql_query('select * from p1_staff'); 
                        while($row1 = mysql_fetch_array($result1)){ 
								$staff_name[$row1['sid']] = $row1['staff'];    
								$number_of_staff = $number_of_staff + 1;
					  	}
						
			  	// Select all Date 
					  
						$start_date = $_POST["From"];
						$check_date =date ("Y-m-d", strtotime ("-1 day", strtotime($start_date))); 
						$end_date = $_POST["To"];
				//	all data staff
								for ($i=1;$i<=$number_of_staff;$i++){
									$staff_count_all[$i] = 0; 
								}
								$staff_total_all = 0;
							
			// Start Detailed
			  
				 if ($_POST['detailed'] <> '') {
				 
						$i = 0; 
						while ($check_date != $end_date) { 
							$check_date = date ("Y-m-d", strtotime ("+1 day", strtotime($check_date))); 
							$first_row = 0;
				// ****** Calc staff  ***** //
							
							
							$plate_type = mysql_query("select sum(plate_no_s+plate_no_l) num_plate , sid from p1_damage where create_date = '".$check_date."' and create_time between '".$time_from."' and '".$time_to."' and delete_uid = 0 group by sid");
							
							
							//	row data number_of_staff
								for ($i=1;$i<=$number_of_staff;$i++){
									$staff_count_row[$i] = 0; 
									
								}
								$staff_total_row = 0;
							// 
								while($row_onsite = mysql_fetch_array($plate_type)){ 
									$staff_count_row[$row_onsite['sid']] =  $row_onsite['num_plate'];
									$staff_total_row = $staff_total_row+$row_onsite['num_plate'];
									$staff_count_all[$row_onsite['sid']] =$staff_count_all[$row_onsite['sid']]+$row_onsite['num_plate'];
								}
							$staff_total_all = $staff_total_all+$staff_total_row;
							
							if ( $staff_total_row <> 0  ) { 
				 	?>
                    	<br />

                    
                        <table width="60%" border="0" align="center">
                          <tr>
                            <td width="20%" class="Main_Text1_case"><div align="center">Date</div></td>
                            <td class="Main_Text1_case"><div align="center">Staff</div></td>
                            <td width="20%" bordercolor="#000000" class="Main_Text1_case"><div align="center">Number of Plate</div></td>
                          </tr>
                        <?  $g=0;
						
							for ($i=1;$i<=$number_of_staff;$i++){
							
								if ($staff_count_row[$i] <> 0 ) { 
									
										?>
									  <tr>
									  <? if ($g == 0 ) { ?>
										<td rowspan="<? echo $number_of_staff+1;?>" class="Main_SText"><div align="center"><? echo $check_date?></div></td>
									  <? } ?>
										<td class="Main_SText_case"><div align="center"><? echo $staff_name[$i] ?></div></td>
										<td class="Main_SText_case"><div align="center"><? echo $staff_count_row[$i];?></div></td>
									  </tr>
									 <?
									$g = 1;
						 		}
						 
						 	 } ?>
                          <tr>
                            <td class="Main_color1"><div align="center">TOTAL</div></td>
                            <td class="Main_color1"><div align="center"><? echo $staff_total_row;?></div></td>
                          </tr>
                        </table>
<?
						// stop  show
							}
						// Stop loop	
						}
						?>
                        
                      <p align="center">&nbsp;</p>
                    
                        <table width="60%" border="0" align="center">
                          <tr>
                            <td width="20%" class="Main_Text1_case"><div align="center">Date</div></td>
                            <td class="Main_Text1_case"><div align="center">Staff</div></td>
                            <td width="20%" bordercolor="#000000" class="Main_Text1_case"><div align="center">Number of Plate</div></td>
                          </tr>
                           <?  $g=0;
						
							for ($i=1;$i<=$number_of_staff;$i++){
							
								if ($staff_count_all[$i] <> 0 ) { 
									
							?>
						  <tr>
                           <? if ($g == 0 ) { ?>
							<td rowspan="<? echo $number_of_staff+1;?>" class="Main_SText"><div align="center">All Date</div></td>
                           <? } ?>
							<td class="Main_SText_case"><div align="center"><? echo $staff_name[$i] ?></div></td>
							<td class="Main_SText_case"><div align="center"><? echo $staff_count_all[$i];?></div></td>
						  </tr>
                           <?
									$g = 1;
						 		}
						 
						 	 } ?>
                          <tr>
                            <td class="Main_color2"><div align="center">TOTAL</div></td>
                            <td class="Main_color2"><div align="center"><? echo $staff_total_all;?></div></td>
                          </tr>
                        </table>

                        
                        <?
			
		/*------------------------------   else  ------------------------------*/
				 }else { 
				 		$i = 0; 
						while ($check_date != $end_date) { 
							$check_date = date ("Y-m-d", strtotime ("+1 day", strtotime($check_date))); 
							$first_row = 0;
				// ****** Calc staff  ***** //
							
							
							$plate_type = mysql_query("select sum(plate_no_s+plate_no_l) num_plate , sid from p1_damage where create_date = '".$check_date."' and create_time between '".$time_from."' and '".$time_to."' and delete_uid = 0 group by sid");
							
							
							//	row data number_of_staff
								for ($i=1;$i<=$number_of_staff;$i++){
									$staff_count_row[$i] = 0; 
									
								}
								$staff_total_row = 0;
							// 
								while($row_onsite = mysql_fetch_array($plate_type)){ 
									$staff_count_row[$row_onsite['sid']] =  $row_onsite['num_plate'];
									$staff_total_row = $staff_total_row+$row_onsite['num_plate'];
									$staff_count_all[$row_onsite['sid']] =$staff_count_all[$row_onsite['sid']]+$row_onsite['num_plate'];
								}
							$staff_total_all = $staff_total_all+$staff_total_row;
							
						// Stop loop	
						}
						?>
                        
                      <p align="center">&nbsp;</p>
                    
                       <table width="60%" border="0" align="center">
                          <tr>
                            <td width="20%" class="Main_Text1_case"><div align="center">Date</div></td>
                            <td class="Main_Text1_case"><div align="center">Staff</div></td>
                            <td width="20%" bordercolor="#000000" class="Main_Text1_case"><div align="center">Number of Plate</div></td>
                          </tr>
                           <?  $g=0;
						
							for ($i=1;$i<=$number_of_staff;$i++){
							
								if ($staff_count_all[$i] <> 0 ) { 
									
							?>
						  <tr>
                           <? if ($g == 0 ) { ?>
							<td rowspan="<? echo $number_of_staff+1;?>" class="Main_SText"><div align="center">All Date</div></td>
                           <? } ?>
							<td class="Main_SText_case"><div align="center"><? echo $staff_name[$i] ?></div></td>
							<td class="Main_SText_case"><div align="center"><? echo $staff_count_all[$i];?></div></td>
						  </tr>
                           <?
									$g = 1;
						 		}
						 
						 	 } ?>
                          <tr>
                            <td class="Main_color2"><div align="center">TOTAL</div></td>
                            <td class="Main_color2"><div align="center"><? echo $staff_total_all;?></div></td>
                          </tr>
                        </table>

                        
                        <?
				}
		 
     } ?>
            </td>
          </tr>
        </table><br />

<script type="text/javascript">
<!--
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "date", {format:"yyyy-mm-dd"});
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "date", {format:"yyyy-mm-dd"});
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "time");
var sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4", "time");
//-->
</script>
<? } ?>
