<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');
$gar_id=$_GET['gar_id'];
$can=mysqli_query($con,"SELECT * FROM garantias_proveedor where empresa='$empresa' and gar_id='$gar_id'") or die(mysqli_error($con)); 
$dato=mysqli_fetch_array($can); 
$gar_estado=$dato["gar_estado"];
?>
<table class="table table-bordered table-hover table-striped">
    <tr>
        <th width="7%">CODIGO</th>
        <th width="5%">CANTIDAD</th>
        <th>ARTICULO</th>
        <th>SERIAL</th>
        <th>PROBLEMA</th>
        <th>ACCESORIOS</th>
        <th width="10%">ESTADO</th>
        <th width="10%">SOLUCION</th>
        <th width="8%"></th>

    </tr>
    <?php
    $sumador_total=0;
    $sql=mysqli_query($con, "SELECT * from garantia_pro_detalle where garntias_proveedor_gar_id='$gar_id'");
    while ($row=mysqli_fetch_array($sql))
        {
            $id=$row["gard_id"];
            $gard_id=$row["gard_id"];
            $gar_art_id=$row["gar_art_id"];
            $gard_cantidad=$row['gard_cantidad'];
            $gard_precio=$row['gard_precio'];
            $precio_total=$gard_precio*$gard_cantidad;
            $sumador_total+=$precio_total;//Sumador
    ?>
    <tr>
        <td><?php echo $row['gard_referencia'];?></td>
        <td><?php echo $gard_cantidad;?></td>
        <td><?php echo $row['gard_nombre'];?></td>
        <td>
            <?php
            $can1=mysqli_query($con,"SELECT * from serial_garantias where garantia_detalle_gard_id='$gard_id'") or die(mysqli_error($con)); 
            while ($roww=mysqli_fetch_array($can1))
            {
                echo $roww['gard_codigo'];
                echo ' - ';
            }
            ?>
        </td>
        <td><?php echo $row['gard_problema'];?></td>
        <td><?php echo $row['gard_accesorios'];?></td>
        <td><?php echo $row["estado"]; ?></td>
        <td><a title="" class=""><span class="badgeazul"><i class="btn-icon-only "><?php  echo $row['solucion']; ?></i></span></a></td>

        <td> 
            <?php if ($gar_estado=='Sin enviar') 
            {
                $can1=mysqli_query($con,"SELECT * FROM serial_garantias where garantia_detalle_gard_id='$gard_id'") or die(mysqli_error($con)); 
                $dat=mysqli_fetch_array($can1);
                $result = mysqli_num_rows($can1);
                if($result == 0)
                {
                ?>
                <a data-id="<?php echo $id;?>" data-toggle="modal" class="btn btn-ser btn-xs btn-default" title="Serial"> <i class="btn-icon-only icon-barcode"></i></a>
                <?php } else { ?>
                <a data-id="<?php echo $id;?>" data-toggle="modal" class="btn btn-ser_tabla btn-xs btn-default" title="Serial"> <i class="btn-icon-only icon-barcode"></i></a>
                <?php } ?>
                <a href="#" id="delete-<?php echo$row['gard_id'];?>" class="btn btn-xs btn-danger"><i class="btn-icon-only icon-remove"></i></a>
                <script>
                $("#delete-"+<?php echo $row["gard_id"];?>).click(function(e){
                    e.preventDefault();
                    p = confirm("Estas seguro?");
                    if(p){
                        $.get("./php_garantias_proveedor/z_eliminar.php","gard_id="+<?php echo $row["gard_id"];?>,function(data){
                        loadTabla();
                        });
                    }
                });
                </script>
            <?php  }  else if ($row['solucion']=="Pendiente"){ ?>
                <a data-id="<?php echo $id;?>" data-toggle="modal" class="btn btn-mod btn-xs btn-success" title="Solucion"> <i class="btn-icon-only  fa-solid fa-list-check"></i></a>

               
            <?php }  ?>
                 
        </td>
    </tr>   
<?php } ?>
</table>

<script>
    $(".btn-mod").click(function(){
        id = $(this).data("id");
        $.get("./php_garantias_proveedor/form_solucion.php","id="+id,function(data){
            $("#form-edit").html(data);
        });
        $('#editModal').modal('show');
    });
</script>
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Entrega del Producto</h4>
            </div>
            <div class="modal-body">
                <div id="form-edit"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<script>
    $(".btn-ser").click(function(){
        id = $(this).data("id");
        $.get("./php_garantias_proveedor/z_form_serial.php","id="+id,function(data){
            $("#form-serial").html(data);
        });
        $('#reg_ser').modal('show');
    });
</script>
<div class="modal fade" id="reg_ser" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Agregar Seriales</h4>
            </div>
            <div class="modal-body">
                <div id="form-serial"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->



<script>
    $(".btn-ser_tabla").click(function(){
        id = $(this).data("id");
        $.get("./php_garantias_proveedor/z_form_serial_tabla.php","id="+id,function(data){
            $("#form-tabla_serial").html(data);
        });
        $('#reg_ser_tabla').modal('show');
    });
</script>
<div class="modal fade" id="reg_ser_tabla" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Seriales</h4>
            </div>
            <div class="modal-body">
                <div id="form-tabla_serial"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->