<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html lang="ko">
<head>
    <title>운영관리코드 설정</title>
    <%@ include file="/WEB-INF/jsp/adm/include/topLinkTag.jsp" %>
</head>
<body>
<h1>ICOMSYS ADMIN</h1>

<div class="skip_navigation">
    <ul>
        <li>
            <a href="#Content" class="go_content">본문 바로가기</a>
        </li>
        <li>
            <a href="#Gnb">메뉴 바로가기</a>
        </li>
    </ul>
</div>

<header id="header">
    <%@ include file="/WEB-INF/jsp/adm/include/header.jsp" %>
</header>

<c:import url="/adm/menu/getLnbMenu.do" />

<script>
    $(document).ready( function () {

        // 고객사 조회
        $('#btnSearchOpr').on('click',function() {
            fncSearchOprMngCode();
        });

        // 신규 운영관리 코드
        $('#btnSaveOpr').on('click',function() {
            fncCreateOprInfo();
        });

        // 고객사 수정
        $('#btnEditOpr').on('click',function() {
            fncUpdateOprInfo();
        });

        // 고객사 삭제
        $('#btnDelOprMngCode').on('click',function() {
            fncDeleteOprMngCode();
        });

        // 추가 modal
        $('#btnRegModal').on('click',function() {
            // 초기화
            $('#oprMngCode').val('');
            $('#dtmf').val('');
            $('#routeUrl').val('');
            $('#oprMngName').val('');
            $('#custCode').val('').prop("selected", true);
            $('#custCode').niceSelect('update');
            $('#custCode').empty();
            $('#serviceCode').val('').prop("selected", true);
            $('#serviceCode').niceSelect('update');
            $('#serviceCode').empty();

            // 고객사 코드 목록
            var custCodeOpt;
            for(var i=0; i<custList.length; i++) {
                custCodeOpt+= '<option value='+custList[i].custCode+'>'+custList[i].custCode+'</option>'
            }

            $('#custCode').append(custCodeOpt);
            $('#custCode').niceSelect('update');

            // 서비스 코드 목록
            var serviceCodeOpt;
            for(var i=0; i<serviceList.length; i++) {
                serviceCodeOpt+= '<option value='+serviceList[i].serviceCode+'>'+serviceList[i].serviceCode+'</option>'
            }

            $('#serviceCode').append(serviceCodeOpt);
            $('#serviceCode').niceSelect('update');
        });

        // 고객사 리스트 그리기
        fncSearchOprMngCode();

    });

    var serviceList;
    var custList;

    function fncKeyPresshandler(e) {
        if (event.keyCode == 13) {
            fncSearchOprMngCode();
            return false;
        }
    }

    // 수정 modal 값 채우기
    function fncViewInfoTr(tr){
        var oprMngCode = $(tr).data('oprMngCode');
        var editCustCode = $(tr).data('custCode');
        var oprMngName = $(tr).data('dtmf');
        var editDtmf = $(tr).data('dtmf');
        var editRouteUrl= $(tr).data('routeUrl');

        $('#editOprMngCode').val(oprMngCode);
        $('#editCustCode').val(editCustCode);
        $('#editOprMngName').val(oprMngName);
        $('#editDtmf').val(editDtmf);
        $('#editRouteUrl').val(editRouteUrl);
    }

    // 고객사 리스트 그리기
    function fncSearchOprMngCode() {
        $('#listTable tbody').empty();

        var param = "keyword=" + $('#keyword').val();
        var resultTr = '';

        $.ajax({
            type : "post",
            async : false,
            datatype : "text",
            url : "<c:url value='/admin/system/searchOprMngCode.do'/>",
            data : param,
            success : function(data){
                serviceList = data.serviceList;
                custList = data.custList;

                $.each(data.nMap, function(i, val){
                    i += 1;
                    resultTr += '<tr class="modalLoad" rel="reg_edit_modal" data-route-url="' + val.routeUrl + '" data-cust-code="' + val.custCode + '" data-opr-mng-name="' + val.oprMngName + '"data-dtmf="' + val.dtmf +'"data-opr-mng-code="' + val.oprMngCode + '">';
                    resultTr += '	<td>'+ i +'</td>';
                    resultTr += '	<td class="oprMngCodes tl">' + val.oprMngCode + '</td>';
                    resultTr += '	<td>' + val.custCode + '</td>';
                    resultTr += '	<td>' + val.serviceCode + '</td>';
                    resultTr += '	<td>' + val.oprMngName + '</td>';
                    resultTr += '	<td>' + val.dtmf + '</td>';
                    resultTr += '	<td>' + val.routeUrl + '</td>';
                    resultTr += '	<td>' + moment(val.registDate).format('YYYY-MM-DD') + '</td>';
                    resultTr += '</tr>';
                });

                $('#listTable tbody').append(resultTr);

                // 고객사 수정 modal open event
                $('#listTable tbody tr').on('dblclick', function() {
                    fncViewInfoTr($(this));
                });

                // scripts.js : 고객사 list modal 연결
                modalUi();

                // scripts.js : 체크 박스 체크에 따른 '모두 체크' 변경
                checkAllChecked();
            }
        });
    }

    // 신규 고객사 저장
    function fncCreateOprInfo() {

        if($('#custCode').val() == '') {
            swal({
                title: "Information",
                text: "고객사 코드는 필수 항목입니다.",
                icon: "warning",
                button: "확인"
            });
        } else if($('#serviceCode').val() == '') {
            swal({
                title: "Information",
                text: "서비스 코드는 필수 항목입니다.",
                icon: "warning",
                button: "확인"
            });
        } else if($('#oprMngName').val() == '') {
            swal({
                title: "Information",
                text: "운영관리코드 이름은 필수 항목입니다.",
                icon: "warning",
                button: "확인"
            });
        } else if ($('#dtmf').val() == '') {
            swal({
                title: "Information",
                text: "dtmf는 필수 항목입니다.",
                icon: "warning",
                button: "확인"
            });
        } else if ($('#routeUrl').val() == '') {
            swal({
                title: "Information",
                text: "라우팅url는 필수 항목입니다.",
                icon: "warning",
                button: "확인"
            });
        } else {
            var isAready;
            $('.oprMngCodes').each(function(){
                var existOprMngCode = $(this).text();
                if (existOprMngCode === $('#oprMngCode').val()){
                    isAready = "Y";
                    return false;
                }
            })
            if (isAready == 'Y') {
                swal({
                    title: "Information",
                    text: "이미 존재하는 운영코드입니다.",
                    icon: "warning",
                    button: "확인"
                });
                return false;
            } else {

                var param = "oprMngCode=" +	$('#oprMngCode').val();
                param += "&custCode=" +	$('#custCode').val();
                param += "&serviceCode=" + $('#serviceCode').val();
                param += "&oprMngName=" + $('#oprMngName').val();
                param += "&dtmf=" + $('#dtmf').val();
                param += "&routeUrl=" + $('#routeUrl').val();
                param += "&useYn=Y";

                $.ajax({
                    type : "post",
                    async : false,
                    datatype : "text",
                    url : "<c:url value='/admin/system/createOprMngCode.do'/>",
                    data : param,
                    success : function(data) {
                        swal({
                            title: "Information",
                            text: "저장되었습니다.",
                            icon: "success",
                            button: "확인"
                        });
                    }
                });
            }

            // edit_modal close
            $('#reg_modal a.close').trigger('click');

            // 고객사 리스트 그리기
            fncSearchOprMngCode();

        }
    }

    // 기존 고객사 수정
    function fncUpdateOprInfo() {

        if($('#editDtmf').val() == '') {
            swal({
                title: "Information",
                text: "DTMF 값을 입력해주세요.",
                icon: "warning",
                button: "확인"
            });
        }
        else if($('#editRouteUrl').val() == '') {
            swal({
                title: "Information",
                text: "라우팅 url을 입력해주세요.",
                icon: "warning",
                button: "확인"
            });
        }
        else {

            var param = "oprMngCode=" + $('#editOprMngCode').val();
            param += "&custCode=" + $('#editCustCode').val();
            param += "&dtmf=" + $('#editDtmf').val();
            param += "&routeUrl=" + $('#editRouteUrl').val();

            $.ajax({
                type : "post",
                async : false,
                datatype : "text",
                url : "<c:url value='/admin/system/updateOprMngCode.do'/>",
                data : param,
                success : function(data) {
                    swal({
                        title: "Information",
                        text: "저장되었습니다.",
                        icon: "success",
                        button: "확인"
                    });
                }
            });
            // edit_modal close
            $('#reg_edit_modal a.close').trigger('click');

            // 고객사 리스트 그리기
            fncSearchOprMngCode();
        }
    }

    // 고객사 삭제
    function fncDeleteOprMngCode() {
        var param = "oprMngCode=" + $('#editOprMngCode').val();

        $.ajax({
            type : "post",
            async : false,
            datatype : "text",
            url : "<c:url value='/admin/system/deleteOprMngCode.do'/>",
            data : param,
            success : function(data) {
                swal({
                    title: "Information",
                    text: "삭제되었습니다.",
                    icon: "success",
                    button: "확인"
                });
            }
        });
        // edit_modal close
        $('#reg_edit_modal a.close').trigger('click');
        // 고객사 리스트 그리기
        fncSearchOprMngCode();
    }
