// cdata.jsx — data + helpers for the Contracts and Finance modules.
// Loaded after data.jsx (uses CUSTOMERS/CONTACTS indirectly via codes).

const TODAY = '2026-06-07';
function vnd(n){ return (Math.round(n || 0)).toLocaleString('vi-VN') + '\u20ab'; }
function vndShort(n){ // compact: 216.000.000 -> 216tr / 1.2 tỷ
  n = Math.round(n || 0);
  if (Math.abs(n) >= 1e9) return (n / 1e9).toLocaleString('vi-VN', { maximumFractionDigits:2 }) + ' tỷ';
  if (Math.abs(n) >= 1e6) return Math.round(n / 1e6).toLocaleString('vi-VN') + ' tr';
  return n.toLocaleString('vi-VN');
}
function daysBetween(a, b){ return Math.round((new Date(b) - new Date(a)) / 86400000); }
function monthKey(d){ return (d || '').slice(0, 7); } // YYYY-MM

/* ---- catalogs ---- */
const CONTRACT_CATEGORIES = [
  { id:'mua-ban', vi:'Hợp đồng mua bán', en:'Sales contract' },
  { id:'dich-vu', vi:'Hợp đồng dịch vụ', en:'Service contract' },
  { id:'bao-tri', vi:'Hợp đồng bảo trì', en:'Maintenance contract' },
  { id:'thue', vi:'Hợp đồng thuê', en:'Lease contract' },
  { id:'hop-tac', vi:'Hợp đồng hợp tác', en:'Partnership contract' },
];
const CONTRACT_STATUS = {
  draft:     { vi:'Nháp',         en:'Draft',     cls:'badge-neutral' },
  active:    { vi:'Hiệu lực',     en:'Active',    cls:'badge-success' },
  expiring:  { vi:'Sắp hết hạn',  en:'Expiring',  cls:'badge-warning' },
  completed: { vi:'Hoàn thành',   en:'Completed', cls:'badge-info' },
  cancelled: { vi:'Đã hủy',       en:'Cancelled', cls:'badge-error' },
};
const TERM_TYPES = { once:{ vi:'Một lần', en:'One-time' }, term:{ vi:'Kỳ hạn', en:'Term' } };

// customer addresses (master-data extension keyed by customer code)
const CUSTOMER_ADDR = {
  KIZUNA:'Lô A, KCN Tân Kim, H. Cần Giuộc, Long An',
  KYANON:'KMS Building, 234/1 Đ. 3/2, Q.10, TP.HCM',
  TSD:'72 Lê Thánh Tôn, Q.1, TP.HCM',
  SEMITEC:'KCN Long Hậu, H. Cần Giuộc, Long An',
  NGOCMINH:'15 Trường Chinh, Q. Tân Bình, TP.HCM',
  VTIDC:'Tòa nhà Viettel, 285 CMT8, Q.10, TP.HCM',
  AIPOWER:'8 Nguyễn Huệ, Q.1, TP.HCM',
  ONECAD:'27 Lê Lợi, Q.1, TP.HCM',
  DASSVN:'KCN Sóng Thần, Dĩ An, Bình Dương',
  NGTSGROUP:'Aeon Mall, Tân Phú, TP.HCM',
  HQGTECH:'123 Nguyễn Văn Linh, Q.7, TP.HCM',
  BAUMANN:'Industriestraße 5, Stuttgart, Germany',
};

function it(name, unit, qty, price, vat){ return { name, unit, qty, price, vat }; }
function pay(no, amount, dueDate, condition, paid, paidDate){ return { no, amount, dueDate, condition, paid:!!paid, paidDate:paidDate || null }; }

