<?php 
include('./config/db.php');
include('funciones.php');
//$clienteId=(isset($_GET['clienteId']))?$_GET['clienteId']:"";
//$valor=(isset($_GET['valor']))?$_GET['valor']:"";
$valor=(isset($_POST['valor']))?$_POST['valor']:"";
$chkActividad=[];
$chkCantidad=[];
$i=0;
$disabled="disabled";
$Selected = "";
$cadena="";
    try{
        if($valor > 0){
            $sql=$conexion->prepare("SELECT * FROM planactividad where planId=:id order by actividadId asc;");   
            $sql->bindParam(":id",$valor);
            $sql->execute();
            $listaActiviadad= $sql->fetchAll(PDO::FETCH_ASSOC);
            foreach($listaActiviadad as $act){
                array_push($chkActividad, $act['actividadId']);
                array_push($chkCantidad, $act['cantidad']);
            }

        }

        $sqlActividad=$conexion->prepare("SELECT id, detalle FROM actividades where activo=1 order by id asc;");  
        $sqlActividad->execute();
        $listaActiviadad = $sqlActividad->fetchAll(PDO::FETCH_ASSOC);
        $cadena="<table class='table table-responsive table-hover'> <tbody>";
        foreach($listaActiviadad as $act) { 
            if($chkActividad != ""){
                if (in_array($act['id'], $chkActividad)) {
                    //print_r($chkCantidad[$i]);
                    $Selected =  "checked";
                    $disabled =  "";
                    $txtCantidad = $chkCantidad[$i];
                }else{
                    $Selected =  "";
                    $disabled =  "disabled";
                    $txtCantidad = "";
                    $i = $i-1;
                }

                $cadena=$cadena."<tr><td>";
                $cadena=$cadena."<input class='form-check-input' type='checkbox' ". $Selected . " value='" .$act['id'] . "'"; 
                $cadena=$cadena." name='chkActividad[]' id='chkActividad". $act['id'] ."' onclick='habilitaCantidad(this.value)'>"; 
                $cadena=$cadena."</td><td>" .$act['detalle']. "</td><td>Cantidad</td><td>";
                $cadena=$cadena."<input required value='". $txtCantidad."' type='number' min='1'" .$disabled. " class='form-control' name='txtCantidad[]' id='txtCantidad" .$act['id']."'>";
                $cadena=$cadena."</td></tr>";
            }
            $i++;
        } 
        $cadena=$cadena."</tbody></table>";
        echo  $cadena;
    }catch (Exception $err) {
        echo ("Ha ocurrido un error en la solicitud. ");  
        throw $err;
    }
?>