<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<object-views xmlns="http://axelor.com/xml/ns/object-views" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://axelor.com/xml/ns/object-views http://axelor.com/xml/ns/object-views/object-views_5.2.xsd">


	<grid name="ticket-grid" title="All tickets" model="com.axelor.apps.helpdesk.db.Ticket">
		<toolbar>
			<button name="takeChargeBtn" title="Assign to me" icon="fa-suitcase" onClick="com.axelor.apps.helpdesk.web.TicketController:assignToMeTicket"/>
		</toolbar>
		<field name="ticketSeq" width="120"/>
		<field name="subject"/>
		<field name="ticketType"/>
		<field name="startDateT"/>
		<field name="endDateT"/>
		<field name="deadlineDateT"/>
		<field name="assignedToUser"/>
		<field name="prioritySelect"/>
		<field name="statusSelect"/>
		<field name="timerState" hidden="true"/>
		<field name="progressSelect" widget="SelectProgress"/>
		<button name="takeChargeBtn" icon="fa-suitcase" help="Take charge" onClick="com.axelor.apps.helpdesk.web.TicketController:assignToMeTicket" readonlyIf="statusSelect == 3"/>
		<button name="startTicketBtn" icon="fa-play" onClick="action-ticket-group-on-start" readonlyIf="timerState == true || statusSelect == 3"/>
		<button name="pauseBtn" icon="fa-pause" onClick="action-ticket-group-on-pause" readonlyIf="timerState == false || statusSelect == 3"/>
		<button name="validateTicketBtn" icon="fa-check" onClick="action-ticket-group-on-validate" readonlyIf="statusSelect == 3"/>
		<button name="finishTicketBtn" icon="fa-power-off" onClick="action-ticket-group-on-close" readonlyIf="statusSelect == 3"/>
	</grid>

	<form model="com.axelor.apps.helpdesk.db.Ticket" title="Ticket" name="ticket-form" onNew="action-ticket-group-on-new" onLoad="action-ticket-group-timer-process">
		<panel name="mainPanel" colSpan="12">
			<field name="statusSelect" colSpan="12" readonly="true" widget="NavSelect" showTitle="false" onChange="action-record-to-set-progress-select"/>
			<field name="ticketSeq" showTitle="false">
				<viewer depends="ticketSeq">
					<![CDATA[
						<h3>
							<span  style="margin: 5px 0 !important; display: inline-table; line-height: initial;"><span x-translate>Ticket N° </span>{{record.ticketSeq}}</span>
						</h3>
					]]>
				</viewer>
			</field>
			<panel name="progressPanel" colSpan="6" itemSpan="12">
				<field name="progressSelect" widget="SelectProgress">
					<editor>
						<field name="progressSelect" colSpan="6" showTitle="false"/>
					</editor>
				</field>
			</panel>
			<field name="subject" colSpan="12"/>
			<field name="mailSubject"/>

			<field name="slaPolicy" showTitle="false" readonly="true" hideIf="statusSelect >= slaPolicy.reachStageSelect">
				<viewer><![CDATA[
					<h3>
			        	<span class="label label-important" ng-if="record.slaPolicy">{{record.slaPolicy.name}}</span>
			        </h3>
				]]></viewer>
			</field>
		</panel>

		<panel name="otherDetailsPanel" colSpan="12">
			<field name="project" if-module="axelor-project" onChange="action-ticket-record-value-on-customer-and-contact" form-view="helpdesk-project-form" grid-view="helpdesk-project-grid"/>
			<field name="customer" domain="self.isCustomer = true" form-view="partner-form" grid-view="partner-grid"/>
			<field name="contactPartner" onSelect="action-ticket-domain-on-contact-partner" form-view="partner-contact-form" grid-view="partner-contact-grid"/>
		</panel>

        <panel-tabs name="mainPanelTab">
		    <panel name="descriptionPanel" colSpan="12" title="Description">
			    <field name="description" showTitle="false" colSpan="12" widget="html"/>
		    </panel>

		    <panel name="timerPanel" title="Timer" showIf="id" hidden="true" sidebar="true" if="__config__.app.getApp('helpdesk').getManageTimer()">
			    <button name="startTimerBtn" title="Start" icon="fa-play" colSpan="4" hidden="true"
				    onClick="action-ticket-method-start-timer"/>
			    <button name="stopTimerBtn" title="Stop" icon="fa-pause" colSpan="4" hidden="true"
				    onClick="action-ticket-method-stop-timer"/>
			    <button name="cancelTimerBtn" title="Cancel" icon="fa-times-circle" colSpan="4"
				    hidden="true" onClick="action-ticket-method-cancel-timer" css="btn-danger"/>
			    <field name="$_totalTimerDuration" type="decimal" readonly="true"
				    title="Total duration (Hours)" colSpan="4"/>
				<field name="realTotalDuration" colSpan="4"/>
				<field name="timerState" hidden="true" colSpan="4"/>
		    </panel>
        </panel-tabs>

		<panel name="actionsPanel" sidebar="true">
			<button name="startTicketBtn" title="Start" icon="fa-play" hideIf="statusSelect > 0" onClick="save,action-ticket-group-on-start"/>
			<button name="validateTicketBtn" title="Resolve" icon="fa-check" hideIf="statusSelect > 1" onClick="save,action-ticket-group-on-validate"/>
			<button name="finishTicketBtn" title="Close" icon="fa-power-off" css="btn-danger" hideIf="statusSelect > 2" onClick="save,action-ticket-group-on-close"/>
		</panel>

		<panel name="characteristicsPanel" title="Characteristics" sidebar="true" canCollapse="true">
			<field name="ticketType" colSpan="12"/>
			<field name="prioritySelect" colSpan="6"/>
			<field name="deadlineDateT" colSpan="6"/>
			<field name="isSlaCompleted" hidden="true"/>
		</panel>
		<panel name="followupPanel" title="Follow-up" sidebar="true" canCollapse="true">
			<field name="assignedToUser" colSpan="6" form-view="user-form" grid-view="user-grid"/>
			<field name="responsibleUser" colSpan="6" form-view="user-form" grid-view="user-grid"/>
			<field name="lead" colSpan="6" form-view="lead-form" grid-view="lead-grid" if-module="axelor-crm" if="__config__.app.isApp('crm')"/>
		</panel>
		<panel name="datesPanel" title="Dates" sidebar="true" canCollapse="true">
			<field name="startDateT" colSpan="6" onChange="action-ticket-method-compute-from-start-date-time"/>
			<field name="endDateT" colSpan="6" onChange="action-ticket-method-compute-from-end-date-time"/>
			<field name="duration" colSpan="6" widget="duration" x-big="true" onChange="action-ticket-method-compute-from-duration"/>
		</panel>
		<panel name="attrsPanel">
			<field name="attrs" hidden="true"/>
		</panel>
		<panel-mail name="mailPanel">
	    	<mail-messages limit="6"/>
	    	<mail-followers/>
	  	</panel-mail>
	</form>

	<calendar name="ticket-calendar" model="com.axelor.apps.helpdesk.db.Ticket"
	eventStart="startDateT"
	eventStop="endDateT"
	title="All tickets"
	colorBy="assignedToUser">
		<field name="subject"/>
		<field name="statusSelect"/>
	</calendar>

	<kanban columnBy="statusSelect" sequenceBy="prioritySelect"  limit="10"
	 name="ticket-kanban" title="Tickets" model="com.axelor.apps.helpdesk.db.Ticket">
	 <field name="ticketSeq"/>
	 <field name="subject"/>
	 <field name="progressSelect"/>
	 <field name="project.name"/>
	 <field name="ticketType"/>
	 <hilite color="primary" if="prioritySelect == 1"/>
	 <hilite color="info" if="prioritySelect == 2"/>
	 <hilite color="warning" if="prioritySelect == 3"/>
	 <hilite color="danger" if="prioritySelect == 4"/>
	 <template>
	 <![CDATA[
     <h4>{{record.ticketSeq}}</h4>
     <div class="card-body">
    	<p>{{record.subject}}</p>
        <dl>
			<dt ng-show="record.progressSelect" x-translate>Progress</dt>
			<dd ng-if="record.progressSelect == 80 || record.progressSelect == 90 || record.progressSelect == 100"><div class="progress progress-striped progress-success">
            	<div class="bar" style="width: {{record.progressSelect}}%;"></div></div>
            </dd>
            <dd ng-if="record.progressSelect == 50 || record.progressSelect == 60 || record.progressSelect == 70"><div class="progress progress-striped progress-primary">
            	<div class="bar" style="width: {{record.progressSelect}}%;"></div></div>
            </dd>
             <dd ng-if="record.progressSelect == 30 || record.progressSelect == 40"><div class="progress progress-striped progress-warning">
            	<div class="bar" style="width: {{record.progressSelect}}%;"></div></div>
            </dd>
             <dd ng-if="record.progressSelect == 10 || record.progressSelect == 20"><div class="progress progress-striped progress-danger">
            	<div class="bar" style="width: {{record.progressSelect}}%;"></div></div>
            </dd>
            <dt ng-show="record.project.name" x-translate>Project</dt><dd ng-show="record.project.name">{{record.project.name}}</dd>
            <dt ng-show="record.ticketType.name" x-translate>Ticket Type</dt><dd ng-show="record.ticketType.name">{{record.ticketType.name}}</dd>
        </dl>
 	 </div>
     ]]>
	 </template>
	 </kanban>

	<action-record name="action-ticket-record-value-on-customer-and-contact" model="com.axelor.apps.helpdesk.db.Ticket">
		<field name="customer" expr="eval: project.clientPartner"/>
		<field name="contactPartner" expr="eval: project.contactPartner"/>
	</action-record>

	<action-record name="action-ticket-record-set-start-status" model="com.axelor.apps.helpdesk.db.Ticket">
		<field name="statusSelect" expr="1" if="statusSelect == 0"/>
	</action-record>

	<action-record name="action-ticket-record-set-validate-status" model="com.axelor.apps.helpdesk.db.Ticket">
		<field name="statusSelect" expr="2" if="statusSelect &lt;= 1"/>
	</action-record>

	<action-record name="action-ticket-record-set-closed-status" model="com.axelor.apps.helpdesk.db.Ticket">
		<field name="statusSelect" expr="3" if="statusSelect &lt;= 2"/>
	</action-record>

	<action-record name="action-ticket-record-to-set-progress-select" model="com.axelor.apps.helpdesk.db.Ticket">
		<field name="progressSelect" expr="100" if="statusSelect == 2"/>
		<field name="progressSelect" expr="0" if="statusSelect != 2"/>
	</action-record>

	<action-record name="action-ticket-record-to-fill-startDate" model="com.axelor.apps.helpdesk.db.Ticket">
		<field name="startDateT" expr="eval: __datetime__"/>
	</action-record>

	<action-record name="action-ticket-record-to-fill-endDate" model="com.axelor.apps.helpdesk.db.Ticket">
		<field name="endDateT" expr="eval: __datetime__"/>
	</action-record>

	<action-record name="action-ticket-record-set-customer" model="com.axelor.apps.helpdesk.db.Ticket">
		<field name="customer" expr="eval: __user__?.partner" if="__user__?.partner?.isCustomer"/>
	</action-record>

	<action-group name="action-ticket-group-on-new">
		<action name="action-ticket-record-to-fill-startDate"/>
		<action name="action-ticket-record-set-customer"/>
	</action-group>

	<action-group name="action-ticket-group-on-start">
		<action name="action-ticket-method-start-timer"/>
		<action name="action-ticket-method-play"/>
		<action name="action-ticket-record-to-fill-startDate"/>
		<action name="action-ticket-method-compute-from-start-date-time"/>
		<action name="action-ticket-record-set-start-status"/>
		<action name="save"/>
	</action-group>

	<action-group name="action-ticket-group-on-validate">
		<action name="action-ticket-method-stop-timer"/>
		<action name="action-ticket-method-pause"/>
		<action name="action-ticket-method-compute-real-total-duration"/>
		<action name="action-ticket-record-to-fill-endDate"/>
		<action name="action-ticket-method-compute-from-end-date-time"/>
		<action name="action-ticket-record-set-validate-status"/>
		<action name="action-ticket-record-to-set-progress-select"/>
		<action name="save"/>
	</action-group>

	<action-group name="action-ticket-group-on-close">
		<action name="action-ticket-method-stop-timer"/>
		<action name="action-ticket-method-pause"/>
		<action name="action-ticket-method-compute-real-total-duration"/>
		<action name="action-ticket-record-set-closed-status"/>
		<action name="save"/>
	</action-group>

	<action-group name="action-ticket-group-on-pause">
		<action name="action-ticket-method-stop-timer"/>
		<action name="action-ticket-method-pause"/>
		<action name="save"/>
	</action-group>

	<action-method name="action-ticket-method-compute-from-start-date-time">
		<call class="com.axelor.apps.helpdesk.web.TicketController" method="computeFromStartDateTime"/>
	</action-method>

	<action-method name="action-ticket-method-compute-from-duration">
		<call class="com.axelor.apps.helpdesk.web.TicketController" method="computeFromDuration"/>
	</action-method>

	<action-method name="action-ticket-method-compute-from-end-date-time">
		<call class="com.axelor.apps.helpdesk.web.TicketController" method="computeFromEndDateTime"/>
	</action-method>

	<action-method name="action-ticket-method-pause" model="com.axelor.apps.helpdesk.db.Ticket">
		<call class="com.axelor.apps.helpdesk.web.TicketController" method="timerStateOff"/>
	</action-method>

	<action-method name="action-ticket-method-play" model="com.axelor.apps.helpdesk.db.Ticket">
		<call class="com.axelor.apps.helpdesk.web.TicketController" method="timerStateOn"/>
	</action-method>
	
	<action-attrs name="action-ticket-domain-on-contact-partner">
		<attribute for="contactPartner" if="customer != null &amp;&amp; !customer.contactPartnerSet.empty" name="domain" expr="eval: &quot;self.id IN (${customer.contactPartnerSet?.collect{it.id}.join(',')})&quot;"/>
 		<attribute for="contactPartner" if="customer != null &amp;&amp; customer.contactPartnerSet.empty" name="domain" expr="eval: &quot;self.id IN (0)&quot;"/>
 		<attribute for="contactPartner" if="customer == null" name="domain" expr="eval: &quot;self.isContact = true&quot;"/>
	</action-attrs>

	<action-method name="action-ticket-method-manage-timer-buttons">
		<call class="com.axelor.apps.helpdesk.web.TicketController" method="manageTimerButtons"/>
	</action-method>

	<action-method name="action-ticket-method-compute-total-timer-duration">
		<call class="com.axelor.apps.helpdesk.web.TicketController"
			method="computeTotalTimerDuration"/>
	</action-method>

	<action-group name="action-ticket-group-timer-process">
		<action name="action-ticket-method-manage-timer-buttons" if="__config__.app.getApp('helpdesk').manageTimer"/>
		<action name="action-ticket-method-compute-total-timer-duration" if="__config__.app.getApp('helpdesk').manageTimer"/>
	</action-group>

	<action-method name="action-ticket-method-start-timer">
		<call class="com.axelor.apps.helpdesk.web.TicketController" method="startTimer"/>
	</action-method>

	<action-method name="action-ticket-method-stop-timer">
		<call class="com.axelor.apps.helpdesk.web.TicketController" method="stopTimer"/>
	</action-method>

	<action-method name="action-ticket-method-cancel-timer">
		<call class="com.axelor.apps.helpdesk.web.TicketController" method="cancelTimer"/>
	</action-method>

	<action-method name="action-ticket-method-compute-real-total-duration">
		<call class="com.axelor.apps.helpdesk.web.TicketController" method="computeRealDuration"/>
	</action-method>
</object-views>
