<?
// Level 9 이하만 사용 가능
$func->checkLevelModal(9);

// 기능 제한 (Admin : 1, Staff : 5, Accounting : 6, Driver : 9 만 가능)
$permit = array("1", "3", "5", "6", "7", "9");
if (in_array($_SESSION['ss_LEVEL'], $permit)) {
    $setTag = "ENABLED";
}
else {
		$setTag = "DISABLED";	
		$msg = "Sorry, You don't have permission. Please contact Administrator.";
		$func -> modalMsg ($msg, "/index_intranet.php");
		exit();
}

//echo "<br><br><br><br><br><br>[$startYear][$tmp]";


$startYear = str_replace("-", "", trim($startYear));

if ($startYear =="") {
	$startYear = date("Y");
	//$date = new DateTime('PREVIOUS MONTH');
	//$startYear = $date->format('Y');
}



for($ii=2021; $ii<= date("Y"); $ii++) {
	if ($ii == $startYear) $selectedTag = "selected";
	else $selectedTag = "";
	
	$yearSTR .= "<option value='$ii' $selectedTag >$ii</option>";

}


//total record
$query = "SELECT COUNT(m_uid) FROM tbl_member WHERE m_level = 9 AND m_status = 'A' AND m_regionuid != '' ";
$driver_cnt=$jdb->rQuery($query, "record query error");

// Get Driver Info
$qry_driver = "SELECT * FROM tbl_member 
								WHERE m_level = 9 AND m_status = 'A' AND m_regionuid != '' ORDER by m_region ASC ";
$rt_driver = $jdb->nQuery($qry_driver, "list error"); 

$driverArray = array();
$driverCnt = 0;

$weekly_data = array();

$getLastday = date('t', strtotime($startYearMonth."10")); 

//echo "[$startYearMonth][$getLastday]<br>";

// Get Driver with Title
$titleSTR = "<tr>
                <th class='td-80'></th>
                <th class='td-50'></th>";

while($lt_driver=mysqli_fetch_array($rt_driver, MYSQLI_ASSOC)) {
	$driverArrayTmp = array($lt_driver['m_uid'] => $lt_driver['m_region']);	
	$driverArray += $driverArrayTmp;
	
	$titleSTR .= "<th class='td-80' style='text-align:center;'>".$lt_driver['m_region']."</th>";
}	

$titleSTR .= "<th class='td-80' style='text-align:center;'>TOTAL</th><th class='td-80' style='text-align:center;'>%</th>";
$titleSTR .= "</tr>\n";

$monthArray = array("","January","February","March","April","May","June","July","August","September","October","November","December");


$rowYearISCC = 0;
$rowYearCORSIA = 0;


