<?
global $db;

$result = $db->query("select * from ecmpricebooks_customview where id='".$_REQUEST['record']."'");
$row=$db->fetchByAssoc($result);
$c=explode("||",$row['columns']);
$t=explode("||",$row['titles']);
$o=explode("||",$row['orders']);

$name=$row['name'];

for($i=0;$i<count($o);$i++){
	$exp=explode(" ",$o[$i]);
	$sorder[$i+1]=$exp[1];
	$oi[$i+1]=$exp[0];
}


require("modules/EcmProducts/vardefs.php");
require("modules/EcmProducts/language/en_us.lang.php");

function showSorder($i,$value){
	require("modules/EcmPriceBooks/language/en_us.lang.php");
	$html='<select name="sorder'.$i.'">';
	$html.='<option value="asc"';
	if($value=="asc")$html.=' selected';
	$html.='>'.$mod_strings['LBL_ASCENDING'].'</option>';
	$html.='<option value="desc"';
	if($value=="desc")$html.=' selected';
	$html.='>'.$mod_strings['LBL_DESCENDING'].'</option>';
	$html.='</select>';
	return $html;
}
function selectColumn($fields,$i,$value)
{
	for($j=1;$j<=9;$j++)$sos.='showOrderSelect('.$j.',document.getElementById(\'order'.$j.'\').value);';
	$html='<select onchange="'.$sos.'" name="column'.$i.'" id="column'.$i.'"><option value="">Select</option>';
	foreach($fields as $field)
	{
		if($field['name'] && $field['vname'])
		{
			$html.='<option value="'.$field['name'].'"';
			if($value==$field['name'])$html.=' selected';
			$html.='>'.$field['vname'].'</option>';
		}
	}
	$html.='</select>';
	return $html;
}

$fields=$dictionary['EcmProduct']['fields'];
$js="";
foreach($fields as $field)$f[]=array("vname"=>$mod_strings[$field['vname']],"name"=>$field['name']);
$f[]=array("vname"=>$mod_strings['LBL_LIST_PRICE'],"name"=>"list_price");
$f[]=array("vname"=>$mod_strings['LBL_MARGIN'],"name"=>"margin_rate");
$f[]=array("vname"=>$mod_strings['LBL_RECIPIENT_CODE'],"name"=>"recipient_code");

$f[]=array("vname"=>$mod_strings['LBL_EAN_PL'],"name"=>"ean_pl");
$f[]=array("vname"=>$mod_strings['LBL_SHORT_DESCRIPTION_PL'],"name"=>"short_description_pl");
$f[]=array("vname"=>$mod_strings['LBL_LONG_DESCRIPTION_PL'],"name"=>"long_description_pl");
$f[]=array("vname"=>$mod_strings['LBL_REMARKS_PL'],"name"=>"remarks_pl");

$f[]=array("vname"=>$mod_strings['LBL_EAN_EN'],"name"=>"ean_en");
$f[]=array("vname"=>$mod_strings['LBL_SHORT_DESCRIPTION_EN'],"name"=>"short_description_en");
$f[]=array("vname"=>$mod_strings['LBL_LONG_DESCRIPTION_EN'],"name"=>"long_description_en");
$f[]=array("vname"=>$mod_strings['LBL_REMARKS_EN'],"name"=>"remarks_en");

$f[]=array("vname"=>$mod_strings['LBL_EAN_DE'],"name"=>"ean_de");
$f[]=array("vname"=>$mod_strings['LBL_SHORT_DESCRIPTION_DE'],"name"=>"short_description_de");
$f[]=array("vname"=>$mod_strings['LBL_LONG_DESCRIPTION_DE'],"name"=>"long_description_de");
$f[]=array("vname"=>$mod_strings['LBL_REMARKS_DE'],"name"=>"remarks_de");

