<%@page import="bbs.Bbs"%>
<%@page import="java.util.ArrayList"%>
<%@page import="cart.Cart"%>
<%@page import="bbs.BbsDAO"%>
<%@page import="user.User"%>
<%@page import="user.UserDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>TKmall</title>
<style>
        /* 스타일링을 위한 CSS 코드 */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        header {
            background-color: white;
            color: white;
            padding: 7px;
            text-align: right;
        }
        header a {
            color: #000;
            text-decoration: none;
            margin-left: 8px;
            font-size: 10px;
        }
    </style>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
</head>
<body>
	<% 
		String ID = null;
		if (session.getAttribute("ID") != null) {
			ID = (String) session.getAttribute("ID");
		}
		
		int USER_TYPE = 1;
		UserDAO u = new UserDAO();
		USER_TYPE = u.fmanager(ID);
		
		String NAME = null;
		NAME = u.yourname(ID);
		
		User user = new User();
		int x= u.yournum(ID);
		user.setUser_no(x);
		System.out.println(user.getUser_no());
		if (x == -1){ %>
			PrintWriter script = response.getWriter();
			script.println("<script>");
			script.println("alert('올바르지 못한 접근.')");
			script.println("location.href = 'main.jsp'");
			script.println("</script>");
		<%} %>
	<!-- 위의 헤더 -->
	<header>
	    <!-- 로그인 여부에 따라 다르게 표시 -->
	    <%	if(ID == null) { %>
	        <a href="login.jsp">로그인</a>
	        <a href="join.jsp">회원가입</a>
	        <a href="cart.jsp">장바구니</a>
	        
	     <% }else if(USER_TYPE == 1) { %>
	     	<a href="main.jsp"><%=NAME %>님 어서오세요</a>
	        <a href="logoutAction.jsp">로그아웃</a>
	        <a href="mypage.jsp">마이페이지</a>
	        <a href="cart.jsp">장바구니</a>
	     <%} else { %>
	    
	    <a href="main.jsp">관리자</a>
	    <a href="logoutAction.jsp">로그아웃</a>
	    <a href="site.jsp">사이트 관리</a>
	    <%} %>
	    &nbsp;&nbsp;&nbsp;
	</header>
	
	<!-- 로고와 검색창 -->
	<div class="container">
    	<div class="row align-items-center flex-row">
    	<!-- align-items-center은 로고와 검색창 위아래정렬 flex-row는 검색 단어 가로쓰기 변경-->
        	<div class="col-md-2">
            	<!-- 상위구획 1 : 로고 -->
            	<button type="button" class="btn" style="background-color: transparent; border: none;" onclick="location.href='main.jsp'">
            		<img src="./base/base_lo.png" class="img-fluid" alt="로고 이미지 손상">
          		</button>
        	</div>
        	<div class="col-md-1">
            	<!-- 상위구획 2 : 빈 공간 -->
            
        	</div>
        	<div class="col-md-6">
            	<!-- 상위구획 3 : 검색창 공간 -->
            	<form class="d-flex" role="search">
      				<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
      				<button class="btn btn-outline-primary" type="submit">
      					<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-search" viewBox="0 0 16 16">
  						<path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/>
						</svg>
      				</button>
    			</form>
        	</div>
        	<div class="col-md-3">
            	<!-- 상위구획 2 : 빈 공간 -->
            
        	</div>
    	</div>
	</div>
	<br>
	<!-- 카테고리창 -->
	<div class="container">
	    <div class="row align-items-center flex-row">
	    	<div class="col-md-1">
	            <!-- 빈 공간 -->
	            
	        </div>
	        <div class="col-md-10">
				<nav class="nav nav-pills nav-fill" style="border: 1px solid #dddddd; padding: 10px; background-color: #0d6efd;">
				  <a class="nav-link" href="blade.jsp"  style="color: white;">블레이드</a>
				  <a class="nav-link" href="rub.jsp" style="color: white;">러버</a>
				  <a class="nav-link" href="ball.jsp" style="color: white;">공</a>
				  <a class="nav-link" href="others.jsp" style="color: white;">기타 용품</a>
				  <a class="nav-link" href="uniform.jsp" style="color: white;">의류/신발</a>
				  <a class="nav-link" href="table.jsp" style="color: white;">탁구대</a>
				  <a class="nav-link disabled"  style="color: white;"><!-- aria-disabled="true" >-->세트</a>
				</nav>
			</div>
			<div class="col-md-1">
	            <!-- 빈 공간 -->
	            
	        </div>
	    </div>
	</div>
		<br>
	<!-- 홍보이미지칸 -->
	<div class="container">
	    <div class="row align-items-center flex-row">
	    	<div class="col-md-1">
	            <!-- 빈 공간 -->
	            
	        </div>
	        <div class="col-md-10">
				<!-- <img src="./base/base_ma.jpg" class="img-fluid" alt="대표 이미지 손상"> -->
				
				
				</div>
				
				
			</div>
			<div class="col-md-1">
	            <!-- 빈 공간 -->
	            
	        </div>
	    </div>
	</div>
	<br>
	<!-- 본문칸 -->
	<div class="container">
	   	<div class="row align-items-start flex-row">
	   		<div class="col-md-1">
	           	<!-- 빈 공간 -->
	           
	       	</div>
	   		
	       	<div class="col-md-8" style="border: 1px solid #dddddd; padding: 3px; margin-right: 1pt;">
	       	<h4>&nbsp장바구니</h4>
	       		<hr>
	       		<!-- 실 판매창 -->
	       		<%
	       		Cart c = new Cart();
	       		Bbs bbs = new Bbs();
	       		ArrayList<Bbs> list = c.getPro(x);
	       		BbsDAO bbsDAO = new BbsDAO();
	       		if (list == null ){
	       			System.out.println("아무것도 없다요");
	       		}else{
				for(int i = 0; i < list.size(); i++) {
					bbs = bbsDAO.getBbs(list.get(i).getPro_num());
					int pp = bbs.getReal_price();
					
					String fnum1 = String.format("%,d", bbs.getReal_price());
					
	       		%>
	       		
	       		 <script>
				     // 체크박스 상태에 따른 총 가격 업데이트
				     // 체크박스 상태에 따른 총 가격 업데이트
function updateTotalPriceByCheckbox(productId) {
    // 체크박스 상태 가져오기
    const isChecked = document.getElementById(`flexCheckDefault_${productId}`).checked;

    // 해당 상품의 가격 가져오기
    const unitPrice = parseInt(document.getElementById(`priceDisplay_${productId}`).innerText.replace(/,/g, ""));
    const quantity = parseInt(document.getElementById(`quantity_${productId}`).value);
    const productTotal = unitPrice * quantity;

    // 총 가격 가져오기
    let total = parseInt(document.getElementById("totalpriceDisplay").innerText.replace(/,/g, ""));

    // 체크 상태에 따라 총 가격 업데이트
    if (isChecked) {
        total += productTotal;
    } else {
        total -= productTotal;
    }

    // 화면에 총 가격 표시
    document.getElementById("totalpriceDisplay").innerText = total.toLocaleString();
}
				 </script>
	       		<div class="container">
  				<div class="row">
  				<div class="col-1">
  				<div class="form-check">
				  <input class="form-check-input" type="checkbox" value="" id="flexCheckDefault" onclick="updateTotalPriceByCheckbox('<%=bbs.getPro_num()%>')">
				  <label class="form-check-label" for="flexCheckDefault">
				  </label>
				</div>
  				</div>
  				<div class="col">
			      <img alt="사진 오류" src="./produc/<%=bbs.getP1() %>" width="100%" height="100%">
			    </div>
			    <div class="col">
			      <h5 style="font-size: 0.6em;"><%=bbs.getBrand() %></h5>
			      <h5 style="font-size: 0.9em;"><%=bbs.getPro_name() %></h5>
			      <h5 style="font-size: 0.6em;"><%=bbs.getPro_kind() %></h5>
			    </div>
			    <div class="col">
			      <h5 style="font-size: 0.6em;">수량</h5>
			      <input type="number" id="quantity" value="1" min="1" max="<%=bbs.getRemaining() %>" onchange="calculatePrice()">
				</div>
				<div class="col">
			      <h5 style="font-size: 0.6em;">재고</h5>
			      <%=bbs.getRemaining() %> 개
			    </div>
			    <div class="col">
			      <h5 style="font-size: 0.6em;">가격</h5><br>
			      <span id="priceDisplay" name="price"><%=fnum1 %></span>원
				</div>
				<div class="col">
			      <a href="buyfast?bbsID=<%=bbs.getPro_num() %>&&" type="button" class="btn btn-outline-primary w-100" style="height: 47%; margin-top: 1pt; margin-bottom: 1pt;">즉시구매</a>
			      <a onclick = "return confirm('삭제하시겠습니까?')"  href="cdAction.jsp?pro_num=<%=bbs.getPro_num() %>" type="button" class="btn btn-outline-primary w-100" style="height: 47%; margin-top: 1pt; margin-bottom: 1pt;">삭제</a>
				</div>
  				</div>
				</div>
				<hr>
				<%}} %>
				
	       		
	       		<br>
	       		<br>
	       		<!-- 넘버링 -->
				
				
			</div>
			<div class="col-md-2 text-right" style=" border-radius: 8px; border: 1px solid #dddddd; padding: 3px; text-align: right; margin-left:1pt;">
	           	<!-- 최종합산화면 -->
	           	<div class="col-12">
	           	<h5>선택 금액</h5>
	           	</div>
	           	<br>
	           	<div class="col-12">
	           	<h4><span id="totalPriceDisplay" name="totalPrice">0</span> 원</h4>
	           	</div>
			</div>

			<div class="col-md-1">
	           	<!-- 빈 공간 -->
	           
	       	</div>
	   	</div>
	   	<div class="row align-items-center flex-row">
	   		<div class="col-md-1">
	           <!-- 빈 공간 -->
	       	</div>
	       	
	   	</div>
	   	<br><br>
	</div>
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</body>
</html>