<?php

    $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '-1';
    
    $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
    $offset = ($page-1)*$rows;
    $result = array();
    
    include '../rdb/conn.php';
        $where ="";
    if ($id <>'') {$where .= " and r.producto_id = $id"; }

    $sql = "select count(*) from relacion_producto_matriz r INNER JOIN matrices s on r.matriz_id = s.id  ";
    $sql .= "where 1  ";
    $sql.= $where;
    
    $rs = mysqli_query( $conn,$sql);
    $row = mysqli_fetch_row($rs);
    $result["total"] = $row[0];
    
    if ($result["total"] === 0) {exit;}  

    $sql = "select s.nombre ,matriz_id id from relacion_producto_matriz r INNER JOIN matrices s on r.matriz_id = s.id ";
    $sql .= "where 1  ";
    $sql.= $where;
    $sql.= "order by s.nombre limit $offset,$rows ";
    
    $rs = mysqli_query( $conn,$sql);
    
    $items = array();
    while($row = mysqli_fetch_object($rs)){
        array_push($items, $row);
    }
    $result["rows"] = $items;
    $result["sql"] = $sql;

    echo json_encode($result);
    
?>