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

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

$c_uid = trim($_POST["c_uid"]);

//echo "[$c_uid]";exit;

if ($mode == "") {
		$mode = "update";
		$btnName = "UPDATE";
		$admTag = "DISABLED";
}

if($c_uid == "") {
		$msg = "Invaild data. Please try again. [Err - c_uid / FORECAST-POPUP]";
		$func -> modalMsg ($msg, 1);
		exit();
} else {
	
		// Get Information
		$query = "SELECT * FROM tbl_customer WHERE c_uid = '$c_uid'";
		$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;
		}

		$c_locationSTR = str_replace("\\", "", $c_location);
		$c_comment_riSTR = str_replace("\\", "", $c_comment_ri);

}

$c_paymenttypeSTR = $arrPaymenttype[$c_paymenttype];

if ($c_paymenttype =="CA") {

		$query = "SELECT * FROM tbl_daily
							WHERE d_customeruid = '$c_uid' AND d_status = 'F' AND d_quantity > 1 ORDER BY d_visitdate DESC LIMIT 1";

		$result = $jdb->fQuery($query, "list error");
		//echo "[$query]";
		
		if ($result[0] != "") {
				for($i=0; $i<sizeof($result); $i++)
				{
					//list($key, $value) = each($result);
					foreach ( (Array) $result as $key => $value )			
						$$key = $value;
				}		
				
				$d_visitdateSTR = $func -> convertFormat ($d_visitdate, 3);

				if ($d_paystatus == "") $d_paystatus = "N";		
				$d_paystatusSTR = $arrPaidStatus[$d_paystatus];
				
				if ($d_paystatus == "P")  $d_paystatusSTR = $d_paystatusSTR." (".$d_visitdateSTR." / $".$d_payamount.")";
				else $d_paystatusSTR = $d_paystatusSTR." (".$d_visitdateSTR.")";
		}
		else $d_paystatusSTR = "-";
}

	$query_img = "
		SELECT i_filepath, i_filename 
		FROM tbl_customer_image
		WHERE i_customeruid = '$c_uid' 
		AND i_type = 'install'
		AND i_status = 'A'
		ORDER BY i_createddate DESC
		LIMIT 1
	";
	$imgResult = $jdb->fQuery($query_img, "fetch image error");

	$installImgUrl = "";
	if (!empty($imgResult)) {
		$installImgUrl = $imgResult['i_filepath'] . $imgResult['i_filename'];
	}

?>


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

<FORM NAME=customerShortInfo id=customerShortInfo METHOD=POST >
    	<input type=hidden name=actionStr value="SHORTINFO">
    	<input type=hidden name=actionPage value="">
    	<input type=hidden name=c_uid id=c_uid value="<?=$c_uid?>">
		<input type=hidden name=c_accountno id=c_accountno value="<?=$c_accountno?>">
			<input type=hidden name=mode id=mode value="update">


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

            <tr>
              <td class="td-title-info">Restaurant Name</td>
              <td colspan="2" class="td-text-info">
                <?=$c_name?>
              </td>
            </tr>

            <tr>
              <td class="td-title-info">Payment Type</td>
              <td colspan="2" class="td-text-info">
                <?=$c_paymenttypeSTR?>
              </td>
            </tr>

<? if ($c_paymenttype =="CA") { ?>
            <tr>
              <td class="td-title-info">Pay Status</td>
              <td colspan="2" class="td-text-info">
								<?=$d_paystatusSTR?>
              </td>
            </tr>
<? } ?>
              
            <tr>
              <td class="td-title-info">Comment</td>
              <td colspan="2" class="td-text-info">
                <textarea id="c_comment_ri" class="textarea" name="c_comment_ri" rows="4" cols="20"><?=htmlspecialchars($c_comment_riSTR, ENT_QUOTES)?></textarea>
              </td>
            </tr>

            <tr>
              <td class="td-title-info">Container Location</td>
              <td colspan="2" class="td-text-info">
              	<textarea id="c_location" class="textarea" name="c_location" rows="2" cols="20"><?=htmlspecialchars($c_locationSTR, ENT_QUOTES)?></textarea>
              </td>
            </tr>

			<tr>
				<td class="td-title-info">Installation Image</td>
				<td class="td-text-info" style="border-right: none;">
					<?php if ($installImgUrl != "") { ?>
						<a href="<?=$installImgUrl?>" target="_blank">View Image</a>
					<?php } else { ?>
						<span>No image</span>
					<?php } ?>
				</td>
				<td style="text-align: right; border-right: 1px solid #dcdcdc; border-bottom:1px solid #dcdcdc;">
					<span style="border: 1px solid #72A864; border-radius: 5px; padding: 3px;"><a href="#" onclick="popup();">ADD</a></span>
				</td>
			</tr>

          </table>
          <br>
          <div class="text-center grid-layout-col-2">
          	<?php if ($_SESSION['ss_LEVEL'] != 10): ?>
				<button type="submit" id="buttonADD" class="btn-sub">SAVE</button>
			<?php endif; ?>
            <button type="button" class="btn-gray" data-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>



