<?php
include_once '../../../tool/dbTool.inc.php';
function quicksort($origArray,$type) {//快速排序//for最低價、最高價
	if (sizeof($origArray) == 1) { 
		return $origArray;
	}
	else if(sizeof($origArray) == 0){
		return 'null';
	}
	else {
		$left = array();
		$right = array();
		$newArray = array();
		$pivot = array_pop($origArray);
		$length = sizeof($origArray);
		for ($i = 0; $i < $length; $i++) {
			if (floatval($origArray[$i][$type]) <= floatval($pivot[$type])) {
				array_push($left,$origArray[$i]);
			} else {
				array_push($right,$origArray[$i]);
			}
		}
		if(sizeof($left)==0){
			if(sizeof($right)==0){
			}
			else{
				$tempright=quicksort($right,$type);
				$newArray=array_merge(array($pivot),$tempright);
			}
		}
		else{
			$templeft=quicksort($left,$type);
			$n=sizeof($templeft);
			$start=$n;
			$newArray=array_merge($templeft,array($pivot));
			if(sizeof($right)==0){
			}
			else{
				$tempright=quicksort($right,$type);
				$newArray=array_merge($newArray,$tempright);
			}
		}
		return $newArray;
	}
}
$conn=sqlconnect('../../../database/','menu.db','','','','sqlite');
$sql='SELECT * FROM itemsdata WHERE inumber="'.$_POST['item'].'" ORDER BY frontsq ASC';
$item=sqlquery($conn,$sql,'sqlite');
sqlclose($conn,'sqlite');
$initsetting=parse_ini_file('../../../database/initsetting.ini',true);
$itemname=parse_ini_file('../../../database/'.$_POST['story'].'-menu.ini',true);
$taste=parse_ini_file('../../../database/'.$_POST['story'].'-taste.ini',true);
if(file_exists('../../../database/'.$_POST['story'].'-tastegroup.ini')){
	$tastegroupdata=parse_ini_file('../../../database/'.$_POST['story'].'-tastegroup.ini',true);
}
else{
}
$grouptaste=array();
if(isset($item)&&isset($item[0]['inumber'])){
	echo '<form class="itemdata" method="post" action="">';
		if(isset($_POST['itemseq'])){
			echo '<input type="hidden" name="itemseq" value="'.$_POST['itemseq'].'">';
		}
		else{
		}
		echo '<table id="detail" style="width:calc(100% - 10px);margin:5px;border-collapse: collapse;font-size:20px;">';
		if(!is_dir('../../../management/menudata/'.$_POST['story'].'/'.$_POST['dep'].'/itemimg/'.$item[0]['imgfile'])&&$item[0]['imgfile']!=''){//250*250px左右
			echo '<tr>
					<td colspan="2" style="width: 100vw;height: 53.33vw;background-image:url(\'../management/menudata/'.$_POST['story'].'/'.$_POST['dep'].'/itemimg/'.$item[0]['imgfile'].'\');background-size: contain;background-repeat: no-repeat;background-position: 50%;">
					</td>
				</tr>';
		}
		else{
		}
		echo '<tr>
				<td colspan="2" style="padding:5px 0;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: #dcdcdc;">
				<input type="hidden" name="typeno" value="'.$item[0]['fronttype'].'">
				<input type="hidden" name="type" value="">
				<input type="hidden" name="no" value="'.$_POST['item'].'">
				<input type="hidden" name="personcount" value="'.$itemname[$_POST['item']]['personcount'].'">
				<input type="hidden" name="needcharge" value="'.$itemname[$_POST['item']]['charge'].'">
				<input type="hidden" name="name" value="'.$itemname[$_POST['item']]['name1'].'">
				<input type="hidden" name="name2" value="">
				<input type="hidden" name="isgroup" value="'.$item[0]['isgroup'].'">
				<input type="hidden" name="childtype" value="'.$item[0]['childtype'].'">
				<input type="hidden" name="seq" value="'.$item[0]['frontsq'].'">';
			if(isset($itemname[$_POST['item']]['insaleinv'])){
				echo '<input type="hidden" name="insaleinv" value="'.$itemname[$_POST['item']]['insaleinv'].'">';
			}
			else{
				echo '<input type="hidden" name="insaleinv" value="1">';
			}
			echo '<input type="hidden" name="discount" value="0">
				<input type="hidden" name="discontent" value="">
				<input type="hidden" name="dis1" value="'.$itemname[$_POST['item']]['dis1'].'">
				<input type="hidden" name="dis2" value="'.$itemname[$_POST['item']]['dis2'].'">
				<input type="hidden" name="dis3" value="'.$itemname[$_POST['item']]['dis3'].'">
				<input type="hidden" name="dis4" value="'.$itemname[$_POST['item']]['dis4'].'">
				<div style="width:100%;float:left;text-align:center;">'.$itemname[$_POST['item']]['name1'].'<br><span style="font-size:15px;color:#898989;">'.$itemname[$_POST['item']]['introduction1'].'</span></div></td>
			</tr>';
		echo '<tr>
				<td style="width:90px;height:36px;padding:5px;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: #dcdcdc;">訂購數量</td>
				<td style="padding:5px 0;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: #dcdcdc;"><input type="tel" style="width:calc(100% - 114px);height:31px;border:2px;padding:1px 0;float:left;font-size:20px;text-align:right;" name="qty" value="';
			if(isset($_POST['qty'])){
				if($_POST['qty']<=1){
					echo '1';
				}
				else{
					echo $_POST['qty'];
				}
			}
			else{
				echo '1';
			}
			echo '" readonly><div id="diff" style="float:left;width:31px;height:31px;margin:0 12px;cursor: pointer;border-radius:100%;background-color:rgb(26,26,26,0.5);"><img src="./img/diff.png?'.date('YmdHis').'" style="width:31px;height:31px;"></div><div id="plus" style="float:left;width:31px;height:31px;margin:0 12px;cursor: pointer;border-radius:100%;background-color:rgb(26,26,26,0.5);"><img src="./img/plus.png?'.date('YmdHis').'" style="width:31px;height:31px;"></div></td>
			</tr>';
		echo '<tr>
				<td style="padding:5px;height:36px;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: #dcdcdc;">價格</td>
				<td style="padding:5px 0;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: #dcdcdc;">
					<select id="money" style="width:calc(100% - 12px);font-size:20px;border:1px solid #898989;border-radius:5px;padding:5px;';if($itemname[$_POST['item']]['openmoney']=='1')echo 'display:none;';echo '">';
				for($i=1;$i<=$itemname[$_POST['item']]['mnumber'];$i++){
					if($itemname[$_POST['item']]['mname'.$i.'1']==''){
						if(isset($_POST['unitpricelink'])&&isset($_POST['unitprice'])&&$_POST['unitprice']==$itemname[$_POST['item']]['money'.$i]){
							echo '<option value=";'.$itemname[$_POST['item']]['money'.$i].'" selected>'.$itemname[$_POST['item']]['money'.$i].'</option>';
						}
						else{
							echo '<option value=";'.$itemname[$_POST['item']]['money'.$i].'">'.$itemname[$_POST['item']]['money'.$i].'</option>';
						}
					}
					else{
						if(isset($_POST['unitpricelink'])&&isset($_POST['unitprice'])&&($_POST['unitpricelink'].';'.$_POST['unitprice'])==($itemname[$_POST['item']]['mname'.$i.'1'].';'.$itemname[$_POST['item']]['money'.$i])){
							echo '<option value="'.$itemname[$_POST['item']]['mname'.$i.'1'].';'.$itemname[$_POST['item']]['money'.$i].'" selected>'.$itemname[$_POST['item']]['mname'.$i.'1'].'('.$itemname[$_POST['item']]['money'.$i].')</option>';
						}
						else{
							echo '<option value="'.$itemname[$_POST['item']]['mname'.$i.'1'].';'.$itemname[$_POST['item']]['money'.$i].'">'.$itemname[$_POST['item']]['mname'.$i.'1'].'('.$itemname[$_POST['item']]['money'.$i].')</option>';
						}
					}
				}
				echo '</select>
					<input type="tel" id="openmoney" style="width:calc(100% - 120px);border:1px solid #171717;padding:2px 5px;border-radius:5px;text-align:right;font-size:20px;';if($itemname[$_POST['item']]['openmoney']=='1')echo '';else echo 'display:none;';echo '" value="'.$itemname[$_POST['item']]['money1'].'">
				</td>
			</tr>';
		echo '<tr>
				<td style="width:90px;height:36px;padding:5px;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: #dcdcdc;">小計</td>
				<td style="padding:5px 0;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: #dcdcdc;"><input type="number" style="width:calc(100% - 114px);height:31px;border:2px;padding:1px 0;float:left;font-size:20px;text-align:right;" name="amt" value="';
			if(isset($_POST['subtotal'])){
				echo $_POST['subtotal'];
			}
			else{
				echo $itemname[$_POST['item']]['money1'];
			}
			echo '" readonly><!-- <div id="send" style="width:100px;height:36px;float:right;line-height:36px;text-align:center;cursor: pointer;background-color: #C4A5C4;color: #4a4a4a;font-weight:bold;border-radius: 5px;">加入清單</div> --></td>
			</tr>';
		echo '<tr style="height:max-content;">
				<td colspan="2" style="padding:5px 0;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: #dcdcdc;"><div style="width:100%;float:left;text-align:center;">客製選項:<span id="tastemoney">';
			if(isset($_POST['tastemoney'])){
				echo $_POST['tastemoney'];
			}
			else{
				echo '0';
			}
			echo '</span></div></td>
			</tr>';
	$tastelist='';
	if(isset($_POST['tasteno'])&&$_POST['tasteno']!=''){
		$tastenolist=preg_split('/,/',$_POST['tasteno']);
	}
	else{
	}
	if(strlen($item[0]['taste'])>0){
		$tasteset=preg_split('/-/',$item[0]['taste']);
		for($l=0;$l<sizeof($tasteset);$l++){
			if(substr($tasteset[$l],0,2)=='1;'){
				$tastecont=preg_split('/;/',$tasteset[0]);
				if(preg_match('/,/',$tastecont[1])){
					$temp1=preg_split('/,/',$tastecont[1]);
					$temp11=array();
					for($t=0;$t<sizeof($temp1);$t++){
						if(!isset($taste[$temp1[$t]]['seq'])||$taste[$temp1[$t]]['seq']==''){
							$taste[$temp1[$t]]['seq']=1;
						}
						else{
						}
						array_push($temp11,array('index'=>$t,'no'=>$temp1[$t],'seq'=>$taste[$temp1[$t]]['seq']));
					}
					//print_r( $temp11);
					$onlytaste=quicksort($temp11,'seq');

				}
				else{
					$onlytaste=array();
					array_push($onlytaste,array('index'=>'0','no'=>$tastecont[1],'seq'=>$taste[$tastecont[1]]['seq']));
				}
			}
		}
	}
	else{
	}
	$temp11=array();
	foreach($taste as $n=>$t){
		if($t['public']=='1'&&$t['state']=='1'){
			if(!isset($t['seq'])||$t['seq']==''){
				array_push($temp11,array('index'=>$n,'no'=>$n,'seq'=>'1'));
			}
			else{
				array_push($temp11,array('index'=>$n,'no'=>$n,'seq'=>$t['seq']));
			}
		}
		else{
		}
		
	}
	if(isset($temp11)&&sizeof($temp11)>0){
		$publictaste=quicksort($temp11,'seq');
	}
	else{
	}
	if(isset($onlytaste)&&sizeof($onlytaste)>0){
		foreach($onlytaste as $n=>$t){
			if($taste[$t['no']]['state']=='1'){
				if(isset($initsetting['init']['tastegroup'])&&$initsetting['init']['tastegroup']=='1'&&isset($tastegroupdata)&&isset($taste[$t['no']]['group'])&&$taste[$t['no']]['group']!=''){
					$grouptaste[$taste[$t['no']]['group']][]=$t['no'];
				}
				else{
					/*$tastelist=$tastelist.'<tr>
											<td style="padding:5px;height:36px;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: rgba(137, 137, 137, 0.5);">'.$t['name1'];
						if(floatval($t['money'])<=0){
						}
						else{
							$tastelist=$tastelist.'('.$t['money'].')';
						}
						$tastelist=$tastelist.'</td>
											<td style="padding:5px 0;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: rgba(137, 137, 137, 0.5);"><div id="n" style="float:left;height:33px;line-height:33px;color:#4a4a4a;font-weight:bold;">NO</div><label class="switch"><input type="hidden" name="money[]" value="'.$t['money'].'"><input type="hidden" name="tastename[]" value="'.$t['name1'].'"><input name="tasteno[]" value="'.$n.'" type="checkbox"><span class="slider"></span></label><div id="y" style="float:left;height:33px;line-height:33px;color:#898989;font-weight:normal;">YES</div></td>
										</tr>';*/
					$grouptaste['-1'][]=$t['no'];
				}
			}
			else{
			}
		}
	}
	else{
	}
	if(isset($publictaste)&&sizeof($publictaste)>0){
		foreach($publictaste as $n=>$t){
			//if($t['state']=='1'&&$t['public']=='1'){
				if(isset($initsetting['init']['tastegroup'])&&$initsetting['init']['tastegroup']=='1'&&isset($tastegroupdata)&&isset($taste[$t['no']]['group'])&&$taste[$t['no']]['group']!=''){
					$grouptaste[$taste[$t['no']]['group']][]=$t['no'];
				}
				else{
					/*$tastelist=$tastelist.'<tr>
											<td style="padding:5px;height:36px;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: rgba(137, 137, 137, 0.5);">'.$t['name1'];
						if(floatval($t['money'])<=0){
						}
						else{
							$tastelist=$tastelist.'('.$t['money'].')';
						}
						$tastelist=$tastelist.'</td>
											<td style="padding:5px 0;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: rgba(137, 137, 137, 0.5);"><div id="n" style="float:left;height:33px;line-height:33px;color:#4a4a4a;font-weight:bold;">NO</div><label class="switch"><input type="hidden" name="money[]" value="'.$t['money'].'"><input type="hidden" name="tastename[]" value="'.$t['name1'].'"><input name="tasteno[]" value="'.$n.'" type="checkbox"><span class="slider"></span></label><div id="y" style="float:left;height:33px;line-height:33px;color:#898989;font-weight:normal;">YES</div></td>
										</tr>';*/
					$grouptaste['-1'][]=$t['no'];
				}
			/*}
			else{
			}*/
		}
	}
	else{
	}
	if(isset($tastegroupdata)){
		if(sizeof($grouptaste)>0){
			foreach($grouptaste as $n=>$v){
				if($n!='-1'){
					$tastelist=$tastelist.'<tr style="height:max-content;">
											<td colspan="2" style="padding:5px;height:36px;background-color: #f0f0f0;font-size:18px;position:relative;">'.$tastegroupdata[$n]['name'];
					if($tastegroupdata[$n]['pos']=='1'){
						$tastelist=$tastelist.'<p style="font-size:16px;float:right;margin:0;position:absolute;bottom:5px;right:5px;color:#b5b5b5;">單選</p>';
					}
					else if($tastegroupdata[$n]['pos']=='-1'){
						$tastelist=$tastelist.'<p style="font-size:16px;float:right;margin:0;position:absolute;bottom:5px;right:5px;color:#b5b5b5;">不限</p>';
					}
					else{
						$tastelist=$tastelist.'<p style="font-size:16px;float:right;position:absolute;bottom:5px;right:5px;color:#b5b5b5;">請選0-'.$tastegroupdata[$n]['pos'].'項</p>';
					}
					$tastelist=$tastelist.'</td>
										</tr>';
				}
				else{
				}
				foreach($v as $ii=>$vv){
					$tastelist=$tastelist.'<tr>
											<td style="padding:0 5px 0 15px;height:46px;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: #dcdcdc;color:#9b9b9b;font-size:14px;"><div style="margin:5px 0;">'.$taste[$vv]['name1'];
						if(floatval($taste[$vv]['money'])==0){
						}
						else{
							$tastelist=$tastelist.'('.$taste[$vv]['money'].')';
						}
						$tastelist=$tastelist.'</div></td>';

						if(isset($tastenolist)&&in_array($vv,$tastenolist)){
							$tastelist=$tastelist.'<td style="padding:0;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: #dcdcdc;font-size:14px;"><div class="needsclick" id="label" onclick="" style="display:inline-block;overflow:hidden;width:100%;height:46px;"><div style="width:max-content;margin:0 auto;padding:0;"><div id="n" style="float:left;height:46px;line-height:46px;color:#dcdcdc;font-weight:normal;margin-right:2px;">NO</div><div class="switch"><input type="hidden" name="money[]" value="'.$taste[$vv]['money'].'"><input type="hidden" name="tastename[]" value="'.$taste[$vv]['name1'].'"><input name="tasteno[]" value="'.$vv.'" type="checkbox" checked><span class="slider"></span></div><div id="y" style="float:left;height:46px;line-height:46px;color:#4a4a4a;font-weight:bold;margin-left:2px;">YES</div></div></div></td>
											</tr>';
						}
						else{
							$tastelist=$tastelist.'<td style="padding:0;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: #dcdcdc;font-size:14px;"><div class="needsclick" id="label" onclick=""  style="display:inline-block;overflow:hidden;width:100%;height:46px;"><div style="width:max-content;margin:0 auto;padding:0;"><div id="n" style="float:left;height:46px;line-height:46px;color:#4a4a4a;font-weight:bold;margin-right:2px;">NO</div><div class="switch"><input type="hidden" name="money[]" value="'.$taste[$vv]['money'].'"><input type="hidden" name="tastename[]" value="'.$taste[$vv]['name1'].'"><input name="tasteno[]" value="'.$vv.'" type="checkbox"><span class="slider"></span></div><div id="y" style="float:left;height:46px;line-height:46px;color:#dcdcdc;font-weight:normal;margin-left:2px;">YES</div></div></div></td>
											</tr>';
						}
				}
			}
		}
		else{
		}
	}
	else{
		if(isset($grouptaste['-1'])){
			foreach($grouptaste['-1'] as $ii=>$vv){
				$tastelist=$tastelist.'<tr>
										<td style="padding:0 5px 0 15px;height:46px;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: #dcdcdc;color:#9b9b9b;font-size:14px;"><div style="margin:5px 0;">'.$taste[$vv]['name1'];
					if(floatval($taste[$vv]['money'])==0){
					}
					else{
						$tastelist=$tastelist.'('.$taste[$vv]['money'].')';
					}
					$tastelist=$tastelist.'</div></td>';

					if(isset($tastenolist)&&in_array($vv,$tastenolist)){
						$tastelist=$tastelist.'<td style="padding:0;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: #dcdcdc;font-size:14px;"><div class="needsclick" id="label" onclick=""  style="display:inline-block;overflow:hidden;width:100%;height:46px;"><div style="width:max-content;margin:0 auto;padding:0;"><div id="n" style="float:left;height:46px;line-height:46px;color:#dcdcdc;font-weight:normal;margin-right:2px;">NO</div><div class="switch"><input type="hidden" name="money[]" value="'.$taste[$vv]['money'].'"><input type="hidden" name="tastename[]" value="'.$taste[$vv]['name1'].'"><input name="tasteno[]" value="'.$vv.'" type="checkbox" checked><span class="slider"></span></div><div id="y" style="float:left;height:46px;line-height:46px;color:#4a4a4a;font-weight:bold;margin-left:2px;">YES</div></div></div></td>
										</tr>';
					}
					else{
						$tastelist=$tastelist.'<td style="padding:0;border-bottom: 1px;border-bottom-style: solid;border-bottom-color: #dcdcdc;font-size:14px;"><div class="needsclick" id="label" onclick=""  style="display:inline-block;overflow:hidden;width:100%;height:46px;"><div style="width:max-content;margin:0 auto;padding:0;"><div id="n" style="float:left;height:46px;line-height:46px;color:#4a4a4a;font-weight:bold;margin-right:2px;">NO</div><div class="switch"><input type="hidden" name="money[]" value="'.$taste[$vv]['money'].'"><input type="hidden" name="tastename[]" value="'.$taste[$vv]['name1'].'"><input name="tasteno[]" value="'.$vv.'" type="checkbox"><span class="slider"></span></div><div id="y" style="float:left;height:46px;line-height:46px;color:#dcdcdc;font-weight:normal;margin-left:2px;">YES</div></div></div></td>
										</tr>';
					}
			}
		}
		else{
		}
	}
	echo $tastelist;
	echo '</table>
			<div style="width:160px;margin:10px auto 25px auto;overflow:hidden;text-align:center;">
				<div id="diff" style="float:left;width:31px;height:31px;margin:0 12px;cursor: pointer;border-radius:100%;background-color:rgb(26,26,26,0.5);"><img src="./img/diff.png?'.date('YmdHis').'" style="width:31px;height:31px;"></div><input type="number" style="width:calc(100% - 110px);height:31px;border:2px;padding:1px 0;float:left;font-size:20px;text-align:center;" name="qty" value="';
			if(isset($_POST['qty'])){
				if($_POST['qty']<=1){
					echo '1';
				}
				else{
					echo $_POST['qty'];
				}
			}
			else{
				echo '1';
			}
			echo '" readonly><div id="plus" style="float:left;width:31px;height:31px;margin:0 12px;cursor: pointer;border-radius:100%;background-color:rgb(26,26,26,0.5);"><img src="./img/plus.png?'.date('YmdHis').'" style="width:31px;height:31px;"></div>
			</div>
			<div class="pagetop" style="width:40px;height:40px;line-height:40px;cursor: pointer;text-align:center;display:none;position:fixed;bottom:80px;right:15px;border-radius:100%;background-color: rgb(26,26,26,0.5);color: #ffffff;"><u>Top</u></div>
		</form>';
}
else{
}
?>