<?php
include('../../includes/checkSession.php');
include('../../includes/con2pma3.php');

$id= $company_id = $_SESSION['id'];
$expand_id=$_REQUEST['expand_id'];

?>

<html><head><meta charset="utf-8"><title>pmAnywhere - Scope</title>
<link href="03-00Scope-Styles0.css" rel="stylesheet" type="text/css" />
<script src="../includes/js/jquery.min.js"></script>

</head>

<div><br></div>
	<style>
        .imgbtn{width:14px; height:14px; border:none; padding: 0}
        .es{display:none}
    </style>
<div style="overflow-x:auto;">
<div id="formdiv" >

  <fieldset>
<legend align="center">REVIEW/ COMMENTS/ ACTIONS</legend>
      <?php
      $result = mysql_query("SELECT * FROM scope_dwg  where dwgID ='$expand_id'");
      $row = mysql_fetch_array($result, MYSQL_ASSOC);
      $dwgTitle = $row['dwgList_Title'];
      $dwgProject = $row['dwgProject'];
      $dwgType = $row['dwgType'];
      $dwgcomplePercent = $row['dwgcomplePercent'];
      $designType = $row['dwg_type'];
      $dwgConsultant = $row['dwg_consultantID'];
      $date = $row['dwgDateCreated'];
      
      ?>
