import { state } from '../core/state';
import { createIcons, Download, Upload, FileSpreadsheet, Plus, X, LayoutDashboard, Monitor, Server, Database, Laptop, CalendarClock, Key, Cpu, Layers, Users, Paperclip, Edit2, RefreshCcw } from 'lucide';
import { openPcModal } from '../components/Modal/PCModal';
import { openHwModal } from '../components/Modal/HWModal';
import { openStorageModal } from '../components/Modal/StorageModal';
import { openSwModal } from '../components/Modal/SWModal';
import { openSwUserModal } from '../components/Modal/SWUserModal';

/**
 * 자산 목록 테이블 렌더링 메인 함수
 */
export function renderTable(mainContent: HTMLElement) {
  mainContent.innerHTML = ''; 
  const container = document.createElement('div');
  container.className = 'view-container'; 
  const table = document.createElement('table');

  if (state.activeCategory === 'hw') {
    renderHwTable(table, container, mainContent);
  } else {
    renderSwTable(table, container, mainContent);
  }

  createIcons({
    icons: { Download, Upload, FileSpreadsheet, Plus, X, LayoutDashboard, Monitor, Server, Database, Laptop, CalendarClock, Key, Cpu, Layers, Users, Paperclip, Edit2 }
  });
}

function renderHwTable(table: HTMLTableElement, container: HTMLElement, mainContent: HTMLElement) {
  const fullList = state.masterData.hw.filter(a => a.type === state.activeSubTab);
  container.innerHTML = ''; 

  // --- 1. Search Bar (Unified Style) ---
  const filterBar = document.createElement('div');
  filterBar.className = 'search-bar';
  
  const corps = Array.from(new Set(fullList.map(a => a.법인))).filter(Boolean).sort();
  const orgUnits = Array.from(new Set(fullList.map(a => a.현사용조직))).filter(Boolean).sort();

  filterBar.innerHTML = `
    <div class="search-item flex-1">
      <label>통합 검색 (자산코드/조직/모델명)</label>
      <input type="text" id="filter-keyword" placeholder="검색어를 입력하세요..." autocomplete="off">
    </div>
    <div class="search-item">
      <label>법인</label>
      <select id="filter-corp">
        <option value="">전체 법인</option>
        ${corps.map(c => `<option value="${c}">${c}</option>`).join('')}
      </select>
    </div>
    ${state.activeSubTab === '서버' ? `
    <div class="search-item">
      <label>현 사용조직</label>
      <select id="filter-org-unit">
        <option value="">전체 조직</option>
        ${orgUnits.map(o => `<option value="${o}">${o}</option>`).join('')}
      </select>
    </div>` : ''}
    <button id="btn-reset-filters" class="btn btn-outline btn-reset" title="초기화">
      <i data-lucide="refresh-ccw" style="width:14px; height:14px;"></i> 필터 초기화
    </button>
  `;
  container.appendChild(filterBar);

  // --- 2. Table Structure (Unified Style) ---
  const tableWrapper = document.createElement('div');
  tableWrapper.className = 'table-container';
  
  if (state.activeSubTab === '개인PC') {
    table.innerHTML = `<thead><tr><th>No</th><th>법인</th><th>자산코드</th><th>사용자</th><th>위치</th><th>CPU</th><th>RAM</th><th>Storage</th><th>구매일</th><th>금액</th><th>품의서</th><th>관리</th></tr></thead><tbody id="dynamic-tbody"></tbody>`;
  } else if (state.activeSubTab === '서버') {
    table.innerHTML = `<thead><tr><th>No</th><th>법인</th><th>현 사용조직</th><th>자산번호</th><th>용도</th><th>상세</th><th>설치위치</th><th>담당자</th><th>IP주소</th><th>모델명</th><th>OS</th><th>CPU/RAM</th><th>Storage</th><th>관리</th></tr></thead><tbody id="dynamic-tbody"></tbody>`;
  } else if (state.activeSubTab === '스토리지') {
    table.innerHTML = `<thead><tr><th>No</th><th>법인</th><th>유형</th><th>자산코드</th><th>명칭</th><th>위치</th><th>모델명</th><th>용량</th><th>IP주소</th><th>구매일</th><th>관리</th></tr></thead><tbody id="dynamic-tbody"></tbody>`;
  } else {
    table.innerHTML = `<thead><tr><th>No</th><th>법인</th><th>자산코드</th><th>명칭</th><th>위치</th><th>관리자</th><th>구매일</th><th>금액</th><th>관리</th></tr></thead><tbody id="dynamic-tbody"></tbody>`;
  }

  tableWrapper.appendChild(table);
  container.appendChild(tableWrapper);
  mainContent.appendChild(container);

  const tbody = document.getElementById('dynamic-tbody')!;

  const updateTable = () => {
    const keyword = (document.getElementById('filter-keyword') as HTMLInputElement).value.toLowerCase().trim();
    const corp = (document.getElementById('filter-corp') as HTMLSelectElement).value;
    const orgUnit = (document.getElementById('filter-org-unit') as HTMLSelectElement)?.value || '';

    const filtered = fullList.filter(asset => {
      const matchKeyword = !keyword || String(asset.자산코드||'').toLowerCase().includes(keyword) || String(asset.현사용조직||'').toLowerCase().includes(keyword) || String(asset.모델명||'').toLowerCase().includes(keyword);
      const matchCorp = !corp || asset.법인 === corp;
      const matchOrg = !orgUnit || asset.현사용조직 === orgUnit;
      return matchKeyword && matchCorp && matchOrg;
    });

    tbody.innerHTML = '';
    if (filtered.length === 0) {
      const colSpan = table.querySelectorAll('th').length;
      tbody.innerHTML = `<tr><td colspan="${colSpan}" style="text-align:center; padding: 3rem; color: var(--text-muted);">검색 결과가 없습니다.</td></tr>`;
      return;
    }

    filtered.forEach((asset, idx) => {
      const tr = document.createElement('tr');
      tr.style.cursor = 'pointer';
      const formatInline = (v: any) => String(v || '').replace(/\n/g, ' / ').trim();

      if (state.activeSubTab === '개인PC') {
        const storage = [asset.SSD1, asset.SSD2, asset.HDD1].filter(v => v).join(' / ');
        tr.innerHTML = `<td>${idx+1}</td><td>${asset.법인}</td><td>${asset.자산코드}</td><td>${asset.사용자||''}</td><td>${asset.위치||''}</td><td>${asset.CPU||''}</td><td>${asset.RAM||''}</td><td>${formatInline(storage)}</td><td>${asset.구매일||''}</td><td>${asset.금액||''}</td><td style="text-align:center;">${asset.품의서명 ? '<i data-lucide="paperclip" class="text-primary"></i>' : '-'}</td><td><button class="btn btn-outline btn-sm btn-edit">수정</button></td>`;
        tr.addEventListener('click', (e) => { if (!(e.target as HTMLElement).closest('button')) openPcModal(asset); });
      } else if (state.activeSubTab === '서버') {
        const cpuRam = [asset.CPU, asset.RAM].filter(v => v).join(' / ');
        const storage = [asset.SSD1, asset.SSD2].filter(v => v).join(' / ');
        const ipInfo = [asset.IP주소, asset.IP2].filter(v => v).join(' / ');
        tr.innerHTML = `<td>${idx+1}</td><td>${asset.법인}</td><td>${asset.현사용조직||''}</td><td>${asset.자산코드}</td><td>${formatInline(asset.용도)}</td><td>${formatInline(asset.상세)}</td><td>${formatInline(asset.위치)}</td><td>${asset.담당자_정||''}</td><td>${formatInline(ipInfo)}</td><td>${asset.모델명||''}</td><td>${asset.OS||''}</td><td>${formatInline(cpuRam)}</td><td>${formatInline(storage)}</td><td><button class="btn btn-outline btn-sm btn-edit">수정</button></td>`;
        tr.addEventListener('click', (e) => { if (!(e.target as HTMLElement).closest('button')) openHwModal(asset); });
      } else if (state.activeSubTab === '스토리지') {
        tr.innerHTML = `<td>${idx+1}</td><td>${asset.법인}</td><td>${asset.storage유형||''}</td><td>${asset.자산코드}</td><td>${asset.명칭}</td><td>${asset.위치||''}</td><td>${asset.모델명||''}</td><td>${asset.용량||''}</td><td>${asset.IP주소||''}</td><td>${asset.구매일||''}</td><td><button class="btn btn-outline btn-sm btn-edit">수정</button></td>`;
        tr.addEventListener('click', (e) => { if (!(e.target as HTMLElement).closest('button')) openStorageModal(asset); });
      } else {
        tr.innerHTML = `<td>${idx+1}</td><td>${asset.법인}</td><td>${asset.자산코드}</td><td>${asset.명칭}</td><td>${asset.위치}</td><td>${asset.관리자}</td><td>${asset.구매일||''}</td><td>${asset.금액||''}</td><td><button class="btn btn-outline btn-sm btn-edit">수정</button></td>`;
        tr.addEventListener('click', (e) => { if (!(e.target as HTMLElement).closest('button')) openHwModal(asset); });
      }
      tbody.appendChild(tr);
    });
    createIcons({ icons: { Paperclip, Edit2, RefreshCcw } });
  };

  const keywordInput = document.getElementById('filter-keyword') as HTMLInputElement;
  const corpSelect = document.getElementById('filter-corp') as HTMLSelectElement;
  const orgSelect = document.getElementById('filter-org-unit') as HTMLSelectElement;
  const resetBtn = document.getElementById('btn-reset-filters') as HTMLButtonElement;

  keywordInput.addEventListener('input', updateTable);
  corpSelect.addEventListener('change', updateTable);
  orgSelect?.addEventListener('change', updateTable);
  resetBtn.addEventListener('click', () => {
    keywordInput.value = ''; corpSelect.value = ''; if(orgSelect) orgSelect.value = '';
    updateTable();
  });

  updateTable();
}