/* ---- contracts ---- */
const CONTRACTS = [
  { code:'HD-2026-001', category:'mua-ban', signDate:'2026-01-15', customer:'KIZUNA', rep:'LH-0001', termType:'once', startDate:'2026-01-15', endDate:null, status:'active', remindBefore:30,
    items:[ it('AppSheet Enterprise — License triển khai', 'gói', 1, 120000000, 8), it('Triển khai & đào tạo vận hành', 'gói', 1, 80000000, 8) ],
    schedule:[ pay(1, 108000000, '2026-01-20', 'Tạm ứng 50% khi ký', true, '2026-01-21'), pay(2, 108000000, '2026-03-15', 'Sau nghiệm thu', true, '2026-03-18') ] },

  { code:'HD-2026-002', category:'dich-vu', signDate:'2026-01-02', customer:'SEMITEC', rep:'LH-0002', termType:'term', startDate:'2026-01-01', endDate:'2026-12-31', status:'active', remindBefore:30,
    items:[ it('Dịch vụ vận hành hệ thống quản lý linh kiện', 'tháng', 12, 25000000, 8) ],
    schedule:[ pay(1, 81000000, '2026-01-05', 'Quý I — tạm ứng', true, '2026-01-06'), pay(2, 81000000, '2026-04-05', 'Quý II', true, '2026-04-07'), pay(3, 81000000, '2026-07-05', 'Quý III', false), pay(4, 81000000, '2026-10-05', 'Quý IV', false) ] },

  { code:'HD-2026-003', category:'dich-vu', signDate:'2026-02-10', customer:'KYANON', rep:'LH-0006', termType:'term', startDate:'2026-02-15', endDate:'2026-08-15', status:'active', remindBefore:30,
    items:[ it('Tổng đài Cloud VoIP — 50 máy nhánh', 'tháng', 6, 9000000, 8), it('Phí khởi tạo & cấu hình', 'lần', 1, 12000000, 8) ],
    schedule:[ pay(1, 38880000, '2026-02-18', 'Tạm ứng khi ký', true, '2026-02-19'), pay(2, 32400000, '2026-05-15', 'Giữa kỳ', true, '2026-05-16'), pay(3, 12000000, '2026-08-10', 'Cuối kỳ', false) ] },

  { code:'HD-2026-004', category:'bao-tri', signDate:'2025-06-20', customer:'NGOCMINH', rep:'LH-0008', termType:'term', startDate:'2025-06-25', endDate:'2026-06-24', status:'expiring', remindBefore:30,
    items:[ it('PC Health Check Annual — 80 thiết bị', 'năm', 1, 96000000, 8) ],
    schedule:[ pay(1, 51840000, '2025-06-25', 'Tạm ứng 50%', true, '2025-06-26'), pay(2, 51840000, '2025-12-25', 'Đợt 2', true, '2025-12-28') ] },

  { code:'HD-2025-018', category:'dich-vu', signDate:'2025-07-01', customer:'VTIDC', rep:'LH-0013', termType:'term', startDate:'2025-07-01', endDate:'2026-06-30', status:'expiring', remindBefore:30,
    items:[ it('Thuê hạ tầng Cloud & vận hành', 'tháng', 12, 18000000, 8) ],
    schedule:[ pay(1, 116640000, '2025-07-03', 'Nửa năm đầu', true, '2025-07-05'), pay(2, 116640000, '2026-01-03', 'Nửa năm sau', true, '2026-01-04') ] },

  { code:'HD-2026-005', category:'mua-ban', signDate:'2026-05-28', customer:'TSD', rep:'LH-0004', termType:'once', startDate:'2026-05-28', endDate:null, status:'draft', remindBefore:30,
    items:[ it('Giải pháp số hóa quy trình AppSheet', 'gói', 1, 150000000, 8), it('Tích hợp API & dữ liệu', 'gói', 1, 45000000, 8) ],
    schedule:[ pay(1, 105300000, '2026-06-15', 'Tạm ứng 50% khi ký', false), pay(2, 105300000, '2026-08-15', 'Sau nghiệm thu', false) ] },

  { code:'HD-2026-006', category:'thue', signDate:'2026-03-05', customer:'HQGTECH', rep:'LH-0011', termType:'term', startDate:'2026-03-10', endDate:'2027-03-09', status:'active', remindBefore:45,
    items:[ it('Thuê phần mềm quản lý vận hành (SaaS)', 'tháng', 12, 14000000, 8) ],
    schedule:[ pay(1, 90720000, '2026-03-12', 'Nửa năm đầu', true, '2026-03-13'), pay(2, 90720000, '2026-09-12', 'Nửa năm sau', false) ] },

  { code:'HD-2026-007', category:'mua-ban', signDate:'2026-04-18', customer:'DASSVN', rep:'LH-0003', termType:'once', startDate:'2026-04-18', endDate:null, status:'active', remindBefore:30,
    items:[ it('Hệ thống quản lý logistics — License', 'gói', 1, 220000000, 8), it('Phần cứng đầu đọc & thiết bị', 'bộ', 10, 6500000, 10), it('Triển khai tại hiện trường', 'gói', 1, 60000000, 8) ],
    schedule:[ pay(1, 178200000, '2026-04-22', 'Tạm ứng 50%', true, '2026-04-23'), pay(2, 106920000, '2026-06-30', 'Sau lắp đặt 30%', false), pay(3, 71280000, '2026-08-30', 'Nghiệm thu 20%', false) ] },

  { code:'HD-2026-008', category:'hop-tac', signDate:'2026-02-01', customer:'AIPOWER', rep:'LH-0010', termType:'term', startDate:'2026-02-01', endDate:'2026-07-31', status:'active', remindBefore:30,
    items:[ it('Hợp tác phát triển module AI', 'giai đoạn', 2, 70000000, 8) ],
    schedule:[ pay(1, 75600000, '2026-02-05', 'Giai đoạn 1', true, '2026-02-06'), pay(2, 75600000, '2026-06-20', 'Giai đoạn 2', false) ] },

  { code:'HD-2025-009', category:'dich-vu', signDate:'2025-09-10', customer:'ONECAD', rep:null, termType:'once', startDate:'2025-09-10', endDate:null, status:'completed', remindBefore:30,
    items:[ it('Tư vấn chuyển đổi số', 'gói', 1, 40000000, 8) ],
    schedule:[ pay(1, 43200000, '2025-09-15', 'Thanh toán 1 lần', true, '2025-09-16') ] },
];

