<?php
include('../headertop.php');
include('../db.php');
//include('../datepicker.php');

$disp = '';
$pagetitle = 'View Estimates';
date_default_timezone_set('Asia/Kolkata');

$project = 1;


if(empty($project))
{
echo '<meta http-equiv="refresh" content="0;URL=estimates.php?sm=Select a project first to view its estimates">';
exit;
}

$q = "select distinct work_type_id from estimates where project_id='$project'";
//$q = "select estimate, work_type_id, work_type_name from estimates where project_id='$project'";
$q2 = "select sum(estimate) as total_estimate, project_name from estimates where project_id='$project'";

$res = mysqli_query($dbcon,$q) or die(mysqli_error($dbcon));
if(mysqli_num_rows($res)<1)
{
$disp .= 'No results found';
include('../template.php');
exit;
}

$res2 = mysqli_query($dbcon,$q2) or die(mysqli_error($dbcon));
$row2 = mysqli_fetch_array($res2);

$disp .= '
<div class="row">
                <div class="col-md-8"><button class="btn btn-primary" style="margin-bottom:15px">Total Estimated Cost: '.$row2['total_estimate'].'</button>
				</div></div>
<div class="row">
                <div class="col-md-12">
				<form role="form" id="estimations" method="post" action="editestimates2.php" >
				<input type="hidden" name="project" value="'.$project.'">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <b>Cost Estimations for Project: '.UCFirst($row2['project_name']).' </b>
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                    <thead>
                                        <tr>
                                            <th>Work Type</th>
											<th>Estimated Cost</th>
										    
                                        </tr>
                                    </thead>
                                    <tbody>';

while($row = mysqli_fetch_array($res))
{
$q3 = "select id, estimate as full_estimate, work_type_name from estimates where project_id='$project' and work_type_id='".$row['work_type_id']."'";
$res3 = mysqli_query($dbcon,$q3) or die(mysqli_error($dbcon));
$row3 = mysqli_fetch_array($res3);

$esn = 'estimate['.$row3['id'].']';

$disp .= '<tr class="odd gradeX"><td>'.$row3['work_type_name'].'</td><td><input type="text" name='.$esn.' value="'.$row3['full_estimate'].'"></td></tr>';
}

$disp .='            </tbody>
                                </table>
                            </div>
                            
                        </div>
						
						
                    </div>
                    <!--End Advanced Tables -->
					
					<button type="submit" class="btn btn-warning">Edit Estimates for this Project</button> &nbsp; <a  href="deleteestimates.php?project='.$project.'" class="btn btn-warning" onclick="return confirm(\'Are you sure you want to delete all estimates for this project?\')">Delete All Estimates for this Project</a>
                </div>
            </div>';
			
include('../header.php');

?>

<script>
            $(document).ready(function () {
                $('#dataTables-example').dataTable();
            });
			
			//$('#dataTables-example').dataTable({"pageLength":100});
			//$('#dataTables-example').dataTable({"paging":false);
    </script>
    
<?php
include('../template.php');
