<?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="configurator-creator-grid" title="ConfiguratorCreators" model="com.axelor.apps.sale.db.ConfiguratorCreator">
	    <toolbar>
	    	<button name="exportCreatorsBtn" title="Export" onClick="action-configurator-creator-export"/>
	    	<button name="importCreatorsBtn" title="Import" onClick="action-configurator-creator-view-import"/>
	    </toolbar>
    	<field name="name"/>
		<field name="isActive"/>
    </grid>


    <form name="configurator-creator-form" title="ConfiguratorCreator" model="com.axelor.apps.sale.db.ConfiguratorCreator">
        <panel name="mainPanel" showIf="!isActive &amp;&amp; attributes.length &gt; 0 &amp;&amp; (configuratorProductFormulaList.length + configuratorSOLineFormulaList.length) &gt; 0">
			<!--it does not matter which field is used here, as long as it is not a boolean-->
            <field name="name" readonly="true" showTitle="false">
				<viewer><![CDATA[

			 			<h4>
			             <span class="label label-important" x-translate>Changes were made to the configurator creator. Please activate the configurator creator to use it.</span>
			           </h4>

					]]></viewer>
			</field>
		</panel>

    	<panel name="otherDetailsPanel">
    		<field name="name"/>
    		<button name="configureBtn" title="Configure" showIf="id == null" onClick="save, action-configurator-creator-method-configure"/>
    		<panel-related name="attributesPanel" field="attributes" hideIf="id == null" onChange="action-configurator-creator-record-desactivate,save"
    				colSpan="12" readonlyIf="id == null" form-view="sale-json-field-form" grid-view="sale-json-field-grid" canMove="true" orderBy="sequence"/>
    	</panel>

		<panel name="actionsPanel" sidebar="true" title="Actions" hideIf="id == null">
			<button name="activateBtn" title="Activate" onClick="action-configurator-creator-group-activate"
				readonlyIf="isActive || !(attributes.length &gt; 0) || (generateProduct &amp;&amp; configuratorProductFormulaList.length == 0) || (generateProduct &amp;&amp; configuratorProductFormulaList.length == 0)"/>
			<button name="testBtn" title="Test this creator" showIf="generateProduct" readonlyIf="!isActive" onClick="action-configurator-creator-view-test"/>
			<field name="generateProduct" widget="boolean-switch"/>
			<field name="isActive" hidden="true"/>
		</panel>

    <panel-related name="configuratorProductFormulaListPanel" field="configuratorProductFormulaList" hideIf="id == null || !generateProduct"
      onChange="action-configurator-creator-record-desactivate,save"
      form-view="configurator-product-formula-form" grid-view="configurator-product-formula-grid"/>

    <panel-related name="configuratorSOLineFormulaListPanel" field="configuratorSOLineFormulaList" hideIf="id == null || generateProduct"
      onChange="action-configurator-creator-record-desactivate,save"
      form-view="configurator-soline-formula-form" grid-view="configurator-soline-formula-grid"/>

		<panel name="authorizationsPanel" title="Authorizations" hideIf="id == null">
			<field name="authorizedUserSet" widget="TagSelect" canEdit="false" canNew="false" form-view="user-form" grid-view="user-grid" colSpan="12"/>
            <field name="authorizedGroupSet" widget="TagSelect" canEdit="false" canNew="false" form-view="group-form" grid-view="group-grid" colSpan="12"/>
		</panel>
		
		<panel name="configuratorBomPanel" title="Configurator BoM" if="__config__.app.isApp('production') &amp;&amp; __config__.app.getApp('production').getEnableConfigurator()" colSpan="12">
            <field name="configuratorBom" showTitle="false" if-module="axelor-production" colSpan="12">
                <editor>
                    <panel>
                        <field name="company"/>
                        <field name="statusSelect" selection-in="[2,3]"/>
                    </panel>
                    <panel name="labelPanel" title="Label">
                        <field name="defNameAsFormula" colSpan="6"/>
                        <field name="name" colSpan="12" showIf="!defNameAsFormula"/>
                        <field name="nameFormula" colSpan="12" height="120"
                               widget="CodeEditor" x-code-syntax="groovy"
                               showTitle="false" showIf="defNameAsFormula"/>
                    </panel>
                    <field name="useCondition" widget="CodeEditor" height="120" colSpan="12"
                           x-code-syntax="groovy"/>
                    <panel name="productPanel" title="Product">
                        <field name="defProductFromConfigurator" colSpan="6" showIf="!defProductAsFormula"/>
                        <field name="defProductAsFormula" colSpan="6" showIf="!defProductFromConfigurator"/>
                        <field name="product" colSpan="12"
                               showIf="!defProductAsFormula &amp;&amp; !defProductFromConfigurator"
                               form-view="product-form" grid-view="product-grid"/>
                        <field name="productFormula" colSpan="12" height="120" widget="CodeEditor"
                               x-code-syntax="groovy" showTitle="false" showIf="defProductAsFormula"/>
                    </panel>
                    <panel name="qtyPanel" title="Qty">
                        <field name="defQtyAsFormula" colSpan="6" type="boolean"/>
                        <field name="qty" colSpan="12" showIf="!defQtyAsFormula"/>
                        <field name="qtyFormula" colSpan="12" height="120" widget="CodeEditor"
                               showTitle="false" x-code-syntax="groovy" showIf="defQtyAsFormula"/>
                    </panel>
                    <panel name="unitPanel" title="Unit">
                        <field name="defUnitAsFormula" colSpan="6" type="boolean"/>
                        <field name="unit" colSpan="12" showIf="!defUnitAsFormula"
                        form-view="unit-form" grid-view="unit-grid"/>
                        <field name="unitFormula" colSpan="12" height="120" widget="CodeEditor"
                               showTitle="false" x-code-syntax="groovy" showIf="defUnitAsFormula"/>
                    </panel>
                    <panel name="productionProcessPanel" title="Production process">
                        <field name="defProdProcessAsFormula" readonlyIf="defProdProcessAsConfigurator" colSpan="6" type="boolean"/>
                        <field name="defProdProcessAsConfigurator" readonlyIf="defProdProcessAsFormula" colSpan="6" type="boolean"/>
                        <field name="prodProcess" form-view="prod-process-form" grid-view="prod-process-form"
                               colSpan="12" showIf="!defProdProcessAsFormula &amp;&amp; !defProdProcessAsConfigurator"/>
                        <field name="prodProcessFormula" colSpan="12" height="120" widget="CodeEditor"
                               showTitle="false" x-code-syntax="groovy" showIf="defProdProcessAsFormula &amp;&amp; !defProdProcessAsConfigurator"/>
                        <field name="configuratorProdProcess" colSpan="12"
                               canNew="true" showIf="defProdProcessAsConfigurator"/>
                    </panel>
                    <field name="configuratorBomList" showTitle="false" form-view="configurator-bom-form" grid-view="configurator-bom-grid" colSpan="12"/>
                </editor>
            </field>
        </panel>
    </form>
    
    <form name="import-configurator-creator-form" title="Import Configurator Creator" model="com.axelor.apps.sale.db.ConfiguratorCreator" >
		<panel name="mainPanel">
			<field name="dataFile" type="many-to-one" target="com.axelor.meta.db.MetaFile" widget="binary-link" title="Data file"/>
			<button name="importCreatorsBtn" title="Import Configurator Creators" onClick="action-configurator-creator-method-import-configurator-creators" showIf="dataFile != null" colSpan="6" />
			<field name="importLog" type="text" colSpan="12" readonly="true" title="Import log"/>
		</panel>	
	</form>

	
	<action-group name="action-configurator-creator-group-create-configurator">
        <action name="save"/>
		<action name="action-configurator-creator-create-configurator"/>
	</action-group>
	
	<action-method name="action-configurator-creator-method-import-configurator-creators">
    	<call class="com.axelor.apps.sale.web.ConfiguratorCreatorController" method="importConfiguratorCreators"/>
    </action-method>
    
    <action-method name="action-configurator-creator-create-configurator">
    	<call class="com.axelor.apps.sale.web.ConfiguratorCreatorController"
			  method="createConfigurator"/>
    </action-method>

	<action-method name="action-configurator-creator-method-configure" model="com.axelor.apps.sale.db.ConfiguratorCreator">
		<call class="com.axelor.apps.sale.web.ConfiguratorCreatorController" method="configure"/>
	</action-method>

    <action-method name="action-configurator-creator-method-update-activate">
        <call class="com.axelor.apps.sale.web.ConfiguratorCreatorController" method="updateAndActivate"/>
    </action-method>

	<action-group name="action-configurator-creator-group-activate">
		<action name="save"/>
		<action name="action-configurator-creator-method-update-activate"/>
	</action-group>

	<action-record name="action-configurator-creator-record-desactivate" model="com.axelor.apps.sale.db.ConfiguratorCreator">
		<field name="isActive" expr="eval: false"/>
	</action-record>
	
	<!-- 19 is the index of the ZoneTime part of the ZonedDateTime __datetime__ and there's no need to display it to the user -->
	<action-export name="action-configurator-creator-export" output="${date}${time}" download="true">
  		<export name="ConfiguratorCreatorExport-${__datetime__.toString().substring(0, 19)}.xml" template="data-export/export-configurator-creator.tmpl" engine="groovy"/>
	</action-export>
	
	<action-view name="action-configurator-creator-view-import" model="com.axelor.apps.sale.db.ConfiguratorCreator" title="Import configurator creators">
		<view name="import-configurator-creator-form" type="form"/>
		<view-param name="popup" value="true"/>
		<view-param name="popup-save" value="false"/>
		<view-param name="show-confirm" value="false"/>
		<view-param name="show-toolbar" value="false"/>
	</action-view>

    <action-view name="action-configurator-creator-view-test" title="Configurator" model="com.axelor.apps.sale.db.Configurator">
        <view type="form" name="configurator-form"/>
        <view type="grid" name="configurator-grid"/>
        <context name="_configuratorCreator" expr="eval: __self__"/>
    </action-view>

</object-views>