function contractTotals(c){
  let sub = 0, vat = 0;
  (c.items || []).forEach(i => { const line = i.qty * i.price; sub += line; vat += line * (i.vat / 100); });
  return { sub, vat, total:sub + vat };
}
function contractStatus(c){
  if (c.status === 'draft' || c.status === 'completed' || c.status === 'cancelled') return c.status;
  if (c.termType === 'term' && c.endDate){
    const d = daysBetween(TODAY, c.endDate);
    if (d < 0) return 'completed';
    if (d <= (c.remindBefore || 30)) return 'expiring';
  }
  return 'active';
}

/* ---- expenses (Chi) ---- */
const EXPENSE_CATEGORIES = [
  { id:'luong', vi:'Lương & phúc lợi', en:'Payroll' },
  { id:'thiet-bi', vi:'Thiết bị & phần cứng', en:'Hardware' },
  { id:'thue-vp', vi:'Thuê văn phòng', en:'Office rent' },
  { id:'ha-tang', vi:'Hạ tầng & Cloud', en:'Infrastructure' },
  { id:'marketing', vi:'Marketing', en:'Marketing' },
  { id:'doi-tac', vi:'Chi trả đối tác', en:'Vendor payment' },
  { id:'khac', vi:'Chi phí khác', en:'Other' },
];
function exp(id, date, cat, desc, amount, status){ return { id, date, cat, desc, amount, status }; }
const EXPENSES = [
  exp('C001','2026-01-05','thue-vp','Thuê văn phòng Q1', 90000000,'paid'),
  exp('C002','2026-01-31','luong','Lương tháng 1', 220000000,'paid'),
  exp('C003','2026-02-12','thiet-bi','Mua 10 màn hình Dell + 5 laptop', 145000000,'paid'),
  exp('C004','2026-02-28','luong','Lương tháng 2', 220000000,'paid'),
  exp('C005','2026-03-08','ha-tang','Gia hạn Google Cloud + AppSheet', 38000000,'paid'),
  exp('C006','2026-03-31','luong','Lương tháng 3', 235000000,'paid'),
  exp('C007','2026-04-05','thue-vp','Thuê văn phòng Q2', 90000000,'paid'),
  exp('C008','2026-04-20','marketing','Chiến dịch quảng bá Q2', 55000000,'paid'),
  exp('C009','2026-04-30','luong','Lương tháng 4', 235000000,'paid'),
  exp('C010','2026-05-15','doi-tac','Chi trả đối tác Viettel IDC', 60000000,'paid'),
  exp('C011','2026-05-31','luong','Lương tháng 5', 240000000,'paid'),
  exp('C012','2026-06-06','ha-tang','Hạ tầng Cloud tháng 6', 18000000,'pending'),
  exp('C013','2026-06-15','luong','Lương tháng 6 (dự kiến)', 240000000,'pending'),
  exp('C014','2026-06-20','thiet-bi','Bổ sung thiết bị đầu đọc', 32000000,'pending'),
];

Object.assign(window, {
  TODAY, vnd, vndShort, daysBetween, monthKey,
  CONTRACT_CATEGORIES, CONTRACT_STATUS, TERM_TYPES, CUSTOMER_ADDR,
  CONTRACTS, contractTotals, contractStatus,
  EXPENSE_CATEGORIES, EXPENSES,
});
