<?php //header("location: viewer.php?doc=dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXRqbGprN2huM2dpdmNpemR6d2NiaGhzbHk4NHB4MTZfdHV0b3JpYWxfYnVja2V0L0NBMzAyLm53ZA=="); 
include_once 'auth.php';
?>

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    
    <meta http-equiv="cache-control" content="max-age=0" />
    <meta http-equiv="cache-control" content="no-cache" />
    <meta http-equiv="expires" content="0" />
    <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
    <meta http-equiv="pragma" content="no-cache" />
    <!-- Third Party package -->
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
	<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
	<script src="https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js"></script>
	
    <!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-144934238-2"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-144934238-2');
</script>


    <!-- The Viewer JS -->

    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/feather-icons/4.9.0/feather.min.js"></script>
     <script src="pretty-doughtnut.js"></script>
    <script src="https://www.chartjs.org/samples/latest/utils.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.js"></script>
    
    
<script src="selection.js"></script>
    
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=no" />
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

    <title>Smart Plant 3D</title>
	
	<style>
	
	.centered {
  position: fixed;
  top: 50%;
  left: 50%;
  /* bring your own prefixes */
  transform: translate(-50%, -50%);
}
	
	#loading {
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  position: fixed;
  display: block;
  opacity: 0.7;
  background-color: #fff;
  z-index: 99;
  text-align: center;
}

#loading-image {
  position: absolute;
  top: 100px;
  left: 240px;
  z-index: 100;
}

	
.h5text{
    display: block;
  white-space: nowrap;
  width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
}
	
	.sidebar .nav-link {
  font-weight: 500;
  color: #333;
}

.sidebar .nav-link .feather {
  margin-right: 4px;
  color: #999;
}

.sidebar .nav-link:hover {
  color: #007bff;
}

.sidebar .nav-link:hover .feather,
.sidebar .nav-link.active .feather {
  color: inherit;
  
}

.my-attr-scrollbar {
position: relative;
max-height: calc(100vh - 56px);
width:100%;
overflow: auto;
}

.table-wrapper-scroll-y {
display: block;
}
	
.clearfix:before,.clearfix:after {content: " "; display: table;}
.clearfix:after {clear: both;}
.clearfix {*zoom: 1;}
            
            
	</style>
	 <link rel="stylesheet" href="css/circle.css">

  </head>
  <body>
    
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>



	<!-- nav bar --> 
	<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <a class="navbar-brand" href="#">
     Smart Plant 3D
	</a>
	
</nav>

<div class="container-fluid">

<div class = "row">
<nav class ="col-md-2 d-none d-md-block bg-light sidebar" id= "sidebar">
<div class="sidebar-sticky" style = "height:calc(100vh - 56px)">
        <ul class="nav flex-column">
          <li class="nav-item">
              
            <a class="nav-link " href="#Files">
                <i class="fa fa-file-text-o" aria-hidden="true"></i>
              Files
            </a>
          </li>
          <!--<li class="nav-item">
            <a class="nav-link" href="#Reports">
                <i class="fa fa-pie-chart" aria-hidden="true"></i>
              Reports
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#Data">
                <i class="fa fa-table" aria-hidden="true"></i>
              Data
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">
              User
            </a>
          </li>-->
          
        </ul>
      </div>
</nav>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4 my-attr-scrollbar">

<div id="Files" class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">Files</h1>
        
      </div>
      
      
      
	  <nav aria-label="breadcrumb">
  <ol class="breadcrumb bg-Light" >
    <li class="breadcrumb-item active" aria-current="page">Auto Generated Files Based on Attribute "MatMan CWP"</li>
  </ol>
</nav>

<div id="loading">
    <i class="centered fa fa-5x fa-spin fa-spinner" aria-hidden="true" alt="Loading..."></i>
  
</div>

<div class="list-group" id="files">
  <!-- 
  <a href="viewer.php?doc=dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXRqbGprN2huM2dpdmNpemR6d2NiaGhzbHk4NHB4MTZfdHV0b3JpYWxfYnVja2V0L09NQS0wMDQtMDAxLUFFQy1BLUJJTS0wMC0wMDAxLm53Yw==" class="list-group-item list-group-item-action">MOO - Level 01 Wall Guards</a>
  <a href="viewer.php?doc=dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXRqbGprN2huM2dpdmNpemR6d2NiaGhzbHk4NHB4MTZfdHV0b3JpYWxfYnVja2V0L0JPSF9HdWFyZHMubndk" class="list-group-item list-group-item-action">MOO - Level 02 Wall Guards</a>
  <a href="viewer.php?doc=dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6ZXRqbGprN2huM2dpdmNpemR6d2NiaGhzbHk4NHB4MTZfdHV0b3JpYWxfYnVja2V0L09NQS0wMDQtMDAxLUFFQy1BLUJJTS0wMC0wMDAzLm53Yw==" class="list-group-item list-group-item-action">MOO - Level 03 Wall Guards</a>
  -->