for ($mmonth = 1; $mmonth <= 12; $mmonth ++) {
			
			$rowISCCSTR = "<td style='text-align:center;background-color:#FFF9F9;'>ISCC</td>";
			$rowCORSIASTR = "<td style='text-align:center;background-color:#F4FFF4;'>CORSIA</td>";
			$rowUCOSTR = "<td style='text-align:center;background-color:#FFFFF4;'>TOTAL UCO</td>";

			$rowMonthSTR .= "<tr><td rowspan='3'><strong>".$monthArray[$mmonth]."</strong></td>";

			$startYearMonth = $startYear.sprintf("%02d", $mmonth);

			foreach ($driverArray as $kkey => $vvalue) {						

					/////////////////
					// ISCC Data
					/////////////////
					
					//$qry_eu = "SELECT 
					//							SUM(CASE WHEN d_visitdate like '".$startYearMonth."%' THEN d_quantity ELSE 0 END) AS EUDATA
					//						FROM  tbl_daily 
					//						WHERE d_customeruid != '' AND d_status = 'F' 
					//							AND (d_customeruid IN (SELECT c.c_uid FROM tbl_customer c WHERE c.c_driveruid = '".$kkey."' ". $status_query. "))
					//							AND d_form_eu != '' AND d_form_eu <= d_orderdate ";
					//echo "<br><br><br><br><br>EU[$qry_eu]<br>";			
					//$rt_eu = $jdb->fQuery($qry_eu, "fetch query error");
					//$EUDataSTR .= "<td style='text-align:right;'>".number_format(floatval($rt_eu['EUDATA']))."</td>";


					$qry_sum = "select sum(rd_quantity) AS quantity_uco, sum(rd_quantity_iscc) AS quantity_iscc, sum(rd_quantity_corsia) AS quantity_corsia
											FROM tbl_report_daily WHERE rd_driveruid = '".$kkey."' AND rd_orderdate LIKE '".$startYearMonth."%' ";
					$rt_sum = $jdb->fQuery($qry_sum, "list error");
					//echo "[$qry_sum]<br>";
					$rowISCCSTR .= "<td style='text-align:right;background-color:#FFF9F9;'>".number_format(floatval($rt_sum['quantity_iscc']))."</td>";
					$rowCORSIASTR .= "<td style='text-align:right;background-color:#F4FFF4;'>".number_format(floatval($rt_sum['quantity_corsia']))."</td>";
					$rowUCOSTR .= "<td style='text-align:right;background-color:#FFFFF4;'>".number_format(floatval($rt_sum['quantity_uco']))."</td>";

					$rowSubUCO += floatval($rt_sum['quantity_uco']);
					$rowSubISCC += floatval($rt_sum['quantity_iscc']);
					$rowSubCORSIA += floatval($rt_sum['quantity_corsia']);				
					
					$driverCnt++;	
					
			}
			
			if ($rowSubUCO != 0) {
					$rowPercentISCC = (floatval($rowSubISCC)/($rowSubUCO))*100;
					$rowPercentCORSIA = (floatval($rowSubCORSIA)/($rowSubUCO))*100;
			}
			else {
					$rowPercentISCC = 0;
					$rowPercentCORSIA = 0;
			}

			//echo "[$rowPercentISCC][$rowPercentCORSIA][$rowSub][$rowSubISCC][$rowSubCORSIA]<br>";

			$rowMonthSTR .= $rowISCCSTR."<td style='text-align:right;font-weight:bold;background-color:#FFF9F9;'>".number_format(floatval($rowSubISCC))."</td><td style='text-align:right;'>".number_format(floatval($rowPercentISCC), 2, '.', '')."%</td></tr>\n";
			$rowMonthSTR .= "<tr>".$rowCORSIASTR."<td style='text-align:right;font-weight:bold;background-color:#F4FFF4;'>".number_format(floatval($rowSubCORSIA))."</td><td style='text-align:right;'>".number_format(floatval($rowPercentCORSIA), 2, '.', '')."%</td></tr>\n";
			$rowMonthSTR .= "<tr>".$rowUCOSTR."<td style='text-align:right;font-weight:bold;background-color:#FFFFF4;'>".number_format(floatval($rowSubUCO))."</td><td></td></tr>\n";

			$rowYearUCO += $rowSubUCO;
			$rowYearISCC += $rowSubISCC;
			$rowYearCORSIA += $rowSubCORSIA;

			$rowSubUCO = 0;
			$rowSubISCC = 0;
			$rowSubCORSIA = 0;
}

// Get Year Data

if ($rowYearUCO != 0) {
		$rowPercentYearISCC = (floatval($rowYearISCC)/($rowYearUCO))*100;
		$rowPercentYearCORSIA = (floatval($rowYearCORSIA)/($rowYearUCO))*100;
}
else {
		$rowPercentYearISCC = 0;
		$rowPercentYearCORSIA = 0;
}


