<?php

include_once "../header.php";

include_once("../php/config.php");

$id = $_GET["idcatalogo"];

    $sql = mysql_query("select * from catalogo_renta where idcatalogo_renta='$id'") or die(mysql_error());

    

?>

<body style="background:gray;">

<div class="container" style="background:white; padding-top:10px">

    <div class="row">

        <div class="col-md-12">



                <form action="datos_producto_renta.php" method="POST" onSubmit="$('.btn').prop('disabled', true)">

                        <button class="btn btn-info" onClick="$('#accion').val('update')"><i class="fa fa-save"></i> Aztualizar</button>

                        <a class="btn btn-default" href="catalogo_renta.php"><i class="fa fa-file"></i> Nuevo</a>

                        <a class="btn btn-default" href="catalogo_renta.php"><i class="fa fa-undo"></i> Salir</a>

                        

                <input type="hidden" id="accion" name="accion">



                <div class="row mt-2">

                    <div class="col-md-1 mt-2">

                        <label>Codigo</label>

                    </div>

                      <div class="col-md-11">

                        <input type="text" class="form-control" id="idcatalogo" name="idcatalogo" value="<?php echo  @mysql_result($sql,0,'idcatalogo_renta')?>">

                     </div>

                    

                </div>

                

                

                <div class="row mt-2">

                    <div class="col-md-1 mt-2">

                        <label>Nombre</label>

                    </div>

                      <div class="col-md-11">

                        <input type="text" class="form-control" id="nombre" name="nombre" value="<?php echo  @mysql_result($sql,0,'nombre')?>">

                     </div>

                    

                </div>

                

                <div class="row mt-2">

                    <div class="col-md-1 mt-2">

                        <label>Valor</label>

                    </div>

                      <div class="col-md-11">

                        <input type="text" class="form-control" id="valor" name="valor" value="<?php echo  @mysql_result($sql,0,'costo')?>">

                     </div>

                    

                </div>

                

                

                <div class="row mt-2">

                    <div class="col-md-1 mt-2">

                        <label>Precio</label>

                    </div>

                      <div class="col-md-11">

                        <input type="text" class="form-control" id="precio" name="precio" value="<?php echo  @mysql_result($sql,0,'precio')?>">

                     </div>

                    

                </div>

                <div class="row mt-2">

                    <div class="col-md-1 mt-2">

                        <label>Stock</label>

                    </div>

                      <div class="col-md-11">

                        <input type="text" class="form-control" id="stock" name="stock" value="<?php echo  @mysql_result($sql,0,'stock')?>">

                     </div>

                    

                </div>

                

                  <div class="row mt-2">

                    <div class="col-md-1 mt-2">

                        <label>Cantidad</label>

                    </div>

                      <div class="col-md-11">

                        <input type="text" class="form-control" id="cantidad" name="cantidad" value="1">

                     </div>

                    

                </div>

                

                <hr>

                <div class="row mt-2">

                    <div class="col-md-1 mt-2">

                        <label>Asignar</label>

                    </div>

                      <div class="col-md-4">

                          <select id="idrentas" class="form-control" name="idrentas">

                             <option value="0" selected>-Seleccione cliente...</option>

                          <?php

                            

                            $sql2 = mysql_query("select * from rentas where estado=''") or die(mysql_error());

                            while($r0 = mysql_fetch_array($sql2)){

                           ?>

                            <option value="<?php echo $r0['idrentas']?>"><?php echo $r0['cliente']?></option>

                            <?php }?>

                           

                           

                       </select>

                     </div>

                    

                </div>

                

                <div class="row mt-2">

                    <div class="col-md-1 mt-2">

                        <button class="btn btn-success" onClick="$('#accion').val('asignar')"><i class="fa fa-copy"></i>  Asignar</button>

                    </div>

                </div>

                

                <div class="row mt-2">

                <div class="col-md-12">

                     <?php 

                    if($_POST["accion"]=="asignar"){

                            if($_POST["idrentas"]=="0" or $_POST["precio"]=="" or $_POST["cantidad"]=="" or $_POST["nombre"]==""){

                            echo "<h3>faltan datos...</h3>";

                            ;

                            

                            }else{

                                mysql_query("insert into detalle_renta set cliente='".$_POST['idrentas']."', precio='".$_POST['precio']."', cantidad='".$_POST['cantidad']."', producto='".$_POST['nombre']."'",$con) or die(mysql_error());

                                mysql_query("update rentas set estado='Terminada' where idrentas='".$_POST['idrentas']."'",$con) or die(mysql_error());



                                echo "<h3 class='text-primary'>Datos grabados...</h3>";

                                sleep(2);

                                echo "<script>window.location.href='rentas.php'</script>";

                                

                            }

                    }

                    

                    if($_POST["accion"]=="update"){

                        if($_POST["precio"]=="" or $_POST["nombre"]=="" or $_POST["stock"]==""){

                            echo "<h3>faltan datos...</h3>";

                            

                            

                            }else{
                               
                                if($_POST["idcatalogo"]==""){

                                    mysql_query("insert into catalogo_renta set nombre='".$_POST['nombre']."', costo='".$_POST['valor']."', precio='".$_POST['precio']."', stock='".$_POST['stock']."'",$con) or die(mysql_error());



                                    echo "<h3 class='text-primary'>Datos grabados...</h3>";

                                    sleep(2);

                                    echo "<script>window.location.href='catalogo_renta.php'</script>";

                                }else{

                                    $id = $_POST["idcatalogo"];
                                    mysql_query("update catalogo_renta set nombre='".$_POST['nombre']."', costo='".$_POST['valor']."', precio='".$_POST['precio']."', stock='".$_POST['stock']."' where idcatalogo_renta='$id'",$con) or die(mysql_error());
                                        
                                    echo "<h3 class='text-primary'>Datos grabados...</h3>";

                                    sleep(2);

                                    echo "<script>window.location.href='catalogo_renta.php'</script>";

                                }

                                

                            }

                    }



                    ?>

                </div>

            </div>

                

                

                

         </form>  

        </div>

    </div>

</div>





<?php

include_once "../footer.php";



?>





