<?php
    global $RUNE_SET;
    global $RUNE_STAT;
    global $QUALITY;
?>
<?php
    if (count($custom_filters) > 0){
?>
        <script>
            /**
             * Changes the filter description.
             *
             * @param id Filter id.
             */
            function customFilterDescription(id){
                switch (id){
<?php
                    foreach($custom_filters AS $filter){
?>
                        case '<?=$filter->id?>':
                            document.getElementById("custom_filter_description").style.display = 'block';
                            document.getElementById("custom_filter_description").innerHTML = '<?=$filter->description?>';
                            break;
<?php
                    }
?>
                }
            }
        </script>
        <form action='/<?=$UID?>/runes/' method='get' id='custom_filter_runes' class='custom_filter'>
            <span class='custom_filter_title'>Custom filters:</span>
            <select name='custom_filter' onChange='customFilterDescription(this.value);' onload='alert("LOAD");customFilterDescription("<?=$_GET["custom_filter"]?>");'>
                <option> </option>
<?php
                 foreach($custom_filters AS $filter){
                    if ($_GET["custom_filter"] == $filter->id){
                        $selected = "selected='selected'";
                    }
                    else{
                        $selected = "";
                    }
?>
                    <option value='<?=$filter->id?>' <?=$selected?>><?=$filter->name?></option>
<?php
                 }
?>
            </select>
            <input type='submit' value='Apply'/>
            <p id='custom_filter_description'> </p>
            <script>
                customFilterDescription("<?=$_GET["custom_filter"]?>");
            </script>
        </form>
<?php
    }
?>
</form>
<form action='/<?=$UID?>/runes/' method='get' id='filter_runes' class='filter'>
    <div class='custom_filters'>
    <table>
        <tr>
            <td class='filter' rowspan='3'>
                <span>Type:</span>
                <select multiple name='type[]' id='filter_type'>
<?php
                    foreach($RUNE_SET as $k=>$n){
                        $selected = "";
                        if (in_array($n, $filters["type"])){
                            $selected = "selected='selected'";
                        }
                        $name = ucwords(strtolower($k));
?>
                        <option value='<?=$n?>' <?=$selected?>><?=$name?></option>
<?php
                    }
?>
                </select>
            </td>
            <td class='filter' rowspan='2'>
                <span>Slot:</span>
                <select multiple name='slot[]' id='filter_slot'>
<?php
                    for ($i = 1; $i <= 6; $i ++){
                        if (in_array($i, $filters["slot"])){
                            $selected = "selected='selected'";
                        }
                        else{
                            $selected = "";
                        }
?>
                        <option value='<?=$i?>' <?=$selected?>><?=$i?></option>
<?php
                    }
?>
                </select>
            </td>
            <td class='filter filter_stars' >
                <span>Stars:</span>
                <input type='number' name='min_stars' id='filter_stars_min' min='1' max='6' value='<?=$filters["min_stars"]?>'/>
                -
                <input type='number' name='max_stars' id='filter_stars_max' min='1' max='6' value='<?=$filters["max_stars"]?>'/>
            </td>
        </tr>
        <tr>
            <td class='filter'>
                <span>Level:</span>
                <input type='number' name='min_level' id='filter_level_min' min='0' max='15' value='<?=$filters["min_level"]?>'/>
                -
                <input type='number' name='max_level' id='filter_level_max' min='0' max='15' value='<?=$filters["max_level"]?>'/>
            </td>
        </tr>
        <tr>
            <td class='filter'>
                <span>Main stat:</span>
                <select multiple name='main_stat[]' id='filter_main_stat'>
<?php
                    foreach($RUNE_STAT as $k=>$n){
                        $selected = "";
                        if (in_array($n, $filters["main"])){
                            $selected = "selected='selected'";
                        }
                        $name = ucwords($k);
?>
                        <option value='<?=$n?>' <?=$selected?>><?=$name?></option>
<?php
                    }
?>
                </select>
            </td>
            <td class='filter'>
                <span>Has sub stat:</span>
                <select multiple name='sub_stat[]' id='filter_sub_stat'>
<?php
                    foreach($RUNE_STAT as $k=>$n){
                        $selected = "";
                        if (in_array($n, $filters["sub"])){
                            $selected = "selected='selected'";
                        }
                        $name = ucwords($k);
?>
                        <option value='<?=$n?>' <?=$selected?>><?=$name?></option>
<?php
                    }
?>
                </select>
            </td>
        </tr>
        <tr>
            <td class='filter'>
                <span>
                    Assigned:
                </span>
<?php
                $selected = Array("", "", "", "", "");
                $selected[$filters["assigned"]] = "selected";
