<html lang="ko"
	  xmlns:th="http://www.thymeleaf.org">
<!-- 옵션변경 팝업 -->
<div id="cartOptionModal" class="modal-dialog cartOptionModal" role="document" th:with="imgGoodsUrl=${@environment.getProperty('upload.goods.view')}, uxImgUrl=${@environment.getProperty('domain.uximage')}, imgUrl=${@environment.getProperty('upload.image.view')}">
	<div class="modal-content">
		<div class="modal-header">
			<h5 class="modal-title" id="optModifyLabel">옵션변경</h5>
			<div class="prod_title">
				<span class="name" th:text="${cart.goodsNm}"></span>
			</div>
		</div>
		<div class="modal-body">
			<div class="pop_cont">
				<div class="area_pic">
					<ul class="pic_list">
						<th:block th:if="${setType.equals(cart.goodsType) or cart.selfGoodsYn.equals('N')}">
							<li th:each="img, index : ${cart.cartImgList}">
								<span class="thumb">
									<img th:src="${cart.imgPath + '/' + img.sysImgNm} + '?RS=180'" src="/" th:onerror="'this.src=\''+@{${uxImgUrl}+ '/images/pc/thumb/bg_item_none.png'}+'\';'"  th:alt="${img.sysImgNm}" alt="" />
								</span>
							</li>
						</th:block>
						<th:block th:if="${!setType.equals(cart.goodsType) and cart.selfGoodsYn.equals('Y')}">
							<th:block th:each="comp, status : ${cart.cartCompsList}">
								<th:block th:each="color, index : ${comp.cartColorList}">
									<th:block th:if="${color.cartOptCd1 != null and color.cartOptCd1.equals(color.optCd1)}">
										<li th:each="img, index : ${color.cartImgList}" th:if="${img.colorCd == color.cartOptCd1}">
											<span class="thumb">
												<img th:src="${cart.imgPath + '/' + img.sysImgNm} + '?RS=180'" src="/" th:onerror="'this.src=\''+@{${uxImgUrl}+ '/images/pc/thumb/bg_item_none.png'}+'\';'"  th:alt="${img.sysImgNm}" alt="" />
											</span>
										</li>
									</th:block>
								</th:block>
							</th:block>
						</th:block>
					</ul>
				</div>
				<div class="area_order">
					<th:block th:each="comp, status : ${cart.cartCompsList}">
						<!-- 세트상품 옵션 -->
						<th:block th:if="${setType.equals(cart.goodsType)}">
							<span class="hiddenData">
								<input type="hidden" class="cartDtlSq" th:value="${comp.cartDtlSq}" />
								<input type="hidden" class="itemCd" th:value="${comp.goodsCd}" />
								<input type="hidden" class="optCd" th:classappend="|option_${comp.goodsCd}|" th:value="${comp.optCd}"/>
								<input type="hidden" class="optCd1" th:classappend="|option1_${comp.goodsCd}|" th:value="${comp.optCd1}"/>
								<input type="hidden" class="optCd2" th:classappend="|option2_${comp.goodsCd}|" th:value="${comp.optCd2}"/>
							</span>

							<div class="opt_select">
								<div class="opt_header">
									<span class="title" th:text="${comp.goodsNm}"></span>
								</div>
								<div class="opt_header">
									<span class="title">옵션1</span>
									<span class="text" th:classappend="|chooseColorNm_${comp.goodsCd}|" th:text="${comp.colorNm}"></span>
								</div>
								<form class="form_wrap">
									<div class="form_field">
										<div class="select_custom item_opt1 colorList">
											<div class="combo">
												<div class="select" th:text="${comp.colorNm}"></div>
												<ul class="list" th:data="${comp.goodsCd}">
													<th:block th:each="color, index : ${comp.cartColorList}">
														<li th:classappend="${color.cartColorNm != null} ? 'selected'" th:colorNo="${color.colorNo}" th:data="${color.optCd1}" th:text="${color.colorNm}" th:aria-disabled="${color.soldoutYn.equals('Y') ? true : false}"></li>
													</th:block>
												</ul>
											</div>
										</div>
									</div>
								</form>
							</div>
							<div class="opt_select">
								<div class="opt_header">
									<span class="title">옵션2</span>
									<span class="text" th:classappend="|chooseSizeNm_${comp.goodsCd}|" th:text="${comp.optCd2}"></span>
								</div>
								<form class="form_wrap">
									<div class="form_field">
										<th:block th:each="color, index : ${comp.cartColorList}">
											<div class="select_custom item_opt2 sizeList" th:classappend="|color_${comp.goodsCd}_${color.colorNo} sizeList${comp.goodsCd}|" th:style="${color.cartColorNm != null ? 'display:block' : 'display:none'}">
												<div class="combo">
													<div class="select" th:classappend="|size_${comp.goodsCd}_size_nm|" th:text="${comp.optCd2}" th:data="${color.goodsCd}"></div>
													<ul class="list setSizeSelect" th:classappend="|size_${comp.goodsCd}|" th:data="${comp.goodsCd}">
														<th:block th:each="size, i : ${color.cartSizeList}" th:if="${color.optCd1.equals(size.optCd1)}">
															<li th:classappend="${size.cartOptCd != null} ? selected" th:optCd="${size.optCd}" th:value="${size.optCd}" th:text="${size.optCd2}" th:aria-disabled="${color.soldoutYn.equals('Y') or size.soldoutYn.equals('Y') or size.minOrdQty > size.currStockQty or comp.qty > size.currStockQty ? true : false}"></li>
														</th:block>
													</ul>
												</div>
											</div>
										</th:block>
									</div>
								</form>
							</div>
						</th:block>

						<!-- 자사 일반 상품 옵션 -->
						<th:block th:if="${!setType.equals(cart.goodsType) and 'Y'.equals(cart.selfGoodsYn)}">
							<div class="opt_color">
								<div class="opt_header">
									<span class="title">컬러</span>
									<th:block th:each="color, index : ${comp.cartColorList}">
										<th:block th:if="${color.cartColorNm != null}">
											<span class="color" th:text="${color.cartColorNm}"></span>
										</th:block>
									</th:block>
								</div>
								<ul>
									<li th:each="color, index : ${comp.cartColorList}" class="selfGoodsColor">
										<a href="javascript:void(0);" th:cartDtlSq="${color.cartDtlSq}" th:classappend="${color.cartColorNm != null} ? 'on'" class="selfGoodsColorSelect" th:data="${color.optCd1}">
											<img th:src="${cart.imgPath + '/' + color.sysImgNm} + '?RS=70'" src="#" th:onerror="'this.src=\''+@{${uxImgUrl}+ '/images/pc/thumb/bg_item_none.png'}+'\';'" th:alt="${color.colorNm}" alt="" />
										</a>
									</li>
								</ul>
							</div>
							<div class="opt_size">
								<div class="opt_header">
									<span class="title">사이즈</span>
								</div>
								<div class="form_field selfGoodsSize">
									<th:block th:each="color, index : ${comp.cartColorList}">
										<th:block th:if="${color.cartColorNm != null}">
											<th:block th:each="size, index : ${color.cartSizeList}">
												<div>
													<input type="radio" name="rdi-optsize" th:soldoutYn="${size.soldoutYn}" th:id="|${'rdi-optsize' + size.goodsCd + '-' + size.optCd}|" th:value="${size.optCd}" th:checked="${size.cartOptCd != null}" th:disabled="${size.soldoutYn == 'Y' or size.minOrdQty > size.currStockQty}">
													<label th:for="|${'rdi-optsize' + size.goodsCd + '-' + size.optCd}|"><span th:text="${size.optCd2}"></span></label>
												</div>
											</th:block>
										</th:block>
									</th:block>
								</div>
							</div>
						</th:block>

						<!-- 입점업체 -->
						<th:block th:if="${!setType.equals(cart.goodsType) and 'N'.equals(cart.selfGoodsYn)}">
							<span class="hiddenData">
								<input type="hidden" class="cartDtlSq" th:value="${comp.cartDtlSq}" />
								<input type="hidden" class="itemCd" th:value="${comp.goodsCd}" />
								<input type="hidden" class="optCd" th:classappend="|option_${comp.goodsCd}|" th:value="${comp.optCd}"/>
								<input type="hidden" class="optCd1" th:classappend="|option1_${comp.goodsCd}|" th:value="${comp.optCd1}"/>
								<input type="hidden" class="optCd2" th:classappend="|option2_${comp.goodsCd}|" th:value="${comp.optCd2}"/>
							</span>

							<div class="opt_select">
								<div class="opt_header">
									<span class="title">옵션1</span>
									<span class="text" th:classappend="|chooseColorNm_${comp.goodsCd}|" th:text="${comp.colorNm}"></span>
								</div>
								<form class="form_wrap">
									<div class="form_field">
										<div class="select_custom item_opt1 colorList">
											<div class="combo">
												<div class="select" th:text="${comp.colorNm}"></div>
												<ul class="list" th:data="${comp.goodsCd}">
													<th:block th:each="color, index : ${comp.cartColorList}">
														<li th:classappend="${color.cartColorNm != null} ? 'selected'" th:colorNo="${color.colorNo}" th:data="${color.optCd1}" th:text="${color.colorNm}" th:aria-disabled="${color.soldoutYn.equals('Y') ? true : false}"></li>
													</th:block>
												</ul>
											</div>
										</div>
									</div>
								</form>
							</div>
							<div class="opt_select">
								<div class="opt_header">
									<span class="title">옵션2</span>
									<span class="text" th:classappend="|chooseSizeNm_${comp.goodsCd}|" th:text="${comp.optCd2}"></span>
								</div>
								<form class="form_wrap">
									<div class="form_field">
										<th:block th:each="color, index : ${comp.cartColorList}">
											<div class="select_custom item_opt2 sizeList" th:classappend="|color_${comp.goodsCd}_${color.colorNo} sizeList${comp.goodsCd}|" th:style="${color.cartColorNm != null ? 'display:block' : 'display:none'}">
												<div class="combo">
													<div class="select" th:classappend="|size_${comp.goodsCd}_size_nm|" th:text="${comp.optCd2}" th:data="${color.goodsCd}"></div>
													<ul class="list setSizeSelect" th:classappend="|size_${comp.goodsCd}|" th:data="${comp.goodsCd}">
														<th:block th:each="size, i : ${color.cartSizeList}" th:if="${color.optCd1.equals(size.optCd1)}">
															<li th:classappend="${size.cartOptCd != null} ? selected" th:optCd="${size.optCd}" th:value="${size.optCd}" th:text="${size.optCd2}" th:aria-disabled="${color.soldoutYn.equals('Y') or size.soldoutYn.equals('Y') or size.minOrdQty > size.currStockQty ? true : false}"></li>
														</th:block>
													</ul>
												</div>
											</div>
										</th:block>
									</div>
								</form>
							</div>
						</th:block>
					</th:block>
					<div class="opt_count">
						<input type="hidden" class="minOrdQty" th:value="${cart.minOrdQty}" />
						<input type="hidden" class="maxOrdQty" th:value="${cart.maxOrdQty}" />
						<div class="opt_header">
							<span class="title">수량</span>
						</div>
						<div class="number_count">
							<span class="minus"><em class="sr-only">감소</em></span>
							<input type="text" name="goodsQty" maxlength="3" style='ime-mode:disabled' th:value="${cart.goodsQty}" readonly/>
							<span class="plus"><em class="sr-only">추가</em></span>
						</div>
					</div>
					<div class="btn_group_block">
						<button class="btn btn_dark btn_block" th:onclick="|fnChangeCartOption(${cart.cartSq})|"><span>옵션변경</span></button>
					</div>
				</div>
			</div>
		</div>
	</div>

	<script th:inline="javascript">
		$(document).ready(function() {
			$(".select_custom").each(function() {
				new sCombo($(this));
			});
		});

		// 세트상품, 입점업체 상품 컬러 변경시
		$("#cartOptionModal .colorList > .combo > .list li").on("click", function(e) {
			let goodsCd = $(this).parent().attr("data");
			let optCd1 = $(this).attr("data");
			let colorNo = $(this).attr("colorNo");
			let firstSize = "";
			if(optCd1 != $(".option1_" + goodsCd).val()) {
				let firstOptCd = $("#cartOptionModal .color_" + goodsCd + "_" + colorNo + " li:first").attr("optcd");
				let firstOptCd2 = $("#cartOptionModal .color_" + goodsCd + "_" + colorNo + " li:first").text();

				$("#cartOptionModal .chooseColorNm_" + goodsCd).text($(this).text());
				$("#cartOptionModal .option_" + goodsCd).val(firstOptCd);
				$("#cartOptionModal .option1_" + goodsCd).val(optCd1);
				$("#cartOptionModal .option2_" + goodsCd).val(firstOptCd2);

				$("#cartOptionModal .sizeList" + goodsCd).hide();
				$("#cartOptionModal .color_" + goodsCd + "_" + colorNo).show();
				$("#cartOptionModal .color_" + goodsCd + "_" + colorNo + " .select").text(firstOptCd2);
				$("#cartOptionModal .chooseSizeNm_" + goodsCd).text(firstOptCd2);
			}
		});

		// 세트상품, 입점업체 상품 사이즈 변경시
		$(".sizeList > .combo > .list li").on("click", function() {
			$(this).parent().parent().parent().parent().parent().parent().find(".opt_header .text").text($(this).text());
			$("#cartOptionModal .option_" + $(this).parent().attr("data")).val($(this).attr("optcd"));
			$("#cartOptionModal .option2_" + $(this).parent().attr("data")).val($(this).text());
		});

		/* 옵션변경 > 수량조절 */
		//수량1개 이하 감소버튼 비활성화
		var valItemCount = $('.optModify_pop .number_count input').val();
		if ( valItemCount == 1 ) {
			$('.optModify_pop .number_count .minus').addClass('min_val');
		}

		// 자사 일반 상품 컬러 선택시
		$(".selfGoodsColorSelect").on("click", function(e) {
			let sizeHtml = "";
			let imgHtml = "";
			let cart = [[${cart}]];
			let color =  [[${cart.cartCompsList[0].cartColorList}]];
			let optCd1 = $(this).attr("data");

			for(let i = 0 ; i < color.length ; i++) {
				let size = color[i].cartSizeList;
				for(let j = 0 ; j < size.length ; j++) {
					if(optCd1 == size[j].optCd1) {
						sizeHtml += "<div>";
						sizeHtml += "<input type='radio' name='rdi-optsize' id='" + size[j].goodsCd + "-" + size[j].optCd + "' value='" + size[j].optCd + "' ";
						if(size[j].soldoutYn == "Y" || size[j].minOrdQty > size[j].currStockQty) {
							sizeHtml += "disabled = 'disabled'";
						}
						sizeHtml += " />\n";
						sizeHtml += "<label for='" + size[j].goodsCd + "-" + size[j].optCd + "'><span>" + size[j].optCd2 + "</span></label>\n"
						sizeHtml += "</div>\n";
					}
				}

				if(optCd1 == color[i].optCd1) {
					for (let j = 0; j < color[i].cartImgList.length; j++) {
						let img = color[i].cartImgList[j];
						if(optCd1 == img.colorCd) {
							imgHtml += "<li>\n<span class='thumb'>\n";
							imgHtml += "<img src='" + cart.imgPath + "/" + img.sysImgNm + "' onerror='this.src='" + [[${@environment.getProperty('domain.uximage')}]] + "/images/pc/thumb/bg_item_none.png' alt='" + img.sysImgNm + "' />\n";
							imgHtml += "</span>\n</li>\n";
						}
					}
				}
			}

			$(".cartOptionModal .pic_list").html(imgHtml);
			$(".cartOptionModal .selfGoodsSize").html(sizeHtml);
		});

		function fnChangeCartOption(cartSq) {
			let cart = [[${cart}]];
			var cartDtlSqArr = [], itemCds = [], optCds = [];
			var data;

			if(cart.goodsType == "G056_S") {
				$("#cartOptionModal .cartDtlSq").each(function() {
					optCds.push($(this).parent().find(".optCd").val());
					cartDtlSqArr.push($(this).parent().find(".cartDtlSq").val());
					itemCds.push($(this).parent().find(".itemCd").val());
				});

				data = {
					  cartSq : cart.cartSq
					, goodsCd : cart.goodsCd
					, goodsType : cart.goodsType
					, cartDtlSqArr : cartDtlSqArr
					, itemCds : itemCds
					, optCds : optCds
					, goodsQty : $("input[name=goodsQty]").val()
				}
			} else if(cart.goodsType != "G056_S" && cart.selfGoodsYn == "Y") {
				if($(".selfGoodsSize").find("input[name=rdi-optsize]:checked").length < 1) {
					mcxDialog.alert("옵션을 선택해 주세요.");
					return false;
				}

				optCds.push($(".selfGoodsSize").find("input[name=rdi-optsize]:checked").val());
				itemCds.push(cart.goodsCd);
				$(".selfGoodsColorSelect").each(function() {
					if($(this).attr("cartDtlSq") > 0) {
						cartDtlSqArr.push($(this).attr("cartDtlSq"));
					}
				});

				data = {
					  cartSq : cart.cartSq
					, goodsCd : cart.goodsCd
					, goodsType : cart.goodsType
					, cartDtlSqArr : cartDtlSqArr
					, itemCds : itemCds
					, optCds : optCds
					, goodsQty : $("input[name=goodsQty]").val()
				}
			} else {
				$("#cartOptionModal .cartDtlSq").each(function() {
					optCds.push($(this).parent().find(".optCd").val());
					cartDtlSqArr.push($(this).parent().find(".cartDtlSq").val());
					itemCds.push($(this).parent().find(".itemCd").val());
				});

				data = {
					cartSq : cart.cartSq
					, goodsCd : cart.goodsCd
					, goodsType : cart.goodsType
					, cartDtlSqArr : cartDtlSqArr
					, itemCds : itemCds
					, optCds : optCds
					, goodsQty : $("input[name=goodsQty]").val()
				}
			}

			$.ajax( {
				type: "POST",
				url : '/cart/change/option',
				contentType: 'application/json',
				dataType : 'json',
				data : JSON.stringify(data),
				error : function(e) {
					mcxDialog.alert(e.message);
				},
				success : function(result) {
					mcxDialog.alert(result.message);
					$(".closeCartOptionModal").trigger("click");
					if(result.status == "SUCCESS") {
						let data = {shotDelvUseYn : $("input[name=shotDelvUseYn]:checked").val()};
						getCartList(data);
					}
				}
			});
		}
	</script>
	<a href="#close-modal" rel="modal:close" id="optModifyPop_close" class="close-modal closeCartOptionModal">Close</a>
</div>
<!-- //옵션변경 팝업 -->
</html>