File size: 8,045 Bytes
efaacf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
document.addEventListener('DOMContentLoaded', function() {
    // Initialize Feather Icons
    feather.replace();
    
    // Initialize Charts
    const categoryCtx = document.getElementById('categoryChart').getContext('2d');
    const statusCtx = document.getElementById('statusChart').getContext('2d');
    
    let categoryChart = new Chart(categoryCtx, {
        type: 'doughnut',
        data: {
            labels: ['Facilities', 'Technology', 'Equipment', 'Vehicles'],
            datasets: [{
                data: [12500, 0, 0, 0],
                backgroundColor: [
                    '#3B82F6',
                    '#10B981',
                    '#F59E0B',
                    '#EF4444'
                ],
                borderWidth: 0
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            cutout: '70%',
            plugins: {
                legend: {
                    position: 'bottom',
                    labels: {
                        boxWidth: 12,
                        padding: 20
                    }
                }
            }
        }
    });
    
    let statusChart = new Chart(statusCtx, {
        type: 'bar',
        data: {
            labels: ['Planned', 'Approved', 'In Progress', 'Completed'],
            datasets: [{
                label: 'Amount',
                data: [12500, 0, 0, 0],
                backgroundColor: '#3B82F6',
                borderRadius: 4
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            scales: {
                y: {
                    beginAtZero: true,
                    grid: {
                        display: false
                    }
                },
                x: {
                    grid: {
                        display: false
                    }
                }
            },
            plugins: {
                legend: {
                    display: false
                }
            }
        }
    });
    
    // Add row functionality
    document.getElementById('addRowBtn').addEventListener('click', function() {
        const tbody = document.querySelector('#capexTable tbody');
        const newRow = document.createElement('tr');
        newRow.innerHTML = `
            <td class="px-6 py-4 whitespace-nowrap">
                <input type="text" class="w-full border-0 focus:ring-2 focus:ring-blue-500 rounded" value="">
            </td>
            <td class="px-6 py-4 whitespace-nowrap">
                <select class="w-full border-0 focus:ring-2 focus:ring-blue-500 rounded bg-transparent">
                    <option>Facilities</option>
                    <option>Technology</option>
                    <option>Equipment</option>
                    <option>Vehicles</option>
                </select>
            </td>
            <td class="px-6 py-4">
                <input type="text" class="w-full border-0 focus:ring-2 focus:ring-blue-500 rounded" value="">
            </td>
            <td class="px-6 py-4 whitespace-nowrap">
                <input type="number" class="w-full border-0 focus:ring-2 focus:ring-blue-500 rounded" value="0">
            </td>
            <td class="px-6 py-4 whitespace-nowrap">
                <input type="date" class="w-full border-0 focus:ring-2 focus:ring-blue-500 rounded" value="${new Date().toISOString().split('T')[0]}">
            </td>
            <td class="px-6 py-4 whitespace-nowrap">
                <select class="w-full border-0 focus:ring-2 focus:ring-blue-500 rounded bg-transparent">
                    <option>Planned</option>
                    <option>Approved</option>
                    <option>In Progress</option>
                    <option>Completed</option>
                </select>
            </td>
            <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                <button class="text-red-500 hover:text-red-700 delete-row">
                    <i data-feather="trash-2"></i>
                </button>
            </td>
        `;
        tbody.appendChild(newRow);
        feather.replace();
    });
    
    // Delete row functionality
    document.addEventListener('click', function(e) {
        if (e.target.closest('.delete-row')) {
            const row = e.target.closest('tr');
            row.remove();
            updateDashboard();
        }
    });
    
    // Update dashboard when inputs change
    document.addEventListener('input', function(e) {
        if (e.target.tagName === 'INPUT' || e.target.tagName === 'SELECT') {
            updateDashboard();
        }
    });
    
    // Export to Excel functionality
    document.getElementById('exportBtn').addEventListener('click', exportToExcel);
    
    // Function to update dashboard metrics
    function updateDashboard() {
        const rows = document.querySelectorAll('#capexTable tbody tr');
        let total = 0;
        const categories = {
            'Facilities': 0,
            'Technology': 0,
            'Equipment': 0,
            'Vehicles': 0
        };
        const statuses = {
            'Planned': 0,
            'Approved': 0,
            'In Progress': 0,
            'Completed': 0
        };
        
        rows.forEach(row => {
            const amount = parseFloat(row.querySelector('td:nth-child(4) input').value) || 0;
            const category = row.querySelector('td:nth-child(2) select').value;
            const status = row.querySelector('td:nth-child(6) select').value;
            
            total += amount;
            categories[category] += amount;
            statuses[status] += amount;
        });
        
        // Update total
        document.getElementById('totalCapex').textContent = '$' + total.toLocaleString();
        
        // Update category chart
        categoryChart.data.datasets[0].data = Object.values(categories);
        categoryChart.update();
        
        // Update status chart
        statusChart.data.datasets[0].data = Object.values(statuses);
        statusChart.update();
    }
    
    // Function to export data to Excel
    function exportToExcel() {
        const wb = XLSX.utils.book_new();
        
        // Create data sheet
        const dataRows = [];
        const headers = ['Project', 'Category', 'Description', 'Amount', 'Date', 'Status'];
        
        dataRows.push(headers);
        
        document.querySelectorAll('#capexTable tbody tr').forEach(row => {
            const rowData = [
                row.querySelector('td:nth-child(1) input').value,
                row.querySelector('td:nth-child(2) select').value,
                row.querySelector('td:nth-child(3) input').value,
                parseFloat(row.querySelector('td:nth-child(4) input').value) || 0,
                row.querySelector('td:nth-child(5) input').value,
                row.querySelector('td:nth-child(6) select').value
            ];
            dataRows.push(rowData);
        });
        
        const dataWs = XLSX.utils.aoa_to_sheet(dataRows);
        XLSX.utils.book_append_sheet(wb, dataWs, "Capex Data");
        
        // Create dashboard sheet
        const dashboardData = [
            ['Metric', 'Value'],
            ['Total Capex', parseFloat(document.getElementById('totalCapex').textContent.replace(/[^0-9.-]+/g,""))],
            ...Object.entries(categoryChart.data.datasets[0].data).map(([i, value]) => 
                [`${categoryChart.data.labels[i]} Capex`, value]
            ),
            ...Object.entries(statusChart.data.datasets[0].data).map(([i, value]) => 
                [`${statusChart.data.labels[i]} Capex`, value]
            )
        ];
        
        const dashboardWs = XLSX.utils.aoa_to_sheet(dashboardData);
        XLSX.utils.book_append_sheet(wb, dashboardWs, "Dashboard");
        
        // Export the workbook
        XLSX.writeFile(wb, 'capex_report.xlsx');
    }
    
    // Initial dashboard update
    updateDashboard();
});