</div>



</main>


</div>


</div>
  </body>



<script>
var data1 = [];
var data2 = [];
var grouped;
var grouped1;
var filesList = document.getElementById("files");
    window.onload = (event) => {
         console.log("Start getting data");
$.ajax({
    url: 'https://developer.api.autodesk.com/modelderivative/v2/designdata/dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6bmV0X2NjY19hcHBzX3dmcC9jd2ElMjA1Mi0wLm53Yw/metadata/0a0d270d-0d30-4f1d-b41c-8b2e41085704/properties?forceget=true',
    headers: {
        'Authorization': <? echo "'Bearer ". json_decode($data,true)[access_token] . "'" ;?>
    },
    method: 'GET',
    dataType: 'json',
    data: "",
    success: function(data){
        console.log(data);
        
        var i = 0;
        data.data.collection.forEach(ele => {
            try{
                if(ele.properties["DWF - SmartPlant 3D"]["CWP"] != "" && ele.properties["DWF - SmartPlant 3D"]["CWP"] !=undefined){
                    data1.push({cwp : ele.properties["DWF - SmartPlant 3D"]["CWP"] , id : ele.objectid});
                    data2.push({cwp : ele.properties["DWF - SmartPlant 3D"]["CWP"] , id : ele.externalId});
                }
            }catch(err){}
            
            if(i == data.data.collection.length -1){
                 grouped = _.mapValues(_.groupBy(data1, 'cwp'),
                          clist => clist.map(matman => _.omit(matman, 'cwp')));
                          grouped1 = _.mapValues(_.groupBy(data2, 'cwp'),
                          clist => clist.map(matman => _.omit(matman, 'cwp')));
                          //console.log(grouped);
                          var j =0;
                Object.entries(grouped).forEach(ele =>{
                    filesList.innerHTML = filesList.innerHTML + 
                    '<a href="#" onclick = "loadModel(\'' + ele[0] + '\',' + j + ')' + '" class="list-group-item list-group-item-action">' + ele[0] + '<span class="pull-right">(' + ele[1].length + ')</span></a>';
                    j++;
                })
                       var loading =    document.getElementById("loading");
                       loading.style.display = "none";
                
            }
            i++;
        });
      //console.log(data);
    },
    error:function(err){
        
        //reload
        location.reload();
        //console.log(err);
    }
  });
  
  $.ajax({
    url: 'https://developer.api.autodesk.com/modelderivative/v2/designdata/dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6bmV0X2NjY19hcHBzX3dmcC9jd2ElMjA1Mi0wLm53Yw/metadata/0a0d270d-0d30-4f1d-b41c-8b2e41085704',
    headers: {
        'Authorization': <? echo "'Bearer ". json_decode($data,true)[access_token] . "'" ;?>
    },
    method: 'GET',
    dataType: 'json',
    data: "",
    success: function(data){
        console.log(data);
    },
    error:function(err){
        //reload
        location.reload();

    }
  });
  
  
    }
/*
    var data = [];
    window.onload = (event) => {
        console.log("Start getting data");
    getFilesByAttrib().then((getdata) => {
        // add spinner layout
        var i =0;
        getdata.data.data.collection.forEach(ele => {
            try{
                if(ele.properties["DWF - SmartPlant 3D"]["MatMan CWP"] != "" && ele.properties["DWF - SmartPlant 3D"]["MatMan CWP"] !=undefined){
                    data.push({cwp : ele.properties["DWF - SmartPlant 3D"]["MatMan CWP"] , id : ele.objectid});
                }
            }catch(err){}
            
            if(i == getdata.data.data.collection.length -1){
                var grouped = _.mapValues(_.groupBy(data, 'cwp'),
                          clist => clist.map(matman => _.omit(matman, 'cwp')));
                          console.log(grouped);
                
            }
            
        })
        
        
    });
    }
    */
    
    
    function loadModel(model,index){
        var ids =[];
        var i =0;
        console.log(ids);
        Object.entries(grouped)[index][1].forEach(ele => {
           ids.push(ele.id + 1 );
           if(i == Object.entries(grouped)[index][1].length -1){
               localStorage.setItem(model,JSON.stringify(ids));
               window.open('viewer?doc=dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6Z3JoZHVwaWhiM3dhYnBwanVoMGg2bDNhb3plZWhnZmRfdHV0b3JpYWxfYnVja2V0LzUyLm53Yw==&model=' + model, '_blank'); 
           }
           i++;
        });
    }
    
    window.onunload = () => {
   // Clear the local storage
   window.localStorage.clear();
}

function download_csv() {
    var csv = '';
    mydata.forEach(function(row) {
            csv += row.join(',');
            csv += "\n";
    });
 
    console.log(csv);
    var hiddenElement = document.createElement('a');
    hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
    hiddenElement.target = '_blank';
    hiddenElement.download = 'data.csv';
    hiddenElement.click();
}


</script>

		
		
</html>