<?php if (!defined('FLUX_ROOT')) exit; ?>
<h2>Items</h2>
<p class="toggler"><a href="javascript:toggleSearchForm()">Search...</a></p>
<form class="search-form" method="get">
	<?php echo $this->moduleActionFormInputs($params->get('module')) ?>
	<p>
		<label for="item_id">Item ID:</label>
		<input type="text" name="item_id" id="item_id" value="<?php echo htmlspecialchars($params->get('item_id')) ?>" />
		...
		<label for="name">Name:</label>
		<input type="text" name="name" id="name" value="<?php echo htmlspecialchars($params->get('name')) ?>" />
		...
		<label for="type">Type:</label>
		<select name="type">
			<option value="-1"<?php if (($type=$params->get('type')) === '-1') echo ' selected="selected"' ?>>
				Any
			</option>
			<?php foreach (Flux::config('ItemTypes')->toArray() as $typeId => $typeName): ?>
				<option value="<?php echo $typeId ?>"<?php if (($type=$params->get('type')) === strval($typeId)) echo ' selected="selected"' ?>>
					<?php echo htmlspecialchars($typeName) ?>
				</option>
				<?php $itemTypes2 = Flux::config('ItemTypes2')->toArray() ?>
				<?php if (array_key_exists($typeId, $itemTypes2)): ?>
					<?php foreach ($itemTypes2[$typeId] as $typeId2 => $typeName2): ?>
					<option value="<?php echo $typeId ?>-<?php echo $typeId2 ?>"<?php if (($type=$params->get('type')) === ($typeId . '-' . $typeId2)) echo ' selected="selected"' ?>>
						<?php echo htmlspecialchars($typeName . ' - ' . $typeName2) ?>
					</option>
					<?php endforeach ?>
				<?php endif ?>
			<?php endforeach ?>
		</select>
		...
		<label for="equip_loc">Equip Locations:</label>
		<select name="equip_loc">
			<option value="-1"<?php if (($equip_loc=$params->get('equip_loc')) === '-1') echo ' selected="selected"' ?>>
				Any
			</option>
			<?php foreach (Flux::config('EquipLocationCombinations')->toArray() as $locId => $locName): ?>
				<option value="<?php echo $locId ?>"<?php if (($equip_loc=$params->get('equip_loc')) === strval($locId)) echo ' selected="selected"' ?>>
					<?php echo htmlspecialchars($locName) ?>
				</option>
			<?php endforeach ?>
		</select>
	</p>
	<p>
		<label for="npc_buy">NPC Buy:</label>
		<select name="npc_buy_op">
			<option value="eq"<?php if (($npc_buy_op=$params->get('npc_buy_op')) == 'eq') echo ' selected="selected"' ?>>is equal to</option>
			<option value="gt"<?php if ($npc_buy_op == 'gt') echo ' selected="selected"' ?>>is greater than</option>
			<option value="lt"<?php if ($npc_buy_op == 'lt') echo ' selected="selected"' ?>>is less than</option>
		</select>
		<input type="text" name="npc_buy" id="npc_buy" value="<?php echo htmlspecialchars($params->get('npc_buy')) ?>" />
		...
		<label for="npc_sell">NPC Sell:</label>
		<select name="npc_sell_op">
			<option value="eq"<?php if (($npc_sell_op=$params->get('npc_sell_op')) == 'eq') echo ' selected="selected"' ?>>is equal to</option>
			<option value="gt"<?php if ($npc_sell_op == 'gt') echo ' selected="selected"' ?>>is greater than</option>
			<option value="lt"<?php if ($npc_sell_op == 'lt') echo ' selected="selected"' ?>>is less than</option>
		</select>
		<input type="text" name="npc_sell" id="npc_sell" value="<?php echo htmlspecialchars($params->get('npc_sell')) ?>" />
		...
		<label for="weight">Weight:</label>
		<select name="weight_op">
			<option value="eq"<?php if (($weight_op=$params->get('weight_op')) == 'eq') echo ' selected="selected"' ?>>is equal to</option>
			<option value="gt"<?php if ($weight_op == 'gt') echo ' selected="selected"' ?>>is greater than</option>
			<option value="lt"<?php if ($weight_op == 'lt') echo ' selected="selected"' ?>>is less than</option>
		</select>
		<input type="text" name="weight" id="weight" value="<?php echo htmlspecialchars($params->get('weight')) ?>" />
	</p>
	<p>
		<label for="range">Range:</label>
		<select name="range_op">
			<option value="eq"<?php if (($range_op=$params->get('range_op')) == 'eq') echo ' selected="selected"' ?>>is equal to</option>
			<option value="gt"<?php if ($range_op == 'gt') echo ' selected="selected"' ?>>is greater than</option>
			<option value="lt"<?php if ($range_op == 'lt') echo ' selected="selected"' ?>>is less than</option>
		</select>
		<input type="text" name="range" id="range" value="<?php echo htmlspecialchars($params->get('range')) ?>" />
		...
		<label for="slots">Slots:</label>
		<select name="slots_op">
			<option value="eq"<?php if (($slots_op=$params->get('slots_op')) == 'eq') echo ' selected="selected"' ?>>is equal to</option>
			<option value="gt"<?php if ($slots_op == 'gt') echo ' selected="selected"' ?>>is greater than</option>
			<option value="lt"<?php if ($slots_op == 'lt') echo ' selected="selected"' ?>>is less than</option>
		</select>
		<input type="text" name="slots" id="slots" value="<?php echo htmlspecialchars($params->get('slots')) ?>" />
		...
		<label for="defense">Defense:</label>
		<select name="defense_op">
			<option value="eq"<?php if (($defense_op=$params->get('defense_op')) == 'eq') echo ' selected="selected"' ?>>is equal to</option>
			<option value="gt"<?php if ($defense_op == 'gt') echo ' selected="selected"' ?>>is greater than</option>
			<option value="lt"<?php if ($defense_op == 'lt') echo ' selected="selected"' ?>>is less than</option>
		</select>
		<input type="text" name="defense" id="defense" value="<?php echo htmlspecialchars($params->get('defense')) ?>" />
	</p>
	<p>
		<label for="atk">ATK:</label>
		<select name="atk_op">
			<option value="eq"<?php if (($atk_op=$params->get('atk_op')) == 'eq') echo ' selected="selected"' ?>>is equal to</option>
			<option value="gt"<?php if ($atk_op == 'gt') echo ' selected="selected"' ?>>is greater than</option>
			<option value="lt"<?php if ($atk_op == 'lt') echo ' selected="selected"' ?>>is less than</option>
		</select>
		<input type="text" name="atk" id="atk" value="<?php echo htmlspecialchars($params->get('atk')) ?>" />
		...
		<label for="matk">MATK:</label>
		<select name="matk_op">
			<option value="eq"<?php if (($matk_op=$params->get('matk_op')) == 'eq') echo ' selected="selected"' ?>>is equal to</option>
			<option value="gt"<?php if ($matk_op == 'gt') echo ' selected="selected"' ?>>is greater than</option>
			<option value="lt"<?php if ($matk_op == 'lt') echo ' selected="selected"' ?>>is less than</option>
		</select>
		<input type="text" name="matk" id="matk" value="<?php echo htmlspecialchars($params->get('matk')) ?>" />
		...
		<label for="refineable">Refineable:</label>
		<select name="refineable" id="refineable">
			<option value=""<?php if (!($refineable=$params->get('refineable'))) echo ' selected="selected"' ?>>All</option>
			<option value="yes"<?php if ($refineable == 'yes') echo ' selected="selected"' ?>>Yes</option>
			<option value="no"<?php if ($refineable == 'no') echo ' selected="selected"' ?>>No</option>
		</select>
		...
		<label for="for_sale">For Sale:</label>
		<select name="for_sale" id="for_sale">
			<option value=""<?php if (!($for_sale=$params->get('for_sale'))) echo ' selected="selected"' ?>>All</option>
			<option value="yes"<?php if ($for_sale == 'yes') echo ' selected="selected"' ?>>Yes</option>
			<option value="no"<?php if ($for_sale == 'no') echo ' selected="selected"' ?>>No</option>
		</select>
		...
		<label for="custom">Custom:</label>
		<select name="custom" id="custom">
			<option value=""<?php if (!($custom=$params->get('custom'))) echo ' selected="selected"' ?>>All</option>
			<option value="yes"<?php if ($custom == 'yes') echo ' selected="selected"' ?>>Yes</option>
			<option value="no"<?php if ($custom == 'no') echo ' selected="selected"' ?>>No</option>
		</select>
		...
		<input type="submit" value="Search" />
		<input type="button" value="Reset" onclick="reload()" />
	</p>
