<?php
$CURRENT_SECTION = 'tenant';
include '../inc/header.php';

// 샘플: 세션 또는 DB에서 기존 데이터 조회 (실제로는 DB에서 가져와야 함)
$tenant = [
    'company_name' => isset($_SESSION['company_name']) ? $_SESSION['company_name'] : '샘플주식회사',
    'code' => isset($_SESSION['tenant_code']) ? $_SESSION['tenant_code'] : 'samplecorp',
    'email' => isset($_SESSION['tenant_email']) ? $_SESSION['tenant_email'] : 'admin@sample.com',
    'phone' => isset($_SESSION['tenant_phone']) ? $_SESSION['tenant_phone'] : '02-1234-5678',
    'address' => isset($_SESSION['tenant_address']) ? $_SESSION['tenant_address'] : '서울시 강남구',
    'business_num' => isset($_SESSION['tenant_business_num']) ? $_SESSION['tenant_business_num'] : '1234567890',
    'corp_reg_no' => isset($_SESSION['tenant_corp_reg_no']) ? $_SESSION['tenant_corp_reg_no'] : '',
    'ceo_name' => isset($_SESSION['tenant_ceo_name']) ? $_SESSION['tenant_ceo_name'] : '홍길동',
    'homepage' => isset($_SESSION['tenant_homepage']) ? $_SESSION['tenant_homepage'] : '',
    'fax' => isset($_SESSION['tenant_fax']) ? $_SESSION['tenant_fax'] : '',
    'admin_memo' => isset($_SESSION['tenant_admin_memo']) ? $_SESSION['tenant_admin_memo'] : '',
    'max_users' => isset($_SESSION['tenant_max_users']) ? $_SESSION['tenant_max_users'] : 10,
    'logo' => isset($_SESSION['tenant_logo']) ? $_SESSION['tenant_logo'] : '',
];
?>

    <div class="container" style="max-width:600px; margin-top:40px; margin-bottom:40px;">
        <div class="card shadow p-4">
            <form id="tenantEditForm" method="post" action="/tenant/tenant/edit_process.php" enctype="multipart/form-data" autocomplete="off">
                <h3 class="mb-4 text-center">회사(테넌트) 정보 수정</h3>

                <div class="mb-3">
                    <label class="form-label">회사/조직명 <span class="text-danger">*</span></label>
                    <input type="text" class="form-control" name="company_name" maxlength="100" value="<?= htmlspecialchars($tenant['company_name'])?>" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">회사 코드 <span class="text-danger">*</span></label>
                    <input type="text" class="form-control" name="code" maxlength="50" value="<?= htmlspecialchars($tenant['code'])?>" readonly>
                </div>
                <div class="mb-3">
                    <label class="form-label">대표 이메일</label>
                    <input type="email" class="form-control" name="email" maxlength="80" value="<?= htmlspecialchars($tenant['email'])?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">대표 전화번호</label>
                    <input type="text" class="form-control" name="phone" maxlength="20" value="<?= htmlspecialchars($tenant['phone'])?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">주소</label>
                    <input type="text" class="form-control" name="address" maxlength="255" value="<?= htmlspecialchars($tenant['address'])?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">사업자등록번호</label>
                    <input type="text" class="form-control" name="business_num" maxlength="12" value="<?= htmlspecialchars($tenant['business_num'])?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">법인등록번호</label>
                    <input type="text" class="form-control" name="corp_reg_no" maxlength="13" value="<?= htmlspecialchars($tenant['corp_reg_no'])?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">대표자명</label>
                    <input type="text" class="form-control" name="ceo_name" maxlength="50" value="<?= htmlspecialchars($tenant['ceo_name'])?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">홈페이지 주소</label>
                    <input type="url" class="form-control" name="homepage" maxlength="255" value="<?= htmlspecialchars($tenant['homepage'])?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">팩스번호</label>
                    <input type="text" class="form-control" name="fax" maxlength="30" value="<?= htmlspecialchars($tenant['fax'])?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">회사 로고</label>
                    <?php if ($tenant['logo']) { ?>
                        <div class="mb-2"><img src="<?= htmlspecialchars($tenant['logo'])?>" alt="로고" style="height:40px;"></div>
                    <?php } ?>
                    <input type="file" class="form-control" name="logo" accept="image/*">
                </div>
                <div class="mb-3">
                    <label class="form-label">관리자 메모</label>
                    <textarea class="form-control" name="admin_memo" maxlength="500" rows="2"><?= htmlspecialchars($tenant['admin_memo'])?></textarea>
                </div>
                <div class="mb-3">
                    <label class="form-label">최대 사용자 수</label>
                    <input type="number" class="form-control" name="max_users" min="1" max="999" value="<?= htmlspecialchars($tenant['max_users'])?>">
                </div>
                <button type="submit" class="btn btn-primary w-100 mt-2">수정하기</button>
            </form>
        </div>
    </div>

    <script>
        $(function(){
            $('#tenantEditForm').on('submit', function(e){
                var company = $('[name="company_name"]').val().trim();
                var biz = $('[name="business_num"]').val();
                if (company.length < 2) {
                    alert('회사명을 2글자 이상 입력하세요.');
                    $('[name="company_name"]').focus();
                    e.preventDefault(); return false;
                }
                if (biz && !/^\d{10}$/.test(biz)) {
                    alert('사업자등록번호는 10자리 숫자만 입력하세요.');
                    $('[name="business_num"]').focus();
                    e.preventDefault(); return false;
                }
                // 기타 밸리데이션 추가 가능
            });
        });
    </script>

<?php include '../inc/footer.php'; ?>