<script>

$(document).ready(function(){

	  $("#customerShortInfo").validate({
	    // Specify validation rules
	    /*ignore: ".ignore",
	    rules: {
	      d_quantity: {
	        required: true,
	        //minlength: 6,
	      },
	      d_payamount: {
	        required: "#d_paystatuspaid:checked",
	        //minlength: 6,
	      }, 	      
	    },
	    messages: {
					d_quantity: {
							required: "Please input Oil Quantity",
							//minlength: "Minimum of 6 characters.",
					},
					d_payamount: {
							required: "Please input Cash Amount",
							//minlength: "Minimum of 6 characters.",
					},						
	    },
	    */
	    errorElement : 'div',


		  submitHandler: function(form) {
		  	
		  	var $inputs = $(form).find("input, select, button, textarea");
		  	var serializedData = $(form).serialize();
		  	
		 							//e.preventDefault();
				         	/*
				         	var d_mode = $('#mode').val();				         	
									var d_uid = $('#d_uid').val();
									var d_visitdate = $('#d_visitdate').val();
									var d_driveruid = $('#d_driveruid').val();
									var d_customeruid = $('#d_customeruid').val();
									var d_quantity = $('#d_quantity').val();
									var d_sludge = $('#d_sludge').val();
									var d_payamount = $('#d_payamount').val();
									var d_paystatus = $('#d_paystatus').val();
									var d_note = $('#d_note').val();
				         	alert(d_mode); */

					        $.ajax({  
					             url:"/lib/shortInfo_lib_process.php",  
					             method:"POST",  
					             data: serializedData,
					             
					             /*
					             data:{d_mode:d_mode, d_uid:d_uid, d_visitdate:d_visitdate, d_driveruid:d_driveruid, 
					             			d_customeruid:d_customeruid, d_quantity:d_quantity, d_sludge:d_sludge, 
					             			d_payamount:d_payamount, d_paystatus:d_paystatus, d_note:d_note	},
					             */
					             //beforeSend:function(){  
					             //     $('#submitid').val("Updating");  
					             //},  
					             success:function(response){  
					                  //$('#addcartformid')[0].reset();  
					                  //For debugging
					                  //$('.rt_debugging').html(response);
					                  //alert(response);
					                  if (response == "1") {
																$('.myModalPopup-body').html("Saved Successfully.");
																$('.myModalPopup-body').css('background-color', '#2A9B56');
																$('#myModalcustomerShortInfo').modal('hide');
																
							                  $('#myModalPopup').modal('show'); 
														    setTimeout(function () {
														        //$('#myModalcustomerShortInfo').modal('hide');
														        $('#myModalPopup').modal('hide'); 
														        parent.location.reload();
														    }, 1000);                  
									  } else if (response == "-1") {
																$('.myModalPopup-body').html("You do not have permission.");
        														$('.myModalPopup-body').css('background-color', '#FF8205');

											  $('#myModalPopup').modal('show');
																setTimeout(function () {
																	$('#myModalPopup').modal('hide');
																}, 1500);

					                  } else {
																$('.myModalPopup-body').html("Failed. Please Try again.");
																$('.myModalPopup-body').css('background-color', '#FF8205');
																
							                  $('#myModalPopup').modal('show'); 
														    setTimeout(function () {
														        $('#myModalPopup').modal('hide'); 
														    }, 1500); 
					                  }
					                  
					                  //$('#myModalAdminInfo').modal('hide');
					                  //$('.cart-item-no').html(response);  

					             }  
					        });  

			}		



	});
  
});

function popup() {
    const url = `/lib/customer_image_upload_popup.php?c_uid=<?=$c_uid?>&goStr=<?=$goStr?>`;
    window.open(url, "uploadImage", "width=600,height=750,scrollbars=yes");
}
</script>


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