<?php

       /*$collection_id = $_GET['collection_id'];
       $collection_status = $_GET['collection_status'];
       $payment_id = $_GET['payment_id'];
       $external_reference = $_GET['external_reference'];
       $payment_type = $_GET['payment_type'];
       $merchand_order = $_GET['external_reference'];
       $preference_id = $_GET['preference_id'];
       $processing_mode =$_GET['processing_mode'];*/
       $error = $_GET['error'];
       
       $bd = $_GET['bd'];
       $id_cliente = $_GET['id_cliente'];
       $suc = $_GET['suc'];
       $id_servicio;
       $costo;
       $plan;
       $tipo = $_GET['tipo'];
       $fecha_visita;
       $paquete_clase;
       $opcion;
       $url_redirect;
       $concepto;
       $idLocker;
       $lock_asig;
       $suc;
        
        $msgQuery;
        $conn = new mysqli("directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$bd);
       if($tipo == 'planes'){
            $id_servicio = $_GET['id_servicio'];
            $costo = $_GET['importe'];
            $plan = $_GET['plan'] ?? 0;
            $tipo = $_GET['tipo'];
            $fecha_visita = $_GET['fecha_visita'];
            $paquete_clase = $_GET['paquete_clase'];
            $opcion = $_GET['opcion'];
              
            
            $hoy = date('d/m/Y h:i:s A');
            $id_insert;
            $mes;
            $nombreGim;
            $desc_plan;
            $fechaI;
            $fechaA;
            
            $query_fecha ="SELECT  * FROM(SELECT 
                    socio_servicio.idServicio as 'id',
                    pagos.FechaInicio,
                    servicios.NombreServicio,
                    socio_servicio.idSocio_Servicio AS 'idss',
                    socio_servicio.idSocioTitular as 'st',
                    pagos.FechaFin
                    FROM 
                    socio_servicio
                        INNER JOIN pagos
                    	    ON socio_servicio.idSocio_Servicio = pagos.idSocio_Servicio
                        INNER JOIN servicios
                    	    ON socio_servicio.idServicio = servicios.idServicio
                        INNER JOIN socios
                    	    ON socio_servicio.idSocio = socios.idSocio
                    WHERE
                        socio_servicio.idSocio = '$id_cliente'
                        AND pagos.Cancelado = 0  
                        AND pagos.Visible = 1
                        AND servicios.idServicio = '$id_servicio'
        			GROUP BY
        				socio_servicio.idSocio_Servicio DESC
                    ) AS tmp
                    GROUP BY
                    	tmp.id
                    ORDER BY
                        tmp.idss DESC";
                    
                $result_fecha = $conn->query($query_fecha);
            
            $query;
            if($opcion == '97'){
                $rowPlanP = $result_fecha->fetch_assoc();
                $idP = $rowPlanP['id'];
                $query = "INSERT INTO socio_servicio(idServicio,idSocio,idSocioTitular,FechaSolicitud,FechaInscripcion)
                VALUES('$idP','$id_cliente','$id_cliente',CURDATE(),CURDATE())";
            }else{
                $query = "INSERT INTO socio_servicio(idServicio,idSocio,idSocioTitular,FechaSolicitud,FechaInscripcion)
                VALUES('$id_servicio','$id_cliente','$id_cliente',CURDATE(),CURDATE())";
                //$resultado = $conn->query($query);
                //$resultado = $conn->insert_query($query,"directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$bd);
            }
            

            if($conn->query($query) == TRUE){
                $id_insert = $conn->insert_id;
                /*$query_suc = "SELECT sucursal_idSucursal AS sucursal FROM socios WHERE idSocio = '$id_cliente'";
                $recult_suc = $conn->query($query_suc);
                $row_suc = $recult_suc->fetch_assoc();
                $id_sucursal = $row_suc['sucursal'];*/
                
                $concepto;
                switch($plan){
                    case 1:
                        $desc_plan = 'VISITA';
                        $mes = 0;
                        $concepto = 0;
                        break;
                    case 2:
                        $desc_plan = 'SEMANAL';
                        $mes = 7;
                        $concepto = 1;
                        break;
                    case 3:
                        $desc_plan = 'QUINCENAL';
                        $mes = 15;
                        $concepto = 2;
                        break;
                    case 4:
                        $desc_plan = 'MENSUAL';
                        $mes = 1;
                        $concepto = 3;
                        break;
                    case 5:
                        $desc_plan = 'BIMESTRAL';
                        $mes = 2;
                        $concepto = 4;
                        break;
                    case 6:
                        $desc_plan = 'TRIMESTRAL';
                        $mes = 3;
                        $concepto = 5;
                        break;
                    case 7:
                        $desc_plan = 'CUATRIMESTRAL';
                        $mes = 4;
                        $concepto = 6;
                        break;
                    case 8:
                        $desc_plan = 'SEMESTRAL';
                        $mes = 6;
                        $concepto = 7;
                        break;
                    case 9:
                        $desc_plan = 'ANUAL';
                        $mes = 12;
                        $concepto = 8;
                        break;
                    default:
                        $desc_plan = 'VISITA';
                        $mes = 0;
                        $concepto = 0;
                        break;
                }
                    
                    //echo $mes;
                
                $row_fecha = $result_fecha->fetch_assoc();
                $fecha_hoy = date('Y-m-d');
                    
                    //$fechaI = $row_fecha['FechaFin'];
                $fecha_actual_hoy = date("d-m-Y", strtotime($fechaI));
                    
                $visible;
                    //echo 'Op '.$obj->opcion."\n";
                if($opcion == "99"){
                    $fechaI = $row_fecha['FechaFin'];
                    $concepto = "99";
                    $visible = '1';
                }else if($opcion == "97"){
                    $fechaI = date('Y-m-d');
                    $concepto = "97";
                    $visible = '1';   
                } else {
                    $fechaI = $row_fecha['FechaFin'];
                    $visible = '1';  
                }
                $visible = '1';  
                $desc_plan = $desc_plan.' '.$row_fecha['NombreServicio'];
                if($mes == 0 || $mes == 7 || $mes == 15){
                    $fecha_vencimiento = "DATE_ADD( '$fechaI', INTERVAL ".$mes." DAY)";
                    $fechaA = date("d/m/Y",strtotime($fechaI."+ ".$mes." days"));
                }else{
                    $fecha_vencimiento = "DATE_ADD( '$fechaI', INTERVAL ".$mes." MONTH)";
                    $fechaA = date("d/m/Y",strtotime($fechaI."+ ".$mes." month"));
                }
                    
                $query_pagos ="INSERT INTO pagos (idSocio_Servicio,Concepto,FechaPago,Importe,idUsuario,Pago,Descuento,Subtotal,
                    FechaInicio,Forma1,Importe1,Forma2,Importe2,idSucursal,Visible,FechaFin,Cancelado) 
                    values ($id_insert,$concepto,NOW(),$costo,2,16,0.00,$costo,CURDATE(),'MercadoPago',0.00,'MercadoPago',0.00,$suc,$visible,$fecha_vencimiento,0)";
                    
                $query_imprimir = $query_pagos;
        
                if ($conn->query($query_pagos) === TRUE) {
                    if($opcion == "97"){
                        $querySocio = "UPDATE socios SET Activo = 1 WHERE idSocio = $id_cliente";
                        if($conn->query($querySocio) == TRUE){
                            $msgQuery = "&iexcl;Transacci&oacute;n Exitosa!";
                        }else{
                            $msgQuery = "error, verificar pago en administracion";
                        }
                    }else{
                        $msgQuery = "&iexcl;Transacci&oacute;n Exitosa!";
                    }
                } else {
                    $msgQuery = "Error al registrar su pago, cheque en Administraci&oacute;n";
                } 
                    //$conn->close();
            }else{
                    //$conn->close();
                $msgQuery = 'Error al registrar su pago, cheque en Administraci&oacute;n';
            }
        
        
        }else{
            //https://directoriofitness.com.mx/estaciones/Progymcloud_app/php/mercado_pago/success.php?error=0&%20%20%20%20%20%20bd=direct18_PG&
            //flag=4&id_cliente=468&concepto=1&costo=25&%20%20%20%20%20%20idLocker=1&lock_asig=44&suc=1&tipo=locker&collection_id=1238579685&
            //collection_status=approved&payment_id=1238579685&status=approved&external_reference=null&payment_type=credit_card&
            //merchant_order_id=2939555003&preference_id=774035859-22749631-3b93-4a20-9bc0-26d0fe03eceb&site_id=MLM&processing_mode=aggregator&merchant_account_id=null
            $costo = $_GET['costo'];
            $idLocker = $_GET['idLocker'];
            $lock_asig = $_GET['lock_asig'];
            
            $bd = $_GET['bd'];
            
            $flag = $_GET['flag'];
            $id_cliente = $_GET['id_cliente'];
            $concepto = $_GET['concepto'];
           
            $lock_asig = $_GET['lock_asig']; 
            $desc_plan;
            
            
            //codigo para insertar
                $fechaA;
                $addFecha;
                $queryLocker ="SELECT 
              	lockers_socios.Socios_idSocio as 'id', 
                lockers.idLocker as 'id_gl', 
                locker.id_locker as 'idlk_c', 
                CONCAT(socios.Nombre, ' ', socios.Apellidos) as 'name', 
                lockers.Clave as 'grupo',
                locker.num_locker as 'num_locker', 
                lockers_socios.FechaAsignacion as 'pago', 
                lockers_socios.FechaVencimiento, 
                lockers_socios.concepto_pago as 'concepto', 
                lockers_socios.importe as 'total', 
                lockers_socios.estado 'status',
                lockers_socios.cancelado,
                sucursal.NombreSucursal as 'sucursal',
                IF(lockers_socios.FechaVencimiento < DATE_FORMAT(NOW(), '%d/%m/%Y') 
            		AND lockers_socios.cancelado = 0 AND lockers_socios.estado = 1, 'TRUE', 'FALSE') as 'vencimiento'
            FROM 
                lockers_socios, 
                socios, 
                lockers, 
                sucursal, 
                locker 
            WHERE 
                lockers_socios.Lockers_idLocker = lockers.idLocker 
                AND lockers_socios.Socios_idSocio = socios.idSocio 
                AND lockers_socios.sucursal_idSucursal = sucursal.idSucursal 
                AND socios.idSocio = $id_cliente
                AND lockers_socios.locker_asignado = locker.id_locker
                AND lockers_socios.cancelado = 0
            ORDER BY 
                lockers_socios.id_lockers_socios DESC LIMIT 1";
                
                //$resultadoL = $conn->insert_query($queryLocker,"directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->bd);
                //$row_fecha = mysqli_fetch_array($resultadoL);
                $resultadoL =$conn->query($queryLocker);
                $row_fecha = $resultadoL->fetch_assoc();
                $fechaVen = $row_fecha['FechaVencimiento'];
                $fecha_act = $row_fecha['FechaAsignacion'];
                switch($concepto){
                    case '1':
                        $desc_plan = 'Pago Locker 1 d&iacute;a';
                        $addFecha = "DATE_ADD( '$fechaVen', INTERVAL 1 DAY)";
                        $fechaA = date("d/m/Y",strtotime($fechaVen."+ 1 days"));
                        break;
                    case '2':
                        $desc_plan = 'Pago Locker semanal';
                        $addFecha = "DATE_ADD( '$fechaVen', INTERVAL 7 DAY)";
                        $fechaA = date("d/m/Y",strtotime($fechaVen."+ 7 days"));
                        break;
                    case '3':
                        $desc_plan = 'Pago Locker quincenal';
                        $addFecha = "DATE_ADD( '$fechaVen', INTERVAL 15 DAY)";
                        $fechaA = date("d/m/Y",strtotime($fechaVen."+ 15 days"));
                        break;
                    case '4':
                        $desc_plan = 'Pago Locker mensual';
                        $addFecha = "DATE_ADD( '$fechaVen', INTERVAL 1 MONTH)";
                        $fechaA = date("d/m/Y",strtotime($fechaVen."+ 1 month"));
                        break;
                    case '5':
                        $desc_plan = 'Pago Locker semestral';
                        $addFecha = "DATE_ADD( '$fechaVen', INTERVAL 6 MONTH)";
                        $fechaA = date("d/m/Y",strtotime($fechaVen."+ 6 month"));
                        break;
                    case '6':
                        $desc_plan = 'Pago Locker anual';
                        $addFecha = "DATE_ADD( '$fechaVen', INTERVAL 12 MONTH)";
                        $fechaA = date("d/m/Y",strtotime($fechaVen."+ 12 month"));
                        break;
                }
                
                $query = "INSERT lockers_socios(Lockers_idLocker,Socios_idSocio,locker_asignado,FechaAsignacion,FechaVencimiento,estado,concepto_pago,tipo_pago,
                cantidad_tarjeta,importe,user_idUsuario,sucursal_idSucursal,cancelado)
                VALUES('$idLocker','$id_cliente','$lock_asig','$fechaVen',$addFecha,1,'$concepto',2,'$costo','$costo',21,'$suc',0)";
                
                //$resultado = $conn->insert_query($query,"directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->bd);
                if($conn->query($query) == TRUE){
                    $msgQuery = '&iexcl;Transacci&oacute;n Exitosa!';
                }else{
                    $msgQuery = 'Error al registrar su pago, cheque en Administraci&oacute;n';
                }
        }
      
    $conn->close();
  
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
  <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>

  <title>Mercado Pago</title>
</head>
<style>
    .padre {

    padding: 0 1rem;
    margin: 1rem;
    }

    .hijo {
        /* IMPORTANTE */
        width: 200px;
        margin-left: auto;
        margin-right: auto;
    }
    .recibo{
        border-style: ridge;
    }

    </style>
<body>
  <nav class="navbar navbar-dark bg-primary">
    <a class="navbar-brand" href="#" style="margin-left: 15px;">ProgymCloud</a>
  </nav>
  <div class="container">
    <div class="padre">
      <p class="hijo" style="text-align: center;">
        <img src="https://i.pinimg.com/originals/71/81/e8/7181e84d50cb87fa4ab9a5a8ab613dbe.jpg" alt="PayPal-Logo" width="130px">
      </p>
      <p class="hijo" style="text-align: center; color: #00E013; font-size: 70px;">
      <i class="fas fa-check-circle"></i>
      <h3 class="hijo" style="text-align: center;"><?= $msgQuery;?></h3>
      </p>
      
    </div>
  </div>

</body>
</html>
