<!-- Modal -->

<div id="modalAddChangePrice" class='modal fade' data-backdrop="static" data-keyboard="false" role="dialog" style=" overflow: hidden;background: transparent">

    <div class="modal-dialog modal-lg" role="document">

        <div class="modal-content">

            <div class="modal-body">

                <div class="box box-primary">

                    <div class="box-header with-border">

                        <button type="button" class="close _closeModalChangePrice"><i class="fa fa-window-close"></i></button>

                        <h4><i class='fa-fw fa fa-tags'></i> <span id="title_modal_salesrecord"><?php echo $_translator->_getTranslation('Listado de cambio de precios'); ?></span></h4>

                    </div>

                    <div class="box-body">

                        <div class='flashmessenger_modal_change_price'><?php $flashmessenger->showMessage(true); ?></div>
                        <div>

                            <div class="col-md-12">
                                <div class="form-group"> <label class="control-label col-lg-3 col-md-3 col-sm-3 col-xs-12"> Sucursal</label>
                                    <div class=" input-group col-lg-8 col-md-8 col-sm-8 col-xs-12">
                                        <select multiple='multiple' id="store_id" name="store_id" class="form-control">

                                            <?php
                                            $repo = new StoreRepository();

                                            $result = $repo->getListSelectStores();
                                            $listLocations = '<option value="0">Seleccionar una opcion...</option>';


                                            if ($result) {

                                                foreach ($result as $key => $value) {

                                                    $listLocations .= "<option value='$key'>$value</option>";
                                                }
                                                echo $listLocations;
                                            }


                                            ?>
                                        </select>
                                    </div>


                                </div>

                                <div class="table-responsive">

                                    <table id='productRecordDetails' class="table table-condensed table-striped table-hover font-size-11">

                                        <thead>

                                            <th class="col-md-3 text-center"><?php echo $_translator->_getTranslation('Categoria cambio') ?></th>

                                            <th class="col-md-1 text-center"><?php echo $_translator->_getTranslation('Codigo') ?></th>

                                            <th class="col-md-3 text-center"><?php echo $_translator->_getTranslation('Descripcion') ?></th>

                                            <th class="col-md-2 text-right"><?php echo $_translator->_getTranslation('Anterior') ?></th>

                                            <th class="col-md-2 text-right"><?php echo $_translator->_getTranslation('Nuevo') ?></th>

                                            <th class="col-md-2 text-right"><?php echo $_translator->_getTranslation('Accion') ?></th>


                                            <th class="col-md-1 text-center"></th>

                                        </thead>

                                        <tbody>

                                        </tbody>

                                    </table>

                                </div>

                            </div>

                        </div>

                    </div>

                    <div class="box-footer">

                        <div class="pull-right">

                            <input type="button" id='btn_save_transaction' value="<?php echo $_translator->_getTranslation('Preparar cambios') ?>" class='btn btn-primary' />

                        </div>

                        <div class="pull-left">

                            <input type="button" value="<?php echo $_translator->_getTranslation('Cancelar') ?>" class="btn btn-default _closeModalChangePrice" />


                        </div>

                    </div>



                </div>

            </div>

        </div>

    </div>
    <script>
        var storesJSON;
        $('#store_id').select2();

        $('#modalAddChangePrice').on('shown.bs.modal', function(e) {

            if ($('#action').val() === 'insert') {
                $('#initial_cash').focus();
            }



            var input = null;

            $("form[name=salesrecord]").find('div').each(function() {

                if ($(this).hasClass('has-error') === true) {

                    input = $(this).data('errorfor');

                    return false;

                }

            });



            if (input !== null) {
                $('#' + input).focus();
            }

        });

        //Llamar a la function saveTransactionProduct al dar click a btn_save_transaction
        $('#btn_save_transaction').on('click', function() {

            // Obtener los valores seleccionados
            var selectedValues = $('#store_id').val();
            if (selectedValues !== "" && selectedValues !== undefined && selectedValues !== null && selectedValues.length > 0) {
                var selectedStores = [];

                // Recorrer cada valor seleccionado y obtener su texto
                selectedValues.forEach(function(value) {
                    var text = $('#store_id option[value="' + value + '"]').text();
                    selectedStores.push({
                        value: value,
                        text: text
                    });
                });

                // Convertir los valores seleccionados en JSON
                storesJSON = JSON.stringify(selectedStores);

                saveCambiosProductos(); //UG enviamos la info al ajax

                // Imprimir el JSON en consola (o realizar otra acción, como una llamada AJAX)
                console.log(storesJSON);

                //  var selectedStores = $('#store_id').val();
                // // Convertir en JSON
                // var storesJSON = JSON.stringify(selectedStores);
                // console.log(storesJSON);

            } else {
                $.alert({
                    content: "Es necesario seleccionar al menos una sucursal",
                    title: "<i class='fa fa-info-circle'></i> Message",
                    columnClass: 'col-md-6 col-md-offset-3',
                    type: 'red'
                });
            }

        });


        function saveCambiosProductos() {
            $.post('/Controller/Product.php', {
                action: 'ajax',
                request: 'saveCambiosProductos',
                cambios: JSON.stringify(cambios),
                store: storesJSON
            }, function(data) {
                if (data.response) {



                    $.alert({
                    content: "Cambios preparados correctamente",
                    title: "<i class='fa fa-info-circle'></i> Message",
                    columnClass: 'col-md-6 col-md-offset-3',
                    type: 'green'
                });
                    window.setTimeout(function() {
                       
                        
                    document.location.href = 'PosProduct.php';
                    }, 2000);


                } else {
                    // $('.flashmessenger_modal_change_price').html(data.msg);
                    // fadeOutAlert();
                    // document.location.href = 'PosProduct.php';

                    $.alert({
                    content: data.msg,
                    title: "<i class='fa fa-info-circle'></i> Message",
                    columnClass: 'col-md-6 col-md-offset-3',
                    type: 'red'
                });

                window.setTimeout(function() {
                       
                        
                       document.location.href = 'PosProduct.php';
                       }, 2500);

                }
            }, 'json');
        }
    </script>

</div>