import { state } from '../../core/state';
import { SoftwareAsset, SWUser } from '../../core/excelHandler';
import { openModal } from './BaseModal';
import { createIcons, Edit2, X, Paperclip, Calendar } from 'lucide';
import { CORP_LIST, ORG_LIST } from './SharedData';
import { generateOptionsHTML, setFieldValue, getFieldValue, applyDateMask } from './ModalUtils';

let currentSwUserAsset: SoftwareAsset | null = null;
let tempSwUsers: any[] = [];

const SW_USER_MODAL_HTML = `
  <div id="sw-user-modal" class="modal-overlay hidden">
    <div class="modal-content wide">
      <div class="modal-header">
        <h2 id="sw-user-title">소프트웨어 사용자 관리</h2>
        <button id="btn-close-sw-user-modal" class="btn-icon"><i data-lucide="x"></i></button>
      </div>
      <div class="modal-body">
        <div class="sw-info-summary" id="sw-user-sw-info"></div>
        
        <div class="user-list-toolbar" style="display:flex; justify-content:space-between; margin-bottom:1rem; align-items:center;">
          <h3 style="font-size:1rem; font-weight:600;">할당된 사용자 목록</h3>
          <button type="button" id="btn-open-add-user" class="btn btn-primary btn-sm"><i data-lucide="plus"></i> 사용자 추가</button>
        </div>

        <div class="table-container">
          <table>
            <thead>
              <tr>
                <th>조직</th>
                <th>부서</th>
                <th>직위</th>
                <th>이름</th>
                <th>사용기간</th>
                <th>신청서</th>
                <th>관리</th>
              </tr>
            </thead>
            <tbody id="sw-user-table-body"></tbody>
          </table>
        </div>
      </div>
      <div class="modal-footer">
        <button id="btn-cancel-sw-user" class="btn btn-outline">취소</button>
        <button id="btn-save-sw-user" class="btn btn-primary">저장</button>
      </div>
    </div>
  </div>

  <!-- 사용자 추가/수정 서브 모달 -->
  <div id="sw-user-edit-modal" class="modal-overlay hidden" style="z-index:1100;">
    <div class="modal-content" style="width:400px;">
      <div class="modal-header">
        <h3 id="sw-user-edit-title">사용자 정보</h3>
        <button id="btn-close-user-edit" class="btn-icon"><i data-lucide="x"></i></button>
      </div>
      <div class="modal-body">
        <form id="sw-user-edit-form" class="grid-form" style="grid-template-columns: 1fr;">
          <input type="hidden" id="edit-user-index" value="-1" />
          <div class="form-group">
            <label>조직</label>
            <select id="new-user-조직">${generateOptionsHTML(ORG_LIST)}</select>
          </div>
          <div class="form-group">
            <label>부서</label>
            <input type="text" id="new-user-부서" />
          </div>
          <div class="form-group">
            <label>직위</label>
            <input type="text" id="new-user-직위" />
          </div>
          <div class="form-group">
            <label>이름</label>
            <input type="text" id="new-user-이름" required />
          </div>
          <div class="form-group">
            <label>사용 시작일</label>
            <div style="display:flex; gap:0.25rem; align-items:center; position:relative;">
              <input type="text" id="new-user-시작일" style="flex:1;" />
              <button type="button" class="btn-icon" onclick="const p = document.getElementById('new-user-시작일-picker'); p.value = document.getElementById('new-user-시작일').value; p.showPicker();" style="padding:0.25rem;">
                <i data-lucide="calendar" style="width:18px; height:18px; color:var(--primary-color);"></i>
              </button>
              <input type="date" id="new-user-시작일-picker" style="position:absolute; width:0; height:0; opacity:0; pointer-events:none;" onchange="document.getElementById('new-user-시작일').value = this.value" tabindex="-1" />
            </div>
          </div>
          <div class="form-group">
            <label>사용 종료일</label>
            <div style="display:flex; gap:0.25rem; align-items:center; position:relative;">
              <input type="text" id="new-user-종료일" style="flex:1;" />
              <button type="button" class="btn-icon" onclick="const p = document.getElementById('new-user-종료일-picker'); p.value = document.getElementById('new-user-종료일').value; p.showPicker();" style="padding:0.25rem;">
                <i data-lucide="calendar" style="width:18px; height:18px; color:var(--primary-color);"></i>
              </button>
              <input type="date" id="new-user-종료일-picker" style="position:absolute; width:0; height:0; opacity:0; pointer-events:none;" onchange="document.getElementById('new-user-종료일').value = this.value" tabindex="-1" />
            </div>
          </div>
          <div class="form-group">
            <label>신청서 (증빙)</label>
            <input type="file" id="new-user-신청서" />
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button id="btn-close-user-sub" class="btn btn-outline">취소</button>
        <button id="btn-confirm-user-edit" class="btn btn-primary">확인</button>
      </div>
    </div>
  </div>
`;

