<?php
  $page_title = 'Job Status Track';
  require_once('includes/load.php');
  // Checkin What level user has permission to view this page
  //page_require_level(1);
  
  $all_plans = find_all('plandetails');
  $all_articles = find_all('articledetails');
  $all_color = find_all('colordetails');
  $all_size = find_all('sizedetails');
  $all_stage = find_all('stagedetails');
  $fulldailyplan=join_dailyplan_table();

  $userid = current_user();
     $p_userid = $userid['id'];
     $p_usertype=$userid['user_level'];

?>
<?php
if(isset($fulldailyplan)) {
    $i=0;
    $statustracker=[];
    foreach($fulldailyplan as $fplan) {
        //echo $fplan['PlanId'];die();
        $sapcode=$fplan['SAPCode'];
        $allstagecomplete=0;
        $trackdetails=findstatusdetails($fplan['PlanId'],$fplan['JobNo']);
        foreach($trackdetails as $trackst) {
          if($trackst['StageId']==8 && $trackst['Status']==1 && $trackst['DailyPlanId']==$fplan['Id']) {
            $allstagecomplete=1;
          }
        }
        $newstageId='';
        $newstageName='';
        if(!empty($trackdetails)) {
          $stage=$trackdetails[0]['StageId'];
          $nextstagedetails=findnextstage($sapcode,$fplan['PlanId'],$fplan['JobNo']);
          if(isset($nextstagedetails[0])) {
          $newstageId=$nextstagedetails[0]['nextstage'];
          $newstageName=$nextstagedetails[0]['StageName'];
          //echo $newstageId."--".$newstageName;die();
          } 
        } 
        if($allstagecomplete!=1) {

           $articlestagedetails=articlestagedetails($sapcode,1);
           //print_r($articlestagedetails);die();
           foreach($articlestagedetails as $artstage) {
            $finalstage=$artstage['StageId'];
            if($newstageId!='') {
              $finalstage=$newstageId;
            }
            if($p_usertype > 1 ) {
            if($finalstage==$p_usertype) {
            $statustracker[$i]['Id']=$fplan['Id'];
            $statustracker[$i]['PlanId']=$fplan['PlanId'];
            $statustracker[$i]['PlanName']=$fplan['PlanName'];
            $statustracker[$i]['TotalQty']=$fplan['CurrentPlan'];
            $statustracker[$i]['JobNo']=$fplan['JobNo'];
            $statustracker[$i]['SAPCode']=$fplan['SAPCode'];
            if($newstageId=='') {
              $statustracker[$i]['Stage']=$artstage['StageId'];
              $statustracker[$i]['StageName']=$artstage['Stage'];
            }else {
              $statustracker[$i]['Stage']=$newstageId;
              $statustracker[$i]['StageName']=$newstageName;
            }
          }
        } else {
          $statustracker[$i]['Id']=$fplan['Id'];
          $statustracker[$i]['PlanId']=$fplan['PlanId'];
          $statustracker[$i]['PlanName']=$fplan['PlanName'];
          $statustracker[$i]['TotalQty']=$fplan['CurrentPlan'];
          $statustracker[$i]['JobNo']=$fplan['JobNo'];
          $statustracker[$i]['SAPCode']=$fplan['SAPCode'];
          if($newstageId=='') {
            $statustracker[$i]['Stage']=$artstage['StageId'];
            $statustracker[$i]['StageName']=$artstage['Stage'];
          }else {
            $statustracker[$i]['Stage']=$newstageId;
            $statustracker[$i]['StageName']=$newstageName;
          }
        }
        $i=$i+1;
            //print_r($statustracker);die();
           }//die();
       // } else {
        //  $stage=$trackdetails[0]['StageId'];
         // $nextstagedetails=findnextstage($sapcode,$fplan['PlanId'],$fplan['JobNo']);
        //  print_r($nextstagedetails);
        //  echo $stage; die();
        //}
          }
        
    }
}
//print_r($statustracker);die();



 if(isset($_POST['SPlanId'])){

    $catid  = remove_junk($db->escape($_POST['SPlanId']));
    $fullplandetails = join_salesplan_table_plan($catid);
    
    header('Content-Type: application/json');
    echo json_encode($fullplandetails);
    exit;
    
    }

