<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/WEB-INF/view/include/tags.jspf" %>

<script src="/clfd/js/utils.js?ver=1.1"></script>
<script src="/clfd/js/validation/common-validation.js?ver=1.1"></script>
<script src="/clfd/js/user/userService.js?ver=1.1"></script>
<script src="/clfd/lib/cleave/cleave.min.js"></script>

<section class="content-header">
    <tg:menuInfoTag/>
</section>

<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-success">
                <div class="box-header">
                    <h3 class="box-title"><i class='material-icons' style='font-size:16px;'>play_arrow</i>&nbsp;&nbsp;기업 정보</h3>
                </div>
                <div class="box-body">
                    <form id="form" name="form" class="form-horizontal form-label-left">
                        <input type="hidden" id="companyId" value="${companyInfo.companyId}"/>
                        <table class="board_write">
                            <tbody>
                            <tr class="column-resize-height">
                                <td rowspan="5">
                                    <div id="companyLogo" style="width: auto; max-width: 200px;margin:auto;">
                                        <c:if test="${companyInfo.companyLogo ne null}">
                                            <img src="/clfd/image/${companyInfo.imageDir}_${companyInfo.companyLogo}" style="object-fit: contain;"/>
                                        </c:if>
                                        <c:if test="${companyInfo.companyLogo eq null}">
                                            <img src="/images/prod/NoImage.png" style="object-fit: contain;"/>
                                        </c:if>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <th>기업구분</th>
                                <td>${companyInfo.companyGbnNm}</td>
                                <th>기업명</th>
                                <td>${companyInfo.companyNm}</td>
                            </tr>
                            <tr>
                                <th>사업자번호</th>
                                <td>
                                    <c:out value="${fn:substring(companyInfo.saupjaNo, 0, 3)}-${fn:substring(companyInfo.saupjaNo, 3, 5)}-${fn:substring(companyInfo.saupjaNo, 5, 10)}"/>
                                </td>
                                <th>대표자명</th>
                                <td>${companyInfo.ownerName}</td>
                            </tr>
                            <tr>
                                <th>전화번호</th>
                                <td>
                                    <c:out value="${fn:substring(companyInfo.telNo, 0, 3)}-${fn:substring(companyInfo.telNo, 3, 6)}-${fn:substring(companyInfo.telNo, 6, 10)}"/>
                                </td>
                                <th>팩스번호</th>
                                <td>
                                    <c:out value="${fn:substring(companyInfo.faxNo, 0, 3)}-${fn:substring(companyInfo.faxNo, 3, 6)}-${fn:substring(companyInfo.faxNo, 6, 10)}"/>
                                </td>
                            </tr>
                            <tr>
                                <th>도메인 주소</th>
                                <td>${companyInfo.companySiteUrl}</td>
                                <th>주소</th>
                                <td>${companyInfo.addrM} ${companyInfo.addrD}</td>
                            </tr>
                            </tbody>
                        </table>
                    </form>
                </div>
                <div class="box-footer">
                    <div class="btn-group pull-right">
                    <c:if test="${userRole == 'ROLE_MANAGER'}">
                        <button type="button" class="btn btn-outline-primary" onclick="showUpdateModal()">기업정보수정</button>
                    </c:if>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade func-modal" data-keyboard="false" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h2>기업 정보 수정</h2>
                </div>
                <div class="modal-body">
                    <form id="funcForm" name="funcForm" class="form-horizontal" enctype="multipart/form-data" method="post">
                        <input type="hidden" name="companyId"/>
                        <input type="hidden" name="companyGbn"/>
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12">기업구분</label>
                            <div class="col-md-8 col-sm-8 col-xs-12">
                                <input type="text" name="companyGbnNm" class="form-control col-md-7 col-sm-7 col-xs-12" readonly>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12">기업명
                                <span class="text-danger">*</span></label>
                            <div class="col-md-8 col-sm-8 col-xs-12">
                                <input type="text" name="companyNm" class="form-control col-md-7 col-sm-7 col-xs-12" placeholder="기업명 입력" maxlength="50">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12">사업자번호
                                <span class="text-danger">*</span></label>
                            <div class="col-md-8 col-sm-8 col-xs-12">
                                <input type="text" name="saupjaNo" class="form-control col-md-7 col-sm-7 col-xs-12" placeholder="사업자번호 입력" maxlength="100" cleave="saupjaNo">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12">대표자명
                                <span class="text-danger">*</span></label>
                            <div class="col-md-8 col-sm-8 col-xs-12">
                                <input type="text" name="ownerName" class="form-control col-md-7 col-sm-7 col-xs-12" placeholder="대표자명 입력" maxlength="50">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12">전화번호
                                <span class="text-danger">*</span></label>
                            <div class="col-md-8 col-sm-8 col-xs-12">
                                <input type="text" name="telNo" class="form-control col-md-7 col-sm-7 col-xs-12" placeholder="전화번호 입력" maxlength="12" cleave="fax">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12">팩스번호
                                <span class="text-danger">*</span></label>
                            <div class="col-md-8 col-sm-8 col-xs-12">
                                <input type="text" name="faxNo" class="form-control col-md-7 col-sm-7 col-xs-12" placeholder="팩스번호 입력" maxlength="12" cleave="fax">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12">URL
                                <span class="text-danger">*</span></label>
                            <div class="col-md-8 col-sm-8 col-xs-12">
                                <input type="text" name="companySiteUrl" class="form-control col-md-7 col-sm-7 col-xs-12" placeholder="URL 입력" maxlength="100">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12">주소
                                <span class="text-danger">*</span></label>
                            <div class="col-md-8 col-sm-8 col-xs-12">
                                <input type="text" name="addrM" class="form-control col-md-7 col-sm-7 col-xs-12" placeholder="주소 입력">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12">상세주소</label>
                            <div class="col-md-8 col-sm-8 col-xs-12">
                                <input type="text" name="addrD" class="form-control col-md-7 col-sm-7 col-xs-12" placeholder="상세주소 입력">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12">기업로고<span>*</span></label>
                            <div class="col-md-8 col-sm-8 col-xs-12 filebox">
                                <input type="text" class="upload-name form-control" value="파일선택" disabled="disabled">
                                <label for="uploadFile">업로드</label>
                                <input type="file" id="uploadFile" name="uploadFile" class="upload-hidden" accept="image/*" onchange="fileCheck(this)">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-primary" onclick="save()">수정</button>
                    <button type="button" class="btn btn-outline-primary" data-dismiss="modal">닫기</button>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
    $(document).ready(function () {
        var fileTarget = $('.filebox .upload-hidden');
        fileTarget.on('change', function () {
            if(window.FileReader){
                var filename = $(this)[0].files[0].name;
            } else {
                var filename = $(this).val().split('/').pop().split('\\').pop();
            }
            $(this).siblings('.upload-name').val(filename);
        });

        $('input[cleave], textarea[cleave]').each(function (index, element) {
            var type = $(element).attr('cleave');
            var option = {};

            switch (type) {
                case 'saupjaNo':
                    option['numericOnly'] = true;
                    option['delimiter'] = '-';
                    option['blocks'] = [3, 2, 5];
                    option['stripLeadingZeroes'] = false;
                    break;
                case 'fax':
                    option['numericOnly'] = true;
                    option['delimiter'] = '-';
                    option['blocks'] = [3, 3, 4];
                    option['stripLeadingZeroes'] = false;
                    break;
                default:
                    break;
            }

            new Cleave(element, $.extend({}, option));

        });
    });

    function showUpdateModal() {
        $.ajax({
            url: "<c:url value='/system/company/list.json' />",
            type: "POST",
            data: {
                "companyId": $('#companyId').val()
            },
            dataType: "json",
            success: function (res) {
                if (res.result == "success") {
                    let form = document.funcForm;
                    const data = res.data;
                    form.companyId.value = data.companyId;
                    form.companyGbn.value = data.companyGbn;
                    form.companyGbnNm.value = data.companyGbnNm;
                    form.companyNm.value = data.companyNm;
                    form.saupjaNo.value = clfdUtils.appendHyphenToSaupjaNo(data.saupjaNo);
                    form.ownerName.value = data.ownerName;
                    form.telNo.value = clfdUtils.appendHyphenToFax(data.telNo);
                    form.faxNo.value = clfdUtils.appendHyphenToFax(data.faxNo);
                    form.companySiteUrl.value = data.companySiteUrl;
                    form.addrM.value = data.addrM;
                    form.addrD.value = data.addrD;

                    if(data.imageDir != null && data.companyLogo != null) {
                        $('.upload-name').val(data.companyLogo);
                    }

                    $('.func-modal').modal("show");
                } else {
                    alert(res.message);
                }
            }
        })
    }

    function save() {
        let form = document.funcForm;
        var companyParams = {
            // 기업정보
            companyId: form.companyId.value,
            companyGbn: form.companyGbn.value,
            companyNm: form.companyNm.value,
            saupjaNo: _.replace(form.saupjaNo.value, /\-/g, ''),
            ownerName: form.ownerName.value,
            telNo: _.replace(form.telNo.value, /\-/g, ''),
            faxNo: _.replace(form.faxNo.value, /\-/g, ''),
            companySiteUrl: form.companySiteUrl.value,
            addrM: form.addrM.value,
            addrD: form.addrD.value,
        };

        var updateAble = userService.companyCheckAll(companyParams);
        if (updateAble) {
            if (confirm("해당 기업을 수정하시겠습니까?")) {
                let funcForm = $('#funcForm')[0];
                let data = new FormData(funcForm);
                $.ajax({
                    url: "<c:url value='/system/company/update.json' />",
                    type: "POST",
                    enctype: "multipart/form-data",
                    data: data,
                    processData: false,
                    contentType: false,
                    success: function (res) {
                        if (res.result == "success") {
                            alert("수정되었습니다.");
                            $('.func-modal').modal("hide");
                            location.reload();
                        } else {
                            alert(res.message);
                        }
                    }
                })
            }
        }
    }

    function fileCheck(obj) {
        if (!/\.(jpeg|jpg|png|gif|bmp)$/i.test(obj.value)) {
            alert('이미지 파일만 업로드 가능합니다.');
            $('.upload-name').val('파일선택');
            obj.value = '';
            obj.focus();
        }
    }

</script>