</script>
<section id="Content" class="system_management">
    <div class="location">
        <ul>
            <li class="home"><a href="#">HOME</a></li>
            <li><a href="#">시스템관리</a></li>
            <li><a href="#">운영관리코드 설정</a></li>
        </ul>
    </div>
    <div class="sub_cont menu06_01_04">
        <div class="cont_box">
            <div class="box_title">
                <div class="fl">
                    <h2 class="mt12">운영관리코드 조회</h2>
                </div>
                <div class="btn_wrap fr">
                    <a href="#" id="btnSearchOpr" class="btn">조회</a>
                </div>
            </div>
            <div class="tbl">
                <table class="search">
                    <colgroup>
                        <col width="130px">
                        <col width="auto">
                    </colgroup>
                    <tbody>
                    <tr>
                        <th class="tl">키워드</th>
                        <td class="tl">
                            <div class="ipt_box">
                                <input type="text" placeholder="키워드를 입력해주세요." id="keyword" name="code" class="" onkeypress="return fncKeyPresshandler(event);">
                            </div>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="cont_box">
            <div class="box_title">
                <div class="fl">
                    <h2 class="mt12">운영관리코드 리스트</h2>
                </div>
                <div class="btn_wrap fr">
                    <a href="#" rel="reg_modal" class="btn modalLoad" id="btnRegModal">추가</a>
                </div>
            </div>
            <div class="tbl">
                <form id="custListForm">
                    <table id="listTable" class="">
                        <colgroup>
                            <col width="60px">
                            <col width="auto">
                            <col width="auto">
                            <col width="auto">
                            <col width="auto">
                            <col width="auto">
                            <col width="auto">
                            <col width="100px">
                        </colgroup>
                        <thead>
                        <tr>
                            <th>NO</th>
                            <th>운영관리 코드</th>
                            <th>고객사 코드</th>
                            <th>서비스 코드</th>
                            <th>운영관리코드 이름</th>
                            <th>DTMF</th>
                            <th>라우팅url</th>
                            <th>등록일</th>
                        </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </form>
            </div>
        </div>
    </div>
