<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php init_head(); ?>
<div id="wrapper">
  <div class="content">
    <div class="row">
      <div class="col-md-12">
        <h4 class="tw-font-bold tw-text-xl">
          <?= isset($group_item) ? _l('edit_group_item') : _l('new_group_item'); ?>
        </h4>
      </div>
    </div>
    <div class="panel_s">
        <?php echo form_open(admin_url('group_items/group_item' . (isset($group_item) ? '/' . $group_item->id : ''))); ?>
        <?php echo form_hidden('groupitemid', isset($group_item) ? $group_item->id : ''); ?>
      <div class="panel-body">

        <div class="col-md-12" id="item_group_information"><!-- Campo Nome, Preço e Grupo -->
          <div class="row">
            <div class="col-md-4">
              <?php echo render_input('name', 'group_item_name', isset($group_item) ? $group_item->name : ''); ?>
            </div>
            <div class="col-md-4">
              <?php echo render_input('price', 'price', isset($group_item) ? $group_item->price : ''); ?>
            </div>
            <div class="col-md-4">
              <?php 
                // render_select(name, options, [value_field,label_field], label, selected)
                echo render_select('item_group_id', $item_groups, ['id','name'], 'item_group', isset($group_item) ? $group_item->item_group_id : '');
              ?>
            </div>
          </div>

          <!-- Habilitar Construtor -->
          <div class="row">
            <div class="col-md-12">
              <div class="form-group">
                <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="enable_builder" name="enable_builder" value="1"
                    <?= isset($group_item) && $group_item->enable_builder ? 'checked' : ''; ?>>
                  <label for="enable_builder"><?= _l('enable_builder'); ?></label>
                </div>
              </div>
            </div>
          </div>

          <!-- Descrição -->
          <div class="row">
            <div class="col-md-12">
              <?php echo render_textarea('description', 'description', isset($group_item) ? $group_item->description : ''); ?>
            </div>
          </div>

          <?php
            // Formatar os profissionais com nome e percentual antes de enviar pro render_select
            foreach ($professionals as &$p) {
                $p['display'] = $p['name'] . ' - ' . number_format($p['percentage_material'], 2) . '%';
            }
            unset($p); // boa prática para evitar referência inesperada
        ?>
        
        <?php echo render_select('professional_id', $professionals, ['id', 'display'], _l('select_professional'), isset($group_item) ? $group_item->professional_id : ''); ?>

          <!-- Tipo de Cálculo -->
          <div class="row">
            <div class="col-md-12">
                <h4 class="tw-mb-4"><?= _l('calculation_type'); ?></h4>
                <div class="form-group">
                    <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                        <input type="checkbox" id="has_height" name="has_height"
                            <?= $group_item->has_height ?? false == 1 ? 'checked' : ''; ?> value="1">
                        <label for="has_height">
                            <?= _l('has_height'); ?></label>
                    </div>
                    <div class="checkbox checkbox-primary checkbox-inline">
                        <input type="checkbox" name="has_width" id="has_width"
                            <?= $group_item->has_width ?? false == 1 ? 'checked' : ''; ?> value="1">
                        <label for="has_width">
                            <?= _l('has_width'); ?>
                        </label>
                    </div>
                    <div class="checkbox checkbox-primary checkbox-inline">
                        <input type="checkbox" name="has_length" id="has_length"
                            <?= $group_item->has_length ?? false == 1 ? 'checked' : ''; ?> value="1">
                        <label for="has_length">
                            <?= _l('has_length'); ?>
                        </label>
                    </div>
                </div>
            </div>
          </div>
          <hr>
          <!-- Campos de Outros Dados -->
          <div class="row">
          <h4 class="tw-mb-4"><?= _l('others_details'); ?></h4>
          
          
          
          
          
          
            <!-- Especifico -->
            <div class="col-md-6 row">
                
                
                <div class="col-md-12">
                    <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                        <input type="checkbox" id="others_has_especifico" name="others_has_especifico" value="1"
                            <?= (isset($group_item) && $group_item->others_has_especifico) ? 'checked' : ''; ?>>
                        <label for="others_has_especifico">
                          <?= _l('has_especifico'); ?>
                        </label>
                        
                    </div>
                </div>
                
                <div class="col-md-7">
                    <?php echo render_input('others_multiplier_especifico', 'others_multiplier_especifico', isset($group_item) ? $group_item->others_multiplier_especifico : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->others_has_especifico) ? false : true]); ?>
                </div>
            
            
                <div class="col-md-5">
                    <?php echo render_input('others_multiplier_especifico_value', 'value', isset($group_item) ? $group_item->others_multiplier_especifico_value : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->others_has_especifico) ? false : true]); ?>
                </div>
              
            </div>
            
            <!-- Kit MDF -->
            <div class="col-md-6 row">
                
                <div class="col-md-12">
                  <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="others_has_kit_mdf" name="others_has_kit_mdf" value="1"
                    <?= (isset($group_item) && $group_item->others_has_kit_mdf) ? 'checked' : ''; ?>>
                <label for="others_has_kit_mdf">
                  <?= _l('has_kit_mdf'); ?>
                </label>
              </div>
              </div>
              
                <div class="col-md-7">
              <?php echo render_input('others_multiplier_kit_mdf', 'others_multiplier_kit_mdf', isset($group_item) ? $group_item->others_multiplier_kit_mdf : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->others_has_kit_mdf) ? false : true]); ?>
              </div>
            
            
                <div class="col-md-5">
                    <?php echo render_input('others_multiplier_kit_mdf_value', 'value', isset($group_item) ? $group_item->others_multiplier_kit_mdf_value : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->others_has_kit_mdf) ? false : true]); ?>
                </div>
                
              </div>
              
            <!-- Especifico -->
            <div class="col-md-6 row">
                
                
                <div class="col-md-12">
                    <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                        <input type="checkbox" id="others_has_insumos" name="others_has_insumos" value="1"
                            <?= (isset($group_item) && $group_item->others_has_insumos) ? 'checked' : ''; ?>>
                        <label for="others_has_insumos">
                          <?= _l('has_insumos'); ?>
                        </label>
                        
                    </div>
                </div>
                
                <div class="col-md-7">
                    <?php echo render_input('others_multiplier_insumos', 'others_multiplier_insumos', isset($group_item) ? $group_item->others_multiplier_insumos : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->others_has_insumos) ? false : true]); ?>
                </div>
            
            
                <div class="col-md-5">
                    <?php echo render_input('others_multiplier_insumos_value', 'value', isset($group_item) ? $group_item->others_multiplier_insumos_value : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->others_has_insumos) ? false : true]); ?>
                </div>
              
            </div>
            
            <!-- Kit MDF -->
            <div class="col-md-6 row">
                
                <div class="col-md-12">
                  <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="others_has_embalagens" name="others_has_embalagens" value="1"
                    <?= (isset($group_item) && $group_item->others_has_embalagens) ? 'checked' : ''; ?>>
                <label for="others_has_embalagens">
                  <?= _l('has_embalagens'); ?>
                </label>
              </div>
              </div>
              
                <div class="col-md-7">
              <?php echo render_input('others_multiplier_embalagens', 'others_multiplier_embalagens', isset($group_item) ? $group_item->others_multiplier_embalagens : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->others_embalagens_mdf) ? false : true]); ?>
              </div>
            
            
                <div class="col-md-5">
                    <?php echo render_input('others_multiplier_embalagens_value', 'value', isset($group_item) ? $group_item->others_multiplier_embalagens_value : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->others_embalagens_mdf) ? false : true]); ?>
                </div>
                
              </div>
              
            </div>
        </div>

        <!-- Campos de Cálculo (Externos + Internos) -->
        <div id="builder_fields" style="display:none;">
          <div class="col-md-2">
            <h4 class="tw-mb-4"><?= _l('external_calculation'); ?></h4>
            <!-- Exemplo: Face Externo -->
            <div class="form-group">
              <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                <input type="checkbox" id="external_has_face" name="external_has_face" value="1"
                  <?= (isset($group_item) && $group_item->external_has_face) ? 'checked' : ''; ?>>
                <label for="external_has_face"><?= _l('has_face'); ?></label>
              </div>
              <?php echo render_input('external_multiplier_face', 'external_multiplier_face', isset($group_item) ? $group_item->external_multiplier_face : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->external_has_face) ? false : true]); ?>
            </div>
            
            
            <!-- Fundo Externo -->
              <div class="form-group">
                  <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="external_has_fundo" name="external_has_fundo" value="1"
                    <?= (isset($group_item) && $group_item->external_has_fundo) ? 'checked' : ''; ?>>
                <label for="external_has_fundo">
                  <?= _l('has_fundo'); ?>
                </label>
              </div>
              <?php echo render_input('external_multiplier_fundo', 'external_multiplier_fundo', isset($group_item) ? $group_item->external_multiplier_fundo : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->external_has_fundo) ? false : true]); ?>
              </div>
            <!-- Superior Externo -->
              <div class="form-group">
                  <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="external_has_superior" name="external_has_superior" value="1"
                    <?= (isset($group_item) && $group_item->external_has_superior) ? 'checked' : ''; ?>>
                <label for="external_has_superior">
                  <?= _l('has_superior'); ?>
                </label>
              </div>
              <?php echo render_input('external_multiplier_superior', 'external_multiplier_superior', isset($group_item) ? $group_item->external_multiplier_superior : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->external_has_superior) ? false : true]); ?>
              </div>
            <!-- Inferior Externo -->
              <div class="form-group">
                  <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="external_has_inferior" name="external_has_inferior" value="1"
                    <?= (isset($group_item) && $group_item->external_has_inferior) ? 'checked' : ''; ?>>
                <label for="external_has_inferior">
                  <?= _l('has_inferior'); ?>
                </label>
              </div>
              <?php echo render_input('external_multiplier_inferior', 'external_multiplier_inferior', isset($group_item) ? $group_item->external_multiplier_inferior : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->external_has_inferior) ? false : true]); ?>
              </div>
            <!-- Prateleira Externo -->
              <div class="form-group">
                  <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="external_has_prateleira" name="external_has_prateleira" value="1"
                    <?= (isset($group_item) && $group_item->external_has_prateleira) ? 'checked' : ''; ?>>
                <label for="external_has_prateleira">
                  <?= _l('has_prateleira'); ?>
                </label>
              </div>
              <?php echo render_input('external_multiplier_prateleira', 'external_multiplier_prateleira', isset($group_item) ? $group_item->external_multiplier_prateleira : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->external_has_prateleira) ? false : true]); ?>
              </div>
            <!-- Lateral Externo -->
              <div class="form-group">
                  <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="external_has_lateral" name="external_has_lateral" value="1"
                    <?= (isset($group_item) && $group_item->external_has_lateral) ? 'checked' : ''; ?>>
                <label for="external_has_lateral">
                  <?= _l('has_laterais'); ?>
                </label>
              </div>
              <?php echo render_input('external_multiplier_lateral', 'external_multiplier_lateral', isset($group_item) ? $group_item->external_multiplier_lateral : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->external_has_lateral) ? false : true]); ?>
              </div>
            <!-- Fitas Externo -->
              <div class="form-group">
                  <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="external_has_fitas" name="external_has_fitas" value="1"
                    <?= (isset($group_item) && $group_item->external_has_fitas) ? 'checked' : ''; ?>>
                <label for="external_has_fitas">
                  <?= _l('has_fitas'); ?>
                </label>
              </div>
              <?php echo render_input('external_multiplier_fitas', 'external_multiplier_fitas', isset($group_item) ? $group_item->external_multiplier_fitas : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->external_has_fitas) ? false : true]); ?>
              </div>
              
          </div>

          <div class="col-md-2">
            <h4 class="tw-mb-4"><?= _l('internal_calculation'); ?></h4>
            <div class="form-group">
              <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                <input type="checkbox" id="internal_has_fundo" name="internal_has_fundo" value="1"
                  <?= (isset($group_item) && $group_item->internal_has_fundo) ? 'checked' : ''; ?>>
                <label for="internal_has_fundo"><?= _l('has_fundo'); ?></label>
              </div>
              <?php echo render_input('internal_multiplier_fundo', 'internal_multiplier_fundo', isset($group_item) ? $group_item->internal_multiplier_fundo : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->internal_has_fundo) ? false : true]); ?>
            </div>

            <!-- Superior Interno -->
              <div class="form-group">
                  <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="internal_has_superior" name="internal_has_superior" value="1"
                    <?= (isset($group_item) && $group_item->internal_has_superior) ? 'checked' : ''; ?>>
                <label for="internal_has_superior">
                  <?= _l('has_superior'); ?>
                </label>
              </div>
                <?php echo render_input('internal_multiplier_superior', 'internal_multiplier_superior', isset($group_item) ? $group_item->internal_multiplier_superior : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->internal_has_superior) ? false : true]); ?>
              </div>
            
            <!-- Inferior Interno -->
              <div class="form-group">
                  <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="internal_has_inferior" name="internal_has_inferior" value="1"
                    <?= (isset($group_item) && $group_item->internal_has_inferior) ? 'checked' : ''; ?>>
                <label for="internal_has_inferior">
                  <?= _l('has_inferior'); ?>
                </label>
              </div>
              <?php echo render_input('internal_multiplier_inferior', 'internal_multiplier_inferior', isset($group_item) ? $group_item->internal_multiplier_inferior : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->internal_has_inferior) ? false : true]); ?>
              </div>

            <!-- Prateleira Interna -->
              <div class="form-group">
                  <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="internal_has_prateleira" name="internal_has_prateleira" value="1"
                    <?= (isset($group_item) && $group_item->internal_has_prateleira) ? 'checked' : ''; ?>>
                <label for="internal_has_prateleira">
                  <?= _l('has_prateleira'); ?>
                </label>
              </div>
              <?php echo render_input('internal_multiplier_prateleira', 'internal_multiplier_prateleira', isset($group_item) ? $group_item->internal_multiplier_prateleira : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->internal_has_prateleira) ? false : true]); ?>
              </div>

            <!-- Laterais Internas -->
              <div class="form-group">
                  <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="internal_has_laterais" name="internal_has_laterais" value="1"
                    <?= (isset($group_item) && $group_item->internal_has_laterais) ? 'checked' : ''; ?>>
                <label for="internal_has_laterais">
                  <?= _l('has_laterais'); ?>
                </label>
              </div>
              <?php echo render_input('internal_multiplier_laterais', 'internal_multiplier_laterais', isset($group_item) ? $group_item->internal_multiplier_laterais : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->internal_has_laterais) ? false : true]); ?>
              </div>

            <!-- Fitas Internas -->
              <div class="form-group">
                  <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="internal_has_fitas" name="internal_has_fitas" value="1"
                    <?= (isset($group_item) && $group_item->internal_has_fitas) ? 'checked' : ''; ?>>
                <label for="internal_has_fitas">
                  <?= _l('has_fitas'); ?>
                </label>
              </div>
              <?php echo render_input('internal_multiplier_fitas', 'internal_multiplier_fitas', isset($group_item) ? $group_item->internal_multiplier_fitas : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->internal_has_fitas) ? false : true]); ?>
              </div>

            <!-- Fitas-Face Internas -->
              <div class="form-group">
                  <div class="checkbox checkbox-primary no-mtop checkbox-inline">
                  <input type="checkbox" id="internal_has_fitas_face" name="internal_has_fitas_face" value="1"
                    <?= (isset($group_item) && $group_item->internal_has_fitas_face) ? 'checked' : ''; ?>>
                <label for="internal_has_fitas_face">
                  <?= _l('has_fitas_face'); ?>
                </label>
              </div>
              <?php echo render_input('internal_multiplier_fitas_face', 'internal_multiplier_fitas_face', isset($group_item) ? $group_item->internal_multiplier_fitas_face : '', 'number', ['step' => '0.01', 'disabled' => (isset($group_item) && $group_item->internal_has_fitas_face) ? false : true]); ?>
              </div>
              
          </div>
        </div>

      </div>

        <div class="panel-footer text-right tw-space-x-2">
          <button type="submit" class="btn btn-primary only-save customer-form-submiter">
            <?= _l('submit'); ?>
          </button>
        </div>
        <?php echo form_close(); ?>
    </div>
  </div>
