<?php
    /**
     * Rune filter view.
     *
     * To be included from the rune view.
     *
     * @category View
     * @property_read mixed[] $filters Selected filters.
     * @property_read int $page Player ID.
     */

    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 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_ID 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_ID 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_ID 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_ID::NORMAL?>' <?=$selected[0]?>>Normal</option>
                    <option value='<?=QUALITY_ID::MAGIC?>' <?=$selected[1]?>>Magic</option>
                    <option value='<?=QUALITY_ID::RARE?>' <?=$selected[2]?>>Rare</option>
                    <option value='<?=QUALITY_ID::HERO?>' <?=$selected[3]?>>Hero</option>
                    <option value='<?=QUALITY_ID::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_ID::NORMAL?>' <?=$selected[0]?>>Normal</option>
                    <option value='<?=QUALITY_ID::MAGIC?>' <?=$selected[1]?>>Magic</option>
                    <option value='<?=QUALITY_ID::RARE?>' <?=$selected[2]?>>Rare</option>
                    <option value='<?=QUALITY_ID::HERO?>' <?=$selected[3]?>>Hero</option>
                    <option value='<?=QUALITY_ID::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_ID::NORMAL?>' <?=$selected[0]?>>Normal</option>
                    <option value='<?=QUALITY_ID::MAGIC?>' <?=$selected[1]?>>Magic</option>
                    <option value='<?=QUALITY_ID::RARE?>' <?=$selected[2]?>>Rare</option>
                    <option value='<?=QUALITY_ID::HERO?>' <?=$selected[3]?>>Hero</option>
                    <option value='<?=QUALITY_ID::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_ID::NORMAL?>' <?=$selected[0]?>>Normal</option>
                    <option value='<?=QUALITY_ID::MAGIC?>' <?=$selected[1]?>>Magic</option>
                    <option value='<?=QUALITY_ID::RARE?>' <?=$selected[2]?>>Rare</option>
                    <option value='<?=QUALITY_ID::HERO?>' <?=$selected[3]?>>Hero</option>
                    <option value='<?=QUALITY_ID::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>

