<?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();
?>
<?php
 if(isset($_POST['add_pla'])){
   $req_field = array('dailyid','planid','jobno','artno','color','size','initialplan','plan');
   validate_fields($req_field);
   $pla_number = remove_junk($db->escape($_POST['planid1']));
   $pla_number1 = remove_junk($db->escape($_POST['planid']));
   $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']));
   $Dailyplanid = remove_junk($db->escape($_POST['dailyid']));
   //$daydate=date('Y-m-d',$daydate);

   $salesplandetials=find_by_id_custom("salesplandetails",$pla_number,'PlanId');
   $sapcode=$salesplandetials['SAPCode'];

   //$sizefull=find_by_id_custom("sizedetails",$artsize,'SizeId');
   $four=remove_junk($db->escape($_POST['four']));
   $five=remove_junk($db->escape($_POST['five']));
   $six=remove_junk($db->escape($_POST['six']));
   $seven=remove_junk($db->escape($_POST['seven']));
   $eight=remove_junk($db->escape($_POST['eight']));
   $nine=remove_junk($db->escape($_POST['nine']));
   $ten=remove_junk($db->escape($_POST['ten']));
   $eleven=remove_junk($db->escape($_POST['eleven']));
   $twelve=remove_junk($db->escape($_POST['twelve']));
   $thirteen=remove_junk($db->escape($_POST['thirteen']));
   $fourteen=remove_junk($db->escape($_POST['fourteen']));

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

 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['DailyId'])){

        $Dailyid = remove_junk($db->escape($_GET['DailyId']));
        $Dailyplandetails1 = join_dailyplan_table_by_Id($Dailyid);
        
        //print_r($Dailyplandetails1);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>Edit Daily Plan</span>
         </strong>
        </div>
        <div class="panel-body">
          <form method="post" action="DailyPlanEdit.php">
          <input type="hidden" class="form-control" name="dailyid" value="<?php echo $Dailyplandetails1[0]['Id']; ?>" >
          <input type="hidden" id="iplan1" class="form-control" name="planid1" placeholder="Planid" value="<?php echo $Dailyplandetails1[0]['PlanId']; ?>">
            <div class="row">
            <div class="col-md-4">
            <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 if($Dailyplandetails1[0]['SalesPlanId']==$cat['SalesPlanId']) { ?> Selected <?php } ?> >
                        <?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 if($Dailyplandetails1[0]['ArticleId']==$art['ArticleId']) { ?> Selected <?php } ?>>
                        <?php echo $art['ArticleName'] ?></option>
                    <?php endforeach; ?>
                    </select>
            </div>
            </div>
            <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 if($Dailyplandetails1[0]['ColorId']==$clr['ColorId']) { ?> Selected <?php } ?>>
                        <?php echo $clr['ColorName'] ?></option>
                    <?php endforeach; ?>
                    </select>
            </div>
            </div>
            </div>
            
            <div class="row">
            <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 if($Dailyplandetails1[0]['SizeId']==$sz['SizeId']) { ?> Selected <?php } ?>>
                        <?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" value="<?php echo $Dailyplandetails1[0]['InitialPlan']; ?>" placeholder="InitialPlan" >
            </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" value="<?php echo $Dailyplandetails1[0]['CurrentPlan']; ?>" placeholder="Plan">
            </div>
            </div>
            <div class="col-md-4">
            <div class="form-group">
            <label for="start-date">Date</label>
                <input type="date" class="form-control" name="daydate" value="<?php echo $Dailyplandetails1[0]['PlanDate']; ?>" placeholder="End Date">
            </div>
            </div>
            <div class="col-md-4">
            <div class="form-group">
            <label for="Job No">Job No:</label>
            <input list="brow" value="<?php echo $Dailyplandetails1[0]['JobNo']; ?>" 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="number" class="form-control" name="jobno" placeholder="Enter Job Number">-->
            </div>
            </div>
            </div>
            
            <div class="row">
            <div class="panel-heading">
          <strong>
            
            <span>Size Wise Pair Count</span>
         </strong>
        </div>
            <div class="col-md-2">
            <div class="form-group">
            <label for="start-date">4</label>
            <input type="text" class="form-control" name="four" value="<?php echo $Dailyplandetails1[0]['Four']; ?>" placeholder="Enter Value">
            </div>
            </div>
            <div class="col-md-2">
            <div class="form-group">
            <label for="start-date">5</label>
            <input type="text" class="form-control" name="five" value="<?php echo $Dailyplandetails1[0]['Five']; ?>" placeholder="Enter Value">
            </div>
            </div>
            <div class="col-md-2">
            <div class="form-group">
            <label for="start-date">6</label>
            <input type="text" class="form-control" name="six" value="<?php echo $Dailyplandetails1[0]['Six']; ?>" placeholder="Enter Value">
            </div>
            </div>
            <div class="col-md-2">
            <div class="form-group">
            <label for="start-date">7</label>
            <input type="text" class="form-control" name="seven" value="<?php echo $Dailyplandetails1[0]['Seven']; ?>" placeholder="Enter Value">
            </div>
            </div>
            <div class="col-md-2">
            <div class="form-group">
            <label for="start-date">8</label>
            <input type="text" class="form-control" name="eight" value="<?php echo $Dailyplandetails1[0]['Eight']; ?>" placeholder="Enter Value">
            </div>
            </div>
            <div class="col-md-2">
            <div class="form-group">
            <label for="start-date">9</label>
            <input type="text" class="form-control" name="nine" value="<?php echo $Dailyplandetails1[0]['Nine']; ?>" placeholder="Enter Value">
            </div>
            </div>
            <div class="col-md-2">
            <div class="form-group">
            <label for="start-date">10</label>
            <input type="text" class="form-control" name="ten" value="<?php echo $Dailyplandetails1[0]['Ten']; ?>" placeholder="Enter Value">
            </div>
            </div>
            <div class="col-md-2">
            <div class="form-group">
            <label for="start-date">11</label>
            <input type="text" class="form-control" name="eleven" value="<?php echo $Dailyplandetails1[0]['Eleven']; ?>" placeholder="Enter Value">
            </div>
            </div>
            <div class="col-md-2">
            <div class="form-group">
            <label for="start-date">12</label>
            <input type="text" class="form-control" name="twelve" value="<?php echo $Dailyplandetails1[0]['Twelve']; ?>" placeholder="Enter Value">
            </div>
            </div>
            <div class="col-md-2">
            <div class="form-group">
            <label for="start-date">13</label>
            <input type="text" class="form-control" name="thirteen" value="<?php echo $Dailyplandetails1[0]['Thirteen']; ?>" placeholder="Enter Value">
            </div>
            </div>
            <div class="col-md-2">
            <div class="form-group">
            <label for="start-date">14</label>
            <input type="text" class="form-control" name="fourteen" value="<?php echo $Dailyplandetails1[0]['Fourteen']; ?>" placeholder="Enter Value">
            </div>
            </div>
            </div>

            <button type="submit" name="add_pla" class="btn btn-primary">Update Plan</button>
        </form>
        </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.ColorId + '>' + value.ColorName + '</option>');
                $('#Size').append('<option value=' + value.SizeId + '>' + value.SizeAssortment + '</option>');
                $('#iplan').val(value.CaseCount);
                $('#iplan1').val(value.PlanId);
            });
        }
    }
  );
    
}
     </script>
  <?php include_once('layouts/footer.php'); ?>
