<?xml version="1.0" encoding="utf-8"?>
<odoo>

    <template id="portal_my_home_helpdesk" name="Portal My Home: Helpdesk"
              inherit_id="portal.portal_my_home" priority="40">
        <xpath expr="//div[hasclass('o_portal_docs')]" position="inside">
            <t t-call="portal.portal_docs_entry">
                <t t-set="title">Helpdesk Tickets</t>
                <t t-set="url" t-value="'/my/helpdesk'"/>
                <t t-set="placeholder_count" t-value="'helpdesk_ticket_count'"/>
            </t>
        </xpath>
    </template>

    <template id="portal_my_tickets" name="My Helpdesk Tickets">
        <t t-call="portal.portal_layout">
            <t t-set="breadcrumbs_searchbar" t-value="True"/>
            <t t-call="portal.portal_searchbar">
                <t t-set="title">Helpdesk Tickets</t>
            </t>
            <t t-if="not tickets">
                <p class="alert alert-warning">You have no helpdesk tickets yet.</p>
            </t>
            <t t-if="tickets" t-call="portal.portal_table">
                <thead>
                    <tr class="active">
                        <th>Reference</th>
                        <th>Subject</th>
                        <th>Team</th>
                        <th>Stage</th>
                        <th>Date</th>
                    </tr>
                </thead>
                <tbody>
                    <tr t-foreach="tickets" t-as="ticket">
                        <td><a t-attf-href="/my/helpdesk/ticket/#{ticket.id}"><t t-esc="ticket.ticket_ref"/></a></td>
                        <td><t t-esc="ticket.name"/></td>
                        <td><t t-esc="ticket.team_id.name"/></td>
                        <td><t t-esc="ticket.stage_id.name"/></td>
                        <td><t t-esc="ticket.create_date"/></td>
                    </tr>
                </tbody>
            </t>
        </t>
    </template>

    <template id="portal_ticket_page" name="Helpdesk Ticket Detail">
        <t t-call="portal.portal_layout">
            <div class="container mt-4">
                <h2><t t-esc="ticket.ticket_ref"/> — <t t-esc="ticket.name"/></h2>
                <div class="row mb-3">
                    <div class="col-md-6">
                        <strong>Team:</strong> <t t-esc="ticket.team_id.name"/><br/>
                        <strong>Stage:</strong> <t t-esc="ticket.stage_id.name"/><br/>
                        <strong>Type:</strong> <t t-esc="ticket.ticket_type_id.name or '-'"/>
                    </div>
                    <div class="col-md-6">
                        <strong>Assigned to:</strong> <t t-esc="ticket.user_id.name or 'Unassigned'"/><br/>
                        <strong>Created:</strong> <t t-esc="ticket.create_date"/>
                    </div>
                </div>
                <div t-if="ticket.description" class="border rounded p-3 bg-light">
                    <t t-out="ticket.description"/>
                </div>
            </div>
        </t>
    </template>

    <template id="helpdesk_submit_form" name="Submit Helpdesk Ticket">
        <t t-call="website.layout">
            <div class="container mt-5 mb-5">
                <h1>Submit a Support Request</h1>
                <p class="lead">Silicon Overdrive Helpdesk — products, software, and cloud services.</p>
                <form action="/helpdesk/submit" method="POST" class="o_mark_required">
                    <input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>
                    <div class="mb-3">
                        <label class="form-label" for="name">Subject *</label>
                        <input type="text" class="form-control" name="name" required="required"/>
                    </div>
                    <div class="mb-3">
                        <label class="form-label" for="team_id">Department *</label>
                        <select class="form-select" name="team_id" required="required">
                            <t t-foreach="teams" t-as="team">
                                <option t-att-value="team.id"><t t-esc="team.name"/></option>
                            </t>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label" for="ticket_type_id">Request Type</label>
                        <select class="form-select" name="ticket_type_id">
                            <option value="">— Select —</option>
                            <t t-foreach="ticket_types" t-as="ttype">
                                <option t-att-value="ttype.id"><t t-esc="ttype.name"/></option>
                            </t>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label" for="partner_email">Your Email *</label>
                        <input type="email" class="form-control" name="partner_email" required="required"/>
                    </div>
                    <div class="mb-3">
                        <label class="form-label" for="partner_phone">Phone</label>
                        <input type="text" class="form-control" name="partner_phone"/>
                    </div>
                    <div class="mb-3">
                        <label class="form-label" for="priority">Priority</label>
                        <select class="form-select" name="priority">
                            <option value="0">Low</option>
                            <option value="1" selected="selected">Normal</option>
                            <option value="2">High</option>
                            <option value="3">Urgent</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label" for="description">Description *</label>
                        <textarea class="form-control" name="description" rows="6" required="required"/>
                    </div>
                    <button type="submit" class="btn btn-primary">Submit Ticket</button>
                </form>
            </div>
        </t>
    </template>

    <template id="helpdesk_submit_thanks" name="Helpdesk Ticket Submitted">
        <t t-call="website.layout">
            <div class="container mt-5 text-center">
                <h1>Thank You!</h1>
                <p class="lead">
                    Your ticket <strong><t t-esc="ticket.ticket_ref"/></strong> has been submitted.
                </p>
                <p>Our team will respond as soon as possible.</p>
            </div>
        </t>
    </template>

</odoo>
