<?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="message-grid" title="Emails" model="com.axelor.apps.message.db.Message" orderBy="-sentDateT">
        <field name="sentDateT"/>
        <field name="senderUser" form-view="user-form" grid-view="user-grid"/>
        <field name="subject"/>
    </grid>

	<grid name="message-not-sent-grid" title="Emails"
		  model="com.axelor.apps.message.db.Message" orderBy="-sentDateT"
		  canNew="false" canDelete="false" canArchive="false">
		<toolbar>
			<button name="regenerateMessageBtn" title="Regenerate messages" onClick="action-message-method-regenerate-messages"/>
			<button name="resendMessageBtn" title="Resend messages" onClick="action-message-method-send-messages"/>
		</toolbar>
		<field name="createdOn"/>
		<field name="senderUser" form-view="user-form" grid-view="user-grid"/>
		<field name="subject"/>
	</grid>

    <form name="message-form" title="Email" model="com.axelor.apps.message.db.Message"
    onNew="action-group-message-message-onnew">
		<toolbar>
	        <button name="printBtn" title="Print" icon="fa-print" onClick="save,action-message-method-print-pdf-email" readonlyIf="id == null"/>
	        <button name="sendBtn" title="Send" icon="fa-envelope" onClick="action-group-message-message-send-click" showIf="statusSelect == 1 " />
        	<button name="trashBtn" title="Delete" icon="fa-trash" onClick="action-group-message-message-delete-click" showIf="statusSelect != 4 "/>
		</toolbar>
    	<panel name="informationsPanel" title="Informations" readonlyIf="statusSelect != 1">
            <field name="mediaTypeSelect" readonlyIf="statusSelect != 1"/>
			<field name="statusSelect"/>
			<field name="senderUser" form-view="user-form" grid-view="user-grid"/>
			<field name="sentDateT" colSpan="4"/>
			<panel name="emailAddressPanel" colSpan="12" showIf="mediaTypeSelect == 2">
			    <field name="fromEmailAddress" widget="SuggestBox" grid-view="email-address-grid" form-view="email-address-form"/>
				<field name="mailAccount" widget="SuggestBox" onChange="action-message-record-get-signature" requiredIf="mediaTypeSelect == 2" grid-view="mail-account-grid" form-view="mail-account-form" domain="self.isValid = true"/>
        		<field name="replyToEmailAddressSet" widget="TagSelect" x-create="address" form-view="email-address-form" colSpan="12" grid-view="email-address-grid" />
			</panel>
        </panel>
		<panel name="recipientAddressPanel" title="Recipient address" readonlyIf="statusSelect != 1" showIf="mediaTypeSelect == 1">
			<field name="addressBlock" colSpan="12" widget="html" x-lite="false" height="5" showTitle="false" showIf="mediaTypeSelect == 1" requiredIf="mediaTypeSelect == 1"/>
		</panel>
        <panel name="recipientsPanel" title="Recipients" readonlyIf="statusSelect != 1" showIf="mediaTypeSelect == 2">
        	<field name="toEmailAddressSet" widget="TagSelect" x-create="address" form-view="email-address-form" colSpan="12" requiredIf="mediaTypeSelect == 2" grid-view="email-address-grid" />
        	<field name="ccEmailAddressSet" widget="TagSelect" x-create="address" form-view="email-address-form" colSpan="12" grid-view="email-address-grid" />
        	<field name="bccEmailAddressSet" widget="TagSelect" x-create="address" form-view="email-address-form" colSpan="12" grid-view="email-address-grid" />
        </panel>
		<panel name="messageDestinatedToPanel" title="Message destinated to" readonlyIf="statusSelect != 1" showIf="mediaTypeSelect == 3">
			<field name="recipientUser" colSpan="6" showTitle="false" showIf="mediaTypeSelect == 3" requiredIf="mediaTypeSelect == 3" form-view="user-form" grid-view="user-grid"/>
		</panel>
        <panel name="contentPanel" title="Content" readonlyIf="statusSelect != 1">
        	<field name="subject" title="Subject" colSpan="12" required="true"/>
       	 	<field name="content" colSpan="12" showTitle="false" widget="html"/>
        </panel>
        <panel name="relatedToPanel" title="Related to" sidebar="true" colSpan="12" readonlyIf="statusSelect == 4">
			<panel name="relatedTo1SelectPanel" colSpan="12">
				<field name="relatedTo1Select" widget="RefSelect" x-related="relatedTo1SelectId" colSpan="12"/>
			</panel>
			<panel name="relatedTo2SelectPanel" colSpan="12">
				<field name="relatedTo2Select" widget="RefSelect" x-related="relatedTo2SelectId" colSpan="12" showIf="relatedTo1Select != null"/>
			</panel>
		</panel>
    </form>
    
    <action-group name="action-group-message-message-onnew">
    	<action name="action-message-record-default"/>
    	<action name="action-message-record-mail-account"/>
    	<action name="action-message-record-get-signature"/>
    	<action name="action-message-record-default-media-type"/>
    </action-group>
    
    <action-group name="action-group-message-message-send-click">
		<action name="action-validate-message-content-onsave"/>
    	<action name="save"/>
    	<action name="action-message-method-send-message"/>
    	<action name="save"/>
    </action-group>
    
    <action-group name="action-group-message-message-delete-click">
    	<action name="save"/>
    	<action name="action-message-record-delete-status"/>
    	<action name="save"/>
    </action-group>
    
	<action-record name="action-message-record-default-media-type" model="com.axelor.apps.message.db.Message">
    	<field name="mediaTypeSelect" expr="eval:defaultMediaType"/>
    	<field name="mediaTypeSelect" expr="eval: _mediaTypeSelect" if="_mediaTypeSelect"/>
    </action-record>

	<action-record name="action-message-record-default" model="com.axelor.apps.message.db.Message">
    	<field name="senderUser" expr="eval:__user__"/>
    	<field name="relatedTo1Select" expr="eval: _templateContextModel" if="_templateContextModel"/>
    	<field name="relatedTo1SelectId" expr="eval: Long.parseLong(_objectId)" if="_objectId"/>
    </action-record>
    
    <action-record name="action-message-record-mail-account" model="com.axelor.apps.message.db.Message">
    	<field name="mailAccount" expr="eval: __repo__(EmailAccount).all().filter('self.isDefault = true and self.user = ?1',senderUser).fetchOne()" if="__config__.app.getApp('base').getEmailAccountByUser()"/>
    	<field name="mailAccount" expr="eval: __repo__(EmailAccount).all().filter('self.isDefault = true').fetchOne()" if="!__config__.app.getApp('base').getEmailAccountByUser()"/>
    </action-record>
    
    <action-record name="action-message-record-get-signature" model="com.axelor.apps.message.db.Message">
    	<field name="content" expr="eval: '&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;' + mailAccount?.signature" if="content == null &amp;&amp; mailAccount?.signature != null"/>
    </action-record>
    
    <action-record name="action-message-record-delete-status" model="com.axelor.apps.message.db.Message">
    	<field name="statusSelect" expr="4"/>
    </action-record>

    
    <!-- ACTION METHOD -->
	<action-method name="action-message-method-send-message">
		<call class="com.axelor.apps.message.web.MessageController" method="sendMessage"/>
	</action-method>

    <action-method name="action-message-method-send-messages">
		<call class="com.axelor.apps.message.web.MessageController" method="sendMessages"/>
	</action-method>

	<action-method name="action-message-method-regenerate-messages">
		<call class="com.axelor.apps.message.web.MessageController" method="regenerateMessages"/>
	</action-method>

    <!-- 	Common action for all messages sent by email using a template and the context -->
	
	<action-method name="action-send-by-email-with-template">
		<call class="com.axelor.apps.message.web.GenerateMessageController" method="callMessageWizard"/>
	</action-method>
	
	<action-validate name="action-validate-message-content-onsave">
		<alert message="No Mail content entered." if="mediaTypeSelect == 1 &amp;&amp; content== null"/>
	</action-validate>

	<action-view name="action.message.view.messages"
				 model="com.axelor.apps.message.db.Message"
				 title="Mail">
		<view name="message-grid" type="grid"/>
		<view name="message-form" type="form"/>
		<domain>
			self.relatedTo1SelectId = :relatedToSelectId
			AND self.relatedTo1Select = :relatedToSelect
			OR self.relatedTo2SelectId = :relatedToSelectId
			AND self.relatedTo2Select = :relatedToSelect
		</domain>
		<context name="relatedToSelectId" expr="eval: id"/>
		<context name="relatedToSelect"
				 expr="eval: _model"/>
	</action-view>

</object-views>
