<?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="chart-builder-grid" title="Chart builder" model="com.axelor.studio.db.ChartBuilder">
  	<field name="name" />
  	<field name="title" />
  	<field name="model" />
  	<field name="chartType" />
  	<field name="appBuilder"  grid-view="app-builder-grid" form-view="app-builder-form"/>
  </grid>
  
  <form name="chart-builder-form" title="Chart builder" model="com.axelor.studio.db.ChartBuilder" width="large"
      onLoad="action-chart-builder-on-load">
      <panel name="mainPanel">
      	<field name="appBuilder" grid-view="app-builder-grid" form-view="app-builder-form"/>
      	<spacer name="appBuilderSpacer"/>
        <field name="name"/>
        <field name="title"/>
        <field name="chartType" required="true"/>
        <field name="isJson" onChange="action-chart-builder-json-change,action-chart-builder-clear-fields" colSpan="2"/>
        <panel name="modelPanel" colSpan="4" itemSpan="12">
        	<field name="model" widget="ref-text" x-target="com.axelor.meta.db.MetaJsonModel" x-target-name="name" showIf="isJson" requiredIf="isJson" onChange="action-chart-builder-clear-fields"/> 
        	<field name="model" widget="ref-text" x-target="com.axelor.meta.db.MetaModel" x-target-name="fullName" showIf="!isJson" requiredIf="!isJson" onChange="action-chart-builder-clear-fields"/> 
        </panel> 
        <panel name="aggregatePanel" colSpan="6" itemSpan="12" title="Aggregate" hideIf="model == null">
        	<field name="isJsonAggregateOn" colSpan="2" />
        	<panel name="subAggregatePanel" itemSpan="12" colSpan="10">
        		<field name="aggregateOnJson" domain="self.jsonModel.name = :model AND self.type IN ('string','integer','datetime','date','many-to-one','json-many-to-one')" onChange="action-chart-builder-aggregate-on-change,action-chart-builder-update-aggregate-on-target-type" widget="SuggestBox" showIf="isJsonAggregateOn" />
        		<field name="aggregateOn" domain="self.metaModel.fullName = :model AND (self.typeName IN ('String','Integer','LocalDate','ZonedDateTime', 'LocalDateTime')  OR self.relationship = 'ManyToOne')"  onChange="action-chart-builder-aggregate-on-change,action-chart-builder-update-aggregate-on-target-type" widget="SuggestBox"  showIf="!isJsonAggregateOn" />
	        </panel>
	        <field name="aggregateOnTarget" title="Target" onChange="action-chart-builder-update-aggregate-on-target-type" showIf="!isJsonAggregateOn &amp;&amp; aggregateOn.relationship != null || isJsonAggregateOn &amp;&amp; (aggregateOnJson.targetModel != null || aggregateOnJson.targetJsonModel != null)" requiredIf="!isJsonAggregateOn &amp;&amp; aggregateOn.relationship != null || isJsonAggregateOn &amp;&amp; (aggregateOnJson.targetModel != null || aggregateOnJson.targetJsonModel != null)" />
	        <field name="aggregateDateType" showIf="['LocalDateTime','LocalDate','ZonedDateTime','Date','Datetime'].indexOf(aggregateOnTargetType) > -1" requiredIf="['LocalDateTime','LocalDate','ZonedDateTime','Date','Datetime'].indexOf(aggregateOnTargetType) > -1" />
        </panel>
        <panel name="groupPanel" colSpan="6" itemSpan="12" title="ChartBuilder.Group" hideIf="model == null">
        	<field name="isJsonGroupOn" colSpan="2" />
        	<panel name="subGroupPanel" itemSpan="12" colSpan="10">
        		<field name="groupOnJson" requiredIf="isJsonGroupOn" domain="self.jsonModel.name = :model AND self.type IN ('string', 'datetime','date','many-to-one','json-many-to-one')"  widget="SuggestBox" onChange="action-chart-builder-group-on-change,action-chart-builder-update-group-on-target-type" showIf="isJsonGroupOn" />
        		<field name="groupOn" requiredIf="!isJsonGroupOn" domain="self.metaModel.fullName = :model AND (self.typeName IN ('String','Integer','LocalDate','ZonedDateTime', 'LocalDateTime')  OR self.relationship = 'ManyToOne')" onChange="action-chart-builder-group-on-change,action-chart-builder-update-group-on-target-type" widget="SuggestBox" showIf="!isJsonGroupOn" />
        	</panel>
        	<field name="groupOnTarget" title="Target" onChange="action-chart-builder-update-group-on-target-type" showIf="!isJsonGroupOn &amp;&amp; groupOn.relationship != null || isJsonGroupOn &amp;&amp; (groupOnJson.targetModel != null || groupOnJson.targetJsonModel != null)" requiredIf="!isJsonGroupOn &amp;&amp; groupOn.relationship != null || isJsonGroupOn &amp;&amp; (groupOnJson.targetModel != null || groupOnJson.targetJsonModel != null)" />
        	<field name="groupDateType" showIf="['LocalDateTime','LocalDate','ZonedDateTime','Date','Datetime'].indexOf(groupOnTargetType) > -1" requiredIf="['LocalDateTime','LocalDate','ZonedDateTime','Date','Datetime'].indexOf(groupOnTargetType) > -1" />
        </panel>
        <panel name="displayPanel" colSpan="6" title="Display" itemSpan="12" hideIf="model == null">
	        <field name="isJsonDisplayField" colSpan="2"/>
	        <panel name="displayFieldPanel" itemSpan="12" colSpan="10">
	          <field name="displayFieldJson" requiredIf="isJsonDisplayField" domain="self.jsonModel.name = :model AND self.type IN ('integer','decimal')" widget="SuggestBox"  showIf="isJsonDisplayField"/>
	       	  <field name="displayField" requiredIf="!isJsonDisplayField" domain="self.metaModel.fullName = :model AND self.typeName IN ('Integer','BigDecimal')" widget="SuggestBox" showIf="!isJsonDisplayField"/>
        	</panel>
        	<field name="aggregateOn.typeName" hidden="true" />  
        </panel>
        <field name="groupOnTargetType" hidden="true" />
        <field name="aggregateOnTargetType" hidden="true" />
        <field name="groupOn.relationship" hidden="true" />
        <field name="aggregateOn.relationship" hidden="true" />
        <field name="aggregateOnJson.targetModel" hidden="true" />
        <field name="aggregateOnJson.targetJsonModel" hidden="true" />
        <field name="groupOnJson.targetModel" hidden="true" />
        <field name="groupOnJson.targetJsonModel" hidden="true" />
       </panel>
      <panel hideIf="model == null" name="filterListPanel">
      	  <label name="userDetailLabel" title="&lt;b&gt;Tags:&lt;/b&gt; $user: Current user, $date: Today's date, $time: Current time" colSpan="12" />
      	  <panel-related name="filterListPanel" field="filterList" colSpan="12" editable="true" edit-window="self" grid-view="chart-builder-filter-grid" />
      </panel>
  </form>
  
  <action-attrs name="action-chart-builder-json-change">
  	<attribute name="value" for="model" expr="eval:null"/>
  	<attribute name="value" for="isJsonAggregateOn" expr="eval:isJson"/>
  	<attribute name="value" for="isJsonGroupOn" expr="eval:isJson"/>
  	<attribute name="value" for="isJsonDisplayField" expr="eval:isJson"/>
  	<attribute name="hidden" for="isJsonAggregateOn" expr="eval:isJson" />
  	<attribute name="hidden" for="isJsonGroupOn" expr="eval:isJson" />
  	<attribute name="hidden" for="isJsonDisplayField" expr="eval:isJson" />
  	<attribute name="value" for="filterList.isJson" expr="eval:isJson"/>
  	<attribute name="hidden" for="filterList.isJson" expr="eval:isJson"/>
  	<attribute name="hidden" for="filterList.metaField"  expr="eval:isJson"/>
  </action-attrs>
  
  <action-attrs name="action-chart-builder-on-load">
  	<attribute name="hidden" for="isJsonAggregateOn" expr="eval:isJson" />
  	<attribute name="hidden" for="isJsonGroupOn" expr="eval:isJson" />
  	<attribute name="hidden" for="isJsonDisplayField" expr="eval:isJson" />
  	<attribute name="hidden" for="filterList.isJson" expr="eval:isJson"/>
  	<attribute name="hidden" for="filterList.metaField"  expr="eval:isJson"/>
  </action-attrs>
  
  <action-attrs name="action-chart-builder-clear-fields">
  	<attribute name="value" for="aggregateOnJson" expr="eval:null"/>
  	<attribute name="value" for="aggregateOn" expr="eval:null"/>
  	<attribute name="value" for="aggregateDateType" expr="eval:null"/>
  	<attribute name="value" for="aggregateOnTarget" expr="eval:null"/>
  	<attribute name="value" for="aggregateOnTargetType" expr="eval:null"/>
  	<attribute name="value" for="groupOnJson" expr="eval:null"/>
  	<attribute name="value" for="groupOn" expr="eval:null"/>
  	<attribute name="value" for="groupDateType" expr="eval:null"/>
  	<attribute name="value" for="groupOnTarget" expr="eval:null"/>
  	<attribute name="value" for="groupOnTargetType" expr="eval:null"/>
  	<attribute name="value" for="displayFieldJson" expr="eval:null"/>
  	<attribute name="value" for="displayField" expr="eval:null"/>
  	<attribute name="value" for="filterList" expr="eval:null"/>
  </action-attrs>
  
  
  <action-record name="action-chart-builder-aggregate-on-change" model="com.axelor.studio.db.ChartBuilder">
  	<field name="aggregateOnTarget" expr="call:com.axelor.studio.service.builder.ChartBuilderService:getDefaultTarget(aggregateOn)" if="!isJsonAggregateOn &amp;&amp; aggregateOn != null"/>
  	<field name="aggregateOnTarget" expr="eval:null" if="!isJsonAggregateOn &amp;&amp; aggregateOn == null"/>
  	<field name="aggregateOnTarget" expr="call:com.axelor.studio.service.builder.ChartBuilderService:getDefaultTarget(aggregateOnJson)" if="isJsonAggregateOn &amp;&amp; aggregateOnJson != null"/>
  	<field name="aggregateOnTarget" expr="eval:null" if="isJsonAggregateOn &amp;&amp; aggregateOnJson == null"/>
  </action-record>
  
  <action-record name="action-chart-builder-update-aggregate-on-target-type" model="com.axelor.studio.db.ChartBuilder">
  	<field name="aggregateOnTargetType" expr="call:com.axelor.studio.service.builder.ChartBuilderService:getTargetType(aggregateOn, aggregateOnTarget)" if="!isJsonAggregateOn"/>
  	<field name="aggregateOnTargetType" expr="call:com.axelor.studio.service.builder.ChartBuilderService:getTargetType(aggregateOnJson, aggregateOnTarget)" if="isJsonAggregateOn"/>
  </action-record>
  
  <action-record name="action-chart-builder-group-on-change" model="com.axelor.studio.db.ChartBuilder">
  	<field name="groupOnTarget" expr="call:com.axelor.studio.service.builder.ChartBuilderService:getDefaultTarget(groupOn)" if="!isJsonGroupOn &amp;&amp; groupOn != null"/>
  	<field name="groupOnTarget" expr="eval:null" if="!isJsonGroupOn &amp;&amp; groupOn == null"/>
  	<field name="groupOnTarget" expr="call:com.axelor.studio.service.builder.ChartBuilderService:getDefaultTarget(groupOnJson)" if="isJsonGroupOn &amp;&amp; groupOnJson != null"/>
  	<field name="groupOnTarget" expr="eval:null" if="isJsonGroupOn &amp;&amp; groupOnJson == null"/>
  </action-record>
  
  <action-record name="action-chart-builder-update-group-on-target-type" model="com.axelor.studio.db.ChartBuilder">
  	<field name="groupOnTargetType" expr="call:com.axelor.studio.service.builder.ChartBuilderService:getTargetType(groupOn, groupOnTarget)" if="!isJsonGroupOn"/>
  	<field name="groupOnTargetType" expr="call:com.axelor.studio.service.builder.ChartBuilderService:getTargetType(groupOnJson, groupOnTarget)" if="isJsonGroupOn"/>
  </action-record>
  
</object-views>