$f[]=array("vname"=>$mod_strings['LBL_DEFAULT_REMARKS_PL'],"name"=>"default_remarks_pl");
$f[]=array("vname"=>$mod_strings['LBL_DEFAULT_REMARKS_EN'],"name"=>"default_remarks_en");
$f[]=array("vname"=>$mod_strings['LBL_DEFAULT_REMARKS_DE'],"name"=>"default_remarks_de");

$f[]=array("vname"=>"Qty in last 1 m","name"=>"sale_qty30");
$f[]=array("vname"=>"Qty in last 3 m","name"=>"sale_qty90");
$f[]=array("vname"=>"Qty in last 6 m","name"=>"sale_qty180");

$f[]=array("vname"=>"Avg price in last 1 m","name"=>"sale_avg_price30");
$f[]=array("vname"=>"Avg price in last 3 m","name"=>"sale_avg_price90");
$f[]=array("vname"=>"Avg price in last 6 m","name"=>"sale_avg_price180");

$f[]=array("vname"=>"Ordered","name"=>"ordered");
sort($f);
foreach($f as $ff)$js.='names["'.$ff['name'].'"]="'.$ff['vname'].'"
';
?>
<script>
function showOrderSelect(no,v)
{
	var names=new Array();
	<?print $js;?>
	var html='<select name="order'+no+'" id="order'+no+'"><option value="">select</option>';
	html=html+'<option value="name"';
	if("name"==v)
	{
		html=html+' selected';
	}
	for(var i=1;i<=9;i++)
	{
		if(document.getElementById('column'+i).value)
		{
			html=html+'<option value="'+document.getElementById('column'+i).value+'"';
			if(v==document.getElementById('column'+i).value)
			{
				html=html+' selected';
			}
			html=html+'>'+names[document.getElementById('column'+i).value]+'</option>';
		}
	}
	html=html+'</select>';
	document.getElementById('dorder'+no).innerHTML=html;
	return html;
}
</script>
<form name="EditView" method="POST" action="index.php?module=EcmPriceBooks&action=SaveCustomView&record=<?print $_REQUEST['record'];?>">
<div style="padding-top: 2px">
 <input title="Save" class="button" type="submit" name="button" value="Save" > 
 <input title="Cancel" class="button" type="submit" name="button" value="Cancel">