</section>

<!--  modal :: 고객사 등록 팝업  -->
<div class="modal" id="reg_modal">
    <div class="modal_header">
        <h3>운영관리코드 신규등록</h3>
    </div>
    <div class="modal_body">
        <div class="modal_cont">
            <dl>
                <dt><h4><em class="">운영관리코드</em></h4></dt>
                <dd>
                    <div class="ipt_box">
                        <input type="text" id="oprMngCode" placeholder="운영관리코드 이름을 입력해주세요" name="required">
                    </div>
                </dd>
            </dl>
            <dl class="">
                <dt><h4><em class="">고객사 코드</em></h4></dt>
                <dd>
                    <div class="sel_box">
                        <select id="custCode" name="custCode" class="wide">
                            <option value="">고객사 코드를 선택해주세요.</option>
                        </select>
                    </div>
                </dd>
            </dl>
            <dl class="">
                <dt><h4><em class="">서비스 코드</em></h4></dt>
                <dd>
                    <div class="sel_box">
                        <select id="serviceCode" name="serviceCode" class="wide">
                            <option value="">서비스 코드를 선택해주세요.</option>
                        </select>
                    </div>
                </dd>
            </dl>
            <dl class="">
                <dt><h4><em class="">운영관리코드 이름</em></h4></dt>
                <dd>
                    <div class="ipt_box">
                        <input type="text" id="oprMngName" placeholder="운영관리코드 이름을 입력해주세요" name="required">
                    </div>
                </dd>
            </dl>
            <dl class="">
                <dt><h4><em class="">dtmf</em></h4></dt>
                <dd>
                    <div class="ipt_box">
                        <input type="text" id="dtmf" placeholder="DTMF를 입력해주세요" name="required">
                    </div>
                </dd>
            </dl>
            <dl class="">
                <dt><h4><em class="">라우팅url</em></h4></dt>
                <dd>
                    <div class="ipt_box">
                        <input type="text" id="routeUrl" placeholder="라우팅url을 입력해주세요" name="required">
                    </div>
                </dd>
            </dl>
        </div>
    </div>
    <div class="modal_footer">
        <div class="btn_wrap fr">
            <a href="#" class="btn" id="btnSaveOpr" name="btnSaveOpr">저장</a>
        </div>
    </div>
    <div class="modal_controller">
        <a href="#" class="close">close</a>
    </div>
