<!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>
                    <li class="nav-item">
                        <a class="nav-link" href="#" data-page="herb-info">
                            <i class="bi bi-book"></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> 재고 자산
                                    <button class="btn btn-sm btn-outline-secondary ms-2" data-bs-toggle="modal" data-bs-target="#inventorySettingsModal" title="계산 설정">
                                        <i class="bi bi-gear"></i>
                                    </button>
                                </h5>
                                <div class="value" id="inventoryValue">0</div>
                                <small class="text-muted" id="inventoryMode">전체 재고</small>
                            </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 width="100">마일리지</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="salesModal" tabindex="-1">
                        <div class="modal-dialog modal-lg">
                            <div class="modal-content">
                                <div class="modal-header bg-success text-white">
                                    <h5 class="modal-title"><i class="bi bi-cash-coin"></i> 판매 처리</h5>
                                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                                </div>
                                <div class="modal-body">
                                    <form id="salesForm">
                                        <input type="hidden" id="salesCompoundId">

                                        <div class="row mb-3">
                                            <div class="col-md-6">
                                                <label class="form-label">처방명</label>
                                                <input type="text" class="form-control" id="salesFormulaName" readonly>
                                            </div>
                                            <div class="col-md-6">
                                                <label class="form-label">환자명</label>
                                                <input type="text" class="form-control" id="salesPatientName" readonly>
                                            </div>
                                        </div>

                                        <div class="card mb-3">
                                            <div class="card-header bg-light">
                                                <h6 class="mb-0"><i class="bi bi-calculator"></i> 가격 정보</h6>
                                            </div>
                                            <div class="card-body">
                                                <div class="row">
                                                    <div class="col-md-4">
                                                        <label class="form-label">약재원가</label>
                                                        <input type="number" class="form-control" id="salesCostTotal" readonly>
                                                    </div>
                                                    <div class="col-md-4">
                                                        <label class="form-label">조제료</label>
                                                        <input type="number" class="form-control" id="salesDispensingFee" value="20000">
                                                    </div>
                                                    <div class="col-md-4">
                                                        <label class="form-label">판매가격 (원가+조제료)</label>
                                                        <input type="number" class="form-control fw-bold" id="salesBasePrice" readonly>
                                                    </div>
                                                </div>
                                                <div class="row mt-3">
                                                    <div class="col-md-12">
                                                        <label class="form-label">할인 방식 선택</label>
                                                        <div class="btn-group w-100" role="group">
                                                            <input type="radio" class="btn-check" name="discountType" id="discountTypeRate" value="rate" checked>
                                                            <label class="btn btn-outline-primary" for="discountTypeRate">할인율 (%)</label>

                                                            <input type="radio" class="btn-check" name="discountType" id="discountTypeAmount" value="amount">
                                                            <label class="btn btn-outline-primary" for="discountTypeAmount">할인액 (원)</label>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row mt-2">
                                                    <div class="col-md-4">
                                                        <label class="form-label">할인 설정</label>
                                                        <div class="input-group">
                                                            <input type="number" class="form-control" id="salesDiscountValue" min="0" value="0">
                                                            <span class="input-group-text" id="discountUnit">%</span>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-4">
                                                        <label class="form-label">할인사유</label>
                                                        <input type="text" class="form-control" id="salesDiscountReason" placeholder="예: 단골 할인">
                                                    </div>
                                                    <div class="col-md-4">
                                                        <label class="form-label">할인 후 가격</label>
                                                        <input type="number" class="form-control fw-bold text-danger" id="salesPriceAfterDiscount" readonly>
                                                    </div>
                                                </div>
                                                <div class="row mt-3">
                                                    <div class="col-md-12">
                                                        <div class="alert alert-info mb-0">
                                                            <i class="bi bi-piggy-bank"></i> <strong>보유 마일리지:</strong>
                                                            <span id="patientMileageBalance">0</span>원
                                                            <button type="button" class="btn btn-sm btn-outline-info float-end" id="applyMaxMileage">전액 사용</button>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="card mb-3">
                                            <div class="card-header bg-light">
                                                <h6 class="mb-0"><i class="bi bi-credit-card"></i> 복합 결제</h6>
                                            </div>
                                            <div class="card-body">
                                                <div class="row">
                                                    <div class="col-md-3">
                                                        <label class="form-label">마일리지 사용</label>
                                                        <input type="number" class="form-control" id="salesMileageUse" min="0" value="0">
                                                    </div>
                                                    <div class="col-md-3">
                                                        <label class="form-label">현금 결제</label>
                                                        <input type="number" class="form-control" id="salesPaymentCash" min="0" value="0">
                                                    </div>
                                                    <div class="col-md-3">
                                                        <label class="form-label">카드 결제</label>
                                                        <input type="number" class="form-control" id="salesPaymentCard" min="0" value="0">
                                                    </div>
                                                    <div class="col-md-3">
                                                        <label class="form-label">계좌이체</label>
                                                        <input type="number" class="form-control" id="salesPaymentTransfer" min="0" value="0">
                                                    </div>
                                                </div>
                                                <div class="row mt-3">
                                                    <div class="col-md-6">
                                                        <div class="alert alert-warning mb-0">
                                                            <strong>결제 필요 금액:</strong> <span id="salesNeedToPay" class="fs-5">0</span>원
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="alert alert-success mb-0">
                                                            <strong>결제 합계:</strong> <span id="salesTotalPayment" class="fs-5">0</span>원
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row mt-2">
                                                    <div class="col-md-6">
                                                        <label class="form-label">주 결제방법</label>
                                                        <select class="form-control" id="salesPaymentMethod">
                                                            <option value="">선택하세요</option>
                                                            <option value="CASH">현금</option>
                                                            <option value="CARD">카드</option>
                                                            <option value="TRANSFER">계좌이체</option>
                                                            <option value="KAKAO">카카오페이</option>
                                                            <option value="COMPLEX">복합결제</option>
                                                        </select>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <label class="form-label">결제일시</label>
                                                        <input type="datetime-local" class="form-control" id="salesPaymentDate">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="card">
                                            <div class="card-header bg-light">
                                                <h6 class="mb-0"><i class="bi bi-truck"></i> 배송 정보</h6>
                                            </div>
                                            <div class="card-body">
                                                <div class="row">
                                                    <div class="col-md-6">
                                                        <label class="form-label">배송방법</label>
                                                        <select class="form-control" id="salesDeliveryMethod">
                                                            <option value="">선택하세요</option>
                                                            <option value="DELIVERY">택배</option>
                                                            <option value="PICKUP">직접수령</option>
                                                            <option value="QUICK">퀵서비스</option>
                                                        </select>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <label class="form-label">배송예정일</label>
                                                        <input type="date" class="form-control" id="salesDeliveryDate">
                                                    </div>
                                                </div>
                                            </div>
                                        </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-success" id="saveSalesBtn">
                                        <i class="bi bi-check-circle"></i> 판매 확정
                                    </button>
                                </div>
                            </div>
                        </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>

                <!-- Herb Information Page -->
                <div id="herb-info" class="main-content">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h3><i class="bi bi-book"></i> 한약재 정보 시스템</h3>
            <div class="btn-group">
                <button type="button" class="btn btn-outline-primary active" data-view="search">
                    <i class="bi bi-search"></i> 검색
                </button>
                <button type="button" class="btn btn-outline-primary" data-view="efficacy">
                    <i class="bi bi-tags"></i> 효능별
                </button>
                <button type="button" class="btn btn-outline-primary" data-view="category">
                    <i class="bi bi-grid-3x3"></i> 분류별
                </button>
            </div>
        </div>

        <!-- Search Section -->
        <div id="herb-search-section" class="mb-4">
            <div class="row">
                <div class="col-md-6">
                    <div class="input-group">
                        <span class="input-group-text"><i class="bi bi-search"></i></span>
                        <input type="text" class="form-control" id="herbSearchInput"
                               placeholder="약재명, 학명, 효능으로 검색...">
                        <button class="btn btn-primary" id="herbSearchBtn">검색</button>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="d-flex gap-2">
                        <select class="form-select" id="herbInfoEfficacyFilter">
                            <option value="">모든 효능</option>
                            <option value="보혈">보혈</option>
                            <option value="보기">보기</option>
                            <option value="활혈">활혈</option>
                            <option value="청열">청열</option>
                            <option value="해독">해독</option>
                            <option value="거담">거담</option>
                            <option value="이수">이수</option>
                            <option value="안신">안신</option>
                        </select>
                        <select class="form-select" id="herbInfoPropertyFilter">
                            <option value="">모든 성미</option>
                            <option value="한">한(寒)</option>
                            <option value="열">열(熱)</option>
                            <option value="온">온(溫)</option>
                            <option value="량">량(涼)</option>
                            <option value="평">평(平)</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <!-- Efficacy Tags Section (Hidden by default) -->
        <div id="herb-efficacy-section" class="mb-4" style="display: none;">
            <div class="row g-3" id="efficacyTagsContainer">
                <!-- Dynamic efficacy tag buttons will be added here -->
            </div>
        </div>

        <!-- Results Grid -->
        <div class="row g-3" id="herbInfoGrid">
            <!-- Herb cards will be dynamically added here -->
        </div>

        <!-- Herb Detail Modal -->
        <div class="modal fade" id="herbDetailModal" tabindex="-1">
            <div class="modal-dialog modal-xl">
                <div class="modal-content">
                    <div class="modal-header bg-success text-white">
                        <h5 class="modal-title">
                            <i class="bi bi-flower1"></i>
                            <span id="herbDetailName">약재명</span>
                            <span id="herbDetailHanja" class="ms-2"></span>
                        </h5>
                        <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <!-- 기본 정보 -->
                            <div class="col-md-6">
                                <div class="card mb-3">
                                    <div class="card-header bg-light">
                                        <h6 class="mb-0"><i class="bi bi-info-circle"></i> 기본 정보</h6>
                                    </div>
                                    <div class="card-body">
                                        <dl class="row mb-0">
                                            <dt class="col-sm-4">성분코드</dt>
                                            <dd class="col-sm-8" id="detailIngredientCode">-</dd>

                                            <dt class="col-sm-4">학명</dt>
                                            <dd class="col-sm-8" id="detailLatinName">-</dd>

                                            <dt class="col-sm-4">약용부위</dt>
                                            <dd class="col-sm-8" id="detailMedicinalPart">-</dd>

                                            <dt class="col-sm-4">기원식물</dt>
                                            <dd class="col-sm-8" id="detailOriginPlant">-</dd>
                                        </dl>
                                    </div>
                                </div>

                                <div class="card mb-3">
                                    <div class="card-header bg-light">
                                        <h6 class="mb-0"><i class="bi bi-thermometer"></i> 성미귀경</h6>
                                    </div>
                                    <div class="card-body">
                                        <dl class="row mb-0">
                                            <dt class="col-sm-4">성(性)</dt>
                                            <dd class="col-sm-8">
                                                <span class="badge bg-info" id="detailProperty">-</span>
                                            </dd>

                                            <dt class="col-sm-4">미(味)</dt>
                                            <dd class="col-sm-8" id="detailTaste">-</dd>

                                            <dt class="col-sm-4">귀경</dt>
                                            <dd class="col-sm-8" id="detailMeridian">-</dd>
                                        </dl>
                                    </div>
                                </div>
                            </div>

                            <!-- 효능 정보 -->
                            <div class="col-md-6">
                                <div class="card mb-3">
                                    <div class="card-header bg-light">
                                        <h6 class="mb-0"><i class="bi bi-heart-pulse"></i> 효능효과</h6>
                                    </div>
                                    <div class="card-body">
                                        <div class="mb-3">
                                            <strong>주요 효능:</strong>
                                            <div id="detailMainEffects" class="mt-2">-</div>
                                        </div>
                                        <div class="mb-3">
                                            <strong>적응증:</strong>
                                            <div id="detailIndications" class="mt-2">-</div>
                                        </div>
                                        <div class="mb-3">
                                            <strong>효능 태그:</strong>
                                            <div id="detailEfficacyTags" class="mt-2">
                                                <!-- Dynamic tags -->
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="card">
                                    <div class="card-header bg-light">
                                        <h6 class="mb-0"><i class="bi bi-capsule"></i> 용법용량</h6>
                                    </div>
                                    <div class="card-body">
                                        <dl class="row mb-0">
                                            <dt class="col-sm-4">상용량</dt>
                                            <dd class="col-sm-8" id="detailDosageRange">-</dd>

                                            <dt class="col-sm-4">극량</dt>
                                            <dd class="col-sm-8" id="detailDosageMax">-</dd>

                                            <dt class="col-sm-4">포제법</dt>
                                            <dd class="col-sm-8" id="detailPreparation">-</dd>
                                        </dl>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- 추가 정보 탭 -->
                        <div class="mt-4">
                            <ul class="nav nav-tabs" role="tablist">
                                <li class="nav-item">
                                    <a class="nav-link active" data-bs-toggle="tab" href="#tabSafety">
                                        <i class="bi bi-shield-check"></i> 안전성
                                    </a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" data-bs-toggle="tab" href="#tabComponents">
                                        <i class="bi bi-diagram-3"></i> 성분정보
                                    </a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" data-bs-toggle="tab" href="#tabClinical">
                                        <i class="bi bi-clipboard2-pulse"></i> 임상응용
                                    </a>
                                </li>
                            </ul>
                            <div class="tab-content p-3 border border-top-0">
                                <div class="tab-pane fade show active" id="tabSafety">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <h6>금기사항</h6>
                                            <div id="detailContraindications" class="text-danger">-</div>
                                        </div>
                                        <div class="col-md-6">
                                            <h6>주의사항</h6>
                                            <div id="detailPrecautions" class="text-warning">-</div>
                                        </div>
                                    </div>
                                </div>
                                <div class="tab-pane fade" id="tabComponents">
                                    <h6>주요 성분</h6>
                                    <div id="detailActiveCompounds">-</div>
                                </div>
                                <div class="tab-pane fade" id="tabClinical">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <h6>약리작용</h6>
                                            <div id="detailPharmacological">-</div>
                                        </div>
                                        <div class="col-md-6">
                                            <h6>임상응용</h6>
                                            <div id="detailClinical">-</div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </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="editHerbInfoBtn">
                            <i class="bi bi-pencil"></i> 정보 수정
                        </button>
                    </div>
                </div>
            </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>

                        <!-- 마일리지 관리 섹션 (편집 시에만 표시) -->
                        <div id="mileageSection" style="display: none;">
                            <hr>
                            <h6 class="text-primary"><i class="bi bi-piggy-bank"></i> 마일리지 관리</h6>
                            <div class="row">
                                <div class="col-md-4">
                                    <label class="form-label">현재 잔액</label>
                                    <input type="number" class="form-control" id="patientMileageBalance" readonly>
                                </div>
                                <div class="col-md-4">
                                    <label class="form-label">총 적립</label>
                                    <input type="number" class="form-control" id="patientMileageEarned" readonly>
                                </div>
                                <div class="col-md-4">
                                    <label class="form-label">총 사용</label>
                                    <input type="number" class="form-control" id="patientMileageUsed" readonly>
                                </div>
                            </div>
                            <div class="mt-2">
                                <button type="button" class="btn btn-sm btn-outline-success" id="addMileageBtn">
                                    <i class="bi bi-plus-circle"></i> 마일리지 적립
                                </button>
                                <button type="button" class="btn btn-sm btn-outline-danger" id="useMileageBtn">
                                    <i class="bi bi-dash-circle"></i> 마일리지 차감
                                </button>
                            </div>
                        </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">
                            <label class="form-label">주요 효능</label>
                            <textarea class="form-control" id="formulaEfficacy" rows="2" placeholder="예: 기혈양허 치료, 병후 회복, 만성 피로 개선"></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>

    <!-- Formula Detail Modal (처방 상세 모달) -->
    <div class="modal fade" id="formulaDetailModal" 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-journal-medical"></i>
                        <span id="formulaDetailName">처방명</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-4">
                        <div class="card-header bg-light">
                            <h6 class="mb-0"><i class="bi bi-info-circle"></i> 기본 정보</h6>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <dl class="row mb-0">
                                        <dt class="col-sm-4">처방 코드:</dt>
                                        <dd class="col-sm-8" id="detailFormulaCode">-</dd>

                                        <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="detailFormulaType">-</dd>
                                    </dl>
                                </div>
                                <div class="col-md-6">
                                    <dl class="row mb-0">
                                        <dt class="col-sm-4">기본 첩수:</dt>
                                        <dd class="col-sm-8" id="detailBaseCheop">-</dd>

                                        <dt class="col-sm-4">기본 파우치:</dt>
                                        <dd class="col-sm-8" id="detailBasePouches">-</dd>

                                        <dt class="col-sm-4">등록일:</dt>
                                        <dd class="col-sm-8" id="detailCreatedAt">-</dd>
                                    </dl>
                                </div>
                            </div>
                            <div class="row mt-3">
                                <div class="col-12">
                                    <dt>설명:</dt>
                                    <dd id="detailDescription" class="text-muted">-</dd>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 구성 약재 정보 카드 -->
                    <div class="card mb-4">
                        <div class="card-header bg-light d-flex justify-content-between align-items-center">
                            <h6 class="mb-0"><i class="bi bi-list-ul"></i> 구성 약재</h6>
                            <div>
                                <span class="badge bg-primary" id="totalIngredientsCount">0개</span>
                                <span class="badge bg-success" id="totalGramsPerCheop">0g</span>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-hover">
                                    <thead class="table-light">
                                        <tr>
                                            <th width="50">#</th>
                                            <th width="200">약재명</th>
                                            <th width="100">1첩당 용량</th>
                                            <th width="150" style="white-space: nowrap;">1제 기준 <small style="font-size: 0.85em;" class="text-muted">(20첩/30파우치)</small></th>
                                            <th style="padding-left: 15px;">효능/역할</th>
                                            <th width="150">재고 상태</th>
                                        </tr>
                                    </thead>
                                    <tbody id="formulaDetailIngredients">
                                        <!-- 동적으로 추가 -->
                                    </tbody>
                                    <tfoot class="table-secondary">
                                        <tr>
                                            <th></th>
                                            <th>합계</th>
                                            <th class="text-end" id="totalGrams1Cheop">0g</th>
                                            <th class="text-end" id="totalGrams1Je">0g</th>
                                            <th></th>
                                            <th></th>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                    </div>

                    <!-- 효능 및 주의사항 카드 -->
                    <div class="row">
                        <div class="col-md-6">
                            <div class="card">
                                <div class="card-header bg-light">
                                    <h6 class="mb-0"><i class="bi bi-heart-pulse"></i> 주요 효능</h6>
                                </div>
                                <div class="card-body">
                                    <div id="formulaEffects">
                                        <p class="text-muted">처방의 주요 효능 정보가 여기에 표시됩니다.</p>
                                    </div>
                                </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-exclamation-triangle"></i> 사용 시 주의사항</h6>
                                </div>
                                <div class="card-body">
                                    <div id="formulaPrecautions">
                                        <p class="text-muted">처방 사용 시 주의사항이 여기에 표시됩니다.</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </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="editFormulaDetailBtn">
                        <i class="bi bi-pencil"></i> 수정
                    </button>
                    <button type="button" class="btn btn-danger" id="deleteFormulaBtn">
                        <i class="bi bi-trash"></i> 삭제
                    </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>

    <!-- 로트 배분 모달 -->
    <div class="modal fade" id="lotAllocationModal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">
                        <i class="bi bi-shuffle"></i> 로트 배분
                        <span id="lotAllocationHerbName" class="text-primary"></span>
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="alert alert-info">
                        <strong>필요량: <span id="lotAllocationRequired">0</span>g</strong>
                        <span class="float-end">배분 합계: <span id="lotAllocationTotal" class="fw-bold">0</span>g</span>
                    </div>

                    <table class="table table-sm table-bordered">
                        <thead class="table-light">
                            <tr>
                                <th>로트 번호</th>
                                <th>원산지</th>
                                <th>재고량</th>
                                <th>단가</th>
                                <th>사용량</th>
                                <th>소계</th>
                            </tr>
                        </thead>
                        <tbody id="lotAllocationList">
                        </tbody>
                        <tfoot class="table-secondary">
                            <tr>
                                <th colspan="4" class="text-end">합계:</th>
                                <th id="lotAllocationSumQty">0g</th>
                                <th id="lotAllocationSumCost">0원</th>
                            </tr>
                        </tfoot>
                    </table>

                    <div id="lotAllocationError" class="alert alert-danger d-none"></div>
                </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="lotAllocationAutoBtn">
                        <i class="bi bi-magic"></i> 자동 배분
                    </button>
                    <button type="button" class="btn btn-primary" id="lotAllocationConfirmBtn">
                        <i class="bi bi-check"></i> 확인
                    </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=20260217"></script>
