<?php 
$title = 'Currency/Territory';
$this->placeholder('breadcrumb')->set('<a href="/warehouse.php">Warehouse</a> > '.$title);
?>
<h2><?=$title?></h2>
<div>&nbsp;</div>
<script type="text/javascript">
var dontShowTerritories = <?=json_encode($this->dataDmsTerritory)?>;
function filterCountries(storeId,elementClass){
$.each( dontShowTerritories, function(dmsId, territoryArray){
	if (storeId == dmsId) {
	   $("." + elementClass +" option").show(); 	   
	   $("." + elementClass +" option").each(function(){
		   var elementVal = parseInt($(this).val());  
		   var territoryPresent = jQuery.inArray(elementVal, territoryArray);
		   if(territoryPresent > -1){
			   $(this).hide();
			  		 }
		   	 });
		 }
	});
}
</script>
<div class="panelHeader">
	<h3>Add new </h3>
</div>
<!--  add currencies block -->
<div class="panelContent">
	<table class="data">
	<tr>
			<th class="center">DMS</th>
			<th class="center">Countries</th>
			<th class="center">Currency</th>
			<th>&nbsp;</th>
		</tr>
	<tr>
			<form action="/warehouse/managecurrencies/" method="post" id="addForm">
				<input type="hidden" name="act" value="customercurrency_add" />
				<td><select name="customerMasterMaster" class="small" onchange="filterCountries(this.value,'addCountries');">
				<option value="">Select</option>
			<?php
			if(!empty($this->cutomerMasters)){
				foreach($this->cutomerMasters as $cutomerMaster){
					?><option value="<?=$cutomerMaster['customer_master_master_id']?>"><?=$cutomerMaster['customer_name']?></option><?php
				}
			}
			?>
			</select></td>
			<td><select name="Countries[]" class="small addCountries" multiple="multiple">
					<option value="" onclick='$(this).parent().children().attr("selected","selected");'>All</option>
			<?php
			if(!empty($this->countries)){
				foreach($this->countries as $country){
					?><option value="<?=$country['id']?>"><?=$country['name']?></option><?php
				}
			}
			?>
			</select></td>
			<td><select name="Currency" class="small">
					<option value="">Select</option>
			<?php
			if(!empty($this->currencies)){
				foreach($this->currencies as $currency){
					?><option value="<?=$currency['currency_id']?>"><?=$currency['currency']?></option><?php
				}
			}
			?>
			</select></td>
			<td><input type="submit" value="Add" class="btn btn-primary btn-sm" /></td>
			</form>
		</tr>
	</table>
</div>

<!-- list currencies block -->
<div class="panelHeader">
	<h3><?=$title?></h3>
</div>
<div class="panelContent">
	<table class="data">
		<tr>
			<th class="center">DMS</th>
			<th class="center">Countries</th>
			<th class="center">Currency</th>
			<th>&nbsp;</th>
		</tr>
		<?php 
		if(!empty($this->data)){
			
		$rowId = 0;
			foreach($this->data as $dmsMasterId => $rowObject){
				
				
				$dmsTerritoryCurrencyIdsHidden = "";
				
				foreach ($rowObject['currency'] as $currencyCode => $countryArray)
				{
					$rowId++;
					$territoriesString = "";
					$deleteIdsString = "";
					$countryIds = array();
					$dmsTerritoryCurrencyIdHidden = "";
					foreach ($countryArray['countries'] as $dmsTerritoryCurrencyId => $countryId){
						$territoriesString .= $countryId['name']. ", ";
						$countryIds[] =  $countryId['id'];
						$deleteIdsString .= '&dmsTerritoryCurrencyId[]='.$dmsTerritoryCurrencyId;
						$dmsTerritoryCurrencyIdHidden .= '<input type="hidden" value="'.$dmsTerritoryCurrencyId.'" name="dmsTerritoryCurrencyIdsOld['.$countryId['id'].']" />';
											}
					?>
		<tr id="view_<?=$rowId?>">
			<td> <?php echo $rowObject['name']; ?></td>
			<td width="30%">
				<?php echo substr($territoriesString, 0,-2);?>
			</td>
			<td>
			<?=$countryArray['name']?>
			</td>
			<td>
			<a href="javascript:;" onclick="$('#view_<?=$rowId?>').hide();$('#edit_<?=$rowId?>').show();">Edit</a> &nbsp;
			<a href="/warehouse/managecurrencies/?act=customercurrency_delete<?=$deleteIdsString?>" onclick="return confirm('Are you sure. You want to delete the customer currency ?');">Delete</a>
			</td>
		</tr>
		
		<!-- update row begins -->
		<tr id="edit_<?=$rowId?>" style="display:none;">
			<form action="/warehouse/managecurrencies/" method="post">
				<input type="hidden" name="act" value="customercurrency_update" />
				<td>
				<input type="hidden" value="<?=$dmsMasterId?>" name="customerMasterMasterOld">
				<select name="customerMasterMaster" class="small" onchange="filterCountries(this.value,'country_<?=$rowId?>');">
				<option value="">Select</option>
			<?php
			if(!empty($this->cutomerMasters)){
				foreach($this->cutomerMasters as $cutomerMaster){
					?><option value="<?=$cutomerMaster['customer_master_master_id']?>" <?php if($cutomerMaster['customer_master_master_id'] == $dmsMasterId){ echo "selected"; }?>><?=$cutomerMaster['customer_name']?></option><?php
				}
			}
			?>
			</select></td>
			<td>
			<select name="Countries[]" class="small country_<?=$rowId?>" multiple="multiple">
					<option value="" onclick='$(this).parent().children().attr("selected","selected");'>All</option>
			<?php
			if(!empty($this->countries)){
				$dontShowTerritories = array();
				foreach($this->countries as $country){
				if(in_array($country['id'], $this->dataDmsTerritory[$dmsMasterId]) && !in_array($country['id'],$countryIds)){ continue; }
					?><option value="<?=$country['id']?>" <?php if(in_array($country['id'],$countryIds)){ echo "selected"; }?>><?=$country['name']?></option><?php
					
				}
			}
			?>
			</select></td>
			<td>
			<input type="hidden" value="<?=$currencyCode?>" name="CurrencyOld">
			<select name="Currency" class="small">
					<option value="">Select</option>
			<?php
			if(!empty($this->currencies)){
				foreach($this->currencies as $currency){
					?><option value="<?=$currency['currency_id']?>" <?php if($currency['currency_id'] == $currencyCode){ echo "selected";}?>><?=$currency['currency']?></option><?php
				}
			}
			?>
			</select></td>
			<td>
			<?=$dmsTerritoryCurrencyIdHidden?>
			<input type="submit" value="Update" class="btn btn-default btn-sm" /></td>
			</form>
		</tr>
		
		<?php 
			}	
	}
}else{
	?>
	<tr><td colspan="3"> No records found.</td></tr>
	<?php 
}	
?>
		
	</table>
</div>