<?
include getenv("DOCUMENT_ROOT")."/include/session_include.php";

$goStr = "switched=$switched&page=$page&key_word=$key_word&column=$column&sorting_type=$sorting_type&switch=$switch";

//$func->checkAdmin("index.php");

// Opening Balance 기능 제한 (Admin : 1, Staff : 5 만 가능)
$permit = array("1", "5");
if (in_array($_SESSION['ss_LEVEL'], $permit)) {
    $setTag = "ENABLED";
}
else $setTag = "DISABLED";	

/*
for($i=0; $i<sizeof($_POST); $i++) {
	list($key, $value) = each($_POST);
	$$key = $value;

	if(is_array($value))
	{
		$count = 10;
		for($i = 0; $i < $count; $i ++) {
			if ($value[$i]) echo "ARRAY[$key][$value[$i]]<br>";
		}
	}
	else 	echo "[$key][$value]<br>";
	//print_r($_POST);
}

//exit;
*/


$h_driveruid = trim($_POST["driverid"]);
$h_date = trim($_POST["dateid"]);
$h_uid = trim($_POST["uid"]);
$mode = trim($_POST["modeid"]);

if($h_driveruid == "" || $h_date == "" || $mode == "" ) {
		$msg = "Invaild data. Please try again. [Err - h_driveruid,h_date,mode / DAILYRECORD]";
		$urlSTR = "/index_intranet.php?view=order_list&".$goStr;
		$func -> modalMsg ($msg, 1);
		exit();
} else {
		// mode = update : tbl_memberhis 에서 해당일,해당드라이버 확인후 update
		if ($mode == "update") {
					// Get Information
					$query = "SELECT * FROM tbl_memberhis 
										WHERE h_driveruid = '".$h_driveruid."' AND h_date = '".$h_date."' 
										ORDER BY h_uid DESC LIMIT 1 ";
					$result=$jdb->fQuery($query, "fetch query error");
					//echo"$query";

					for($i=0; $i<sizeof($result); $i++)
					{
						//list($key, $value) = each($result);
						foreach ( (Array) $result as $key => $value )			
							$$key = $value;
					}
					
					$h_commentSTR = str_replace("\\", "", $h_comment);
					
					$cashreturn = $h_balance_r;	
					
					
		} else if ($mode == "create"){
			
					// Get Opening Balance
					/*
					$qry_before = "SELECT h_balance_t, h_balance_r FROM tbl_memberhis 
										WHERE h_driveruid = '".$h_driveruid."' AND h_date < $h_date
										ORDER BY h_date DESC LIMIT 1 ";
					$rt_before=$jdb->fQuery($qry_before, "fetch query error");
					//echo"[$qry_before]";
					
					$beforeBalance = $rt_before['h_balance_t'];	
					$cashreturn = $rt_before['h_balance_r'];	
					*/
		}

		// Get Cash Out SUM
		$query = "SELECT SUM(d_payamount) FROM tbl_daily 
							WHERE d_driveruid = '".$h_driveruid."' AND d_visitdate LIKE '".$h_date."%' AND d_paystatus = 'P' AND d_status = 'F' ";
		$dailySUM=$jdb->rQuery($query, "fetch query error");
		//echo"$query";
		


		$d_orderdateSTR = $func -> convertFormat ($d_orderdate, 3);
	
}


if ($setTag != "ENABLED") $readonlySTR = "readonly";

// D (Balance)  = A (Opening Balance) + B (Cash In) + C (Cash Out)
// Cash Out : Cash(CA)계정의 경우 당일 Paid 된것들의 합

// Opening Balance 는 운영자만 수정할수 있음. 매월 1일부터 리셋
// D (Balance)는 다음날 A (Opening Balance)


if (($d_orderdate == date("Ymd") && $d_status != "F") || $_SESSION['ss_LEVEL'] == "1") { 
		$gridSTR = " grid-layout-col-2";
		$buttonSTR = "<button type=\"submit\" id=\"buttonADD\" class=\"btn-sub\">ADD</button>";
} 
else { 
		$gridSTR = " width-200";
		$buttonSTR = "";	
} 


?>


<script>

$(document).ready(function(){

		// Decimal (ex 11, 11.0)
		$("#h_balance_o, #h_balance_in, #h_balance_g, #h_balance_r").on("input", function(evt) {
				var self = $(this);
				self.val(self.val().replace(/[^0-9\.]/g, ''));
				if ((evt.which != 46 || self.val().indexOf('.') != -1) && (evt.which < 48 || evt.which > 57)) 
				{
				 evt.preventDefault();
				}
		});

		/* 삭제 요청함 (2023.10.30)
		$('#h_departuretime, #h_arrivaltime').timepicker({ 
					'timeFormat': 'H:i', 
					'step': 1, 
					'scrollDefault': 'now', 
					'disableTextInput': 'true', 
					//'minTime': '12:30pm', 
					//'maxTime': '08:46pm',
					'disableTextInput': 'true', 
					//'disableTimeRanges': [['12:00am', '11:30am'], ['14:01pm', '14:59pm']] 
		});
		*/
  
});
</script>


