<?php 

    class docs_view
    {
        function docs_dashboard($token)
        {
            //return json object of doc dashboard 
            include 'helpers/clive.php';
           
            $dataslave = new dataslave();
            $clive = new clive();
            $dbn = $dataslave->install_pdo_docs();
            $token_data =explode(';',$token);
            $user_role=$token_data[1];
            $user_id='';
            $lw_id=$token_data[0];
            $doc_dashboard='';
            $docs_query='';
            $query_token ='';
            $docs_html ='';

            //construct usage bar 

            $usage_bar_meter = ($clive->total_space_used_lw($lw_id)/2000)*100;
            $usage_bar_info = $clive->total_space_used_lw($lw_id);

             $docs_query = $dbn->prepare('SELECT * FROM documents_registry WHERE lw_id=:lID');
             $docs_query->execute(array("lID"=>14));

             while($f=$docs_query->fetch(PDO::FETCH_ASSOC))
             {
                echo 1;
             }
            if($user_role=='lw')
            {
                //if user is lawyer fetch all documents with lw ID
                $docs_query = $dbn->prepare('SELECT * FROM documents_registry WHERE lw_id=:lID');
                $docs_query->execute(array(':lID'=>$lw_id));
                $query_token =base64_encode($lw_id.';lw');
            }
            if($user_role=='client')
            {
                $docs_query = $dbn->prepare('SELECT * FROM documents_registry WHERE client_id=:cID');
                $docs_query->execute(array(':cID'=>$token_data[0]));
            }
            while($doc = $docs_query->fetch(PDO::FETCH_ASSOC))

            {  
                $cipher ="AES-128-CTR";
                
                $ivlen = openssl_cipher_iv_length($cipher);

                $iv = "1234567891011121";
                $docID = $doc['doc_id'];
                $key="projectA2021";
                $options = 0 ;
                $doc_id_enc =openssl_encrypt($docID,$cipher,$key,$options,$iv);
               
                $doc_type=explode('/',$doc['doc_type']);
               
                $href='https://documents.khambers.com/documents/'.$doc['lw_id'].$doc['doc_name'].'.'.$doc_type[1];
               
                $doc_name = str_split($doc['doc_name'],10);
              
                $docs_html .="<div class='single_doc_container'>
               
                <span class='single_doc fa fa-file'></span>
        
                <div class='single_doc_btns'>
                <span class='single_file_name'>".$doc_name[0]."...</span>
                <br />
              
                
                <a class='doc_download fa fa-download' href='".$href."' target='_blank' download='".$doc['doc_name']."'></a>
                <a href='https://www.khambers.com/v/single_doc_view?d=".$doc_id_enc."' target='_blank' class='view_doc fa fa-eye'></a>
                <script>
              //  $('.doc_download').click(function()
              //  {
             //       var data ={token:'".base64_encode($doc['doc_name'].";".$doc['doc_id'])."',download_doc:1};
             //       $.post('https://documents.khambers.com/docs.php',data,function(response)
             //       {
                        
             //       }
             //   );
             //   });
                </script>
                <span class=' delete_doc fa fa-trash'></span>
                </div>
        
                <div class='client_name'>";
                if($user_role=='lw')
                {
                    $docs_html .=$clive->get_client_name($doc['client_id'])."</div>";
                }
                else
                {
                    $docs_html .=$clive->get_lw_name($doc['lw_id'])."</div>";
                }
                $docs_html.="
            </div>";
            }

            //build  docs dashboard
            $search_script="<script>
            $('.search').keyup(function(){
                var query = $(this).val();
                var tk ='".$query_token."';
                var data  = {q:query,token:tk,search:1};
                $.post('https://documents.khambers.com/docs.php',data,function(search_results_json)
                {
                    
                    var results = $.parseJSON(search_results_json);
                    $('.single_docs_container').empty();
                    $('.single_docs_container').append(results.results);
                });
            });
            </script>";
            $doc_dashboard="<div class='docs_view_container'>
            <form class='direct_file_upload_form'>

            <input type='file' id='single_file' />
           
            </form>
            <script>
            $('#single_file').change(function()
			{
				 $('.direct_file_upload_form').submit();
			

			});
            $('.direct_file_upload_form').submit(function(){
           
            var form = new FormData();
            var file = document.getElementById('single_file');

            form.append('doc_file',file.files[0]);
            form.append('single_direct_upload',1);
            form.append('cookie',cookie);
            var request1 = new XMLHttpRequest();
            request1.open('POST','https://documents.khambers.com/docs.php');
            request1.send(form);
            $(this).hide();
            

           return false;
            });
            </script>
            <input type='text' class='docs_search_box search' placeholder='Search Documents' />
        
            <div class='single_docs_container'>";
            
            $doc_dashboard .=$docs_html;
            //close single_docs_container div tag
            $doc_dashboard.="</div>";

            //clos docs_view _container
            $usage_container ="	<div class='space_usage_container'>";
            if($user_role=='lw')
            {
                $usage_container .="<div class='usage_bar_container'>
                <span class='usage_bar_filler' style='background-color:lightblue; width:".$usage_bar_meter."%;'></span>
                <span class='usage_info'>".round($usage_bar_info,2)."MB/2G</span>
            </div>";
            }
            $usage_container .=" <span class='doc_upload'>
                <span class=' fa fa-cloud-upload'></span>
                Upload File
            </span>
            <script>
            $('.doc_upload').click(function(){
                $('.direct_file_upload_form').show();
            });
            </script>
        </div>";
    
           



        $doc_dashboard .=$usage_container;
        $doc_dashboard .=$search_script;
        //close dahsboard container 
        $doc_dashboard .="</div>";

        //echo dahsboard as json for cleint code 
        echo json_encode(array('docs_dasboard'=>12));
        }

        function search()
        {
            include 'helpers/clive.php';
            $clive = new clive();
            $dataslave = new dataslave();
            $dbn = $dataslave->install_pdo_docs();
            $token = base64_decode($_POST['token']);
            $token_data = explode(';',$token);
            $user_role = $token_data[1];
            $docs_html='';
            $docs_query='';
            $lw_id=$token_data[0];
            if($user_role=='lw')
            {
                $docs_query = $dbn->prepare('SELECT * FROM documents_registry WHERE doc_name LIKE ?');
                $docs_query->execute(array($_POST['q'].'%'));
            }
            elseif($user_role=='client')
            {
                $docs_query = $dbn->prepare('SELECT * FROM documents_registry WHERE name LIKE :query AND client_id=:lID');
            }

            while($doc = $docs_query->fetch(PDO::FETCH_ASSOC))
            {
                $doc_name = str_split($doc['doc_name'],10);
                if($doc['lw_id']!=$lw_id)
                {
                    continue;
                }
                else 
                {
                        $docs_html .="<div class='single_doc_container'>
                <span class='single_doc fa fa-file'></span>
        
                <div class='single_doc_btns'>
                <span class='single_file_name'>".$doc_name[0]."...</span>
                <br />
                <span class=' share_doc fa fa-share-square'></span>
                <span class='doc_download fa fa-download'></span>
                <span class=' delete_doc fa fa-trash'></span>
                </div>
        
                <div class='client_name'>".$clive->get_client_name($doc['client_id'])."</div>
            </div>";
                }
            
            }

            echo json_encode(array('results'=>$docs_html));
        }
    }
?>