<?xml version="1.0" encoding="UTF-8"?>
<panel-form xmlns="http://www.davinti.com.br/vitruvio/form/mobile/panel" 
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://www.davinti.com.br/vitruvio/form/mobile/panel vitruvio-mobile-panel-form.xsd">

	<form formKey="mobile2">
		<name>Geral X Dia</name>
		
		<ServerSide>
			<DataSources>
				<QueryDataSource key="veiculo" autoSyncOnInit="false" refreshInSeconds="3600" autoSyncOnDiscovery="true" />
            <QueryDataSource key="clientes" autoSyncOnInit="false" refreshInSeconds="3600" autoSyncOnDiscovery="true" />
			</DataSources>
		</ServerSide>

		<initScript language="JavaScript">
			<![CDATA[
				function run() {
					engine.getWidgetController('btnFilter').getButton().setIcon("FILTRAR");
					engine.getWidgetController('btnApply').getButton().setIcon("APLICAR");
					var hoje = moment().format();
					var primeiro = moment().startOf("month");
					engine.getField("f_data").setValue(primeiro);
					engine.getField("f_data2").setValue(hoje);
               engine.getField("frotaTerceiro").setValue('A');
               engine.getField("caminhaoCarreta").setValue('A');
				}			
			]]>
		</initScript>

		<components>
			<VerticalLayout>

				<ButtonWidget id="btnFilter" caption="">
					<onClickScript language="JavaScript">
						<![CDATA[
							function run() {
								engine.getWidgetController('btnFilter').getButton().setVisible(false);
								engine.getField("f_data").setVisible(true);
								engine.getField("f_data2").setVisible(true);
								engine.getField("placa").setVisible(true);
                        engine.getField("cliente").setVisible(true);
                        engine.getField("frotaTerceiro").setVisible(true);
                        engine.getField("caminhaoCarreta").setVisible(true);
								engine.getWidgetController('btnApply').getButton().setVisible(true);
							}
						]]>
					</onClickScript>
				</ButtonWidget>

				<DateField id="f_data" caption="Data início" type="date" visible="false" required="false" format="dd/MM/yyyy" resolution="DAY"/>
				<DateField id="f_data2" caption="Data final" type="date" visible="false" required="false" format="dd/MM/yyyy" resolution="DAY"/>
				<DBComboBox type="string" id="placa" caption="Placa veículo" visible="false" >
					<datasource>
						<query ref="veiculo" />
					</datasource>
					<key-field>CODIGO</key-field>
					<caption-field>PLACA</caption-field>
				</DBComboBox>
            <DBTwinColSelect id="cliente" caption="Cliente" type="string" required="true" setProcessTag="true" enabled="true" visible="false" >
               <datasource>
                  <query ref="clientes" />
               </datasource>
               <key-field>CODIGO</key-field>
               <caption-field>NOME</caption-field>
            </DBTwinColSelect> 
				<OptionGroup width="100%" type="string" id="frotaTerceiro" visible="false" disposition="vertical" caption="Selecione o tipo de frete" >
               <entry value="Ambos" key="A" />
               <entry value="Frota" key="F" />
               <entry value="Terceiro" key="T" />
            </OptionGroup>
            <OptionGroup width="100%" type="string" id="caminhaoCarreta" visible="false" disposition="vertical" caption="Selecione o tipo de transporte" >
               <entry value="Todos" key="A" />
               <entry value="Caminhão" key="C" />
               <entry value="Carreta" key="T" />
               <entry value="Outros" key="O"/>
            </OptionGroup>
				<ButtonWidget id="btnApply" visible="false" caption=" Aplicar">
					<onClickScript language="JavaScript">
						<![CDATA[
							function run() {

								engine.getWidgetController('btnApply').getButton().setVisible(false);
								engine.getField("f_data").setVisible(false);
								engine.getField("f_data2").setVisible(false);
								engine.getField("placa").setVisible(false);
                        engine.getField("cliente").setVisible(false);
                        engine.getField("frotaTerceiro").setVisible(false);
                        engine.getField("caminhaoCarreta").setVisible(false);
								
								engine.getWidgetController('btnFilter').getButton().setVisible(true);								

								var data = moment(engine.getField('f_data').getValue()).valueOf();
								var data2 = moment(engine.getField('f_data2').getValue()).valueOf();
								var placa = engine.getField('placa').getValue();
                        var clientes;
                             
								engine.getDesktopPanel("listaGraficoSemana").addFieldId("dataInicio", data);
								engine.getDesktopPanel("listaGraficoSemana").addFieldId("dataFinal", data2);
								if (engine.getField('placa').getValue()) {
									engine.getDesktopPanel("listaGraficoSemana").addFieldId("placa", placa);
								}
                        
                        if (engine.getField('cliente').getValue()) {
                           clientes = engine.getField('cliente').getValue();
                           console.log("clientes: "+clientes);
                           engine.getDesktopPanel("listaGraficoSemana").addFieldId("clientes", clientes);
                        }
                        
                        engine.getDesktopPanel("listaGraficoSemana").addFieldId("ftFrotaTerceiro", engine.getField('frotaTerceiro').getValue());
                        engine.getDesktopPanel("listaGraficoSemana").addFieldId("ftCaminhaoCarreta", engine.getField('caminhaoCarreta').getValue());
								engine.getDesktopPanel("listaGraficoSemana").reload();
							}
						]]>
					</onClickScript>
				</ButtonWidget>
				<DesktopPanel  height="400px" id="listaGraficoSemana" panelKey="geralplaca" layoutId="pnlGeral" forceFieldsRender="dataInicio, dataFinal, placa, clientes, ftFrotaTerceiro, ftCaminhaoCarreta" />
			</VerticalLayout>
		</components>
	</form>
	
</panel-form>
