<?php
require '../../conexion.php';
include '../../session.php';
?>

<div class="modal-header " >
    <button type="button" class="close"     
            data-dismiss="modal" arial-label="Close">x</button>
    <h4 class="modal-title"><i class="fa fa-plus"></i> <strong>Registrar Orden de Trabajo</strong></h4>
</div>
<form action="orden_control.php" method="post" accept-charset="utf-8" class="form-horizontal" >
    <div class="modal-body">
        <input type="hidden" name="accion"  value="1">
        <input type="hidden" name="vusu" value="<?php echo $_SESSION['usu_cod']; ?>">
        <input type="hidden" name="vsuc" value="<?php echo $_SESSION['cod_suc']; ?>">
        <input type="hidden" name="pagina" value="orden_index.php" id="vcod">
        <input type="hidden" name="vcod" value="0"/> 
        <div class="form-group">
            <label class="col-md-2 control-label">Fecha:</label>
            <div class="col-md-4 col-sm-4 col-lg-4">
                <input type="date" required="" class="form-control" value="<?php echo date("Y-m-d") ?>" name="vfechainicio"  max="<?php echo date('Y-m-d'); ?>">
            </div>
            <div class="col-md-5 col-sm-5 col-lg-5">
                <label class="control-label">Presupuesto ?: </label>
                <input type="checkbox" id="conpre" name="conpre" class="form-control toggle" value="1" data-toggle="toggle" data-on="<i class='fa fa-check'></i> SI" data-off="<i class='fa fa-close'></i> NO" data-width="80" data-offstyle="danger">
            </div>            
        </div>
        <div class="form-group">
            <label class="col-md-2 control-label">Cliente:</label>
            <div class="col-md-9 col-sm-9 col-lg-9">
                <?php $clientes = consultas::get_datos("select * from v_clientes order by id_cliente"); ?>                                 
                <select name="vcliente" id="id_cliente" class="select2 form-control" required="" style="width: 100%">
                    <option value="">Seleccione un cliente</option>
                    <?php if(!empty($clientes)) {
                        foreach ($clientes as $cli) { ?>
                            <option value="<?php echo $cli['id_cliente']; ?>">
                                <?php echo "(".$cli['per_ci'].") ".$cli['cliente']; ?></option>
                            <?php
                        }
                    } else {
                        ?>
                        <option value="">Debe insertar un registro</option>
                    <?php } ?>
                </select>
            </div>
        </div>
        <div id="div_pre"></div>         
        <div class="form-group">
            <label class="col-lg-2 col-md-2 col-sm-2 control-label">Observación:</label>
            <div class="col-md-9 col-sm-9 col-lg-9">
                <textarea class="form-control" name="vord_obs" id="vped_obs"></textarea>
            </div>
        </div>        
        <div class="form-group">
            <label class="col-md-2 control-label">Receta?:</label>
            <div class="col-md-4 col-sm-4 col-lg-4">
                <input type="checkbox" id="conre" name="conre" class="form-control toggle" value="1" data-toggle="toggle" data-on="<i class='fa fa-check'></i> SI" data-off="<i class='fa fa-close'></i> NO" data-width="80" data-offstyle="danger">
            </div>        
        </div>
        <div id="datos_rec" style="display: none">
        <div class="form-group">
            <label class="col-md-2 control-label">Medico:</label>
            <div class="col-md-6 col-sm-6 col-lg-6">
                <input type="text" class="form-control receta" name="vmedico">
            </div>
            <label class="col-md-2 control-label">Registro :</label>
            <div class="col-md-2 col-sm-2 col-lg-2">
                <input type="text" id="nroreg" name="nroreg" class="form-control receta">
            </div>            
        </div>
        <div class="form-group">
            <label class="col-md-2 control-label">Especialidad:</label>
            <div class="col-md-6 col-sm-6 col-lg-6">
                <input type="text" class="form-control receta" name="vespecialidad">
            </div>          
        </div>         
        <div class="form-group">
            <label class="col-md-2 control-label">Hospital:</label>
            <div class="col-md-10 col-sm-10 col-lg-10">
                <input type="text" class="form-control receta" name="vhospital">
            </div>          
        </div> 
        </div>
    <div class="modal-footer" id="div_pre_detalle"></div> 
    <div class="modal-footer">
        <button type="reset" data-dismiss="modal" class="btn btn-default pull-left">
            <i class="fa fa-close"></i> Cerrar</button>
        <button type="submit" class="btn btn-primary pull-right">
            <i class="fa fa-floppy-o"></i> Registrar</button>
    </div>
    </div>
</form>

<script type="text/javascript">
    $("#conpre").prop("checked", false).trigger('change');
    $("#conre").prop("checked", false).trigger('change');
    $(".select2").select2();
    $('.toggle').bootstrapToggle();
</script>

<script>
    $('#conpre').on('change', function() {
        if( $('#conpre').prop('checked')) {                       
            $('#id_cliente').val("").trigger('change');
        }else{
          $('#div_pre').html("");  
        }
    }); 
    $('#conre').on('change', function() {
        if( $('#conre').prop('checked')) {                                   
            $('#datos_rec').show();
            $(".receta").attr({"required" : true}); 
            $(".receta").val(""); 
        }else{
          $('#datos_rec').hide(); 
          $(".receta").attr({"required" : false});
          $(".receta").val(""); 
        }
    });     
    $('#id_cliente').on('change',function(){
        if(this.value && $('#conpre').prop('checked')){
            $.ajax({
                type: "POST", url: "div_pre.php", data: {id_cliente: this.value}
            }).done(function (datos) {
                $("#div_pre").html(datos);
                $('#div_pre_detalle').html(""); 
            });            
            $("#div_pre").show();                      
        }else{
          $('#div_pre').html("");           
        }
    });    
</script>