export function openSwUserModal(asset: SoftwareAsset) {
  currentSwUserAsset = asset;
  const modal = document.getElementById('sw-user-modal')!;
  
  const swInfo = document.getElementById('sw-user-sw-info')!;
  swInfo.innerHTML = `
    <div style="background:var(--bg-light); padding:1rem; border-radius:6px; margin-bottom:1.5rem;">
      <div style="font-size:0.8rem; color:var(--text-muted); margin-bottom:0.25rem;">${asset.법인}</div>
      <div style="font-size:1.1rem; font-weight:700; color:var(--primary-color);">${asset.제품명}</div>
    </div>
  `;

  // 기존 사용자 데이터 복사 (원본 보호를 위해 temp 사용)
  const existingMapping = state.masterData.swUsers.find(u => u.sw_id === asset.id);
  tempSwUsers = existingMapping ? (existingMapping.userData || []).map((u: any) => ({
    조직: u[0], 부서: u[1], 직위: u[2], 이름: u[3], 사용기간: u[4], 신청서명: u[5]
  })) : [];
  
  renderUserList();
  modal.classList.remove('hidden');
  createIcons({ icons: { Edit2, X, Paperclip } });
}

function renderUserList() {
  const tbody = document.getElementById('sw-user-table-body')!;
  tbody.innerHTML = '';
  
  if (tempSwUsers.length === 0) {
    tbody.innerHTML = '<tr><td colspan="6" style="text-align:center; padding:2rem; color:var(--text-muted);">할당된 사용자가 없습니다.</td></tr>';
    return;
  }

  tempSwUsers.forEach((user, idx) => {
    const tr = document.createElement('tr');
    tr.innerHTML = `
      <td>${user.조직 || ''}</td>
      <td>${user.부서 || ''}</td>
      <td>${user.직위 || ''}</td>
      <td>${user.이름 || ''}</td>
      <td>${user.사용기간 || ''}</td>
      <td style="text-align:center;">${user.신청서명 ? '<i data-lucide="paperclip" class="text-primary"></i>' : '-'}</td>
      <td>
        <div style="display:flex; gap:0.5rem;">
          <button class="btn btn-outline btn-sm btn-edit-user" data-idx="${idx}">수정</button>
          <button class="btn btn-outline btn-sm btn-danger btn-del-user" data-idx="${idx}">삭제</button>
        </div>
      `;
    tbody.appendChild(tr);
  });

  // 이벤트 연결
  tbody.querySelectorAll('.btn-edit-user').forEach(btn => {
    btn.addEventListener('click', (e) => {
      const idx = parseInt((e.currentTarget as HTMLElement).getAttribute('data-idx')!);
      openUserEditSubModal(idx);
    });
  });

  tbody.querySelectorAll('.btn-del-user').forEach(btn => {
    btn.addEventListener('click', (e) => {
      const idx = parseInt((e.currentTarget as HTMLElement).getAttribute('data-idx')!);
      if (confirm('사용자 할당을 삭제하시겠습니까?')) {
        tempSwUsers.splice(idx, 1);
        renderUserList();
      }
    });
  });

  createIcons({ icons: { Paperclip } });
}