if(isset($_GET['excel'])) {
  
  $planid=$_GET['PlanId'];

  $results=join_salesplan_table_plan_export($planid);

  $tasks = array();
  while( $rows = mysqli_fetch_assoc($results) ) {
  $tasks[] = $rows;
  }

  $filename = "MM_Reqirement_Plan_export_".date('Ymd') . ".xls";
      header("Content-Type: application/vnd.ms-excel");
  header("Content-Disposition: attachment; filename=\"$filename\"");
  ExportFile($tasks);
  //print_r($tasks);
  //echo "yes";die();

}


?>
<?php include_once('layouts/header.php'); ?>

  <div class="row">
     <div class="col-md-12">
       <?php echo display_msg($msg); ?>
     </div>
  </div>
  
   <div class="row">
    <div class="col-md-12">
    <div class="panel panel-default">
      <div class="panel-heading">
        <strong>
          <span class="glyphicon glyphicon-th"></span>
          <span>All Daily Plans Status</span>
       </strong>
      </div>
        <div class="panel-body">
          <table id="plantable" class="table table-bordered table-striped table-hover">
            <thead>
                <tr>
                    <th class="text-center" style="width: 50px;">#</th>
                    <th>Job NO:</th>
                    <th>Plan Number</th>
                    <th>Plan Name</th>
                    <th>Article</th>
                    <th>Total Quantity</th>
                    <th>Current Stage</th>
                    <!--<th class="text-center" style="width: 100px;">Status</th>-->
                    <th class="text-center" style="width: 100px;">Actions</th>
                </tr>
                
            </thead>
            <tbody>
              <?php foreach ($statustracker as $pla):?>
                <tr>
                    <td class="text-center"><?php echo count_id(); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['JobNo']))?></td>
                    <td><?php echo remove_junk(ucfirst($pla['PlanId'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['PlanName'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['SAPCode'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['TotalQty'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['StageName'])); ?></td>
                  
                    <!--<td><?php //if($pla['Status']==1) { echo "Completed"; } else { ?>
                      <a href="plan.php?complete=1&PlanId=<?php //echo (int)$pla['PlanId'];?>" class="btn btn-success btn-xs"  title="Complete" data-toggle="tooltip">
                      Mark as completed
                    </a>-->
                      <?php// }; ?></td>
                    <td class="text-center">
                      <div class="btn-group">
                        <a href="StatusTrackEdit.php?DailyId=<?php echo (int)$pla['Id'];?>"  class="btn btn-xs btn-warning" data-toggle="tooltip" title="View">
                          <span class="glyphicon glyphicon-eye-open"></span>
                        </a>
                        <!--<a href="plan.php?excel=1&PlanId=<?php //echo (int)$pla['PlanId'];?>" class="btn btn-info btn-xs"  title="Edit" data-toggle="tooltip">
                      <span class="glyphicon glyphicon-download-alt"></span>
                    </a>-->
                        <!--<a href="DailyPlanDelete.php?DailyId=<?php echo (int)$pla['Id'];?>"  class="btn btn-xs btn-danger" data-toggle="tooltip" title="Remove" onclick="return confirm('Are you sure you want to delete this item?');">
                          <span class="glyphicon glyphicon-trash"></span>
                        </a>-->
                      </div>
                    </td>

                </tr>
              <?php endforeach; ?>
            </tbody>
          </table>
       </div>
    </div>
    </div>
   </div>
  </div>
  <script type="text/JavaScript"> 
  function plandetailsselect() {
      var Catvalue=document.getElementById('PlanId').value;
      console.log(Catvalue);

      $.ajax({
    url      : 'DailyPlan.php',
    data     : {'SPlanId':Catvalue},
    dataType: 'json',
    type     : 'POST',
    success  : function(Result){
              console.log(Result[0]);
              //var myObj = $.parseJSON(Result);
              var myObj =Result;
              $("#Artno").empty();
              $('#Artno').append('<option value="">Select Article </option>');
              $("#Color").empty();
              $('#Color').append('<option value="">Select Color </option>');
              $("#Size").empty();
              $('#Size').append('<option value="">Select Size </option>');
              //console.log(myObj);
              console.log(Object.keys(myObj).length);
              //you can now access data like this:
              //myObj.Address_1
              $.each(myObj, function (i, value) {
                $('#Artno').append('<option value=' + value.ArticleId + '>' + value.ArticleName + '</option>');
                $('#Color').append('<option value=' + value.StageId + '>' + value.StageName + '</option>');
                $('#Size').append('<option value=' + value.SizeId + '>' + value.SizeAssortment + '</option>');
                $('#iplan').val(value.CaseCount);
            });
        }
    }
  );
    
}
     </script>
  <?php include_once('layouts/footer.php'); ?>