</div><br />
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="tabForm">
 <tr>
  <td>
  	<table width="100%" border="0" cellspacing="0" cellpadding="0">
  	 <tr>
    	<td width="15%" class="dataLabel"><span sugar='slot1'>View name <span  class="required">*</span></span sugar='slot'></td>
	    <td width="35%" class="dataField"><span sugar='slot1b'><input name='name' type="text" tabindex='1' size='35' maxlength='50' value="<? print $name; ?>"></span sugar='slot'></td>
  	  <td width="15%" class="dataLabel" >&nbsp;</td>
  	  <td width="35%" class="dataField">&nbsp;</td>
  	 </tr>
  	   	 

     <tr>
       <td  class="dataLabel"><span sugar='slot3'>Choose columns </span sugar='slot'></td>
       <td class="dataField">&nbsp;</td>
       <td valign="top" class="dataLabel">&nbsp;</td>
       <td valign="top" class="dataField">&nbsp;</td>
     </tr>
     <tr>
       <td colspan="4" valign="top"  class="dataLabel"><table width="100%" border="0" cellspacing="0" cellpadding="0">
         <tr>
           <td><?print selectColumn($f,1,$c[0]);?></td>
           <td><?print selectColumn($f,2,$c[1]);?></td>
           <td><?print selectColumn($f,3,$c[2]);?>;</td>
         </tr>
         <tr>
           <td><?print selectColumn($f,4,$c[3]);?></td>
           <td><?print selectColumn($f,5,$c[4]);?></td>
           <td><?print selectColumn($f,6,$c[5]);?></td>
         </tr>
         <tr>
           <td><?print selectColumn($f,7,$c[6]);?></td>
           <td><?print selectColumn($f,8,$c[7]);?></td>
           <td><?print selectColumn($f,9,$c[8]);?></td>
         </tr>
       </table></td>
       </tr>
     <tr>
       <td valign="top"  class="dataLabel">Columns titles </td>
       <td class="dataField">&nbsp;</td>
       <td class="dataLabel" valign="top">&nbsp;</td>
       <td class="dataField">&nbsp;</td>
     </tr>
     <tr>
       <td colspan="4" valign="top"  class="dataLabel"><table width="100%" border="0" cellspacing="0" cellpadding="0">
         <tr>
           <td><input name="title1" type="text" id="title1" value="<?print $t[0];?>" size="30" /></td>
           <td><input name="title2" type="text" id="title2" value="<?print $t[1];?>" size="30" /></td>
           <td><input name="title3" type="text" id="title3" value="<?print $t[2];?>" size="30" /></td>
         </tr>
         <tr>
           <td><input name="title4" type="text" id="title4" value="<?print $t[3];?>" size="30" /></td>
           <td><input name="title5" type="text" id="title5" value="<?print $t[4];?>" size="30" /></td>
           <td><input name="title6" type="text" id="title6" value="<?print $t[5];?>" size="30" /></td>
         </tr>
         <tr>
           <td><input name="title7" type="text" id="title7" value="<?print $t[6];?>" size="30" /></td>
           <td><input name="title8" type="text" id="title8" value="<?print $t[7];?>" size="30" /></td>
           <td><input name="title9" type="text" id="title9" value="<?print $t[8];?>" size="30" /></td>
         </tr>
       </table></td>
       </tr>
     <tr>
       <td valign="top"  class="dataLabel">Sort order </td>
       <td class="dataField">&nbsp;</td>
       <td class="dataLabel" valign="top">&nbsp;</td>
       <td class="dataField">&nbsp;</td>
     </tr>
     <tr>
       <td colspan="4" valign="top"  class="dataLabel"><table width="100%" border="0" cellspacing="0" cellpadding="0">
         <tr>
           <td><div style="display:inline;" id="dorder1"></div><? print showSorder(1,$sorder[1]);?></td>
           <td><div style="display:inline;" id="dorder2"></div><? print showSorder(2,$sorder[2]);?></td>
           <td><div style="display:inline;" id="dorder3"></div><? print showSorder(3,$sorder[3]);?></td>
         </tr>
         <tr>
           <td><div style="display:inline;" id="dorder4"></div><? print showSorder(4,$sorder[4]);?></td>
           <td><div style="display:inline;" id="dorder5"></div><? print showSorder(5,$sorder[5]);?></td>
           <td><div style="display:inline;" id="dorder6"></div><? print showSorder(6,$sorder[6]);?></td>
         </tr>
         <tr>
           <td><div style="display:inline;" id="dorder7"></div><? print showSorder(7,$sorder[7]);?></td>
           <td><div style="display:inline;" id="dorder8"></div><? print showSorder(8,$sorder[8]);?></td>
           <td><div style="display:inline;" id="dorder9"></div><? print showSorder(9,$sorder[9]);?></td>
         </tr>
       </table></td>
       </tr>
   </table>
  </td>
 </tr>
</table>
<div style="padding-top: 2px">
 <input title="Save" class="button" type="submit" name="button" value="Save" > 
 <input title="Cancel" class="button" type="submit" name="button" value="Cancel">
</div>
</form>
<script>
showOrderSelect(1,'<?print $oi[1];?>');
showOrderSelect(2,'<?print $oi[2];?>');
showOrderSelect(3,'<?print $oi[3];?>');
showOrderSelect(4,'<?print $oi[4];?>');
showOrderSelect(5,'<?print $oi[5];?>');
showOrderSelect(6,'<?print $oi[6];?>');
showOrderSelect(7,'<?print $oi[7];?>');
showOrderSelect(8,'<?print $oi[8];?>');
showOrderSelect(9,'<?print $oi[9];?>');
</script>
