<?php
  $page_title = 'Daily Plans';
  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');
  $fulldailyplan=join_dailyplan_table_full();
  $fulldailyplan1=join_dailyplan_table();
?>
<?php
 if(isset($_POST['add_pla'])){
   $req_field = array('planid','jobno','artno','color','size','initialplan','plan');
   validate_fields($req_field);
   $pla_number = remove_junk($db->escape($_POST['planid']));
   $pla_number1 = remove_junk($db->escape($_POST['planid1']));
   $artnumber = remove_junk($db->escape($_POST['artno']));
   $artcolor = remove_junk($db->escape($_POST['color']));
   $artsize = remove_junk($db->escape($_POST['size']));
   $initialplan = remove_junk($db->escape($_POST['initialplan']));
   $currentplan = remove_junk($db->escape($_POST['plan']));
   $daydate = remove_junk($db->escape($_POST['daydate']));
   $jobno = remove_junk($db->escape($_POST['jobno']));
   //$daydate=date('Y-m-d',$daydate);
   $salesplandetials=find_by_id_custom("salesplandetails",$pla_number1,'PlanId');
   $sapcode=$salesplandetials['SAPCode'];
   //echo $pla_number;
   //print_r($salesplandetials);die();

   $sizefull=find_by_id_custom("sizedetails",$artsize,'SizeId');
   $four=$currentplan*$sizefull['Four'];
   $five=$currentplan*$sizefull['Five'];
   $six=$currentplan*$sizefull['Six'];
   $seven=$currentplan*$sizefull['Seven'];
   $eight=$currentplan*$sizefull['Eight'];
   $nine=$currentplan*$sizefull['Nine'];
   $ten=$currentplan*$sizefull['Ten'];
   $eleven=$currentplan*$sizefull['Eleven'];
   $twelve=$currentplan*$sizefull['Twelve'];
   $thirteen=$currentplan*$sizefull['Thirteen'];
   $fourteen=$currentplan*$sizefull['Fourteen'];

   //print_r($currentplan);die();
   $db= getDatabaseConnection();
  
   $userid = current_user();
     $p_userid = $userid['id'];
   if(empty($errors)){
      $sql  = "INSERT INTO dailyplandetails (PlanId,SalesPlanId,ArticleId,ColorId,SizeId,SAPCode,InitialPlan,CurrentPlan,PlanDate,JobNo,Four,Five,Six,Seven,Eight,Nine,Ten,Eleven,Twelve,UserId)";
      $sql .= " VALUES ({$pla_number1},{$pla_number},{$artnumber},{$artcolor},{$artsize},'{$sapcode}',{$initialplan},{$currentplan},'{$daydate}','{$jobno}',{$four},{$five},{$six},{$seven},{$eight},{$nine},{$ten},{$eleven},{$twelve},{$thirteen},{$fourteen},{$p_userid})";
      //echo $sql;die();
      if($db->query($sql)){
        $session->msg("s", "Successfully Added New Plan");
        redirect('DailyPlan.php',false);
      } else {
        $session->msg("d", "Sorry Failed to insert.");
        redirect('DailyPlan.php',false);
      }
   } else {
     $session->msg("d", $errors);
     redirect('DailyPlan.php',false);
   }
 }

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

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

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

    $catid  = remove_junk($db->escape($_POST['SPlanId']));
    $fullplandetails = join_salesplan_table_plan_by_id($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();

}

function getDatabaseConnection() {
    $mysqli = new mysqli("localhost", "root", "", "simplepr_ptpmanagementfortune");
    if ($mysqli->connect_errno) {
      die("Database connection failed: " . $mysqli->connect_error);
    }
    return $mysqli;
  }

