<?php (defined('BASEPATH')) OR exit('No direct script access allowed'); ?>
<link href="<?= $assets ?>plugins/bootstrap-toggle/bootstrap-toggle.min.css" rel="stylesheet">
<script src="<?= $assets ?>plugins/bootstrap-toggle/bootstrap-toggle.min.js"></script>

<!--Estilos CSS de imagen-->
<style>
    .slow .toggle-group {
        transition: left 0.7s;
        -webkit-transition: left 0.7s;
    }
    #preview img {
        width: 34rem;
        height: 21rem;
    }
    
    .nav-tabs{
        background-color:#1b609c;
    }
    .nav-tabs > li > a{
        color:black;
        background-color: white;
    }
    .nav-tabs > li > a:hover{
        border: medium none;
        border-radius: 0;
    }
    .nav-tabs>li.active>a, .nav-tabs>li.active>a:focus, .nav-tabs>li.active>a:hover{
        background-color: #1b609c !important;
        border: medium none;
        border-radius: 0;
        color:#fff;
    }
    
</style>

<div id="loader" style="display:none;"></div>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <ul class="nav nav-tabs nav-justified">          
                <li class="active"><a href="#tabgeneral" data-toggle="tab">General</a></li>
                
            </ul>
            <div class="tab-content">
                <div class="tab-pane fade in active" id="tabgeneral">
                    <div class="box" style="border-top: 2.5px solid #9fc10b;">
                        <div class="box-header">
                            <!-- <h3 class="box-title"><?= lang('enter_info'); ?></h3> -->
                        </div>
                        <div class="box-body">
                            <div class="col-lg-12">
                                <?= form_open_multipart("products/add", 'class="validation"');?>
                                <div class="row">
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <?= lang('type', 'type'); ?>
                                            <?php $opts = array('standard' => lang('standard'), 'combo' => lang('combo'), 'service' => lang('service')); ?>
                                            <?= form_dropdown('type', $opts, set_value('type', (isset($form_data['type'])?$form_data['type']:'standard')), 'class="form-control tip select2" id="type"  required="required"  style="width:100%;" '); ?>
                                            
                                        </div>
                                    </div>
                                    <div class="col-md-3 categoria">
                                        <div class="form-group">
                                            <?= lang('category', 'category'); ?>
                                            <?php
                                            
                                            $categories_list= array();
                                            $categories_list[""]= lang('select_categorie', 'select_categorie');
        
                                            foreach($categories as $item) {
                                                $categories_list[$item['id']] = $item['name'];
                                            }
                                                                                       
                                            ?>
                                            <?= form_dropdown('category', $categories_list, set_value('category', (isset($form_data['category'])?$form_data['category']:'0')), 'class="form-control select2" id="categories" style="width:100%;"'); ?>
                                            
                                        </div>
                                        
                                    </div>

                                         

                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <?= lang("barcode_symbology", "barcode_symbology") ?>
                                            <?php
                                            $bs = array('code25' => 'Code25', 'code39' => 'Code39', 'code128' => 'Code128', 'ean8' => 'EAN8', 'ean13' => 'EAN13', 'upca ' => 'UPC-A', 'upce' => 'UPC-E');
                                            echo form_dropdown('barcode_symbology', $bs,set_value('barcode_symbology',(isset($form_data['barcode_symbology'])?$form_data['barcode_symbology']:'code39')), 'class="form-control select2" id="barcode_symbology" required="required" style="width:100%;"');
                                            ?>
                                        </div>
                                    </div>
                                </div>
                                <div class="row">   
                                    <div class="col-md-3">
                                        <div class="form-group">
                                        <?= lang('code', 'code'); ?> 
                                            <?= form_input('code', set_value('code',(isset($form_data['code'])?$form_data['code']:'')), 'class="form-control tip" id="code" placeholder="Código Puede utilizar el código de barras" required="required" maxlength="20"'); ?>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                        <?= lang('name', 'name'); ?>
                                            <?= form_input('name', set_value('name',(isset($form_data['name'])?$form_data['name']:'')), 'class="form-control tip" id="name" placeholder="Nombre"  required="required" maxlength="80"'); ?>
                                        </div>
                                    </div>
                                   

                                    <div class="col-md-3">
                                        <div class="form-group">
                                        <?= lang('short_name', 'short_name'); ?>
                                            <?= form_input('short_name', set_value('short_name',(isset($form_data['short_name'])?$form_data['short_name']:'')), 'class="form-control" placeholder="Nombre Corto" id="short_name" required="required" maxlength="40"'); ?>
                                        </div>
                                    </div>
                                    </div>
                                <div class="row"> 
                                    <div class="col-md-12">
                                        <div id="ct" style="display:none;">
                                            <div class="panel panel-default">
                                                <div class="panel-body">
                                                    <div class="form-group">
                                                        <?= lang("add_product", "add_item"); ?>
                                                        <?php echo form_input('add_item', '', 'class="form-control ttip" id="add_item" data-placement="top" data-trigger="focus" data-bv-notEmpty-message="' . lang('please_add_items_below') . '" placeholder="' . $this->lang->line("add_item") . '" maxlength="150"'); ?>
                                                    </div>
                                                    <div class="control-group table-group">
                                                        <label class="table-label" for="combo"><?= lang("combo_products"); ?></label>

                                                        <div class="controls table-controls">
                                                            <table id="prTable"
                                                                class="table items table-striped table-bordered table-condensed table-hover">
                                                                <thead>
                                                                <tr>
                                                                    <th class="col-xs-9"><?= lang("product_name") . " (" . $this->lang->line("product_code") . ")"; ?></th>
                                                                    <th class="col-xs-2"><?= lang("quantity"); ?></th>
                                                                    <th class=" col-xs-1 text-center"><i class="fa fa-trash-o trash-opacity-50"></i></th>
                                                                </tr>
                                                                </thead>
                                                                <tbody></tbody>
                                                            </table>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    </div>
                                <div class="row">        
                                    <div class="col-md-3 brand">
                                        <div class="form-group">
                                            <?= lang('brand', 'brand'); ?>
                                            <?php
                                            $brand_list = array();
                                            $brand_list[0] = lang('select_brand', 'select_brand');
                                            foreach($brands as $item) {
                                                $brand_list[$item['id']] = $item['brand'];
                                            }
                                            ?>
                                            <?= form_dropdown('brand', $brand_list, set_value('brand',(isset($form_data['brand'])?$form_data['brand']:'0')), 'class="form-control select2" id="brand" style="width:100%;"'); ?>
                                        </div>
                                    </div>
                                    <div class="col-md-3 model">
                                        <div class="form-group">
                                            <?= lang('model', 'model'); ?>
                                            <?php
                                            $model_list = array();
                                            $model_list[0] = lang('select_model', 'select_model');
                                            ?>
                                            <?= form_dropdown('model', $model_list, '0', 'class="form-control select2" id="model" style="width:100%;"'); ?>
                                        </div>
                                    </div>
                                      <div class="col-md-3 maker">
                                        <div class="form-group">

                                            <?= lang('maker', 'maker'); ?>
                                            <?php
                                            
                                            $maker_list=array();
                                            $maker_list[0]= lang('select_maker', 'select_maker');

                                            foreach($makers as $item) {
                                                $maker_list[$item['id']] = $item['maker'];
                                            }

                                            ?>

                                            <?= form_dropdown('maker', $maker_list, set_value('maker',(isset($form_data['maker'])?$form_data['maker']:'0')), 'class="form-control select2" id="maker" style="width:100%;"'); ?>
                
                                        </div>
                                    </div>
                                    <div class="col-md-3 medida">
                                        <div class="form-group">
                                            <?= lang('unit_measure', 'unit_measure'); ?>
                                            <?php
                                            $unit_measure_list = array();
                                            $unit_measure_list[0] = lang('select_unit_measure', 'select_unit_measure');
                                            foreach($unit_measure as $item) {
                                                $unit_measure_list[$item['id']] = $item['description'];
                                            }
                                            ?>
                                            <?= form_dropdown('unit_measure', $unit_measure_list, set_value('unit_measure',(isset($form_data['unit_measure'])?$form_data['unit_measure']:'59')), 'class="form-control select2" id="unit_measure" required="required" style="width:100%;"'); ?>

                                        </div>
                                    </div>
                                </div>
                                <div class="row"> 
                                    <div class="col-md-2 cantida">
                                        <div class="form-group" id="st">
                                            <?= lang('alert_quantity', 'alert_quantity'); ?>
                                            <?= form_input('alert_quantity', set_value('alert_quantity',(isset($form_data['alert_quantity'])?$form_data['alert_quantity']:0)), 'class="form-control tip" id="alert_quantity"  name="numero" onkeypress="return solonumeros(event)"  required="required" maxlength="7" max="1000000"');  ?>
                                        </div>
                                    </div>
                                    <div class="col-md-6 col-md-offset-1">
                                        <div class="form-group">
                                            <?= lang('', '&nbsp;'); ?>
                                            <div class="form-group">
                                                <div class="col-md-6 serie_lote">
                                                    <div class="form-group">
                                                        <?= lang('requires_lot_number_texto', 'requires_lot_number_texto'); ?>
                                                        &nbsp;
                                                        <!--< ?= form_checkbox('requires_lot_number', '0', FALSE, 'id="requires_lot_number"'); ? >-->
                                                        <input type="checkbox" id="requires_lot_number_checkbox" name="requires_lot_number_checkbox" data-offstyle="danger"
                                                            data-style="slow" data-onstyle="success" data-toggle="toggle" data-on="Si" data-off="No" data-size="mini" <?=(isset($form_data['requires_lot_number'])?(($form_data['requires_lot_number']==1)?'checked':''):'') ?> />
                                                        <input type="hidden" name="requires_lot_number" id="requires_lot_number" value="<?=set_value('requires_lot_number',(isset($form_data['requires_lot_number'])?$form_data['requires_lot_number']:0))?>">
                                                    </div>
                                                </div>
                                                <div class="col-md-6 serie">
                                                    <div class="form-group">
                                                        <?= lang('requires_serial_number', 'requires_serial_number'); ?>
                                                        &nbsp;
                                                        <!--< ?= form_checkbox('requires_serial_number', '0', FALSE, 'id="requires_serial_number"'); ? >-->
                                                        <input type="checkbox" id="requires_serial_number_checkbox" name="requires_serial_number_checkbox" data-offstyle="danger"
                                                            data-style="slow" data-onstyle="success" data-toggle="toggle" data-on="Si" data-off="No" data-size="mini" <?=(isset($form_data['requires_serial_number'])?(($form_data['requires_serial_number']==1)?'checked':''):'') ?> >
                                                        <input type="hidden" name="requires_serial_number" id="requires_serial_number" value="<?=set_value('requires_serial_number',(isset($form_data['requires_serial_number'])?$form_data['requires_serial_number']:0))?>">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-3 presentacion">
                                        <div class="form-group">
                                            <?= lang('presentation', 'presentation'); ?>
                                            <?= form_input('presentation', set_value('presentation',(isset($form_data['presentation'])?$form_data['presentation']:"")), 'class="form-control tip" id="presentation" maxlength="30"'); ?>
                                        </div>
                                    </div>
                                </div>
                                <div class="row"> 
                                    <div class="col-md-8">
                                        <div class="row">
                                            <!--<div class="col-md-12">
                                                <div class="form-group" id="st">
                                                    < ?= lang('alert_quantity', 'alert_quantity'); ? >
                                                    < ?= form_input('alert_quantity', set_value('alert_quantity', 0), 'class="form-control tip" id="alert_quantity"  required="required"'); ? >
                                                </div>
                                            </div>-->

                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <?= "<label>Tipo impuesto</label>"; ?>
                                                    <?php $tm = array(0 => lang('inclusive'), 1 => lang('exclusive'), 2 => lang('exonerated')); ?>
                                                    <?= form_dropdown('tax_method', $tm, set_value('tax_method',(isset($form_data['tax_method'])?$form_data['tax_method']:"")), 'class="form-control tip select2" id="tax_method"  required="required" style="width:100%;"'); ?>
                                                </div>
                                            </div>
                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <?= lang('price', 'price'); ?>
                                                    <?= form_input('price', set_value('price', (isset($form_data['price'])?$form_data['price']:0)), 'class="form-control tip" id="price"  name="numero" onkeypress="return solonumeros(event)"  required="required" maxlength="11"'); ?>
                                                </div>
                                            </div>
                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <?= lang('currency_name', 'currency_name'); ?>
                                                    <?php $cy = array('PEN' => lang('currency_pen'), 'USD' => lang('currency_usd')); ?>
                                                    <?= form_dropdown('currency', $cy, set_value('currency', $this->Settings->currency_prefix), 'class="form-control tip select2" id="currency"  required="required" style="width:100%;"'); ?>
                                                </div>
                                            </div>
                                            <div class="col-md-3 costo">
                                                <div class="form-group">
                                                    <?= "<label>Costo ref.</label>"; ?>
                                                    <!--< ?= lang('cost', 'cost'); ? >-->
                                                    <?= form_input('cost', set_value('cost', (isset($form_data['cost'])?$form_data['cost']:0)), 'class="form-control" id="cost"  name="numero" onkeypress="return solonumeros(event)" required="required" maxlength="11"'); ?>
                                                </div>
                                            </div>
                                            
                                            <div class="col-md-12">
                                                <div class="form-group">
                                                    
                                                    <?= form_textarea('details', set_value('details',(isset($form_data['details'])?$form_data['details']:"")), 'class="form-control tip redactor" placeholder="Detalles" id="details" maxlength="255"'); ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <?= lang('image', 'image'); ?>
                                            <input type="file" name="userfile" id="image">
                                            <small>*Ingrese imagen con medidas minimas de 800 x 800 pixeles con formato jpg/png.</small>
                                            
                                        </div>
                                        <center><div id="preview"></div></center>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                        <?php switch($this->session->userdata('negocio')){
                                            case 0: ?>
                                                <?php break;
                                            case 1: ?>
                                                <div class="form-group" id="st">
                                                    <?= lang('active_principle', 'active_principle'); ?>
                                                    <?= form_input('custom_field_1', set_value('custom_field_1', ""), 'class="form-control tip" id="custom_field_1"'); ?>
                                                </div>
                                                <div class="form-group" id="st">
                                                    <?= lang('pharmacological_action', 'pharmacological_action'); ?>
                                                    <?= form_input('custom_field_2', set_value('custom_field_2', ""), 'class="form-control tip" id="custom_field_2"'); ?>
                                                </div>
                                                <?php break;
                                        } ?>
                                    </div>
                                </div>

                                <div class="row">

                                </div>
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="col-md-12 text-right">
                                            <?php echo form_submit('ad_product', $this->lang->line("accept"), 'class="btn btn-primary" id="btn-accept"'); ?>
                                            <a id="btn-cancel" class="btn btn-danger"><?= lang("cancel") ?></a>
                                        </div>
                                    </div>
                                </div>
                               
                                <?= form_close();?>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                </div>
                <div class="tab-pane fade in" id="tabfe">
                    <div class="box" style="border-top: 2.5px solid #9fc10b;">
                        <div class="box-header">
                            <!-- <h3 class="box-title"><?= lang('enter_info'); ?></h3> -->
                        </div>
                        <div class="box-body">
                            
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<script src="<?= $assets ?>dist/js/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
    var price = 0; cost = 0; items = {};

    $(document).ready(function() {
        /*$("#requires_lot_number").click(function () {
            if ($(this).is(':checked')) {
                $("#requires_lot_number").val('1');
            } else {
                $("#requires_lot_number").val('0');
            }
        });

        $("#requires_serial_number").click(function () {
            if ($(this).is(':checked')) {
                $("#requires_serial_number").val('1');
            } else {
                $("#requires_serial_number").val('0');
            }
        });*/

        $('#brand').change(function(e) {
            var model = $("#model");
            var brand = $(this).val();
            var url = '<?= site_url('products/get_models'); ?>/' + brand;

            $.ajax({
                type: "GET",
                dataType: "json",
                url: url,
                success: function(data){
                    model.empty();
                    model.append($("<option />").val(0).text('<?= lang('select_model'); ?>'));
                    model.val(0);

                    if (data.length > 0)  {
                        data.forEach((item) => {
                            model.append($("<option />").val(item.id).text(item.model));
                        });
                    }
                }
            });  
        });

        $('#type').change(function(e) {
            var type = $(this).val();
            if(type == 'combo') {
                // $('#st').slideUp();
                $('#ct').slideDown();
                //$('#cost').attr('readonly', true);
            } else if(type == 'service') {
                
                // $('#st').slideUp();
                $('#ct').slideUp();
                //$('#cost').attr('readonly', false);
            } else {
                $('#ct').slideUp();
                // $('#st').slideDown();
                //$('#cost').attr('readonly', false);
            }
        });

        $("#add_item").autocomplete({
            source: '<?= site_url('products/suggestions'); ?>',
            minLength: 1,
            autoFocus: false,
            delay: 200,
            response: function (event, ui) {
                if ($(this).val().length >= 16 && ui.content[0].id == 0) {
                    bootbox.alert('<?= lang('no_product_found') ?>', function () {
                        $('#add_item').focus();
                    });
                    $(this).val('');
                }
                else if (ui.content.length == 1 && ui.content[0].id != 0) {
                    ui.item = ui.content[0];
                    $(this).data('ui-autocomplete')._trigger('select', 'autocompleteselect', ui);
                    $(this).autocomplete('close');
                    $(this).removeClass('ui-autocomplete-loading');
                }
                else if (ui.content.length == 1 && ui.content[0].id == 0) {
                    bootbox.alert('<?= lang('no_product_found') ?>', function () {
                        $('#add_item').focus();
                    });
                    $(this).val('');

                }
            },
            select: function (event, ui) {
                event.preventDefault();
                if (ui.item.id !== 0) {
                    var row = add_product_item(ui.item);
                    if (row) {
                        $(this).val('');
                    }
                } else {
                    bootbox.alert('<?= lang('no_product_found') ?>');
                }
            }
        });
        $('#add_item').bind('keypress', function (e) {
            if (e.keyCode == 13) {
                e.preventDefault();
                $(this).autocomplete("search");
            }
        });

        $(document).on('click', '.del', function () {
            var id = $(this).attr('id');
            delete items[id];
            $(this).closest('#row_' + id).remove();
        });
        


        $(document).on('change', '.rqty', function () {
            var item_id = $(this).attr('data-item');
            items[item_id].row.qty = (parseFloat($(this).val())).toFixed(2);
            add_product_item(null, 1);
        });

        $(document).on('change', '.rprice', function () {
            var item_id = $(this).attr('data-item');
            items[item_id].row.price = (parseFloat($(this).val())).toFixed(2);
            add_product_item(null, 1);
        });

        function add_product_item(item, noitem) {
            if (item == null && noitem == null) {
                return false;
            }
            if(noitem != 1) {
                item_id = item.row.id;
                if (items[item_id]) {
                    items[item_id].row.qty = (parseFloat(items[item_id].row.qty) + 1).toFixed(2);
                } else {
                    items[item_id] = item;
                }
            }
            price = 0;
            cost = 0;

            $("#prTable tbody").empty();
            $.each(items, function () {
                var item = this.row;
                var row_no = item.id;
                var newTr = $('<tr id="row_' + row_no + '" class="item_' + item.id + '"></tr>');
                tr_html = '<td><input name="combo_item_id[]" type="hidden" value="' + item.id + '"><input name="combo_item_code[]" type="hidden" value="' + item.code + '"><input name="combo_item_name[]" type="hidden" value="' + item.name + '"><input name="combo_item_cost[]" type="hidden" value="' + item.cost + '"><span id="name_' + row_no + '">' + item.name + ' (' + item.code + ')</span></td>';
                tr_html += '<td><input class="form-control text-center rqty" name="combo_item_quantity[]" type="text" value="' + formatDecimal(item.qty) + '" data-id="' + row_no + '" data-item="' + item.id + '" id="quantity_' + row_no + '" onClick="this.select();"></td>';
                //tr_html += '<td><input class="form-control text-center rprice" name="combo_item_price[]" type="text" value="' + formatDecimal(item.price) + '" data-id="' + row_no + '" data-item="' + item.id + '" id="combo_item_price_' + row_no + '" onClick="this.select();"></td>';
                tr_html += '<td class="text-center"><i class="fa fa-times tip del" id="' + row_no + '" title="Remove" style="cursor:pointer;"></i></td>';
                newTr.html(tr_html);
                newTr.prependTo("#prTable");
                //price += formatDecimal(item.price*item.qty);
                cost += formatDecimal(item.cost*item.qty);
            });
            $('#cost').val(cost);
            return true;

        }
        var type = $('#type').val();
        if(type == 'combo') {
            $('#st').slideUp();
            $('#ct').slideDown();
            //$('#cost').attr('readonly', true);
        } else if(type == 'service') {
            
            $('.categoria').slideUp();
            $('.brand').slideUp();
            $('.model').slideUp();
            $('.maker').slideUp();
            $('.medida').slideUp();
            $('.cantida').slideUp();
            $('.serie_lote').slideUp();
            $('.serie').slideUp();
            $('.presentacion').slideUp();
            $('.costo').slideUp();

        }
        <?php 
    if (isset($form_data['type'])) {
        if ($form_data['type'] == 'combo') {
            $c = sizeof($form_data['combo_item_code']);
            $items = array();
            for ($r = 0; $r < $c; $r++) {
                if (isset($form_data['combo_item_code'][$r]) && isset($form_data['combo_item_quantity'][$r])) {
                    $items[] = array(
                        'id' => $form_data['combo_item_id'][$r],
                        'row' => array(
                            'id' => $form_data['combo_item_id'][$r],
                            'name' => $form_data['combo_item_name'][$r],
                            'code' => $form_data['combo_item_code'][$r],
                            'qty' => $form_data['combo_item_quantity'][$r],
                            'cost' => $form_data['combo_item_cost'][$r]
                        )
                    );
                }
            }
            echo 'var ci = ' . json_encode($items) . ';';
            echo '$.each(ci, function() { add_product_item(this); });';
        }

        if ($form_data['type']) {
            echo 'var type = \'' . $form_data['type'] . '\';';
            echo 'if(type == \'combo\') {';
            echo '$(\'#st\').slideUp();';
            echo '$(\'#ct\').slideDown();';
            // Otras acciones según tu lógica
            echo '}';
            echo 'else if(type == \'service\') {';
            echo '$(\'#st\').slideUp();';
            echo '$(\'#ct\').slideUp();';
            // Otras acciones según tu lógica
            echo '}';
            echo 'else {';
            echo '$(\'#ct\').slideUp();';
            echo '$(\'#st\').slideDown();';
            // Otras acciones según tu lógica
            echo '}';
        }
    }
    else {
        if ($this->input->post('type') == 'combo') {
            $c = sizeof($_POST['combo_item_code']);
            $items = array();
            for ($r = 0; $r < $c; $r++) {
                if (isset($_POST['combo_item_code'][$r]) && isset($_POST['combo_item_quantity'][$r])) {
                    $items[] = array(
                        'id' => $_POST['combo_item_id'][$r],
                        'row' => array(
                            'id' => $_POST['combo_item_id'][$r],
                            'name' => $_POST['combo_item_name'][$r],
                            'code' => $_POST['combo_item_code'][$r],
                            'qty' => $_POST['combo_item_quantity'][$r],
                            'cost' => $_POST['combo_item_cost'][$r]
                        )
                    );
                }
            }
            echo 'var ci = ' . json_encode($items) . ';';
            echo '$.each(ci, function() { add_product_item(this); });';
        }

        if ($this->input->post('type')) {
            echo 'var type = \'' . $this->input->post('type') . '\';';
            echo 'if(type == \'combo\') {';
            echo '$(\'#st\').slideUp();';
            echo '$(\'#ct\').slideDown();';
            // Otras acciones según tu lógica
            echo '}';
            echo 'else if(type == \'service\') {';
            echo '$(\'#st\').slideUp();';
            echo '$(\'#ct\').slideUp();';
            // Otras acciones según tu lógica
            echo '}';
            echo 'else {';
            echo '$(\'#ct\').slideUp();';
            echo '$(\'#st\').slideDown();';
            // Otras acciones según tu lógica
            echo '}';
        }
    }
    ?>

        $( "#requires_lot_number_checkbox" ).on('change',function() {
            var isChecked = document.getElementById('requires_lot_number_checkbox').checked;
            if(isChecked){
                $('#requires_lot_number').val(1);
            }else{
                $('#requires_lot_number').val(0);
            }
        });
        $( "#requires_serial_number_checkbox" ).on('change',function() {
            var isChecked = document.getElementById('requires_serial_number_checkbox').checked;
            if(isChecked){
                $('#requires_serial_number').val(1);
            }else{
                $('#requires_serial_number').val(0);
            }
        });
    });
    document.getElementById("image").onchange = function(e) {
        let reader = new FileReader();
        reader.readAsDataURL(e.target.files[0]);
        reader.onload = function(){
            let preview = document.getElementById('preview'),
                    image = document.createElement('img');
            image.src = reader.result;

            preview.innerHTML = '';
            preview.append(image);
        };
    }