<table name="entryform" align="center" class="table3" width="80%" border="0" cellpadding="0">
    <tr>
      <td width="10%" align="left" valign="top"><strong>Project Code</strong></td>
      <td width="22%" align="left" valign="top"><input type="text" name="Project" value="<?php
          $result = mysql_query("SELECT projectSerial,projectCode, projectName FROM projects where companySerial = '$id' AND projStatus=1 AND projectSerial='$dwgProject' ");
          if($rows = mysql_fetch_array($result, MYSQL_ASSOC)) {
                  echo $rows["projectCode"] . " - " . $rows["projectName"];

          } ?>" readonly style="background-color:gainsboro"></td>
      <td width="10%" align="left" valign="top"><strong>DWG Set Title</strong></td>
      <td width="22%" align="left" valign="top"><input type="text" name="Project" value="<?php echo $dwgTitle ?>" readonly style="background-color:gainsboro"></td>
      <td width="10%" align="left" valign="top"><strong>Related SOW</strong></td>
      <td width="23%" align="left" valign="top"><input type="text" name="handover" value="<?php
          $resultSW = mysql_query("SELECT sowName FROM scope_sow WHERE sowID='".$row['dwg_sowID']."'");
          if($rowSW = mysql_fetch_array($resultSW, MYSQL_ASSOC)){
                $SOW = $rowSW['sowName'];
                  echo $SOW;
          }
          ?>" readonly style="background-color:gainsboro"></td>
      <td width="3%" align="left" valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td align="left" valign="top"><strong>Type</strong></td>
      <td align="left" valign="top"><input type="text" name="Project" value="<?php echo $dwgType; ?>" readonly style="background-color:gainsboro"></td>
      <td align="left" valign="top" class="table1"><strong>Stage</strong></td>
      <td align="left" valign="top" class="table1"><input type="text" name="plantitle" value="<?php echo $dwgcomplePercent; ?> Percent" readonly style="background-color:gainsboro"></td>
      <td align="left" valign="top"><strong>of</strong></td>
      <td align="left" valign="top"><input type="text" name="planid" value="<?php echo $designType = $row['dwg_type']; ?>" readonly style="background-color:gainsboro"></td>
      <td align="left" valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td align="left" valign="top"><strong>Consultant</strong></td>
      <td align="left" valign="top"><input type="text" name="Project" value="<?php
          $resultC = mysql_query("SELECT conSerial,conContactName,conDepartment,conDesignation FROM consultants WHERE companySerial = '$id' AND
                                          conStatus=1 AND conSerial = '$dwgConsultant'");

          if ($rowC = mysql_fetch_array($resultC, MYSQL_ASSOC)) {
              echo $rowC["conContactName"]." ";
          }
          ?>" readonly style="background-color:gainsboro"></td>
      <td align="left" valign="top" class="table1"><strong>Date</strong></td>
      <td align="left" valign="top" class="table1"><input type="text" name="datetime" value="<?php echo $date; ?>" readonly style="background-color:gainsboro"></td>
      <td align="left" valign="top"><strong>Proponent</strong></td>
      <td align="left" valign="top"><input type="text" name="Proponent" value="Nothing to display" readonly style="background-color:gainsboro"></td>
      <td align="left" valign="top">&nbsp;</td>
    </tr>
</table>


<input id="edit_id" name="edit_id" type="hidden" value="<?php echo $_REQUEST['expand_id']; ?>">
  </fieldset>
  </div>
</div>
<table width="100%" border="0" cellpadding="0" align="center">
  <tbody>
    <tr>
      <td align="right"><a href="03-06Scope-01Drawings01.php"><img src="img/deletecb.png" width="12" height="12" alt=""/></a></td>
    </tr>
  </tbody>
</table>
<table width="90%" border="0" align="center" class="table2" cellpadding="0">
  <tbody>
    <tr bgcolor="black">
      <td width="3%" ><span bgcolor="black" STYLE="color: gainsboro; font-size: 10pt" class="subTitle">#</span></td>
      <td width="6%" ><span bgcolor="black" STYLE="color: gainsboro; font-size: 10pt" class="subTitle">ISSUED</span></td>
      <td width="10%" ><span STYLE="color: gainsboro; font-size: 10pt" class="subTitle">Discipline</span></td>
      <td width="10%" ><span STYLE="color: gainsboro; font-size: 10pt" class="subTitle">SUFFIX/DWG</span></td>
      <td width="11%" ><span STYLE="color: gainsboro; font-size: 10pt" class="subTitle">SHEET/REV</span></td>
      <td width="17%" ><span STYLE="color: gainsboro; font-size: 10pt" class="subTitle">DWG TITLE</span></td>
      <td width="17%" ><span STYLE="color: gainsboro; font-size: 10pt" class="subTitle">DWG SUB-TITLE</span></td>
      <td width="15%" ><span STYLE="color: gainsboro; font-size: 10pt" class="subTitle">FILENAME</span></td>
     <!-- <td width="10%" ><span class="subTitle">ACTIONS</span></td>  -->
    </tr>
    <?php
    $DID = $expand_id;
    $reserved_sheets = array();
    $reserved_rev = array();
    $query = "SELECT * FROM scope_dwg_revisions WHERE dwg_id =$DID";
    $result = mysql_query($query);
	$range = mysql_num_rows($result);
    while ($rr = mysql_fetch_array($result)) {
        array_push($reserved_sheets, $rr['sheet_no']);
        array_push($reserved_rev, $rr['rev_no']);
    }

    //getting dynamic ranges of dwg doc
    $drawingRange = array();
    $rangeQ = "SELECT * FROM scope_dwg_range WHERE dwgID = '$DID' AND companyID = '$company_id' AND status = '1'";
    $rangeRes = mysql_query( $rangeQ );
    while( $rangeRow = mysql_fetch_assoc($rangeRes)){
        $dRange = range($rangeRow[range_from],$rangeRow[range_to]);
        $dRange = preg_filter('/^/', $rangeRow[dwg_suffix].'_', $dRange);  //appending prefix with dwg suffix

        $drawingRange = array_merge(  $drawingRange, $dRange );
    }


$serial =0;
//    for ($i = $row['range_from']; $i <= $row['range_to']; $i++) {
    foreach ($drawingRange as $range) {

        $rangeSuffix = explode( '_', $range);       //splitting range no and suffix
        $suffix = $rangeSuffix[0];
        $i = $rangeSuffix[1];
    $query = "SELECT * FROM scope_dwg_revisions WHERE dwg_id =$DID AND range_no=$i order by sheet_no, rev_no ";
    $result = mysql_query($query);
	$have_rows = mysql_num_rows($result);
    //$rew = mysql_fetch_object($result);

    if ($have_rows) {
		while($rew = mysql_fetch_object($result)){
			$dwgDiscipline = $rew->discipline;
    ?>

        <!-------------------------------------------------- Show DWG Row --------------------------------------------------->
    <tr bgcolor="#D4D4D4" class="rs read_section<?php echo $rew->id ?>">
      <td valign="middle" bgcolor="#D4D4D4"><strong><?php echo ++$serial; ?></strong></td>
      <td valign="middle" bgcolor="#D4D4D4"><strong><?php echo $rew->dwg_idate ?></strong></td>
      <td valign="middle" bgcolor="#D4D4D4">          <strong>
        <?php
          $resultD = mysql_query("SELECT discipline_name,discipline_code FROM discipline where discipline_company_id = '$id' 
                                          AND discipline_status=1 AND discipline_id='$dwgDiscipline' ");

          if ($rowD = mysql_fetch_array($resultD, MYSQL_ASSOC)) {
              echo $rowD['discipline_name'];
          }
          ?>
          
                                            </strong></td>
      <td valign="middle" bgcolor="#D4D4D4"><strong><?php echo $suffix .  " - ". $i; ?></strong></td>
      <td valign="middle" bgcolor="#D4D4D4"><strong><?php echo str_pad($rew->sheet_no, 3, '0', STR_PAD_LEFT) . "-" . str_pad($rew->rev_no, 3, '0', STR_PAD_LEFT); ?>
      </strong></td>
      <td valign="middle" bgcolor="#D4D4D4"><strong><?php echo $rew->dwg_title ?></strong></td>
      <td valign="middle" bgcolor="#D4D4D4"><strong><?php echo $rew->dwg_title2 ?></strong></td>
      <td valign="middle" bgcolor="#D4D4D4">
          <strong>
          <input type="hidden" name="old_filename"  value="<?php echo $rew->attachment ?>"/>
          <?php if ($rew->attachment != "") { ?>
              <a href="uploads/<?php echo $rew->attachment ?>" target="_blank" ><?php echo $rew->attachment ?></a> 
              <?php } ?>
      </strong></td>

      </tr>

    <tr>
      <td colspan="8" valign="middle">
          <?php
          $commentQ = "SELECT * FROM scope_dwg_comments WHERE revision_id = '$rew->id' AND dwg_id = '$expand_id' AND status = '1' AND companyID = '$id'";
          $commentRes = mysql_query( $commentQ );
          $num_rows = mysql_num_rows($commentRes);
          if($num_rows > 0){?>
      <br>

          <table width="97.5%" class="table6" border="0" align="right">
              <tr>
                  <th width="5%" align="left">CommentID</th>
                  <th width="10%" align="left">CommentType</th>
                  <th width="10%" align="left">Discipline</th>
                  <th width="15%" align="left">ActionType</th>
                  <th width="15%" align="left">Subject</th>
                  <th width="20%" align="left">Details</th>
                  <th width="10%" align="left">PostedBy</th>
                  <th width="10%" align="left">PostedAt</th>

              </tr>
              <?php }
              while( $commentRow = mysql_fetch_assoc( $commentRes )){

                  $revisionID = $commentRow['revision_id'];
                  $commentID = $commentRow['comment_id'];
                  $commentType = $commentRow['comment_type'];

                  $discipline = $commentRow['discipline'];
                  $disQ = "SELECT discipline_name FROM discipline WHERE discipline_id = '$discipline'";
                  $disRes = mysql_query( $disQ );
                  $disRow = mysql_fetch_assoc( $disRes );
                  $disciplineName = $disRow['discipline_name'];

                  $actionType = $commentRow['action_type'];
                  $subject = $commentRow['subject'];
                  $details = $commentRow['details'];
                  $createdBy = $commentRow['created_by'];
                  $creationDateTime = $commentRow['creation_date'];
                  ?>

                  <tr class="commentsRow">
                      <td align="left"><?php echo $commentID ; ?></td>
                      <td  align="left"><?php echo $commentType; ?></td>
                      <td align="left" ><span class="disciplineName"><?php echo $disciplineName;     ?></span></td>
                      <td align="left"><span class="actionType"><?php echo $actionType; ?></span></td>
                      <td align="left"><?php echo $subject; ?></td>
                      <td align="left"><?php echo $details; ?></td>
                      <td align="left"><span class="reviewer"> <?php echo $createdBy;  ?></span></td>
                      <td align="left"><?php echo $creationDateTime; ?></td>

                  </tr>



                  <?php
              }

              if ($num_rows > 0){
              ?>
          </table>
      <?php } ?>
          <br>
      </td>
    </tr>


            <?php
        }
    }
    }
    ?>
</table>
<br><br>


</body>
</html>