?>
<?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>Add Daily Plan</span>
         </strong>
        </div>
        <div class="panel-body">
          <form method="post" action="DailyPlan.php">
            <div class="row">
            <div class="col-md-4">
            <div class="form-group">
            <label for="Planid">Plan</label> 
            <select id="PlanIdold" class="form-control" name="planidold" onChange="planselect()">
                      <option value="">Select Plan</option>
                    <?php  foreach ($all_plans as $pla): ?>
                      <option value="<?php echo $pla['PlanId']; ?>">
                        <?php echo $pla['PlanName']; ?></option>
                    <?php endforeach; ?>
                    </select>
            </div>
            </div>
            <div class="col-md-4">
            <input type="hidden" id="iplan1" class="form-control" name="planid1" placeholder="Planid" value="">
            <div class="form-group">
            <label for="Planid">Plan</label> 
            <select id="PlanId" class="form-control" name="planid" onChange="plandetailsselect()">
                      <option value="">Select Plan</option>
                    <?php  foreach ($fulldailyplan as $cat): ?>
                      <option value="<?php echo $cat['SalesPlanId']; ?>">
                        <?php echo $cat['PlanName']."-".$cat["ArticleName"]."-".$cat["ColorName"]."-".$cat["SizeAssortment"] ?></option>
                    <?php endforeach; ?>
                    </select>
            </div>
            </div>
            <div class="col-md-4">
            <div class="form-group">
            <label for="artcle">Article</label>   
            <select id="Artno" class="form-control" name="artno" >
                      <option value="">Select Article</option>
                    <?php  foreach ($all_articles as $art): ?>
                      <option value="<?php echo $art['ArticleId']; ?>">
                        <?php echo $art['ArticleName'] ?></option>
                    <?php endforeach; ?>
                    </select>
            </div>
            </div>
            </div>

         
            <div class="row">
            <div class="col-md-4">
            <div class="form-group">
            <label for="colour">Color</label>
            <select id="Color" class="form-control" name="color" >
                      <option value="">Select Color</option>
                    <?php  foreach ($all_color as $clr): ?>
                      <option value="<?php echo $clr['ColorId']; ?>">
                        <?php echo $clr['ColorName'] ?></option>
                    <?php endforeach; ?>
                    </select>
            </div>
            </div>
            <div class="col-md-4">
            <div class="form-group">
            <label for="size">Size</label>
              <select id="Size" class="form-control" name="size" >
                      <option value="">Select Size</option>
                    <?php  foreach ($all_size as $sz): ?>
                      <option value="<?php echo $sz['SizeId']; ?>">
                        <?php echo $sz['SizeAssortment'] ?></option>
                    <?php endforeach; ?>
                    </select>
            </div>
            </div>
            <div class="col-md-4">
            <div class="form-group">
            <label for="initial">Initial Plan</label>
            <input type="text" id="iplan" class="form-control" name="initialplan" placeholder="InitialPlan" readonly="true">
            </div>
            </div>
            <div class="col-md-4">
            <div class="form-group">
            <label for="plan">Today's Plan</label>
            <input type="text" class="form-control" name="plan" placeholder="Plan">
            </div>
            </div>
            <div class="col-md-4">
            <div class="form-group">
            <label for="Plan-date">Date</label>
                <input type="date" class="form-control" name="daydate" placeholder="End Date">
            </div>
            </div>
            <div class="col-md-4">
            <div class="form-group">
            <label for="Job No">Job No:</label>
           <!-- <input list="brow" class="form-control" name="jobno">
            <datalist id="brow">
                <?php //for($i=1;$i<=100;$i++) {?>
                <option value="<?php //echo "Job ".$i; ?>">
                <?php //} ?>
            </datalist>--->
                <input type="text" class="form-control" name="jobno" placeholder="Enter Job Number">
            </div>
            </div>
            </div>
            <button type="submit" name="add_pla" class="btn btn-primary">Add Plan</button>
        </form>
        </div>
      </div>
    </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</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>Date</th>
                    <th>Plan Number</th>
                    <th>Plan Name</th>
                    <th>Article</th>
                    <th>Color</th>
                    <th>Size</th>
                    <th>InitialPlan</th>
                    <th>Current Plan</th>
                    <th>4</th>
                    <th>5</th>
                    <th>6</th>
                    <th>7</th>
                    <th>8</th>
                    <th>9</th>
                    <th>10</th>
                    <th>11</th>
                    <th>12</th>
                    <th>13</th>
                    <th>14</th>

                    <!--<th class="text-center" style="width: 100px;">Status</th>-->
                    <th class="text-center" style="width: 100px;">Actions</th>
                </tr>
                
            </thead>
            <tbody>
              <?php foreach ($fulldailyplan1 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['PlanDate'])); ?></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['ArticleName'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['ColorName'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['SizeAssortment'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['InitialPlan'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['CurrentPlan'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['Four'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['Five'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['Six'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['Seven'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['Eight'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['Nine'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['Ten'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['Eleven'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['Twelve'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['Thirteen'])); ?></td>
                    <td><?php echo remove_junk(ucfirst($pla['Fourteen'])); ?></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="DailyPlanEdit.php?DailyId=<?php echo (int)$pla['Id'];?>"  class="btn btn-xs btn-warning" data-toggle="tooltip" title="Edit">
                          <span class="glyphicon glyphicon-edit"></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 planselect() {
      var Catvalue=document.getElementById('PlanIdold').value;
      console.log(Catvalue);

      $.ajax({
    url      : 'DailyPlan.php',
    data     : {'SPlanId1':Catvalue},
    dataType: 'json',
    type     : 'POST',
    success  : function(Result){
              console.log(Result[0]);
              //var myObj = $.parseJSON(Result);
              var myObj =Result;
              $("#PlanId").empty();
              $('#PlanId').append('<option value="">Select Plan </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) {
                $('#PlanId').append('<option value=' + value.Id + '>' + value.ArticleName + ' - ' + value.ColorName + ' - ' + value.SizeAssortment + '</option>');
            });
        }
    }
  );
    
}

  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 + ' Selected>' + value.ArticleName + '</option>');
                $('#Color').append('<option value=' + value.ColorId + ' Selected>' + value.ColorName + '</option>');
                $('#Size').append('<option value=' + value.SizeId + ' Selected>' + value.SizeAssortment + '</option>');
                $('#iplan').val(value.CaseCount);
                $('#iplan1').val(value.PlanId);
            });
        }
    }
  );
    
}
     </script>
  <?php include_once('layouts/footer.php'); ?>