<!-- 재고 자산 계산 설정 모달 -->
<div class="modal fade" id="inventorySettingsModal" tabindex="-1" aria-labelledby="inventorySettingsModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="inventorySettingsModalLabel">
                    <i class="bi bi-calculator"></i> 재고 자산 계산 설정
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="mb-3">
                    <label class="form-label fw-bold">계산 방식 선택</label>
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="inventoryMode" id="modeAll" value="all" checked>
                        <label class="form-check-label" for="modeAll">
                            <strong>전체 재고</strong>
                            <div class="text-muted small">모든 LOT의 재고를 포함하여 계산</div>
                        </label>
                    </div>
                    <div class="form-check mt-2">
                        <input class="form-check-input" type="radio" name="inventoryMode" id="modeReceiptOnly" value="receipt_only">
                        <label class="form-check-label" for="modeReceiptOnly">
                            <strong>입고장 기준</strong>
                            <div class="text-muted small">입고장과 연결된 LOT만 계산</div>
                        </label>
                    </div>
                    <div class="form-check mt-2">
                        <input class="form-check-input" type="radio" name="inventoryMode" id="modeVerified" value="verified">
                        <label class="form-check-label" for="modeVerified">
                            <strong>검증된 재고</strong>
                            <div class="text-muted small">검증 확인된 LOT만 계산</div>
                        </label>
                    </div>
                </div>

                <div class="alert alert-info" id="modeInfo" style="display: none;">
                    <h6 class="alert-heading"><i class="bi bi-info-circle"></i> 현재 상태</h6>
                    <div id="modeInfoContent"></div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">취소</button>
                <button type="button" class="btn btn-primary" onclick="saveInventorySettings()">적용</button>
            </div>
        </div>
    </div>
</div>

</body>
</html>