<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" session="false"%>
<%@ include file="/WEB-INF/jsp/include/taglib.jsp"%>
<form id="infoPrefix-form" data-doctx="user">
	<input type="hidden" name="cntnSeCd" data-map="CNTN_SE_CD" />

	<div class="row g-3">
		<div class="col-md-6">
			<div class="row">
				<label class="col-sm-3 text-sm-end col-form-label required">
				사용자구분
				</label>
				<div class="col-sm-9">
					<select name="userSeCd" class="form-select" data-map="USER_SE_CD" 
						required title="사용자구분">
					</select>
				</div>
			</div>
		</div>
		<div class="col-md-6">
		</div>
		<div class="col-md-12">
			<label class="col-form-label required">
			시군구/기관 및 부서
			</label>
			<select name="institute" class="form-select no-bgi" data-map="NSTT_CD" 
				required title="시군구/기관 및 부서" disabled>
				<option value=""></option>
				<c:forEach items="${allOgdpList}" var="item">
					<option value="${item.INST_CD}">${item.SGG_NM} / ${item.INST_NM}</option>
                </c:forEach>
			</select>
			<select name="deptCode" class="form-select no-bgi" data-map="DEPT_CD" 
				required title="시군구/기관 및 부서" disabled>
				<option value=""></option>
					<c:forEach items="${allOgdpList}" var="item">
						<option value="${item.DEPT_CD}">${item.DEPT_NM}</option>
					</c:forEach>
			</select>
			<input type="hidden" name="orgID" data-map="ORG_ID" />
                     
			<div class="dropdown d-inline-flex">
				<button type="button" 
					id="btnSelectOgdp"
					class="btn btn-md btn-secondary dropdown-toggle"
					data-bs-toggle="dropdown"
				>선택</button>
				<ul class="dropdown-menu oy-auto mh-px-300">
					<c:forEach items="${ableOgdpList}" var="item">
						<li>
							<a class="dropdown-item" 
								onclick="setOgdpCodeValues('${item.INST_CD}','${item.DEPT_CD}','${item.SGG_CD}')">
								${item.OGDP_NM}
							</a>
						</li>
					</c:forEach>
				</ul>
			</div>
		</div>
		
		<div class="col-md-6">
			<div class="row">
				<label class="col-sm-3 col-form-label text-sm-end required" for="account"
				>계정</label>
				<div class="col-sm-9">
					<input name="id" type="hidden" data-map="USER_ID" />
					<input name="account" type="text" required data-map="USER_ACNT" class="form-control" placeholder="prefixName 계정" />
				</div>
			</div>
		</div>
                  <div class="col-md-6">
                    <div class="row">
                      <label class="col-sm-3 col-form-label text-sm-end required" for="name"
                        >이름</label
                      >
                      <div class="col-sm-9">
                        <input name="name" type="text" required data-map="USER_NM" class="form-control" placeholder="prefixName 이름" />
                      </div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="row">
                      <label class="col-sm-3 col-form-label text-sm-end required" for="password"
                        >비밀번호</label
                      >
                      <div class="col-sm-9">
                        <input name="password" type="password" required class="form-control" placeholder="비밀번호" />
                      </div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="row">
                      <label class="col-sm-3 col-form-label text-sm-end required" for="confirmPassword"
                        >비밀번호 확인</label
                      >
                      <div class="col-sm-9">
                        <input name="confirmPassword" type="password" required class="form-control" placeholder="비밀번호 확인" />
                      </div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="row">
                      <label class="col-sm-3 col-form-label text-sm-end required" for="emailAddress"
                        >이메일 주소</label
                      >
                      <div class="col-sm-9">
                        <input name="emailAddress" type="email" required data-map="EML_ADRS" class="form-control" placeholder="이메일 주소" />
                      </div>
                    </div>
                  </div>
                  <div class="col-md-6 select2-primary">
                    <div class="row">
                      <label class="col-sm-3 col-form-label text-sm-end" for="phoneNo"
                        >전화번호(유선)</label
                      >
                      <div class="col-sm-9">
                        <input name="phoneNo" type="text" data-map="TELNO" class="form-control phone-mask" placeholder="000-0000-0000" />
                      </div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="row">
                      <label class="col-sm-3 col-form-label text-sm-end" for="faxNo">팩스</label>
                      <div class="col-sm-9">
                        <input name="faxNo" type="text" data-map="FXNO" class="form-control phone-mask" placeholder="000-0000-0000"/>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="row">
                      <label class="col-sm-3 col-form-label text-sm-end" for="positionName">직위</label>
                      <div class="col-sm-9">
                        <input name="positionName" type="text" data-map="POS_NM" class="form-control" placeholder="직위"/>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="row">
                      <label class="col-sm-3 col-form-label text-sm-end" for="mobilePhoneNo">휴대전화</label>
                      <div class="col-sm-9">
                        <input name="mobilePhoneNo" type="text" data-map="MBL_TELNO" class="form-control" placeholder="휴대전화"/>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-6 ">
                  	<div class="row">
                      <label class="col-sm-3 col-form-label text-sm-end" for="status">세외수입 사용자키</label>
                      <div class="col-sm-9">
						  <input name="ntrisKey" type="text" data-map="NTRIS_KEY" class="form-control" placeholder="세외수입 사용자키"/>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="row">
                      <label class="col-sm-3 col-form-label text-sm-end" for="status">상태</label>
                      <div class="col-sm-9">
                      <c:if test='${currentUser.hasAuthorities("ROLE_ADMIN", "ROLE_RVS_ADMIN")}'>
						<div class="form-check form-check-inline mt-3">
						  <input name="status" value="P" type="radio" data-map="STTS" class="form-check-input"/>
						  <label class="form-check-label" for="approved">승인</label>
						</div>
						<div class="form-check form-check-inline">
						  <input name="status" value="A" type="radio" data-map="STTS" class="form-check-input"/>
						  <label class="form-check-label" for="applied">신청</label>
						</div>
						<div class="form-check form-check-inline">
						  <input name="status" value="D" type="radio" data-map="STTS" class="form-check-input"/>
						  <label class="form-check-label" for="removed">삭제</label>
						</div>
					  </c:if>
                      <c:if test='${!currentUser.hasAuthorities("ROLE_ADMIN", "ROLE_RVS_ADMIN")}'>
                        <input name="status" type="hidden" data-map="STTS" class="form-control" placeholder="상태"/>
                        <label class="col-form-label" for=STTS></label>
                      </c:if>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="row mt-4 justify-content-end">
                  <div class="col-md-6">
                    <div class="row justify-content-end">
                      <div class="col-sm-3">
                        <button onclick="saveinfoPrefix();" type="button" class="btn btn-primary">저장</button>
                      </div>
                    </div>
                  </div>
                </div>
              </form>
              
	<datalist id="allOgdpList">
		<c:forEach items="${allOgdpList}" var="item">
			<option value="${item.OGDP_CD}">${item.OGDP_NM}</option>
		</c:forEach>
	</datalist>
              
	<datalist id="userSeCdList">
		<c:forEach items="${CMN008List}" var="item">
			<option value="${item.code}">${item.value}</option>
		</c:forEach>
	</datalist>
              