function renderSwTable(table: HTMLTableElement, container: HTMLElement, mainContent: HTMLElement) {
  const fullList = state.masterData.sw.filter(a => a.type === state.activeSubTab);
  const isSub = state.activeSubTab === '구독SW';
  container.innerHTML = ''; 
  const filterBar = document.createElement('div');
  filterBar.className = 'search-bar';
  filterBar.innerHTML = `<div class="search-item flex-1"><label>통합 검색 (제품명/부서)</label><input type="text" id="filter-keyword" placeholder="검색어를 입력하세요..." autocomplete="off"></div><div class="search-item"><label>분야</label><select id="filter-field"><option value="">전체 분야</option><option value="업무공통">업무공통</option><option value="개발S/W">개발S/W</option><option value="디자인">디자인</option><option value="설계S/W">설계S/W</option></select></div><div class="search-item"><label>법인</label><select id="filter-corp"><option value="">전체 법인</option><option value="한맥">한맥</option><option value="삼안">삼안</option><option value="바론">바론</option></select></div><button id="btn-reset-filters" class="btn btn-outline btn-reset" title="검색 조건 초기화"><i data-lucide="refresh-ccw" style="width:14px; height:14px;"></i> 필터 초기화</button>`;
  container.appendChild(filterBar);

  const tableWrapper = document.createElement('div');
  tableWrapper.className = 'table-container';
  table.classList.add('sw-table');
  table.innerHTML = `<thead><tr><th style="text-align:center;">No.</th><th style="text-align:center;">분야</th><th style="text-align:center;">법인</th><th style="text-align:center;">부서</th><th style="text-align:center;">제품명</th><th style="text-align:center;">구매일</th>${isSub ? '<th style="text-align:center;">구독일</th>' : ''}<th style="text-align:center;">금액</th><th style="text-align:center;">수량</th><th style="text-align:center;">사용가능</th><th style="text-align:center;">관리</th></tr></thead><tbody id="dynamic-tbody"></tbody>`;
  tableWrapper.appendChild(table);
  container.appendChild(tableWrapper);
  mainContent.appendChild(container);
  
  const tbody = document.getElementById('dynamic-tbody')!;
  const updateTable = () => {
    const keyword = (document.getElementById('filter-keyword') as HTMLInputElement).value.toLowerCase().trim();
    const field = (document.getElementById('filter-field') as HTMLSelectElement).value;
    const corp = (document.getElementById('filter-corp') as HTMLSelectElement).value;
    const filtered = fullList.filter(asset => {
      const matchKeyword = !keyword || (asset.제품명 || '').toLowerCase().includes(keyword) || (asset.부서 || '').toLowerCase().includes(keyword);
      const matchField = !field || asset.분야 === field;
      const matchCorp = !corp || asset.법인 === corp;
      return matchKeyword && matchField && matchCorp;
    });
    tbody.innerHTML = '';
    if (filtered.length === 0) {
      tbody.innerHTML = `<tr><td colspan="${isSub ? 11 : 10}" style="text-align:center; padding: 3rem; color: var(--text-muted);">검색 결과가 없습니다.</td></tr>`;
      return;
    }
    filtered.forEach((asset, idx) => {
      const assigned = state.masterData.swUsers.filter(u => u.swId === asset.id).length;
      const qty = typeof asset.수량 === 'number' ? asset.수량 : parseInt(asset.수량||'0', 10);
      const avail = qty - assigned;
      const tr = document.createElement('tr');
      tr.style.cursor = 'pointer';
      tr.innerHTML = `<td>${idx+1}</td><td>${asset.분야||''}</td><td>${asset.법인}</td><td>${asset.부서||''}</td><td>${asset.제품명}</td><td>${asset.구매일||''}</td>${isSub ? `<td>${asset.구독일||''}</td>` : ''}<td>${asset.금액||'0'}</td><td>${qty}</td><td><strong style="color: ${avail > 0 ? 'var(--primary-color)' : 'var(--danger)'}">${avail}</strong></td><td style="display:flex; justify-content:center; align-items:center; gap:0.5rem;"><button type="button" class="btn-icon btn-edit" title="수정" style="color: var(--text-muted);"><i data-lucide="edit-2" style="width:18px; height:18px;"></i></button><button type="button" class="btn-icon btn-users" title="사용자 관리" style="color: var(--primary-color);"><i data-lucide="users" style="width:18px; height:18px;"></i></button></td>`;
      tr.addEventListener('click', (e) => { if (!(e.target as HTMLElement).closest('button')) openSwModal(asset); });
      tr.querySelector('.btn-edit')?.addEventListener('click', () => openSwModal(asset));
      tr.querySelector('.btn-users')?.addEventListener('click', () => openSwUserModal(asset));
      tbody.appendChild(tr);
    });
    createIcons({ icons: { Edit2, Users, RefreshCcw } });
  };

  const keywordInput = document.getElementById('filter-keyword') as HTMLInputElement;
  const fieldSelect = document.getElementById('filter-field') as HTMLSelectElement;
  const corpSelect = document.getElementById('filter-corp') as HTMLSelectElement;
  const resetBtn = document.getElementById('btn-reset-filters') as HTMLButtonElement;
  keywordInput.addEventListener('input', updateTable);
  fieldSelect.addEventListener('change', updateTable);
  corpSelect.addEventListener('change', updateTable);
  resetBtn.addEventListener('click', () => {
    keywordInput.value = ''; fieldSelect.value = ''; corpSelect.value = '';
    updateTable();
  });
  updateTable();
}
