<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Info</title>
    <script>

    function replaceGeomType(url, geomtype) {
        if (geomtype) {
            const parts = url.split('&')
                .map(part=>{
                    if (part.startsWith('geomtype=')) {
                        part = `geomtype=${geomtype}`
                    }
                    return part;
            })
            url = parts.join('&');
        }
        return url;
    }

    function addBboxllToLinks(bboxll,geomtype)
    {
        if (bboxll) {
            document.querySelectorAll('#columns > li > a')
                .forEach(a=>(a.setAttribute('href', replaceGeomType(a.getAttribute('href'),geomtype) + '&bboxll='+JSON.stringify(bboxll))))
        }
    }
    
    function init() {
        const urlParams = new URLSearchParams(window.location.search);
        const fullTableName = urlParams.get('table');
        const geomType = urlParams.get('geomtype');
        const geomcolumn = urlParams.get('geom_column');
        let bboxll = null;
        document.querySelector('#tablename').innerHTML = fullTableName;
        const parts = fullTableName.split('.');
        const tableName = (parts.length > 1) ? parts[1] : parts[0];
        fetch(`data/layer_columns/${fullTableName}`).then(response=>{
            const list = document.querySelector('#columns');
            if (response.ok) {
                response.json().then(json=> {
                    for (item of json) {
                        const li = document.createElement('li');
                        li.innerHTML = `<a href="./attrinfo.html?table=${fullTableName}&geom_column=${geomcolumn?geomcolumn:''}&column=${item.field_name}&columntype=${item.field_type}&geomtype=${geomType}"><b>${item.field_name}</b></a> (${item.field_type})`
                        list.appendChild(li);
                    }
                    fetch(`api/bbox/${fullTableName}${geomcolumn?`?geom_column=${geomcolumn}`:''}`).then(response=>{
                        const bbox = document.querySelector('#bbox');
                        if (response.ok) {
                            response.json().then(json=> {
                                addBboxllToLinks(json.bboxll,json.geomtype);
                                bbox.innerHTML = `number of rows: ${json.allrows}<br>
                                                number of geometries: ${json.geomrows}<br>
                                                srid: EPSG:${json.srid}<br>
                                                bbox lon/lat: ${json.bboxll?`sw: ${json.bboxll[0][0]},${json.bboxll[0][1]}, ne: ${json.bboxll[1][0]},${json.bboxll[1][1]}`: 'not defined'}<br>
                                                bbox (EPSG:${json.srid}): ${json.srid?`ll: ${json.bboxsrid[0][0]},${json.bboxsrid[0][1]}, tr: ${json.bboxsrid[1][0]},${json.bboxsrid[1][1]}`: 'not defined'}<br>
                                                `
                            })
                        } else {
                            response.json().then(json=>{
                                bbox.innerHTML = `Error getting bbox: ${json.error}`;
                            }).catch(err => {
                                bbox.innerHTML = `Error parsing bbox: ${err}`;
                            })
                        }
                    })
                })
            } else {
                const li = document.createElement('li');
                response.json().then(json=>{
                    li.innerHTML = `Error getting column info: ${json.error}`;
                }).catch(err => {
                    li.innerHTML = `Error parsing column info: ${err}`;
                })
                list.appendChild(li);
            }
        })
        
    }
    </script>
</head>
<body onload="init()">
    <h1>Layer info</h1>
    <h2 id="tablename"></h2>
    <div id="bbox">waiting for table stats...</div>
    <ul id="columns"></ul>
    <a href="info.html">Back to layer overview</a>
</body>
</html>