<?PHP
/* Copyright 2010, Lime Technology LLC.
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 */
/* Adapted by Bergware International (May 2012) */
?>
<?
/* default values when adding new share */
$share = array( "nameOrig" =>      "",
                "name" =>          "",
                "comment" =>       "",
                "allocator" =>     "highwater",
                "floor" =>         "0",
                "splitLevel" =>    "",
                "include" =>       "",
                "exclude" =>       "",
                "useCache" =>      "no");
?>
<?
if ($name != ""):
  if (array_key_exists( $name, $shares)):
    $share = $shares[$name];
  else:
?>  <p class="notice">Share <?=$name?> has been deleted.</p><br>
    <button type="button" onClick="done();">OK</button>
<?  return;
  endif;
endif;
?>
<form method="POST" action="/update.htm" target="progressFrame" onsubmit="prepareEdit(this)">
<input type="hidden" name="shareNameOrig" value="<?=$share['nameOrig']?>">
<table class="settings">
  <tr>
  <td>Name:</td>
  <td><input type="text" name="shareName" maxlength="40" value="<?=$share['name']?>"></td>
  </tr>
  <tr>
  <td>Comments:</td>
  <td><input type="text" name="shareComment" maxlength="256" value="<?=$share['comment']?>"></td>
  </tr>
  <tr>
  <td>Allocation method:</td>
  <td><select name="shareAllocator" size="1">
  <?=mk_option($share['allocator'], "highwater", "High-water")?>
  <?=mk_option($share['allocator'], "mostfree", "Most-free")?>
  <?=mk_option($share['allocator'], "fillup", "Fill-up")?>
  </select></td>
  </tr>
  <tr>
  <td>Min. free space:</td>
  <td><input type="text" name="shareFloor" maxlength="16" value="<?=$share['floor']?>"></td>
  </tr>
  <tr>
  <td>Split level:</td>
  <td><input type="text" name="shareSplitLevel" maxlength="256" value="<?=$share['splitLevel']?>"></td>
  </tr>
  <tr>
  <td>Included disk(s):</td>
  <td><select id="s1" name="shareInclude" size="1" multiple="multiple" style="display:none">
  <option value='' selected></option>
<?foreach ($disks as $disk):?>
  <?=mk_option_check($disk['name'], $share['include'])?>
<?endforeach;?>
  </select></td>
  </tr>
  <tr>
  <td>Excluded disk(s):</td>
  <td><select id="s2" name="shareExclude" size="1" multiple="multiple" style="display:none">
  <option value='' selected></option>
<?foreach ($disks as $disk):?>
  <?=mk_option_check($disk['name'], $share['exclude'])?>
<?endforeach;?>
  </select></td>
  </tr>
<?if ($var['cacheActive']=="yes"):?>
  <tr>
  <td>Use cache disk:</td>
  <td><select name="shareUseCache" size="1">
  <?=mk_option($share['useCache'], "no", "No")?>
  <?=mk_option($share['useCache'], "yes", "Yes")?>
  <?=mk_option($share['useCache'], "only", "Only")?>
  </select></td>
  </tr>
<?endif;
  if ($share['name'] == ""):
?><tr>
  <td></td>
  <td><input type="submit" name="cmdEditShare" value="Add Share"><button type="button" onclick="done();">Cancel</button>
  </td>
  </tr>
<?elseif ($share['empty']=="yes"):?>
  <tr>
  <td>Share empty?</td>
  <td>Yes</td>
  </tr>
  <tr>
  <td>Delete<input type="checkbox" name="confirmDelete" onchange="chkDelete(this.form, this.form.cmdEditShare);"></td>
  <td><input type="submit" name="cmdEditShare" value="Apply"><button type="button" onclick="done();">Done</button></td>
  </tr>
<?else:?>
  <tr>
  <td>Share empty?</td>
  <td>No</td>
  </tr>
  <tr>
  <td></td>
  <td><input type="submit" name="cmdEditShare" value="Apply"><button type="button" onclick="done();">Done</button></td>
  </tr>
<?endif;?>
</table>
</form>

<script>
$(document).ready(function() {
  $("#s1").dropdownchecklist({emptyText:'All', width:300, explicitClose:'...close'});
  $("#s2").dropdownchecklist({emptyText:'None', width:300, explicitClose:'...close'});
});

// Fool unRAID by simulating the original input field
function prepareEdit(form) {
  var include = '';
  for (var i=0,item; item=form.shareInclude.options[i]; i++) {
    if (item.selected) {
      if (include.length) include += ',';
      include += item.value;
      item.selected = false;
    }
  }
  item = form.shareInclude.options[0];
  item.value = include;
  item.selected = true;
  var exclude = '';
  for (var i=0,item; item=form.shareExclude.options[i]; i++) {
    if (item.selected) {
      if (exclude.length) exclude += ',';
      exclude += item.value;
      item.selected = false;
    }
  }
  item = form.shareExclude.options[0];
  item.value = exclude;
  item.selected = true;
}
</script>