function openUserEditSubModal(idx: number = -1) {
  const subModal = document.getElementById('sw-user-edit-modal')!;
  const form = document.getElementById('sw-user-edit-form') as HTMLFormElement;
  form.reset();
  
  setFieldValue('edit-user-index', idx);
  
  if (idx > -1) {
    const user = tempSwUsers[idx];
    setFieldValue('new-user-조직', user.조직);
    setFieldValue('new-user-부서', user.부서);
    setFieldValue('new-user-직위', user.직위);
    setFieldValue('new-user-이름', user.이름);
    
    // 사용기간 파싱 (yyyy-mm-dd ~ yyyy-mm-dd)
    if (user.사용기간 && user.사용기간.includes('~')) {
      const parts = user.사용기간.split('~');
      setFieldValue('new-user-시작일', parts[0].trim());
      setFieldValue('new-user-종료일', parts[1].trim());
    } else {
      setFieldValue('new-user-시작일', '');
      setFieldValue('new-user-종료일', '');
    }
  }

  subModal.classList.remove('hidden');
}

export function initSwUserModal(onSave: () => void, closeModals: () => void) {
  if (!document.getElementById('sw-user-modal')) {
    document.body.insertAdjacentHTML('beforeend', SW_USER_MODAL_HTML);
  }

  const mainSaveBtn = document.getElementById('btn-save-sw-user')!;
  const addUserBtn = document.getElementById('btn-open-add-user')!;
  const confirmUserBtn = document.getElementById('btn-confirm-user-edit')!;

  ['new-user-시작일', 'new-user-종료일'].forEach(id => {
    applyDateMask(document.getElementById(id) as HTMLInputElement);
  });

  createIcons({ icons: { Calendar } });

  addUserBtn.addEventListener('click', () => openUserEditSubModal());

  confirmUserBtn.addEventListener('click', () => {
    saveUserDataToList();
  });

  mainSaveBtn.addEventListener('click', () => {
    if (!currentSwUserAsset) return;
    
    // 전역 상태 업데이트
    const existingIdx = state.masterData.swUsers.findIndex(u => u.sw_id === currentSwUserAsset!.id);
    const newMapping = {
      sw_id: currentSwUserAsset!.id,
      userData: tempSwUsers.map(u => [u.조직, u.부서, u.직위, u.이름, u.사용기간, u.신청서명])
    };

    if (existingIdx > -1) state.masterData.swUsers[existingIdx] = newMapping as any;
    else state.masterData.swUsers.push(newMapping as any);

    onSave();
    document.getElementById('sw-user-modal')?.classList.add('hidden');
  });

  document.getElementById('btn-close-sw-user-modal')?.addEventListener('click', () => {
    document.getElementById('sw-user-modal')?.classList.add('hidden');
  });
  document.getElementById('btn-cancel-sw-user')?.addEventListener('click', () => {
    document.getElementById('sw-user-modal')?.classList.add('hidden');
  });
  document.getElementById('btn-close-user-edit')?.addEventListener('click', () => {
    document.getElementById('sw-user-edit-modal')?.classList.add('hidden');
  });
  document.getElementById('btn-close-user-sub')?.addEventListener('click', () => {
    document.getElementById('sw-user-edit-modal')?.classList.add('hidden');
  });
}

function saveUserDataToList() {
  const idx = parseInt(getFieldValue('edit-user-index'));
  const 신청서Input = document.getElementById('new-user-신청서') as HTMLInputElement;
  const 신청서명 = 신청서Input.files && 신청서Input.files.length > 0 ? 신청서Input.files[0].name : (idx > -1 ? tempSwUsers[idx].신청서명 : '');

  const userData: any = {
    조직: getFieldValue('new-user-조직'),
    부서: getFieldValue('new-user-부서'),
    직위: getFieldValue('new-user-직위'),
    이름: getFieldValue('new-user-이름'),
    사용기간: `${getFieldValue('new-user-시작일')} ~ ${getFieldValue('new-user-종료일')}`,
    신청서명
  };
  
  if (idx === -1) tempSwUsers.push(userData);
  else tempSwUsers[idx] = userData;
  
  document.getElementById('sw-user-edit-modal')?.classList.add('hidden');
  renderUserList();
}