?>
                <select name='assigned' id='filter_assigned'>
                    <option value='0' <?=$selected[0]?>>All</option>
                    <option value='1' <?=$selected[1]?>>Assigned</option>
                    <option value='2' <?=$selected[2]?>>Unassigned</option>
                    <option value='3' <?=$selected[3]?>>All (no storage)</option>
                    <option value='4' <?=$selected[4]?>>Assigned (no storage)</option>
                </select>
            </td>
            <td class='filter' colspan='2'>
                <span>Quality:</span>
<?php
                $selected = Array("", "", "", "", "");
                $selected[$filters["min_quality"] - 1] = "selected";
?>
                <select name='min_quality' id='filter_quality_min'>
                    <option value='<?=$QUALITY["NORMAL"]?>' <?=$selected[0]?>>Normal</option>
                    <option value='<?=$QUALITY["MAGIC"]?>' <?=$selected[1]?>>Magic</option>
                    <option value='<?=$QUALITY["RARE"]?>' <?=$selected[2]?>>Rare</option>
                    <option value='<?=$QUALITY["HERO"]?>' <?=$selected[3]?>>Hero</option>
                    <option value='<?=$QUALITY["LEGEND"]?>' <?=$selected[4]?>>Legend</option>
                </select>
                -
<?php
                $selected = Array("", "", "", "", "");
                $selected[$filters["max_quality"] - 1] = "selected";
?>
                <select name='max_quality' id='filter_quality_max'>
                    <option value='<?=$QUALITY["NORMAL"]?>' <?=$selected[0]?>>Normal</option>
                    <option value='<?=$QUALITY["MAGIC"]?>' <?=$selected[1]?>>Magic</option>
                    <option value='<?=$QUALITY["RARE"]?>' <?=$selected[2]?>>Rare</option>
                    <option value='<?=$QUALITY["HERO"]?>' <?=$selected[3]?>>Hero</option>
                    <option value='<?=$QUALITY["LEGEND"]?>' <?=$selected[4]?>>Legend</option>
                </select>
            </td>
        </tr>
        <tr>
            <td class='filter'>
            </td>
            <td class='filter' colspan='2'>
                <span>Original quality:</span>
<?php
                $selected = Array("", "", "", "", "");
                $selected[$filters["min_original_quality"] - 1] = "selected";
?>
                <select name='min_original_quality' id='filter_original_quality_min'>
                    <option value='<?=$QUALITY["NORMAL"]?>' <?=$selected[0]?>>Normal</option>
                    <option value='<?=$QUALITY["MAGIC"]?>' <?=$selected[1]?>>Magic</option>
                    <option value='<?=$QUALITY["RARE"]?>' <?=$selected[2]?>>Rare</option>
                    <option value='<?=$QUALITY["HERO"]?>' <?=$selected[3]?>>Hero</option>
                    <option value='<?=$QUALITY["LEGEND"]?>' <?=$selected[4]?>>Legend</option>
                </select>
                -
<?php
                $selected = Array("", "", "", "", "");
                $selected[$filters["max_original_quality"] - 1] = "selected";
?>
                <select name='max_original_quality' id='filter_original_quality_max'>
                    <option value='<?=$QUALITY["NORMAL"]?>' <?=$selected[0]?>>Normal</option>
                    <option value='<?=$QUALITY["MAGIC"]?>' <?=$selected[1]?>>Magic</option>
                    <option value='<?=$QUALITY["RARE"]?>' <?=$selected[2]?>>Rare</option>
                    <option value='<?=$QUALITY["HERO"]?>' <?=$selected[3]?>>Hero</option>
                    <option value='<?=$QUALITY["LEGEND"]?>' <?=$selected[4]?>>Legend</option>
                </select>
            </td>
        </tr>
        <tr>
            <td class='filter'>
            </td>
            <td class='filter' colspan='2'>
                <span>Efficiency:</span>
                <input type='number' name='min_efficiency' id='filter_efficiency_min' min='0' max='100' value='<?=$filters["min_efficiency"]?>'/>%
                -
                <input type='number' name='max_efficiency' id='filter_efficiency_max' min='0' max='100' value='<?=$filters["max_efficiency"]?>'/>%
            </td>
        </tr>
        <tr>
            <td class='filter'>
                <span>Group by:</span>
<?php
                $selected = Array("", "");
                $selected[$filters["group"]] = "selected";
?>
                <select name='group' id='filter_group'>
                    <option value='0' <?=$selected[0]?>>Slot</option>
                    <option value='1' <?=$selected[1]?>>Type</option>
                </select>
            </td>
            <td class='filter' colspan='2'>
                <span>Maximum efficiency:</span>
                <input type='number' name='min_max_efficiency' id='filter_max_efficiency_min' min='0' max='100' value='<?=$filters["min_max_efficiency"]?>'/>%
                -
                <input type='number' name='max_max_efficiency' id='filter_max_efficiency_max' min='0' max='100' value='<?=$filters["max_max_efficiency"]?>'/>%
            </td>
        </tr>
    </table>
    <div id='filter_apply'>
        <input type='submit' value='Apply'/>
    </div>
</form>