</script>

<!--Script que te permite que en el cambo cantidad, precio y precio de referencia solo se escriba numeros y punto y coma -->
<script>
    function solonumeros(e) {
        key=e.keyCode || e.which;

        teclado=String.fromCharCode(key);
        numero ="0123456789.";
        especiales="8-37-38-46";
        teclado_especial=false;

        for(var i in especiales) {
            if(key==especiales[i]){
                teclado_especial=true;
            }
        }
        if(numero.indexOf(teclado)==-1 && !teclado_especial){
            return false;
        }
    }

</script>

<!--Campo nombre se autocompleta en nombre corto-->
<script>
$(document).ready(function () {
    $("#name").keyup(function () {
        var value = $(this).val();
        $("#short_name").val(value);
    });
    $('#btn-cancel').click(function (e) {
        if (confirm("¿Está seguro que desea cancelar?")) {
            window.location.href = base_url + "products"
        }

    });
});

</script>

<!--Deshabilitar los divs para registrar Servicios-->
<script>

    $('#type').on('change', function(){
        var selectValor = $(this).val()
        if(selectValor == 'service') {
            $('.categoria').hide(600);
            $('.brand').hide(600);
            $('.model').hide(600);
            $('.maker').hide(600);
            $('.medida').hide(600);
            $('.cantida').hide(600);
            $('.serie_lote').hide(600);
            $('.serie').hide(600);
            $('.costo').hide(600);
            $('.presentacion').hide(600);
            
        }
        if(selectValor == 'standard') {
            $('.categoria').show(600);
            $('.brand').show(600);
            $('.model').show(600);
            $('.maker').show(600);
            $('.medida').show(600);
            $('.cantida').show(600);
            $('.serie_lote').show(600);
            $('.serie').show(600);
            $('.costo').show(600);
            $('.presentacion').show(600);
        }
        if(selectValor == 'combo') {
            $('.categoria').show(600);
            $('.brand').show(600);
            $('.model').show(600);
            $('.maker').show(600);
            $('.medida').show(600);
            $('.cantida').show(600);
            $('.serie_lote').show(600);
            $('.serie').show(600);
            $('.costo').show(600);
            $('.presentacion').show(600);
        }
    });    
</script>
<!--Desabilitar y habiltar el select unit_measure-->
<script>
    $("#type").change(function() {
        if($("#type").val() != "service") {
            $('#unit_measure').attr('disabled', false);
            $('#categories').attr('disabled', false);
            $('#brand').attr('disabled', false);
            $('#maker').attr('disabled', false);
            $('#model').attr('disabled', false);
            // $('#alert_quantity').attr('disabled', false);
            $('#requires_lot_number_checkbox').attr('disabled', false);
            $('#requires_serial_number_checkbox').attr('disabled', false);
            $('#presentation').attr('disabled', false);
        }else{
            $('#unit_measure').attr('disabled', true);
            $('#categories').attr('disabled', true);
            $('#brand').attr('disabled', true);
            $('#maker').attr('disabled', true);
            $('#model').attr('disabled', true);
            // $('#alert_quantity').attr('disabled', true);
            $('#requires_lot_number_checkbox').attr('disabled',true);
            $('#requires_serial_number_checkbox').attr('disabled', true);
            $('#presentation').attr('disabled', true);
        }                
    });

</script>



