<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>한약 재고관리 시스템</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans KR', sans-serif;
            background-color: #f8f9fa;
        }
        .navbar-brand {
            font-weight: bold;
            color: #2c3e50 !important;
        }
        .sidebar {
            min-height: calc(100vh - 56px);
            background-color: #fff;
            box-shadow: 2px 0 5px rgba(0,0,0,0.05);
        }
        .sidebar .nav-link {
            color: #495057;
            padding: 12px 20px;
            border-left: 3px solid transparent;
            transition: all 0.3s;
        }
        .sidebar .nav-link:hover {
            background-color: #f8f9fa;
            border-left-color: #007bff;
        }
        .sidebar .nav-link.active {
            background-color: #e7f1ff;
            border-left-color: #007bff;
            color: #007bff;
            font-weight: 500;
        }
        .content-area {
            padding: 20px;
        }
        .stat-card {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.08);
            margin-bottom: 20px;
        }
        .stat-card h5 {
            color: #6c757d;
            font-size: 14px;
            margin-bottom: 10px;
        }
        .stat-card .value {
            font-size: 24px;
            font-weight: bold;
            color: #2c3e50;
        }
        .main-content {
            display: none;
        }
        .main-content.active {
            display: block;
        }
    </style>
</head>
<body>
    <!-- Navigation Bar -->
    <nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">
                <i class="bi bi-heart-pulse-fill text-primary"></i> 한약 재고관리 시스템
            </a>
            <div class="navbar-nav ms-auto">
                <span class="navbar-text me-3">
                    <i class="bi bi-person-circle"></i> 관리자
                </span>
                <button class="btn btn-outline-secondary btn-sm">
                    <i class="bi bi-box-arrow-right"></i> 로그아웃
                </button>
            </div>
        </div>
    </nav>

    <div class="container-fluid">
        <div class="row">
            <!-- Sidebar -->
            <div class="col-md-2 sidebar p-0">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link active" href="#" data-page="dashboard">
                            <i class="bi bi-speedometer2"></i> 대시보드
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#" data-page="patients">
                            <i class="bi bi-people"></i> 환자 관리
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#" data-page="purchase">
                            <i class="bi bi-file-earmark-arrow-up"></i> 입고 관리
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#" data-page="formulas">
                            <i class="bi bi-journal-medical"></i> 처방 관리
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#" data-page="compound">
                            <i class="bi bi-prescription2"></i> 조제 관리
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#" data-page="inventory">
                            <i class="bi bi-box-seam"></i> 재고 현황
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#" data-page="herbs">
                            <i class="bi bi-flower1"></i> 약재 관리
                        </a>
                    </li>
                </ul>
            </div>

            <!-- Main Content Area -->
            <div class="col-md-10 content-area">
                <!-- Dashboard Page -->
                <div id="dashboard" class="main-content active">
                    <h3 class="mb-4">대시보드</h3>
                    <div class="row">
                        <div class="col-md-3">
                            <div class="stat-card">
                                <h5><i class="bi bi-people-fill"></i> 총 환자수</h5>
                                <div class="value" id="totalPatients">0</div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card">
                                <h5><i class="bi bi-box-seam-fill"></i> 재고 품목</h5>
                                <div class="value" id="totalHerbs">0</div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card">
                                <h5><i class="bi bi-calendar-check"></i> 오늘 조제</h5>
                                <div class="value" id="todayCompounds">0</div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card">
                                <h5><i class="bi bi-cash-stack"></i> 재고 자산</h5>
                                <div class="value" id="inventoryValue">0</div>
                            </div>
                        </div>
                    </div>

                    <div class="row mt-4">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="mb-0">최근 조제 내역</h5>
                                </div>
                                <div class="card-body">
                                    <table class="table table-hover">
                                        <thead>
                                            <tr>
                                                <th>조제일</th>
                                                <th>환자명</th>
                                                <th>처방명</th>
                                                <th>제수</th>
                                                <th>파우치</th>
                                                <th>상태</th>
                                            </tr>
                                        </thead>
                                        <tbody id="recentCompounds">
                                            <!-- Dynamic content -->
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Patients Page -->
                <div id="patients" class="main-content">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h3>환자 관리</h3>
                        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#patientModal">
                            <i class="bi bi-person-plus"></i> 새 환자 등록
                        </button>
                    </div>
                    <div class="card">
                        <div class="card-body">
                            <div class="mb-3">
                                <input type="text" class="form-control" id="patientSearch" placeholder="환자명 또는 전화번호로 검색...">
                            </div>
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>환자명</th>
                                        <th>전화번호</th>
                                        <th>성별</th>
                                        <th>생년월일</th>
                                        <th width="80">처방 횟수</th>
                                        <th>메모</th>
                                        <th width="180">작업</th>
                                    </tr>
                                </thead>
                                <tbody id="patientsList">
                                    <!-- Dynamic content -->
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <!-- Purchase Page -->
                <div id="purchase" class="main-content">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h3>입고 관리</h3>
                    </div>

                    <!-- 입고장 목록 -->
                    <div class="card mb-4">
                        <div class="card-header">
                            <h5 class="mb-0">입고장 목록</h5>
                        </div>
                        <div class="card-body">
                            <div class="row mb-3">
                                <div class="col-md-3">
                                    <label class="form-label">시작일</label>
                                    <input type="date" class="form-control" id="purchaseStartDate">
                                </div>
                                <div class="col-md-3">
                                    <label class="form-label">종료일</label>
                                    <input type="date" class="form-control" id="purchaseEndDate">
                                </div>
                                <div class="col-md-3">
                                    <label class="form-label">공급업체</label>
                                    <select class="form-control" id="purchaseSupplier">
                                        <option value="">전체</option>
                                    </select>
                                </div>
                                <div class="col-md-3">
                                    <label class="form-label">&nbsp;</label>
                                    <button class="btn btn-primary w-100" id="searchPurchaseBtn">
                                        <i class="bi bi-search"></i> 조회
                                    </button>
                                </div>
                            </div>
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>입고일</th>
                                        <th>공급업체</th>
                                        <th>품목 수</th>
                                        <th>총 금액</th>
                                        <th>총 수량</th>
                                        <th>파일명</th>
                                        <th>작업</th>
                                    </tr>
                                </thead>
                                <tbody id="purchaseReceiptsList">
                                    <!-- Dynamic content -->
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <!-- Excel 업로드 -->
                    <div class="card">
                        <div class="card-header d-flex justify-content-between align-items-center">
                            <h5 class="mb-0">새 입고 등록 (Excel 업로드)</h5>
                            <button class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#supplierModal">
                                <i class="bi bi-plus"></i> 도매상 등록
                            </button>
                        </div>
                        <div class="card-body">
                            <form id="purchaseUploadForm" enctype="multipart/form-data">
                                <div class="row">
                                    <div class="col-md-6">
                                        <label for="uploadSupplier" class="form-label">도매상 선택 *</label>
                                        <select class="form-control" id="uploadSupplier" required>
                                            <option value="">도매상을 선택하세요</option>
                                        </select>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="purchaseFile" class="form-label">Excel 파일 선택 *</label>
                                        <input type="file" class="form-control" id="purchaseFile" accept=".xlsx,.xls" required>
                                    </div>
                                </div>
                                <div class="form-text mt-2">
                                    <i class="bi bi-info-circle"></i> Excel 형식: 한의사랑, 한의정보 (자동 감지)<br>
                                    <i class="bi bi-info-circle"></i> Excel 내 업체명은 제조사(제약사)로 저장됩니다
                                </div>
                                <button type="submit" class="btn btn-primary mt-3">
                                    <i class="bi bi-upload"></i> 업로드 및 처리
                                </button>
                            </form>
                            <div id="uploadResult" class="mt-3"></div>
                        </div>
                    </div>
                </div>

                <!-- Formulas Page -->
                <div id="formulas" class="main-content">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h3>처방 관리</h3>
                        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#formulaModal">
                            <i class="bi bi-plus-circle"></i> 새 처방 등록
                        </button>
                    </div>
                    <div class="card">
                        <div class="card-body">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>처방코드</th>
                                        <th>처방명</th>
                                        <th>기본 첩수</th>
                                        <th>기본 파우치</th>
                                        <th>구성 약재</th>
                                        <th>작업</th>
                                    </tr>
                                </thead>
                                <tbody id="formulasList">
                                    <!-- Dynamic content -->
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <!-- Compound Page -->
                <div id="compound" class="main-content">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h3>조제 관리</h3>
                        <button class="btn btn-primary" id="newCompoundBtn">
                            <i class="bi bi-plus-circle"></i> 새 조제
                        </button>
                    </div>
                    <div class="card" id="compoundForm" style="display: none;">
                        <div class="card-body">
                            <h5>조제 입력</h5>
                            <form id="compoundEntryForm">
                                <div class="row">
                                    <div class="col-md-6">
                                        <label class="form-label">환자 선택</label>
                                        <select class="form-control" id="compoundPatient" required>
                                            <option value="">환자를 선택하세요</option>
                                        </select>
                                    </div>
                                    <div class="col-md-6">
                                        <label class="form-label">처방 선택</label>
                                        <select class="form-control" id="compoundFormula" required>
                                            <option value="">처방을 선택하세요</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="row mt-3">
                                    <div class="col-md-4">
                                        <label class="form-label">제수</label>
                                        <input type="number" class="form-control" id="jeCount" value="1" min="0.5" step="0.5" required>
                                    </div>
                                    <div class="col-md-4">
                                        <label class="form-label">총 첩수</label>
                                        <input type="number" class="form-control" id="cheopTotal" readonly>
                                    </div>
                                    <div class="col-md-4">
                                        <label class="form-label">총 파우치수</label>
                                        <input type="number" class="form-control" id="pouchTotal" required>
                                    </div>
                                </div>
                                <div class="mt-3">
                                    <h6>약재 구성 (가감 가능)</h6>
                                    <table class="table table-sm">
                                        <thead>
                                            <tr>
                                                <th>약재명</th>
                                                <th>1첩당 용량(g)</th>
                                                <th>총 용량(g)</th>
                                                <th>제품/로트 선택</th>
                                                <th>재고</th>
                                                <th>작업</th>
                                            </tr>
                                        </thead>
                                        <tbody id="compoundIngredients">
                                            <!-- Dynamic content -->
                                        </tbody>
                                    </table>
                                    <button type="button" class="btn btn-sm btn-outline-primary" id="addIngredientBtn">
                                        <i class="bi bi-plus"></i> 약재 추가
                                    </button>
                                </div>
                                <div class="mt-3">
                                    <button type="submit" class="btn btn-success">
                                        <i class="bi bi-check-circle"></i> 조제 실행
                                    </button>
                                    <button type="button" class="btn btn-secondary ms-2" id="cancelCompoundBtn">
                                        취소
                                    </button>
                                </div>
                            </form>
                        </div>
                    </div>

                    <div class="card mt-4">
                        <div class="card-header bg-light">
                            <div class="d-flex justify-content-between align-items-center">
                                <h5 class="mb-0"><i class="bi bi-clipboard-pulse"></i> 조제 내역</h5>
                                <div>
                                    <span class="badge bg-primary me-2">오늘 조제: <span id="todayCompoundCount">0</span>건</span>
                                    <span class="badge bg-info">이번달 조제: <span id="monthCompoundCount">0</span>건</span>
                                </div>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="row mb-3">
                                <div class="col-md-4">
                                    <input type="date" class="form-control" id="compoundDateFilter" placeholder="날짜 선택">
                                </div>
                                <div class="col-md-4">
                                    <input type="text" class="form-control" id="compoundPatientFilter" placeholder="환자명 검색">
                                </div>
                                <div class="col-md-4">
                                    <button class="btn btn-outline-secondary" id="refreshCompoundsBtn">
                                        <i class="bi bi-arrow-clockwise"></i> 새로고침
                                    </button>
                                    <button class="btn btn-outline-info ms-2" id="exportCompoundsBtn">
                                        <i class="bi bi-download"></i> 엑셀 다운로드
                                    </button>
                                </div>
                            </div>
                            <div class="table-responsive">
                                <table class="table table-hover table-striped">
                                    <thead class="table-dark">
                                        <tr>
                                            <th width="40">#</th>
                                            <th width="100">조제일시</th>
                                            <th width="80">환자명</th>
                                            <th width="100">연락처</th>
                                            <th>처방명</th>
                                            <th width="60">제수</th>
                                            <th width="60">첩수</th>
                                            <th width="80">파우치</th>
                                            <th width="100">원가</th>
                                            <th width="100">판매가</th>
                                            <th width="80">상태</th>
                                            <th width="100">처방전번호</th>
                                            <th width="120">작업</th>
                                        </tr>
                                    </thead>
                                    <tbody id="compoundsList">
                                        <!-- Dynamic content -->
                                    </tbody>
                                </table>
                            </div>
                            <nav aria-label="Page navigation">
                                <ul class="pagination justify-content-center" id="compoundsPagination">
                                    <!-- Dynamic pagination -->
                                </ul>
                            </nav>
                        </div>
                    </div>

                    <!-- 조제 상세보기 모달 -->
                    <div class="modal fade" id="compoundDetailModal" tabindex="-1">
                        <div class="modal-dialog modal-xl">
                            <div class="modal-content">
                                <div class="modal-header bg-info text-white">
                                    <h5 class="modal-title"><i class="bi bi-file-medical"></i> 조제 상세 정보</h5>
                                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                                </div>
                                <div class="modal-body">
                                    <div class="row mb-3">
                                        <div class="col-md-6">
                                            <div class="card">
                                                <div class="card-header bg-light">
                                                    <h6 class="mb-0"><i class="bi bi-person"></i> 환자 정보</h6>
                                                </div>
                                                <div class="card-body">
                                                    <dl class="row mb-0">
                                                        <dt class="col-sm-4">환자명:</dt>
                                                        <dd class="col-sm-8" id="detailPatientName"></dd>
                                                        <dt class="col-sm-4">연락처:</dt>
                                                        <dd class="col-sm-8" id="detailPatientPhone"></dd>
                                                        <dt class="col-sm-4">조제일:</dt>
                                                        <dd class="col-sm-8" id="detailCompoundDate"></dd>
                                                    </dl>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="card">
                                                <div class="card-header bg-light">
                                                    <h6 class="mb-0"><i class="bi bi-journal-medical"></i> 처방 정보</h6>
                                                </div>
                                                <div class="card-body">
                                                    <dl class="row mb-0">
                                                        <dt class="col-sm-4">처방명:</dt>
                                                        <dd class="col-sm-8" id="detailFormulaName"></dd>
                                                        <dt class="col-sm-4">처방전번호:</dt>
                                                        <dd class="col-sm-8" id="detailPrescriptionNo"></dd>
                                                        <dt class="col-sm-4">제수/첩수/파우치:</dt>
                                                        <dd class="col-sm-8" id="detailQuantities"></dd>
                                                    </dl>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="card">
                                        <div class="card-header bg-light">
                                            <h6 class="mb-0"><i class="bi bi-list-ul"></i> 처방 구성 약재</h6>
                                        </div>
                                        <div class="card-body">
                                            <table class="table table-sm">
                                                <thead>
                                                    <tr>
                                                        <th>약재명</th>
                                                        <th>보험코드</th>
                                                        <th>1첩당 용량</th>
                                                        <th>총 사용량</th>
                                                        <th>비고</th>
                                                    </tr>
                                                </thead>
                                                <tbody id="detailIngredients">
                                                    <!-- Dynamic content -->
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>

                                    <div class="card mt-3">
                                        <div class="card-header bg-light">
                                            <h6 class="mb-0"><i class="bi bi-box-seam"></i> 재고 소비 내역</h6>
                                        </div>
                                        <div class="card-body">
                                            <table class="table table-sm">
                                                <thead>
                                                    <tr>
                                                        <th>약재명</th>
                                                        <th>원산지</th>
                                                        <th>공급처</th>
                                                        <th>사용량</th>
                                                        <th>단가</th>
                                                        <th>금액</th>
                                                    </tr>
                                                </thead>
                                                <tbody id="detailConsumptions">
                                                    <!-- Dynamic content -->
                                                </tbody>
                                                <tfoot>
                                                    <tr>
                                                        <th colspan="5" class="text-end">총 원가:</th>
                                                        <th id="detailTotalCost"></th>
                                                    </tr>
                                                </tfoot>
                                            </table>
                                        </div>
                                    </div>

                                    <div class="row mt-3">
                                        <div class="col-md-12">
                                            <label>비고:</label>
                                            <p id="detailNotes" class="form-control-plaintext"></p>
                                        </div>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-info" id="printCompoundBtn">
                                        <i class="bi bi-printer"></i> 인쇄
                                    </button>
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">닫기</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Inventory Page -->
                <div id="inventory" class="main-content">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h3><i class="bi bi-box-seam"></i> 재고 현황</h3>
                        <div>
                            <button class="btn btn-warning me-2" id="showStockAdjustmentBtn">
                                <i class="bi bi-sliders"></i> 재고 보정
                            </button>
                            <button class="btn btn-outline-warning me-2" id="showAdjustmentHistoryBtn">
                                <i class="bi bi-clock-history"></i> 보정 내역
                            </button>
                            <button class="btn btn-outline-info" id="showStockLedgerBtn">
                                <i class="bi bi-journal-text"></i> 입출고 원장
                            </button>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-3">
                            <div class="stat-card bg-primary text-white">
                                <h6>총 재고 금액</h6>
                                <h4 id="totalInventoryValue">₩0</h4>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card bg-success text-white">
                                <h6>재고 보유 약재</h6>
                                <h4 id="totalHerbsInStock">0종</h4>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card bg-warning text-white">
                                <h6>오늘 입고</h6>
                                <h4 id="todayPurchases">0건</h4>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card bg-info text-white">
                                <h6>오늘 출고</h6>
                                <h4 id="todayConsumptions">0건</h4>
                            </div>
                        </div>
                    </div>

                    <div class="card">
                        <div class="card-body">
                            <div class="mb-3">
                                <input type="text" class="form-control" id="inventorySearch" placeholder="약재명으로 검색...">
                            </div>
                            <div class="table-responsive">
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <th>보험코드</th>
                                            <th>약재명</th>
                                            <th>현재 재고(g)</th>
                                            <th>로트 수</th>
                                            <th>평균 단가</th>
                                            <th>재고 금액</th>
                                            <th>작업</th>
                                        </tr>
                                    </thead>
                                    <tbody id="inventoryList">
                                        <!-- Dynamic content -->
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>

                    <!-- 재고 원장 모달 -->
                    <div class="modal fade" id="stockLedgerModal" tabindex="-1">
                        <div class="modal-dialog modal-xl">
                            <div class="modal-content">
                                <div class="modal-header bg-dark text-white">
                                    <h5 class="modal-title"><i class="bi bi-journal-text"></i> 입출고 원장</h5>
                                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                                </div>
                                <div class="modal-body">
                                    <div class="row mb-3">
                                        <div class="col-md-6">
                                            <select class="form-control" id="ledgerHerbFilter">
                                                <option value="">전체 약재</option>
                                            </select>
                                        </div>
                                        <div class="col-md-6">
                                            <select class="form-control" id="ledgerTypeFilter">
                                                <option value="">전체 내역</option>
                                                <option value="RECEIPT">입고만</option>
                                                <option value="CONSUME">출고만</option>
                                                <option value="ADJUST">보정만</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="table-responsive" style="max-height: 500px; overflow-y: auto;">
                                        <table class="table table-sm table-striped">
                                            <thead class="table-dark sticky-top">
                                                <tr>
                                                    <th>일시</th>
                                                    <th>구분</th>
                                                    <th>약재명</th>
                                                    <th>수량(g)</th>
                                                    <th>단가</th>
                                                    <th>원산지</th>
                                                    <th>공급처/환자</th>
                                                    <th>참조번호</th>
                                                </tr>
                                            </thead>
                                            <tbody id="stockLedgerList">
                                                <!-- Dynamic content -->
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">닫기</button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 재고 보정 모달 -->
                    <div class="modal fade" id="stockAdjustmentModal" tabindex="-1">
                        <div class="modal-dialog modal-xl">
                            <div class="modal-content">
                                <div class="modal-header bg-warning text-dark">
                                    <h5 class="modal-title"><i class="bi bi-sliders"></i> 재고 보정</h5>
                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                </div>
                                <div class="modal-body">
                                    <form id="stockAdjustmentForm">
                                        <div class="row mb-3">
                                            <div class="col-md-4">
                                                <label class="form-label">보정일자 *</label>
                                                <input type="date" class="form-control" id="adjustmentDate" required>
                                            </div>
                                            <div class="col-md-4">
                                                <label class="form-label">보정 유형 *</label>
                                                <select class="form-control" id="adjustmentType" required>
                                                    <option value="">선택하세요</option>
                                                    <option value="LOSS">감모/손실</option>
                                                    <option value="FOUND">발견</option>
                                                    <option value="RECOUNT">재고조사</option>
                                                    <option value="DAMAGE">파손</option>
                                                    <option value="EXPIRE">유통기한 경과</option>
                                                </select>
                                            </div>
                                            <div class="col-md-4">
                                                <label class="form-label">담당자</label>
                                                <input type="text" class="form-control" id="adjustmentCreatedBy" placeholder="담당자 이름">
                                            </div>
                                        </div>

                                        <div class="row mb-3">
                                            <div class="col-md-12">
                                                <label class="form-label">비고</label>
                                                <textarea class="form-control" id="adjustmentNotes" rows="2" placeholder="보정 사유를 입력하세요"></textarea>
                                            </div>
                                        </div>

                                        <hr>

                                        <div class="d-flex justify-content-between align-items-center mb-3">
                                            <h6><i class="bi bi-list-check"></i> 보정 대상 약재</h6>
                                            <button type="button" class="btn btn-sm btn-primary" id="addAdjustmentItemBtn">
                                                <i class="bi bi-plus-circle"></i> 약재 추가
                                            </button>
                                        </div>

                                        <div class="table-responsive">
                                            <table class="table table-sm table-bordered">
                                                <thead class="table-light">
                                                    <tr>
                                                        <th width="200">약재명</th>
                                                        <th width="250">로트/원산지</th>
                                                        <th width="100">보정 전 재고</th>
                                                        <th width="100">보정 후 재고</th>
                                                        <th width="80">증감량</th>
                                                        <th>보정 사유</th>
                                                        <th width="60">작업</th>
                                                    </tr>
                                                </thead>
                                                <tbody id="adjustmentItemsList">
                                                    <!-- Dynamic content -->
                                                </tbody>
                                            </table>
                                        </div>

                                        <div class="alert alert-info mt-3">
                                            <i class="bi bi-info-circle"></i>
                                            <strong>안내:</strong> 보정 전 재고는 현재 시스템 재고가 자동으로 표시됩니다.
                                            실사 재고를 "보정 후 재고"에 입력하면 증감량이 자동 계산됩니다.
                                        </div>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">취소</button>
                                    <button type="button" class="btn btn-warning" id="saveAdjustmentBtn">
                                        <i class="bi bi-check-circle"></i> 보정 실행
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 재고 보정 내역 모달 -->
                    <div class="modal fade" id="adjustmentHistoryModal" tabindex="-1">
                        <div class="modal-dialog modal-xl">
                            <div class="modal-content">
                                <div class="modal-header bg-warning text-dark">
                                    <h5 class="modal-title"><i class="bi bi-clock-history"></i> 재고 보정 내역</h5>
                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                </div>
                                <div class="modal-body">
                                    <div class="table-responsive">
                                        <table class="table table-hover">
                                            <thead class="table-dark">
                                                <tr>
                                                    <th width="120">보정일자</th>
                                                    <th width="150">보정번호</th>
                                                    <th width="100">보정 유형</th>
                                                    <th width="80">항목 수</th>
                                                    <th width="100">담당자</th>
                                                    <th>비고</th>
                                                    <th width="120">작업</th>
                                                </tr>
                                            </thead>
                                            <tbody id="adjustmentHistoryList">
                                                <!-- Dynamic content -->
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">닫기</button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 재고 보정 상세 모달 -->
                    <div class="modal fade" id="adjustmentDetailModal" tabindex="-1">
                        <div class="modal-dialog modal-xl">
                            <div class="modal-content">
                                <div class="modal-header bg-info text-white">
                                    <h5 class="modal-title"><i class="bi bi-file-text"></i> 재고 보정 상세</h5>
                                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                                </div>
                                <div class="modal-body">
                                    <!-- 보정 헤더 정보 -->
                                    <div class="row mb-4">
                                        <div class="col-md-6">
                                            <div class="card">
                                                <div class="card-body">
                                                    <h6 class="card-title">보정 정보</h6>
                                                    <table class="table table-sm table-borderless">
                                                        <tr>
                                                            <th width="100">보정번호:</th>
                                                            <td id="detailAdjustmentNo"></td>
                                                        </tr>
                                                        <tr>
                                                            <th>보정일자:</th>
                                                            <td id="detailAdjustmentDate"></td>
                                                        </tr>
                                                        <tr>
                                                            <th>보정 유형:</th>
                                                            <td id="detailAdjustmentType"></td>
                                                        </tr>
                                                        <tr>
                                                            <th>담당자:</th>
                                                            <td id="detailAdjustmentCreatedBy"></td>
                                                        </tr>
                                                    </table>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="card">
                                                <div class="card-body">
                                                    <h6 class="card-title">비고</h6>
                                                    <p id="detailAdjustmentNotes" class="mb-0"></p>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- 보정 상세 항목 -->
                                    <h6><i class="bi bi-list-check"></i> 보정 상세 내역</h6>
                                    <div class="table-responsive">
                                        <table class="table table-sm table-bordered">
                                            <thead class="table-light">
                                                <tr>
                                                    <th>약재명</th>
                                                    <th>보험코드</th>
                                                    <th>원산지</th>
                                                    <th>로트ID</th>
                                                    <th>보정 전</th>
                                                    <th>보정 후</th>
                                                    <th>증감량</th>
                                                    <th>사유</th>
                                                </tr>
                                            </thead>
                                            <tbody id="detailAdjustmentItems">
                                                <!-- Dynamic content -->
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">닫기</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Herbs Page -->
                <div id="herbs" class="main-content">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h3>약재 관리 <small class="text-muted">(주성분코드 기준)</small></h3>
                        <div>
                            <div class="btn-group me-2" role="group">
                                <button type="button" class="btn btn-outline-primary active" data-filter="all">
                                    <i class="bi bi-list"></i> 전체
                                </button>
                                <button type="button" class="btn btn-outline-success" data-filter="stock">
                                    <i class="bi bi-check-circle"></i> 재고 있음
                                </button>
                                <button type="button" class="btn btn-outline-secondary" data-filter="no-stock">
                                    <i class="bi bi-x-circle"></i> 재고 없음
                                </button>
                            </div>
                            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#herbModal">
                                <i class="bi bi-plus-circle"></i> 새 약재 등록
                            </button>
                        </div>
                    </div>

                    <!-- 통계 정보 -->
                    <div id="herbMasterSummary" class="alert alert-info mb-3">
                        <!-- Dynamic summary -->
                    </div>

                    <!-- 검색 바 -->
                    <div class="card mb-3">
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <input type="text" class="form-control" id="herbSearch"
                                           placeholder="약재명 또는 주성분코드로 검색...">
                                </div>
                                <div class="col-md-6">
                                    <select class="form-select" id="efficacyFilter">
                                        <option value="">모든 효능</option>
                                        <option value="보혈">보혈</option>
                                        <option value="보기">보기</option>
                                        <option value="활혈">활혈</option>
                                        <option value="온중">온중</option>
                                        <option value="청열">청열</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="card">
                        <div class="card-body">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>주성분코드</th>
                                        <th>약재명</th>
                                        <th>효능</th>
                                        <th>재고량</th>
                                        <th>평균단가</th>
                                        <th>제품수</th>
                                        <th>상태</th>
                                        <th>작업</th>
                                    </tr>
                                </thead>
                                <tbody id="herbMastersList">
                                    <!-- Dynamic content -->
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Patient Modal -->
    <div class="modal fade" id="patientModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">환자 등록</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="patientForm">
                        <div class="mb-3">
                            <label class="form-label">환자명 *</label>
                            <input type="text" class="form-control" id="patientName" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">전화번호 *</label>
                            <input type="tel" class="form-control" id="patientPhone" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">주민번호</label>
                            <input type="text" class="form-control" id="patientJumin" placeholder="000000-0000000">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">성별</label>
                            <select class="form-control" id="patientGender">
                                <option value="">선택</option>
                                <option value="M">남성</option>
                                <option value="F">여성</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">생년월일</label>
                            <input type="date" class="form-control" id="patientBirth">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">주소</label>
                            <input type="text" class="form-control" id="patientAddress">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">메모</label>
                            <textarea class="form-control" id="patientNotes" rows="2"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">취소</button>
                    <button type="button" class="btn btn-primary" id="savePatientBtn">저장</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 환자 처방 내역 모달 -->
    <div class="modal fade" id="patientCompoundsModal" tabindex="-1">
        <div class="modal-dialog modal-xl">
            <div class="modal-content">
                <div class="modal-header bg-primary text-white">
                    <h5 class="modal-title">
                        <i class="bi bi-person-circle"></i>
                        <span id="patientCompoundsName">환자명</span>의 처방 내역
                    </h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <!-- 환자 기본 정보 -->
                    <div class="card mb-3">
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-3">
                                    <strong>환자명:</strong> <span id="patientInfoName"></span>
                                </div>
                                <div class="col-md-3">
                                    <strong>전화번호:</strong> <span id="patientInfoPhone"></span>
                                </div>
                                <div class="col-md-3">
                                    <strong>성별:</strong> <span id="patientInfoGender"></span>
                                </div>
                                <div class="col-md-3">
                                    <strong>생년월일:</strong> <span id="patientInfoBirth"></span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 처방 통계 -->
                    <div class="row mb-3">
                        <div class="col-md-3">
                            <div class="stat-card bg-primary text-white">
                                <h6>총 처방 횟수</h6>
                                <h4 id="patientTotalCompounds">0회</h4>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card bg-success text-white">
                                <h6>최근 방문</h6>
                                <h4 id="patientLastVisit">-</h4>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card bg-info text-white">
                                <h6>총 제수</h6>
                                <h4 id="patientTotalJe">0제</h4>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card bg-warning text-white">
                                <h6>총 처방비</h6>
                                <h4 id="patientTotalAmount">₩0</h4>
                            </div>
                        </div>
                    </div>

                    <!-- 처방 내역 테이블 -->
                    <h6><i class="bi bi-list-ul"></i> 처방 내역</h6>
                    <div class="table-responsive">
                        <table class="table table-sm">
                            <thead class="table-dark">
                                <tr>
                                    <th width="40"></th>
                                    <th width="100">조제일</th>
                                    <th>처방명</th>
                                    <th width="60">제수</th>
                                    <th width="60">첩수</th>
                                    <th width="80">파우치</th>
                                    <th width="100">원가</th>
                                    <th width="100">판매가</th>
                                    <th width="80">상태</th>
                                    <th width="100">처방전번호</th>
                                </tr>
                            </thead>
                            <tbody id="patientCompoundsList">
                                <!-- Dynamic content with collapsible details -->
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">닫기</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Formula Modal -->
    <div class="modal fade" id="formulaModal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">처방 등록</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="formulaForm">
                        <div class="row">
                            <div class="col-md-6">
                                <label class="form-label">처방코드</label>
                                <input type="text" class="form-control" id="formulaCode">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">처방명 *</label>
                                <input type="text" class="form-control" id="formulaName" required>
                            </div>
                        </div>
                        <div class="row mt-3">
                            <div class="col-md-4">
                                <label class="form-label">처방 유형</label>
                                <select class="form-control" id="formulaType">
                                    <option value="CUSTOM">약속처방</option>
                                    <option value="INSURANCE">보험처방</option>
                                </select>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">기본 첩수</label>
                                <input type="number" class="form-control" id="baseCheop" value="20">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">기본 파우치수</label>
                                <input type="number" class="form-control" id="basePouches" value="30">
                            </div>
                        </div>
                        <div class="mt-3">
                            <label class="form-label">설명</label>
                            <textarea class="form-control" id="formulaDescription" rows="2"></textarea>
                        </div>
                        <div class="mt-3">
                            <h6>구성 약재</h6>
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>약재명</th>
                                        <th>1첩당 용량(g)</th>
                                        <th>비고</th>
                                        <th>삭제</th>
                                    </tr>
                                </thead>
                                <tbody id="formulaIngredients">
                                    <!-- Dynamic content -->
                                </tbody>
                            </table>
                            <button type="button" class="btn btn-sm btn-outline-primary" id="addFormulaIngredientBtn">
                                <i class="bi bi-plus"></i> 약재 추가
                            </button>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">취소</button>
                    <button type="button" class="btn btn-primary" id="saveFormulaBtn">저장</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Supplier Modal -->
    <div class="modal fade" id="supplierModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">도매상 등록</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="supplierForm">
                        <div class="mb-3">
                            <label class="form-label">도매상명 *</label>
                            <input type="text" class="form-control" id="supplierName" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">사업자번호</label>
                            <input type="text" class="form-control" id="supplierBusinessNo" placeholder="000-00-00000">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">담당자</label>
                            <input type="text" class="form-control" id="supplierContactPerson">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">전화번호</label>
                            <input type="tel" class="form-control" id="supplierPhone">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">주소</label>
                            <input type="text" class="form-control" id="supplierAddress">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">취소</button>
                    <button type="button" class="btn btn-primary" id="saveSupplierBtn">저장</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Scripts -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="/static/app.js?v=20260215"></script>
</body>
</html>