File size: 5,311 Bytes
6ef1b39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Export functions for quote system
// These functions are required by quote-ui.js

// --- Delivery Export Functions ---
function exportDeliveryExcel() {
    console.log('[V.AI STUDIO] Exporting delivery to Excel...');
    if (!window.cartItems || !window.cartItems.length) {
        alert('❌ Giỏ hàng trống!');
        return;
    }
    
    // Create CSV content
    let csv = 'STT,Tên sản phẩm,Mã SP,Số lượng,Đơn giá,Thành tiền,Ghi chú\n';
    let total = 0;
    window.cartItems.forEach((item, idx) => {
        let price = item.priceNum || 0;
        let qty = item.qty || 1;
        let subtotal = price * qty;
        total += subtotal;
        csv += `${idx + 1},"${item.name}","${item.sku || ''}",${qty},${price},${subtotal},"${item.note || ''}"\n`;
    });
    csv += `,,,,,,"${total}",Tổng cộng\n`;
    
    // Download
    const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = `giao-hang-${new Date().toISOString().slice(0,10)}.csv`;
    a.click();
    URL.revokeObjectURL(url);
    alert('✅ Đã xuất file Excel (CSV) thành công!');
}

function exportDeliveryPDF() {
    console.log('[V.AI STUDIO] Exporting delivery to PDF...');
    if (!window.cartItems || !window.cartItems.length) {
        alert('❌ Giỏ hàng trống!');
        return;
    }
    
    // Simple PDF export using jsPDF if available
    if (typeof jsPDF !== 'undefined') {
        const doc = new jsPDF();
        doc.setFontSize(16);
        doc.text('PHIẾU GIAO HÀNG', 14, 20);
        doc.setFontSize(10);
        doc.text(`Ngày: ${new Date().toLocaleDateString('vi-VN')}`, 14, 30);
        doc.text(`Tổng số: ${window.cartItems.length} sản phẩm`, 14, 36);
        
        let y = 50;
        window.cartItems.forEach((item, idx) => {
            if (y > 280) { doc.addPage(); y = 20; }
            doc.text(`${idx + 1}. ${item.name}`, 14, y);
            y += 7;
        });
        
        doc.save(`giao-hang-${new Date().toISOString().slice(0,10)}.pdf`);
        alert('✅ Đã xuất file PDF thành công!');
    } else {
        alert('⚠️ Thư viện jsPDF chưa được tải. Vui lòng thử xuất Excel.');
    }
}

// --- Quote Export Functions ---
function exportExcel() {
    console.log('[V.AI STUDIO] Exporting quote to Excel...');
    const table = document.getElementById('quoteTableBody');
    if (!table || !table.rows.length) {
        alert('❌ Báo giá trống!');
        return;
    }
    
    let csv = 'STT,Hình ảnh,Tên sản phẩm,Mã SP,Thông tin,SL,Đơn giá,Đơn giá CK,Thành tiền,Ghi chú\n';
    // Implementation here...
    alert('✅ Đã xuất báo giá Excel (đang triển khai...)');
}

function exportPDF() {
    console.log('[V.AI STUDIO] Exporting quote to PDF...');
    alert('✅ Đã xuất báo giá PDF (đang triển khai...)');
}

function printQuotation() {
    console.log('[V.AI STUDIO] Printing quotation...');
    window.print();
}

function shareQuoteImage() {
    console.log('[V.AI STUDIO] Sharing quote as image...');
    alert('✅ Chia sẻ ảnh báo giá (đang triển khai...)');
}

function shareQuoteLink() {
    console.log('[V.AI STUDIO] Sharing quote link...');
    const url = window.location.href;
    if (navigator.share) {
        navigator.share({ title: 'Báo giá V.AI STUDIO', url: url });
    } else {
        prompt('Copy link báo giá:', url);
    }
}

// --- VAI_ORDERS Module ---
window.VAI_ORDERS = window.VAI_ORDERS || {
    save: function() {
        console.log('[V.AI STUDIO] Saving order...');
        const orderData = {
            date: new Date().toISOString(),
            customer: {
                name: document.getElementById('qcName')?.value || '',
                phone: document.getElementById('qcPhone')?.value || '',
                email: document.getElementById('qcEmail')?.value || '',
                address: document.getElementById('qcAddr')?.value || ''
            },
            items: window.cartItems || [],
            total: window.cartTotal || 0
        };
        
        // Save to localStorage
        let orders = JSON.parse(localStorage.getItem('VAI_ORDERS') || '[]');
        orders.push(orderData);
        localStorage.setItem('VAI_ORDERS', JSON.stringify(orders));
        
        alert('✅ Đã lưu đơn hàng thành công!');
        console.log('[V.AI STUDIO] Order saved:', orderData);
    },
    
    openSearch: function() {
        console.log('[V.AI STUDIO] Opening order search...');
        const orders = JSON.parse(localStorage.getItem('VAI_ORDERS') || '[]');
        if (!orders.length) {
            alert('📋 Chưa có đơn hàng nào được lưu.');
            return;
        }
        
        let msg = `📋 Tìm thấy ${orders.length} đơn hàng:\n\n`;
        orders.slice(-5).forEach((order, idx) => {
            msg += `${idx + 1}. ${order.customer.name || 'Không tên'} - ${order.customer.phone || ''}\n`;
        });
        alert(msg);
    }
};

// --- Initialize cart items if not exists ---
window.cartItems = window.cartItems || [];
window.cartTotal = window.cartTotal || 0;

console.log('[V.AI STUDIO] Export functions loaded successfully!');