<script type="text/javascript">
var infoPrefixFields = new FimsFormFields("#infoPrefix-form");

infoPrefixCtrl.setInfo = (obj) => {
	let create = isEmpty(obj.data.USER_ACNT);
	if(create){
		obj.data.CNTN_SE_CD = "01";
	}
	
	let isInnerUser = (obj.data.CNTN_SE_CD == "01");
	if(!isInnerUser){
		$("#infoPrefix-form select[name='userSeCd']").html($("#userSeCdList option[value='21']").clone());
		$("#btnSelectOgdp").attr("hidden","hidden");
	} else {
		$("#infoPrefix-form select[name='userSeCd']").html($("#userSeCdList option[value!='21']").clone());
		$("#btnSelectOgdp").removeAttr("hidden");
	}
	
	infoPrefixFields.set(infoPrefixCtrl,obj);

	$("#infoPrefix-form input[name='account']").prop("readonly", !create);
	$("#infoPrefix-form input[name='name']").prop("readonly", !create);
	
	if(create){
		$("#infoPrefix-form :radio[name='status'][value='D']").attr("hidden","hidden");
		$("#infoPrefix-form label[for='removed']").attr("hidden","hidden");
	} else {
		$("#infoPrefix-form :radio[name='status'][value='D']").removeAttr("hidden");
		$("#infoPrefix-form label[for='removed']").removeAttr("hidden");
	}
	
	$("#infoPrefix-form input[type='password']").each(function(){
		let password = $(this).prop("required", create);
		let div = password.parent().parent().parent();
		if (create) {
			div.removeAttr("hidden");
		} else {
			div.attr("hidden","hidden");
		}
	});
	
	let positionNameDiv = $("#infoPrefix-form input[name='positionName']").parent().parent().parent();
	if (isInnerUser) {
		positionNameDiv.removeAttr("hidden");
	} else {
		positionNameDiv.attr("hidden","hidden");
	}
	
	let mobilePhoneNoDiv = $("#infoPrefix-form input[name='mobilePhoneNo']").parent().parent().parent();
	if (!isInnerUser) {
		mobilePhoneNoDiv.removeAttr("hidden");
	} else {
		mobilePhoneNoDiv.attr("hidden","hidden");
	}

	let stts = "";
	if(obj.data.STTS == "D"){
		stts = "삭제";
	} else if(obj.data.STTS == "P"){
		stts = "승인";
	} else {
		stts = "신청";
	}
	$("#infoPrefix-form label[for='STTS']").html(stts);
	
	$("#infoPrefix-form").find("input,select")
		.change(function() {
			let input = $(this),
				name = input.attr("data-map"),
				val = input.val();
			infoPrefixCtrl.setValue(name, val);
		})
		.onEnterPress(saveinfoPrefix);
	
	document.querySelector("#infoPrefix-form input[name='" + (create ? "account" : "name") + "']").focus();
}

infoPrefixCtrl.onModify = (changed) => {
	if (["USER_NM", "EML_ADRS", "MBL_TELNO"].filter(e => changed.includes(e)).length < 1)
		return;
	
	infoPrefixCtrl.renderList();
	infoPrefixCtrl.dataset.setState();
}

function saveinfoPrefix() {
	if (!$("#infoPrefix-form").find("input,select").validInputs()) return;
	
	let match = Array.from(document.querySelectorAll("#infoPrefix-form input[type='password']"))
		.map(input => input.value)
		.reduce((total, current) => total == current);
	if (!match) {
		dialog.alert({
			content:"비밀번호와 비밀번호 확인이 다릅니다.",
			onClose:function(){
				document.querySelector("#infoPrefix-form input[name='confirmPassword']").focus();
			}
		});
		return;
	}
	
	dialog.alert({
		content:"현재 prefixName 정보를 저장하시겠습니까?",
		onOK:() => {
			infoPrefixCtrl.save(infoPrefixFields.get());
		}
	});
}

function setOgdpCodeValues(institute, deptCode, sggCd){
	$("#infoPrefix-form [name='institute']").val(institute).trigger("change");
	$("#infoPrefix-form [name='deptCode']").val(deptCode).trigger("change");
	$("#infoPrefix-form [name='orgID']").val(sggCd).trigger("change");
}

//# sourceURL=user-info.jsp
</script>