?>
	
	
<script type="text/javascript">
$(document).ready(function(){
	
		$(function() {
		     $('.date-picker').datepicker(
		                    {
		                        dateFormat: "yy-mm",
		                        changeMonth: true,
		                        changeYear: true,
		                        showButtonPanel: true,
		                        onClose: function(dateText, inst) {


		                            function isDonePressed(){
		                                return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
		                            }

		                            if (isDonePressed()){
		                                var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
		                                var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
		                                $(this).datepicker('setDate', new Date(year, month, 1)).trigger('change');
		                                
		                                 $('.date-picker').focusout()//Added to remove focus from datepicker input box on selecting date
		                            }
		                        },
		                        beforeShow : function(input, inst) {

		                            inst.dpDiv.addClass('month_year_datepicker')

		                            if ((datestr = $(this).val()).length > 0) {
		                                year = datestr.substring(datestr.length-4, datestr.length);
		                                month = datestr.substring(0, 2);
		                                $(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
		                                $(this).datepicker('setDate', new Date(year, month-1, 1));
		                                $(".ui-datepicker-calendar").hide();
		                            }
		                        }
		                    })
		});
	
});
</script>

<style>
.ui-datepicker-calendar {
    display: none;
    }
</style>


<SCRIPT LANGUAGE=JAVASCRIPT>
<!--
function goSearch(f){
    var f = document.form3;

			//f.searchtype.value = document.getElementById('searchtype').value;
			//f.searchDriver.value = document.getElementById('searchDriver').value;
			//f.key_word.value = document.getElementById('key_word').value;
			f.submit();
}
//-->
</SCRIPT>




  <main id="main" class="main">

    <!-- ======= Breadcrumbs ======= -->
    <div class="breadcrumbs">
      <div class="container">

        <div class="d-flex justify-content-between align-items-center">
          <h2>ISCC/CORSIA REPORT</h2>
          <ol>
            <li><a href="index.html">HOME</a></li>
            <li>REPORT</li>
            <li>ISCC/CORSIA REPORT</li>
          </ol>
        </div>

      </div>
    </div><!-- End Breadcrumbs -->

    <section class="page">


    <div class="container" data-aos="fade-up">

        <div class="wrap-border">
						<FORM CLASS="form-report" METHOD=POST NAME=form3 ACTION='<?=$_SERVER["PHP_SELF"]?>'>
							<INPUT TYPE=HIDDEN NAME=view VALUE='report_iscccorsia'>
							<INPUT TYPE=HIDDEN NAME=key_word VALUE=''>

            <table class="table-search-report col-float-right">
              <tr>
                <!--td>
                  <input type="date" placeholder="2022-12-31" class="custom-select">     

                </td-->
                <td> 
	                <select name="startYear" id="startYear" class="custom-select" required='required' style="width:200px" onChange="goSearch();">
	                  <?=$yearSTR?>
	                </select>
	              </td>
              
                <td>
                    <button class="btn btn-search" type="submit"><i class="bi-search"></i></button>
                </td>

              </tr>

            </table>
          </FORM>
        </div><!--wrap-border-->

    </div><!--container Ends-->
    
    
      <div class="container" data-aos="fade-up" style="clear: both;">
        <hr>
        <h4 class="text-center">ISCC/CORSIA Report (<?=$startYear?>)</h2>
        <hr>

      </div>





  <div class="container margin-top-25">
    <div class="box-table">
      <table class="tb-list-yearly tb-report">
        <tr>
          <th colspan="3">Yearly ISCC/CORSIA Volumes (L)</th>
        </tr>
        <tr>
          <td style='text-align:left;font-weight:bold;background-color:#FFF9F9;'>ISCC</td>
          <td style='text-align:right;font-weight:bold;background-color:#FFF9F9;'><?=number_format(floatval($rowYearISCC))?></td>
          <td style='text-align:right;font-weight:bold;background-color:#FFF9F9;'><?=number_format(floatval($rowPercentYearISCC), 2, '.', '')?>%</td>
        </tr>
        <tr>
          <td style='text-align:left;font-weight:bold;background-color:#F4FFF4;'>CORSIA</td>
          <td style='text-align:right;font-weight:bold;background-color:#F4FFF4;'><?=number_format(floatval($rowYearCORSIA))?></td>
          <td style='text-align:right;font-weight:bold;background-color:#F4FFF4;'><?=number_format(floatval($rowPercentYearCORSIA), 2, '.', '')?>%</td>
        </tr>
        <tr>
          <td style='font-weight:bold;background-color:#FFFFF4;'>TOTAL UCO</td>
          <td style='text-align:right;font-weight:bold;background-color:#FFFFF4;'><?=number_format(floatval($rowYearUCO))?></td>
          <td style='text-align:right;font-weight:bold;background-color:#FFFFF4;'></td>
        </tr>        
      </table>
    </div>
    
        <div class="wrap-overflow forecast-info report-wrap margin-top-25">
            <table class="tb-list tb-report"> <!-- tb-list-daily tb-report -->

<?=$titleSTR?>
<?=$rowMonthSTR?>

<!--
              <tr>
                <td rowspan="3">오일 픽업 양</td>
                <td>(L)</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>
              <tr>
                <td>(Mton)</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>
              <tr>
                <td style='text-align:center;background-color:#FBC02D;'>계정</td>
                <td style='text-align:right;background-color:#FBC02D;'>81982</td>
                <td style='text-align:right;background-color:#FBC02D;'>67097</td>
                <td style='text-align:right;background-color:#FBC02D;'>81982</td>
                <td style='text-align:right;background-color:#FBC02D;'>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>
              <tr>
                <td>총 계정 수</td>
                <td></td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>

              <tr>
                <td>상위 20% 계정 수</td>
                <td></td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>
              <tr>
                <td>총 계약건 수</td>
                <td></td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>

              
              <tr>
                <td rowspan="2">총 근무 시간</td>
                <td>(hr)</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>
              <tr>
                <td>(days)</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>
-->

            </table>
        </div>
    </div><!--container Ends-->
     

    </section>

  </main><!-- End #main -->
