<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Support Tickets</title>
    <link rel="stylesheet" href="/css/main.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>

<div id="app" class="support-ticket-container">
    <button @click="fetchResolved">Fetch Resolved Tickets</button>
    <button @click="fetchTickets">Fetch Unresolved Tickets</button>
    <h1>Support Tickets</h1>
    <div class="ticket-list">
        <div class="ticket-item" v-for="ticket in tickets" :key="ticket.ticketID">
            <div class="ticket-info">
                <p><strong>Subject:</strong> {{ ticket.subject }}</p>
                <p><strong>Description:</strong> {{ ticket.description }}</p>
                <p><strong>Date Resolved:</strong> {{ ticket.dateResolved }}</p>
            </div>
            <div class="ticket-actions">
                <button @click="resolveTicket(ticket.ticketID)">Resolve</button>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

<script>
    new Vue({
        el: '#app',
        data: {
            tickets: []
        },
        mounted() {
            this.fetchTickets();
        },
        methods: {
            fetchTickets() {
                axios.get('http://localhost:8080/api/support-tickets')
                    .then(response => {
                        this.tickets = response.data;
                        console.log(response.data);
                    })
                    .catch(error => {
                        console.error("Error fetching tickets:", error);
                    });
            },
            fetchResolved() {
                axios.get('http://localhost:8080/api/support-tickets/resolved')
                    .then(response => {
                        this.tickets = response.data;
                        console.log(response.data);
                    })
                    .catch(error => {
                        console.error("Error fetching resolved tickets:", error);
                    });
            },
            resolveTicket(ticketID) {
                console.log(ticketID);
                const status = 'RESOLVED';
                axios.put(`http://localhost:8080/api/support-tickets/${ticketID}/status`, status,
                {
                    headers: {
                        'Content-Type': 'text/plain'
                    }
                })
                    .then(() => {
                        alert(`Ticket ${ticketID} resolved.`);
                        this.fetchTickets();
                    })
                    .catch(error => {
                        console.error("Error resolving ticket:", error.response.data);
                    });
            }

        }
    });
</script>


</body>
</html>
