<?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="campaign-grid" title="Campaigns" model="com.axelor.apps.marketing.db.Campaign">
        <field name="name"/>
        <field name="campaignType"/>
        <field name="stageSelect"/>
    </grid>

	<form name="campaign-form" title="Campaign" model="com.axelor.apps.marketing.db.Campaign" width="large"
		onNew="action-campaign-attrs-on-new-on-load" onLoad="action-campaign-attrs-on-new-on-load">
		<menubar>
			<menu name="campaignToolsMenu" title="Tools" icon="fa-wrench" showTitle="true">
				<item name="launchEmailingItem" title="Launch emailing" action="save,action-campaign-validate-send-email,action-campaign-method-send-email" showIf="emailing"/>
			</menu>
		</menubar>
		<panel name="mainPanel" >
			<field name="stageSelect" widget="NavSelect" colSpan="12" showTitle="false" readonly="true"/>
			<field name="name" showTitle="false" colSpan="12">
				<viewer depends="name"><![CDATA[
					<h3>
						<span style="margin: 5px 0 !important; display: inline-table; line-height: initial;">{{record.name}}</span>
					</h3>
				]]></viewer>
				<editor>
					<field name="name" showTitle="true" />
				</editor>
			</field>
			<spacer name="nameSpacer"/>
			<panel name="informationPanel" title="Informations" colSpan="12">
				<field name="campaignType" canEdit="false" colSpan="4"/>
				<field name="eventStartDateT" colSpan="4"/>
				<field name="eventEndDateT" colSpan="4"/>
				<spacer name="campaignTypeSpacer"/>
				<field name="emailing" colSpan="4" widget="boolean-switch" />
				<field name="manageAnEventPerTarget" colSpan="4" widget="boolean-switch"/>
				<field name="manageAttendees" colSpan="4" widget="boolean-switch"/>
			</panel>
		</panel>
		<panel-tabs name="mainPanelTab">
			<panel title="Target List" name="targetListPanel">
				<field name="isAllowEditingOfTargets" colSpan="3" widget="boolean-switch" onChange="action-campaign-attrs-is-allow-editing-of-targets-on-change"/>
				<panel-related name="targetModelSetPanel" field="targetModelSet" colSpan="12"/>
				<button name="generateTargetsBtn" title="Generate Targets" onClick="save,action-campaign-generate-targets,save" colSpan="3"/>
				<button name="inviteSelectedTargetBtn" title="Invite selected targets" showIf="manageAttendees &amp;&amp; isAllowEditingOfTargets" onClick="save,action-campaign-method-invite-selected-targets" colSpan="4"/>
				<button name="inviteAllTargetsBtn" title="Invite all targets" showIf="manageAttendees &amp;&amp; isAllowEditingOfTargets" colSpan="4" onClick="save,action-campaign-method-invite-all-targets"/>
				<panel-related name="partnerSetPanel" field="partnerSet" colSpan="12" showIf="isAllowEditingOfTargets" grid-view="partner-campaign-grid" form-view="partner-form" canEdit="false"/>
				<panel-related name="leadSetPanel" field="leadSet" colSpan="12" showIf="isAllowEditingOfTargets" grid-view="lead-campaign-grid" form-view="lead-form" canEdit="false"/>
			</panel>
			<panel name="emailingPanel" title="Emailing" showIf="emailing" colSpan="12">
				<field name="partnerTemplate" domain="self.metaModel.name = 'Partner' AND self.mediaTypeSelect IN (4)" showIf="emailing"/>
				<field name="leadTemplate" domain="self.metaModel.name = 'Lead' AND self.mediaTypeSelect IN (4)" showIf="emailing"/>
				<field name="partnerReminderTemplate" domain="self.metaModel.name = 'Partner' AND self.mediaTypeSelect IN (4)"/>
				<field name="leadReminderTemplate" domain="self.metaModel.name = 'Lead' AND self.mediaTypeSelect IN (4)"/>
				<field name="emailAccount" showIf="emailing"/>
				<panel-dashlet name="generatedEmailsPanel" action="dashlet.generated.emails" height="350" colSpan="12"/>
			</panel>
			<panel name="eventsPanel" title="Events" showIf="manageAnEventPerTarget" colSpan="12">
				<field name="generateEventPerPartnerOrLead" widget="inline-checkbox" colSpan="12"/>
				<field name="team" onChange="action-campaign-attrs-team-value-fill" hideIf="generateEventPerPartnerOrLead" />
				<field name="eventUser" hideIf="generateEventPerPartnerOrLead" onChange="action-campaign-record-set-team" onSelect="action-campaign-attrs-eventuser-domain" />
				<field name="subject" requiredIf="manageAnEventPerTarget"/>
				<field name="eventTypeSelect" domain="self.value IN ('0','1','2','3')"  placeholder="Event Type" required="true" />
				<field name="eventStartDateTime" requiredIf="manageAnEventPerTarget"/>
				<field name="duration" widget="duration" x-big="true" />
				<button name="generateEventsBtn" title="Generate Events" colSpan="3" onClick="action-campaign-alert-event-user,save,action-campaign-method-generate-events" readonlyIf="duration == 0"/>
			</panel>
			<panel name="targetsToContactPanel" title="Targets to contact" showIf="manageAnEventPerTarget">
				<button name="openPlannedEventsBtn" title="Open planned events" colSpan="3" onClick="dashlet.target.to.contact.planned"/>
				<panel-dashlet name="plannedEventsPanel" action="dashlet.target.to.contact.planned" height="350" colSpan="12"/>
				<panel-dashlet name="realiazedEventsPanel" action="dashlet.target.to.contact.realized.cancel" height="350" colSpan="12"/>
			</panel>
			<panel name="resultsPanel" title="Results">
				<field name="report" widget="html" colSpan="12" />
				<panel name="invitedPanel" title="Invited" showIf="manageAttendees" colSpan="12">
					<panel-related name="invitedLeadSetPanel" field="invitedLeadSet" colSpan="12" grid-view="lead-campaign-grid" form-view="lead-form" canNew="false" canEdit="false" canSelect="false"/>
					<panel-related name="invitedPartnerSetPanel" field="invitedPartnerSet" colSpan="12" grid-view="partner-campaign-grid" form-view="partner-form" canNew="false" canEdit="false" canSelect="false"/>
					<button name="participateBtn" title="Participate" colSpan="3" onClick="save,action-campaign-method-participate-targets"/>
					<button name="notParticipatingBtn" title="Not participating" colSpan="3" onClick="save,action-campaign-method-not-participate-targets"/>
					<button name="sendReminderEmailBtn" title="Send a reminder" colSpan="4" onClick="save,action-campaign-validate-send-reminder-email,action-campaign-method-send-reminder-email,save" showIf="emailing"/>
				</panel>
				<panel name="enrolledPanel" title="Enrolled" showIf="manageAttendees" colSpan="12">
					<panel-related name="leadsPanel" field="leads" colSpan="12" grid-view="lead-campaign-grid" form-view="lead-form" canNew="false" canEdit="false" canSelect="false"/>
					<panel-related name="partnersPanel" field="partners" colSpan="12" grid-view="partner-campaign-grid" form-view="partner-form" canNew="false" canEdit="false" canSelect="false"/>
				</panel>
				<panel name="notParticipatingPanel" title="Not participating" showIf="manageAttendees" colSpan="12">
					<panel-related name="notParticipatingLeadSetPanel" field="notParticipatingLeadSet" colSpan="12" grid-view="lead-campaign-grid" form-view="lead-form" canNew="false" canEdit="false" canSelect="false"/>
					<panel-related name="notParticipatingPartnerSetPanel" field="notParticipatingPartnerSet" colSpan="12" grid-view="partner-campaign-grid" form-view="partner-form" canNew="false" canEdit="false" canSelect="false"/>
				</panel>
				<panel name="emailingReportPanel" title="Emailing report" colSpan="12">
					<field name="emailLog" widget="binary-link" showIf="emailing"/>
				</panel>
			</panel>
			<panel-related name="campaignReminderListPanel" field="campaignReminderList" colSpan="12" showTitle="false" form-view="campaign-reminder-form" grid-view="campaign-reminder-grid"/>
			<panel name="presencePanel" title="Presence" showIf="manageAttendees &amp;&amp; stageSelect &gt;= 2">
				<panel-related name="presentLeadSetPanel" field="presentLeadSet" colSpan="12" grid-view="lead-campaign-grid" form-view="lead-form"/>
				<panel-related name="presentPartnerSetPanel" field="presentPartnerSet" colSpan="12" grid-view="partner-campaign-grid" form-view="partner-form"/>
			</panel>
		</panel-tabs>
		<panel name="actionsPanel" sidebar="true">
			<button name="planBtn" showIf="stageSelect == 0" title="Plan" onClick="save,action-campaign-record-plan,save,action-campaign-attrs-on-new-on-load"/>
			<button name="startBtn" showIf="stageSelect == 1 || stageSelect == 3" title="Start" onClick="save,action-campaign-record-start,save,action-campaign-attrs-on-new-on-load"/>
			<button name="suspendBtn" showIf="stageSelect == 2" title="Suspend" onClick="save,action-campaign-record-suspend,save,action-campaign-attrs-on-new-on-load"/>
			<button name="endedBtn" showIf="stageSelect == 2" title="End" onClick="save,action-campaign-record-ended,save,action-campaign-attrs-on-new-on-load"/>
			<button name="cancelBtn" showIf="stageSelect != 0" title="Cancel" onClick="save,action-campaign-record-cancel,save,action-campaign-attrs-on-new-on-load" css="btn-danger" icon="fa-times" />
		</panel>
	</form>

	<kanban name="campaign-kanban" title="Campaign" model="com.axelor.apps.marketing.db.Campaign" columnBy="stageSelect" sequenceBy="sequence" draggable="true" limit="10">
	  <field name="name"/>
	  <field name="campaignType" />
	  <hilite color="danger" if="stageSelect &lt;= 1"/>
	  <hilite color="warning" if="stageSelect &lt;= 2"/>
	  <hilite color="info" if="stageSelect &lt;= 3"/>
	  <hilite color="success" if="stageSelect &lt;= 4"/>
	  <template><![CDATA[
	        <h4>{{name}}</h4>
	        <div class="card-body">{{campaignType.name}}</div>
	]]></template>
	</kanban>

	<action-attrs name="action-campaign-attrs-on-new-on-load">
		<attribute name="hidden" expr="eval: stageSelect &lt; 2"
			 for="invitedLeadSet.leadPresentAtEventBtn,leads.leadPresentAtEventBtn,notParticipatingLeadSet.leadPresentAtEventBtn,
				invitedPartnerSet.partnerPresentAtEventBtn,partners.partnerPresentAtEventBtn,notParticipatingPartnerSet.partnerPresentAtEventBtn"/>
	</action-attrs>

	<action-attrs name="action-campaign-attrs-eventuser-domain">
  		<attribute name="domain" if="team != null" expr = "eval: &quot; self.activeTeam.id IN (${team.id}) &quot;" for="eventUser"/>
  		<attribute name="domain" if="team == null" expr = "eval: null" for="eventUser"/>
	</action-attrs>

	<action-attrs name="action-campaign-attrs-team-value-fill">
  		<attribute  name="value" if="eval: team != null &amp;&amp; eventUser != null &amp;&amp; team != eventUser.activeTeam" expr="eval: null" for="eventUser"/>
	</action-attrs>

	<action-attrs name="action-campaign-attrs-is-allow-editing-of-targets-on-change">
		<attribute name="value" for="manageAttendees" expr="eval: false" if="!isAllowEditingOfTargets"/>
	</action-attrs>

	<action-record name="action-campaign-record-set-team" model="com.axelor.apps.marketing.db.Campaign">
  		<field name="team" expr="eval:eventUser.activeTeam" if="eventUser != null"/>
	</action-record>
	
	<action-record name="action-campaign-record-plan" model="com.axelor.apps.marketing.db.Campaign">
  		<field name="stageSelect" expr="1" />
	</action-record>
	
	<action-record name="action-campaign-record-start" model="com.axelor.apps.marketing.db.Campaign">
  		<field name="stageSelect" expr="2" />
	</action-record>
	
	<action-record name="action-campaign-record-suspend" model="com.axelor.apps.marketing.db.Campaign">
  		<field name="stageSelect" expr="3" />
	</action-record>
	
	<action-record name="action-campaign-record-ended" model="com.axelor.apps.marketing.db.Campaign">
  		<field name="stageSelect" expr="4" />
	</action-record>
	
	<action-record name="action-campaign-record-cancel" model="com.axelor.apps.marketing.db.Campaign">
  		<field name="stageSelect" expr="0" />
	</action-record>

	<action-method name="action-campaign-method-send-email">
		<call class="com.axelor.apps.marketing.web.CampaignController" method="sendEmail"/>
	</action-method>
	
	<action-method name="action-campaign-method-send-reminder-email">
		<call class="com.axelor.apps.marketing.web.CampaignController" method="sendReminderEmail"/>
	</action-method>
	
	<action-view name="action-lead-view-marketing-campaigns" title="Marketing Campaigns" model="com.axelor.apps.marketing.db.Campaign">
		<view type="grid" name="campaign-grid"/>
		<view type="form" name="campaign-form"/>
		<domain>self.leadSet.id IN (:id)</domain>
	</action-view>
	
	<action-view name="action-partner-view-marketing-campaigns" title="Marketing Campaigns" model="com.axelor.apps.marketing.db.Campaign">
		<view type="grid" name="campaign-grid"/>
		<view type="form" name="campaign-form"/>
		<domain>self.partnerSet.id IN (:id)</domain>
	</action-view>
    
     <action-validate name="action-campaign-alert-event-user">
     	<alert message="An event will be generated by target, are you sure you want to continue ?"/>
     </action-validate>
     
	<action-method name="action-campaign-method-generate-events" model="com.axelor.apps.marketing.db.Campaign">
		<call class="com.axelor.apps.marketing.web.CampaignController" method="generateEvents"/>
	</action-method>
	
	<action-view name="dashlet.target.to.contact.planned" title="Planned events"
		model="com.axelor.apps.crm.db.Event">
		<view type="grid" name="event-grid" />
		<view type="form" name="event-form" />
		<domain>self.campaign.id = :_id AND self.statusSelect IN (1)</domain>
	</action-view>
	
	<action-view name="dashlet.target.to.contact.realized.cancel" title="Past events" model="com.axelor.apps.crm.db.Event">
		<view type="grid" name="event-grid" />
		<view type="form" name="event-form" />
		<domain>self.campaign.id = :_id AND self.statusSelect IN (2,3)</domain>
	</action-view>
	
	 <action-view name="dashlet.generated.emails" title="Generated emails" model="com.axelor.apps.message.db.Message">
    	<view type="grid" name="message-grid"/>
    	<view type="form" name="message-form"/>
    	<domain>self.relatedTo1Select = 'com.axelor.apps.marketing.db.Campaign' AND self.relatedTo1SelectId = :id</domain>
    </action-view>
	
	<action-method name="action-campaign-generate-targets"  model="com.axelor.apps.marketing.db.Campaign">
		<call class="com.axelor.apps.marketing.web.CampaignController" method="generateTargets"/>
	</action-method>
	
	<action-validate name="action-campaign-validate-send-email">
		<error message="Please select at least one template" if="partnerTemplate == null &amp;&amp; leadTemplate == null"/>
	</action-validate>

	<action-validate name="action-campaign-validate-send-reminder-email">
		<error message="Please select at least one template" if="partnerReminderTemplate == null &amp;&amp; leadReminderTemplate == null"/>
	</action-validate>

	<action-method name="action-campaign-method-invite-selected-targets">
		<call class="com.axelor.apps.marketing.web.CampaignController" method="inviteSelectedTargets"/>
	</action-method>

	<action-method name="action-campaign-method-invite-all-targets">
		<call class="com.axelor.apps.marketing.web.CampaignController" method="inviteAllTargets"/>
	</action-method>

	<action-method name="action-campaign-method-participate-targets">
		<call class="com.axelor.apps.marketing.web.CampaignController" method="addParticipatingTargets"/>
	</action-method>

	<action-method name="action-campaign-method-not-participate-targets">
		<call class="com.axelor.apps.marketing.web.CampaignController" method="addNotParticipatingTargets"/>
	</action-method>

	<action-method name="action-campaign-method-mark-partner-present">
		<call class="com.axelor.apps.marketing.web.CampaignController" method="markPartnerPresent"/>
	</action-method>

	<action-method name="action-campaign-method-mark-lead-present">
		<call class="com.axelor.apps.marketing.web.CampaignController" method="markLeadPresent"/>
	</action-method>

</object-views>