<style>
.error{
	color: red;
	font-weight: 200;
	margin-left: 0em;
}		
</style>

<div class="rt_debugging"></div>

      <div class="modal-header">
        <h4 class="modal-title">DAILY RECORD</h4>
        <button type="button" class="btn" data-bs-dismiss="modal" aria-label="Close" style="color:#fff;"><i class="bi bi-x"></i></button>
      </div>
      <div class="modal-body">

<FORM NAME=dailyrecord id=dailyrecord METHOD=POST ACTION="/lib/user_process.php">
    	<input type=hidden name=actionStr value="DAILYRECORD">
    	<input type=hidden name=actionPage value="">
    	<input type=hidden name=h_uid id=h_uid value="<?=$h_uid?>">
    	<input type=hidden name=h_date id=h_date value="<?=$h_date?>">
    	<input type=hidden name=h_driveruid id=h_driveruid value="<?=$h_driveruid?>">
			<input type=hidden name=mode value="<?=$mode?>">
			    	
			<input type=hidden name=switched value="<?=$getSWHStr?>">
			<input type=hidden name=page value="<?=$page?>">
			<input type=hidden name=key_word value="<?=$key_word?>">
			<input type=hidden name=column value="<?=$column?>">
			<input type=hidden name=sorting_type value="<?=$sorting_type?>">
			<input type=hidden name=switch value="<?=$switch?>">

			
          <table class="tb-info-box">

            <tr>
              <td class="td-title-info">Departure Time</td>
              <td class="td-text-info">
                <input type="text" id="h_departuretime" name="h_departuretime" value="<?=$h_departuretime?>">
              </td>
            </tr>

            <tr>
              <td class="td-title-info">Arrival Time</td>
              <td class="td-text-info">
                <input type="text" id="h_arrivaltime" name="h_arrivaltime" value="<?=$h_arrivaltime?>">
              </td>
            </tr>

            <!--tr>
              <td class="td-title-info">Opening Balance</td>
              <td class="td-text-info">
                <input type="text" id="h_balance_o" name="h_balance_o" value="<?=$beforeBalance?>" <?=$readonlySTR?> placeholder="100" maxlength="7">
              </td>
            </tr-->


            <tr>
              <td class="td-title-info">Cash Return</td>
              <td class="td-text-info">
                <input type="text" id="h_balance_r" name="h_balance_r" value="<?=$cashreturn?>" <?=$readonlySTR?> placeholder="100" maxlength="7">
              </td>
            </tr>
   
            <tr>
              <td class="td-title-info">Cash In</td>
              <td class="td-text-info">
                <input type="text" id="h_balance_in" name="h_balance_in" value="<?=$h_balance_in?>" placeholder="100" maxlength="7">
              </td>
            </tr>

            <!--tr>
              <td class="td-title-info">Cash Out</td>
              <td class="td-text-info">
                <input type="text" id="h_balance_out" name="h_balance_out" value="<?=$dailySUM?>" readonly placeholder="100" maxlength="7">
              </td>
            </tr-->

            <tr>
              <td class="td-title-info">Gas</td>
              <td class="td-text-info">
                <input type="text" id="h_balance_g" name="h_balance_g" value="<?=$h_balance_g?>" placeholder="100" maxlength="7">
              </td>
            </tr>


            <tr>
              <td class="td-title-info">Comment</td>
              <td class="td-text-info">
                <textarea id="h_comment" class="textarea" name="h_comment" rows="4" cols="20"><?=$h_commentSTR?></textarea>
              </td>
            </tr>
          </table>
          <br>
          <div class="text-center grid-layout-col-2">
            <button type="submit" class="btn-sub">SAVE</button>
            <button type="button" class="btn-gray" data-bs-dismiss="modal">CLOSE</button>
          </div>
</FORM>

      </div>



<div class="modal fade" id="myModalPopup" tabindex="-1" role="dialog" aria-labelledby="myModalPopup"  style="opacity: 0.5; padding-right: 0px !important;">
  <div class="modal-dialog modal-dialog-centered modal-xl" role="document">
      <div class="modal-content" style="background-color:#2A9B56 !important; max-width:260px; margin: 0 auto;">
        <div class="myModalPopup-body" style="text-align: center; border-radius: 5px; max-height:60px;	font-size:18px; background-color:#2A9B56; color: #FFFFFF; font-weight: bold;padding:0.5em 1em; ">

        </div>     	
      </div>
  </div>
</div>