<?php

    class RGPD {

        private $con;

        public function __construct($con)
        {
            $this->con = $con;
        }

        public function getRGPD($cliente = false, $id = false)
        {
            $html = '';
            $nr = 0;
            $id = $id ? $id : false;

            if(!$cliente)
            {
                $sql = 'SELECT * FROM RGPD_PERMISOS';
                $res = $this->con->query($sql);
                while($row = $res->fetch_array())
                {
                    $nr++;
                    $rpgd = $id . "rgpd" . $nr;
                    $required = $row['CODIGO'] == 1 ? 'required' : '';

                    $html .= '<div class="col-12 mb-2">';
                        $html .= '<div class="custom-control custom-switch">';
                            $html .= '<input type="checkbox" class="custom-control-input" name="rgpd[]" id="' . $rpgd . '" value="' . $row['CODIGO'] . '"' . $required . '>';
                            $html .= '<label class="custom-control-label" for="' . $rpgd . '">' . $row['TITULO'] .'</label>';
                        $html .= '</div>';
                    $html .= '</div>';
                }
            }
            else
            {

                $idRGPD = [];
                $titleRGPD = [];


                $sql = 'SELECT C.* 
                FROM CONTACTOS C
                WHERE C.CODIGO = ? ';
                $stmt = $this->con->prepare($sql);
                $stmt->bind_param('i', $cliente);
                $stmt->execute();
                $res = $stmt->get_result();
                $row = $res->fetch_array();
                $clienteRGPD = [$row['RGPD_1'], $row['RGPD_2'], $row['RGPD_3'], $row['RGPD_4']];
                

                $sqlRGPD = 'SELECT * FROM RGPD_PERMISOS';
                $resRGPD = $this->con->query($sqlRGPD);
                while($rowRGPD = $resRGPD->fetch_array())
                {
                    array_push($idRGPD, $rowRGPD['CODIGO']);
                    array_push($titleRGPD, $rowRGPD['TITULO']);
                }


                for($i = 0; $i < count($idRGPD); $i++)
                {
                    $nr++;
                    $rpgd = "rgpd" . $nr;
                    $required = $i == 0 ? 'required' : '';
                    $checked = $clienteRGPD[$i] == 1 ? 'checked' : '';

                    $html .= '<div class="col-12 mb-2">';
                        $html .= '<div class="custom-control custom-switch">';
                            $html .= '<input type="checkbox" class="custom-control-input" name="rgpd[]" id="' . $rpgd . '" value="' . $idRGPD[$i] . '"' . $required . ' ' . $checked . '>';
                            $html .= '<label class="custom-control-label" for="' . $rpgd . '">' . $titleRGPD[$i] .'</label>';
                        $html .= '</div>';
                    $html .= '</div>';
                }
                $html .= '<input type="hidden" name="cliente" value="' . $cliente . '">';


            }
            return $html;
        }

        public function update_rgpd ($codigo_cliente, $rgpd) {
            
            $Schema = new Schema($this->con);
            $variable = '';
            $text_in_query = '';
            $tipo_variable = '';
            $array_variable = [];
            $cero = 0;
            $sql = "UPDATE CONTACTOS SET RGPD_1 = ?, RGPD_2 = ?, RGPD_3 = ?, RGPD_4 = ? WHERE CODIGO = ?";
            $stmt = $this->con->prepare($sql);
            $stmt->bind_param('iiiii', $cero, $cero, $cero, $cero, $codigo_cliente);
            $stmt->execute();
            for($i = 0; $i < count($rgpd); $i++)
            {
                if($rgpd[$i] == $rgpd[0]) {
                    $text_in_query .= ' RGPD_' . Statics::SafeInput($this->con, $rgpd[$i]) . ' = ?';
                    $tipo_variable .= 'i';
                } else {
                    $text_in_query .= ', RGPD_' . Statics::SafeInput($this->con, $rgpd[$i]) . ' = ?';
                    $tipo_variable .= 'i';
                }

                array_push($array_variable, 1);
            }

            $tipo_variable .= 'i';
            array_push($array_variable, $codigo_cliente);
            $sql = 'UPDATE CONTACTOS SET ' . $text_in_query . ' WHERE CODIGO = ?';
            $Schema->executeQuery($sql, $tipo_variable, $array_variable);

        }

    }

?>