/* ##############################################################
	설  명 : [Modal]직원 검색 팝업
	작성일 : 2024. 04. 02.
	작성자 : NTsoft
	수정일 	        수정자 	 수정내용
	-------         -------- ---------------------------
	2024. 03. 14.	NTsoft 	 최초 생성
*/
let memberSearchModal = {
    callback: null,
    reqParam: null,
    dataList: null,
    /* 모달 초기화 작업 */
    init: function () {
        $('#memberSearchModal').remove();
        memberSearchModal.searchMemberList();
        $('body').append(this.HtmlTemplate);
        //그리드 이벤트
        this.setEvent();
    },
    /* 그리드 이벤트 설정 */
    setEvent: function () {
        $(document).on('keypress', '#memberSearchKeyword', function(e) {
            fn_attendanceEnter(e);
        });
        $('#memberSearchModal .btnCancle').on("click", function () {
            memberSearchModal.close();
        });

        $('#memberSearchModal .btnSave').on("click", function () {
            memberSearchModal.save();
        });
    },
    /* 데이터 선택 */
    setSelect: function (selectNumber) {
        let selectMemberName = $('#' + selectNumber).text();
        let selectUserPhoneNumber = $('#' + selectNumber).closest('tr').find('td:eq(3)').text();
        let obj=
            {'selectMemberId': selectNumber,
                'selectMemberName': selectMemberName,
                'introUserPhoneNumber':selectUserPhoneNumber};
        if (memberSearchModal.callback) {
            memberSearchModal.callback(obj);

        }
        memberSearchModal.close();
    },
    searchMemberList:function (searchKeyword){

        let formData = new FormData();
        formData.append("menuClass", menuClass);
        formData.append("memberSearchKeywordParam", searchKeyword??'');

        $.ajax({
            url: encodeURI('/webmember/selectListMember.do'),
            data: formData,
            dataType: "json",
            processData: false,
            contentType: false,
            type: 'POST',
            async: true,
            success: function (data) {
                if ('0' == data.msgCode) {
                    let tbody = document.querySelector('.list1_modal_table tbody');
                    while (tbody.firstChild) {
                        tbody.removeChild(tbody.firstChild);
                    }
                    data.rows.forEach(function(member) {
                        let tr = document.createElement('tr');
                        tr.innerHTML = '<td id="' + member.muMemberId + '" style="color: #3985EA;cursor: pointer;">' + member.memberName+'('+member.loginId+')</td>' +
                            '<td>' + fn_setFormatPhone(member.phoneNumber) + '</td>' +
                            '<td>' + member.groupName + '</td>' +
                            '<td>' + member.dutyName + '</td>'+
                            '<td>' + member.authName + '</td>';
                        tbody.appendChild(tr);
                    });
                    tbody.addEventListener('click', function(event) {
                        if (event.target.tagName === 'TD' && event.target.id) {
                            memberSearchModal.setSelect(event.target.id);
                        }
                    });
                }
            },
            error: function (xhr, status, error) {
                modalEvent.danger("조회 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
            },
            beforeSend: function () {
            },
            complete: function () {
            },
        })
    },
    /*
     * 팝업
     * callback : 콜백함수, reqParam: 팝업파라미터
     */
    popup: function (callback, reqParam) {
        // 초기화 후 열기
        memberSearchModal.init();

        //콜백함수 지정.
        memberSearchModal.callback = callback;
        memberSearchModal.reqParam = reqParam;
        memberSearchModal.dataList = null;

        setTimeout(function () {
            $('#memberSearchModal').modal('show')
        }, 200);

    },
    /*
     * 팝업 닫기
     */
    close: function () {
        $('#memberSearchModal').modal('hide');

		setTimeout(function(){
			$('#memberSearchModal').remove();
		},500);
    },
    HtmlTemplate :`
        <div id="memberSearchModal" class="modal list1_diagnosis fade" tabIndex="-1" aria-labelledby="exampleModalLabel" aria-hidden="">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="list1_modal_header">
                        <label>직원검색</label>
                    </div>
                    <div class="list1_modal_body">
                        <div class="input_box">
                            <div class="search_box">
                                <img src="../../../../image/web/search_B.svg"/>
                                <input id="memberSearchKeyword" type="text" placeholder="이름, 연락처"/>
                            </div>
                            <button id="searchMemberBtn">검색</button>
                        </div>
                        <div class="list1_modal_table">
                            <table>
                                <thead>
                                <tr>
                                    <th>이름</th>
                                    <th>연락처</th>
                                    <th>조직</th>
                                    <th>직무</th>
                                    <th>권한그룹</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr>
                                    <td>홍직원(test1)</td>
                                    <td>010-0000-0000</td>
                                    <td>피부과</td>
                                    <td>닥터</td>
                                    <td>관리자</td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="list1_modal_footer">
                        <div class="btn_box">
                            <button type="button" class="cancel_btn btnCancle">취소</button>
                        </div>
                    </div>
                </div>
            </div>
            <script>
                $("#searchMemberBtn").on('click',()=>{
                    let searchKeyword = document.querySelector("#memberSearchKeyword").value;
                    memberSearchModal.searchMemberList(searchKeyword);
                });
            </script>
        </div>`
};