</form>
<?php if ($items): ?>
	<?php foreach ($items as $item): ?>
		<?php $icon = $this->iconImage($item->item_id); ?>
		<table class = 'horizontal-table' border = 1 cellpadding='3' width = '100%'>
		  <tr >
			<td colspan=10 >

				<p><strong>
					<?php if ($icon): ?><img src="<?php echo $icon ?>" /><?php endif ?>
						<?php if ($auth->actionAllowed('item', 'view')): ?>
							#<?php echo $this->linkToItem($item->item_id, $item->item_id) ?>: <?php echo htmlspecialchars($item->name) ?>
						<?php else: ?>
							#<?php echo htmlspecialchars($item->item_id) ?>: <?php echo htmlspecialchars($item->name) ?>
						<?php endif ?>
					<?php if (($item->slots) > 0) : ?>
						[<?php echo number_format((int)$item->slots) ?>]
					<?php endif ?>
							(<?php echo htmlspecialchars($item->identifier) ?>)
				</strong></p>
		  </td>
		 </tr>
		 <tr>
		  <td>Type</td>
		  <td><?php echo $this->itemTypeText($item->type) ?></td>
		  <td>Class</td>
		  <td>
			<?php echo $this->itemTypeText($item->type, $item->view_sprite) ?> 
		  </td>
		  <td>Buy</td>
		  <td><?php echo number_format((int)$item->price_buy) ?></td>
		  <td>Sell</td>
		  <td>
					<?php if (is_null($item->price_sell) && $item->price_buy): ?>
						<?php echo number_format(floor($item->price_buy / 2)) ?>
					<?php else: ?>
						<?php echo number_format((int)$item->price_sell) ?>
					<?php endif ?>

		  </td>
		  <td>Weight</td>
		  <td><?php echo round($item->weight, 1) ?></td>
		 </tr>

		<tr>
		 <br>
			<?php if (($item->atk) > 0) : ?>
				<td>Atk</td>
				<td><?php echo number_format((int)$item->atk) ?></td>
			<?php else: ?>

				<td>Defense</td>
				<td><?php echo number_format((int)$item->defence) ?></td>
			<?php endif ?>

		  <td>Req Lvl</td>
		  <td><?php echo number_format((int)$item->equip_level_min) ?></td>
		  <td>Refinable</td>
		  <td>
					<?php if ($item->refineable): ?>
						Yes
					<?php else: ?>
						No
					<?php endif ?>

		  </td>
		  <td>Slot</td>
		  <td><?php echo number_format((int)$item->slots) ?></td>
		  <td></td>
		  <td></td>
		 </tr>
		 <tr>
		  <td>Applicable Jobs</td>
		  <td colspan=9>
					<?php if ($jobs=$this->equippableJobs($item->equip_jobs)): ?>
						<?php echo htmlspecialchars(implode(' / ', $jobs)) ?>
					<?php else: ?>
						<span class="not-applicable">None</span>
					<?php endif ?>
		 </td>
		 </tr>

		<?php
		/* DESCRIPTION */
		$sql = 'select itemid, itemdesc from cp_itemdesc where itemid = '.$item->item_id;
		$sth = $server->connection->getStatement($sql);
		$sth->execute();
		$temDescriptions = $sth->fetchAll();
		?>
		<?php if ($temDescriptions): ?>
			<?php foreach ($temDescriptions as $temDescription): ?>
		<tr>
			<td>Item Description</td>
			<td colspan=9><?php echo $temDescription->itemdesc ?></td>
		</tr>
			<?php endforeach ?>
		<?php endif ?>

		<?php if ($locs=$this->equipLocations($item->equip_locations)): ?>

		 <tr>
			<td>Location</td>
			<td colspan=9>
			<?php echo htmlspecialchars(implode(' + ', $locs)) ?>
			</td>
		 </tr>
		<?php endif ?>
		<?php if ($item->script): ?>	
			<tr>
				<td>Item Use Script</td>
				<td colspan=9>
					<?php if ($script=$this->displayScript($item->script)): ?>
						<?php echo $script ?>
					<?php else: ?>
						<span class="not-applicable">None</span>
					<?php endif ?>
				</td>
			</tr>
		<?php endif ?>	
		<?php if ($item->equip_script): ?>	
			<?php if ($script=$this->displayScript($item->equip_script)): ?>
			<tr>
				<td>Equip Script</td>
				<td colspan=9>
					<?php if ($script=$this->displayScript($item->equip_script)): ?>
						<?php echo $script ?>
					<?php else: ?>
						<span class="not-applicable">None</span>
					<?php endif ?>
				</td>
			</tr>
			<?php endif ?>
		<?php endif ?>
		<?php if ($item->unequip_script): ?>
			<?php if ($script=$this->displayScript($item->unequip_script)): ?>
			<tr>
				<td>Unequip Script</td>
				<td colspan=9>
					<?php if ($script=$this->displayScript($item->unequip_script)): ?>
						<?php echo $script ?>
					<?php else: ?>
						<span class="not-applicable">None</span>
					<?php endif ?>
				</td>
			</tr>
			<?php endif ?>
		<?php endif ?>


	<?php 
	$mobDB  = "{$server->charMapDatabase}.mob_db";
	$col  = 'ID AS monster_id, iName AS monster_name, LV AS monster_level, ';
	$col .= 'Race AS monster_race, (Element%10) AS monster_element, (Element/20) AS monster_ele_lv, MEXP AS mvp_exp, ';
	// Normal drops.
	$col .= 'Drop1id AS drop1_id, Drop1per AS drop1_chance, ';
	$col .= 'Drop2id AS drop2_id, Drop2per AS drop2_chance, ';
	$col .= 'Drop3id AS drop3_id, Drop3per AS drop3_chance, ';
	$col .= 'Drop4id AS drop4_id, Drop4per AS drop4_chance, ';
	$col .= 'Drop5id AS drop5_id, Drop5per AS drop5_chance, ';
	$col .= 'Drop6id AS drop6_id, Drop6per AS drop6_chance, ';
	$col .= 'Drop7id AS drop7_id, Drop7per AS drop7_chance, ';
	$col .= 'Drop8id AS drop8_id, Drop8per AS drop8_chance, ';
	$col .= 'Drop9id AS drop9_id, Drop9per AS drop9_chance, ';
	
	// Card drops.
	$col .= 'DropCardid AS dropcard_id, DropCardper AS dropcard_chance, ';
	
	// MVP rewards.
	$col .= 'MVP1id AS mvpdrop1_id, MVP1per AS mvpdrop1_chance, ';
	$col .= 'MVP2id AS mvpdrop2_id, MVP2per AS mvpdrop2_chance, ';
	$col .= 'MVP3id AS mvpdrop3_id, MVP3per AS mvpdrop3_chance';
	
	$sql  = "SELECT $col FROM $mobDB WHERE ";
	
	// Normal drops.
	$sql .= 'Drop1id = '.$item->item_id.' OR ';
	$sql .= 'Drop2id = '.$item->item_id.' OR ';
	$sql .= 'Drop3id = '.$item->item_id.' OR ';
	$sql .= 'Drop4id = '.$item->item_id.' OR ';
	$sql .= 'Drop5id = '.$item->item_id.' OR ';
	$sql .= 'Drop6id = '.$item->item_id.' OR ';
	$sql .= 'Drop7id = '.$item->item_id.' OR ';
	$sql .= 'Drop8id = '.$item->item_id.' OR ';
	$sql .= 'Drop9id = '.$item->item_id.' OR ';
	
	// Card drops.
	$sql .= 'DropCardid = '.$item->item_id.' OR ';
	
	// MVP rewards.
	$sql .= 'MVP1id = '.$item->item_id.' OR ';
	$sql .= 'MVP2id = '.$item->item_id.' OR ';
	$sql .= 'MVP3id = '.$item->item_id;

	$sth  = $server->connection->getStatement($sql);
	$res = $sth->execute(array_fill(0, 13, $item->item_id));
	
	$dropResults = $sth->fetchAll();

	$itemDrops   = array();
	$dropNames   = array(
		'drop1', 'drop2', 'drop3', 'drop4', 'drop5', 'drop6', 'drop7', 'drop8', 'drop9',
		'dropcard', 'mvpdrop1', 'mvpdrop2', 'mvpdrop3'
	);
	

	
	foreach ($dropResults as $drop) {
		foreach ($dropNames as $dropName) {
			$dropID     = $drop->{$dropName.'_id'};
			$dropChance = $drop->{$dropName.'_chance'};
			
			if ($dropID == $item->item_id) {
				$dropArray = array(
					'monster_id'      => $drop->monster_id,
					'monster_name'    => $drop->monster_name,
					'drop_chance'     => $dropChance
				);
				
				if (preg_match('/^dropcard/', $dropName)) {
					$adjust = ($drop->mvp_exp) ? $server->dropRates['CardBoss'] : $server->dropRates['Card'];
					$dropArray['type'] = 'card';
				}
				elseif (preg_match('/^mvp/', $dropName)) {
					$adjust = $server->dropRates['MvpItem'];
					$dropArray['type'] = 'mvp';
				}
				elseif (preg_match('/^drop/', $dropName)) {
					switch($item->type) {
						case 0: // Healing
							$adjust = ($drop->mvp_exp) ? $server->dropRates['HealBoss'] : $server->dropRates['Heal'];
							break;
						
						case 2: // Useable
						case 18: // Cash Useable
							$adjust = ($drop->mvp_exp) ? $server->dropRates['UseableBoss'] : $server->dropRates['Useable'];
							break;
						
						case 4: // Weapon
						case 5: // Armor
						case 8: // Pet Armor
							$adjust = ($drop->mvp_exp) ? $server->dropRates['EquipBoss'] : $server->dropRates['Equip'];
							break;
						
						default: // Common
							$adjust = ($drop->mvp_exp) ? $server->dropRates['CommonBoss'] : $server->dropRates['Common'];
							break;
					}
					
					$dropArray['type'] = 'normal';
				}
				
				$dropArray['drop_chance'] = $dropArray['drop_chance'] * $adjust / 10000;

				if ($dropArray['drop_chance'] > 100) {
					$dropArray['drop_chance'] = 100;
				}
				$itemDrops[] = $dropArray;
			}
		}
	}
	// Sort so that monsters are ordered by drop chance and name.
	//usort($itemDrops, '__tmpSortDrops');	
	?>

		<?php if ($itemDrops): ?>
		 <tr>
		  <td>Drop By</td>
		  <td colspan=9>
			<?php foreach ($itemDrops as $itemDrop): ?>
				<?php if ($auth->actionAllowed('monster', 'view_sprite')): ?>
						<?php echo $this->linkToMonster($itemDrop['monster_id'], $itemDrop['monster_name']) ?>
					<?php else: ?>
						<?php echo htmlspecialchars($itemDrop['monster_name']) ?>
					<?php endif ?>

					<?php if ($itemDrop['type'] == 'mvp'): ?>
						<span class="mvp">MVP!</span>
					<?php endif ?>
					
				
					(<strong><?php echo $itemDrop['drop_chance'] ?>%</strong>)
			<?php endforeach ?>

		  </td>
		 </tr>
		<?php endif ?>
		
		<?php
		/* OBTAINED FROM */

		try {
			$sql = 'select g.id, g.nameid, rate, i.name_japanese from item_group g left join item_db i on g.id=i.id where nameid = '.$item->item_id;
			$sth = $server->connection->getStatement($sql);
			$sth->execute(array($itemID));
			if((int)$sth->stmt->errorCode()){
				throw new Flux_Error('db not found');
			}		
			$itemobtains = $sth->fetchAll();
		} catch(Exception $e){
			$itemobtains = false;
		}
		?>
		<?php if ($itemobtains): ?>
		 <tr>
		  <td>Obtained from</td>
		  <td colspan=9>
			<?php foreach ($itemobtains as $itemobtain): ?>
				<?php if ($icon=$this->iconImage($itemobtain->id)) ?>
					<img src="<?php echo htmlspecialchars($icon); ?>" />
					<?php if (($itemobtain->id)==732): ?>
						<a href="/?module=quest_db&action=40007">Comodo Gambling</a>
					<?php else: ?>
						<?php echo $this->linkToItem($itemobtain->id,($itemobtain->name_japanese))?>
						(<strong><?php echo $itemobtain->rate ?>%</strong>)


						
					<?php endif ?>	
			<?php endforeach ?>

		  </td>
		 </tr>
		<?php endif ?>
		</table>
	<?php endforeach ?>
	<?php echo $paginator->getHTML() ?>
<?php else: ?>
	<p>No items found. <a href="javascript:history.go(-1)">Go back</a>.</p>
<?php endif ?>