</div>
<!--  modal :: 고객사 등록 팝업 end -->

<!--  modal :: 고객사 수정 팝업  -->
<div class="modal" id="reg_edit_modal">
    <div class="modal_header">
        <h3>운영관리코드 수정</h3>
    </div>
    <div class="modal_body">
        <div class="modal_cont">
            <dl>
                <dt><h4><em class="">운영관리코드</em></h4></dt>
                <dd>
                    <div class="ipt_box">
                        <input type="text" id="editOprMngCode" name="required" readonly>
                        <input type="hidden" id="editCustCode">
                    </div>
                </dd>
            </dl>
            <dl class="">
                <dt><h4><em class="">dtmf</em></h4></dt>
                <dd>
                    <div class="ipt_box">
                        <input type="text" id="editDtmf" placeholder="DTMF를 입력해주세요" name="required">
                    </div>
                </dd>
            </dl>
            <dl class="">
                <dt><h4><em class="">라우팅url</em></h4></dt>
                <dd>
                    <div class="ipt_box">
                        <input type="text" id="editRouteUrl" placeholder="라우팅url을 입력해주세요" name="required">
                    </div>
                </dd>
            </dl>
        </div>
    </div>
    <div class="modal_footer">
        <div class="btn_wrap fr">
            <a href="#" id="btnEditOpr" class="btn">저장</a>
            <a href="#" class="btn grey" id="btnDelOprMngCode">삭제</a>
        </div>
    </div>
    <div class="modal_controller">
        <a href="#" class="close">close</a>
    </div>
</div>
<!--  modal :: 고객사 수정 팝업 end -->

</body>
</html>