</div>
<?php init_tail(); ?>
<script>
// Quando a página carregar, associar evento para habilitar/desabilitar os inputs de multiplicador
$(document).ready(function() {
    // Toggle Builder Fields
    function toggleBuilder() {
      // checkbox
      const enabled = document.getElementById('enable_builder').checked;
    
      // mostra/oculta os campos do builder
      document.getElementById('builder_fields').style.display = enabled ? '' : 'none';
    
      // troca colunas em item_group_information
      const info = document.getElementById('item_group_information');
      if (enabled) {
        info.classList.remove('col-md-12');
        info.classList.add('col-md-8');
      } else {
        info.classList.remove('col-md-8');
        info.classList.add('col-md-12');
      }
    }
    
    // associa o evento e dispara ao carregar
    document
      .getElementById('enable_builder')
      .addEventListener('change', toggleBuilder);
    toggleBuilder();

    // Função para associar checkbox a input correspondente
    function toggleMultiplier(checkboxId, inputName, inputValue = null) {
        $(checkboxId).on('change', function() {
            var isChecked = $(this).is(':checked');
            $('input[name="' + inputName + '"]').prop('disabled', !isChecked);
            if (inputValue !== '') {
                $('input[name="' + inputValue + '"]').prop('disabled', !isChecked);
            }
        });
    }

    // Interno
    toggleMultiplier('#internal_has_fundo', 'internal_multiplier_fundo');
    toggleMultiplier('#internal_has_superior', 'internal_multiplier_superior');
    toggleMultiplier('#internal_has_inferior', 'internal_multiplier_inferior');
    toggleMultiplier('#internal_has_prateleira', 'internal_multiplier_prateleira');
    toggleMultiplier('#internal_has_laterais', 'internal_multiplier_laterais');
    toggleMultiplier('#internal_has_fitas', 'internal_multiplier_fitas');
    toggleMultiplier('#internal_has_fitas_face', 'internal_multiplier_fitas_face');

    // Externo
    toggleMultiplier('#external_has_face', 'external_multiplier_face');
    toggleMultiplier('#external_has_fundo', 'external_multiplier_fundo');
    toggleMultiplier('#external_has_superior', 'external_multiplier_superior');
    toggleMultiplier('#external_has_inferior', 'external_multiplier_inferior');
    toggleMultiplier('#external_has_prateleira', 'external_multiplier_prateleira');
    toggleMultiplier('#external_has_lateral', 'external_multiplier_lateral');
    toggleMultiplier('#external_has_fitas', 'external_multiplier_fitas');

    // Outros
    toggleMultiplier('#others_has_especifico', 'others_multiplier_especifico', 'others_multiplier_especifico_value');
    toggleMultiplier('#others_has_kit_mdf', 'others_multiplier_kit_mdf', 'others_multiplier_kit_mdf_value');
    toggleMultiplier('#others_has_insumos', 'others_multiplier_insumos', 'others_multiplier_insumos_value');
    toggleMultiplier('#others_has_embalagens', 'others_multiplier_embalagens', 'others_multiplier_embalagens_value');
});
</script>
</body>
</html>
