<div  class="panel panel-primary" id="main_supp">
  <div class="panel-heading"style="font-size:1.2em;">إضافة رصيد جديد للمخزن</div>
  <div class="panel-body"style="background-color:#00324d;color:#cce5ff;">
<form method="post" class="form-horizontal" style="float:center;" id="new_mat_invoice" autocomplete="off">
    <div class="row" style="padding:4px;">
        <input type="hidden" class="form-control" id="mat_in_idd" name="mat_in_idd" />
        <div class="col-md-2"style="float:right;">
          <label for="invoice_no" class="control-label"style="float:right;">رقم الفاتورة</label>
          <input type="text" class="form-control" style="float:right;" id="invoice_no" name="invoice_no" placeholder="فاتورة البائع"/>
        </div>
        <div class="col-md-4"style="float:right;">
          <input type="hidden" id="mat_supp_id" name="mat_supp_id" />
          <label for="mat_supp_sr" class="control-label "style="float:right;">المورد</label>
            <input type="text" style="float:right;" class="form-control" id="mat_supp_sr" name="mat_supp_sr" placeholder="بحث بالأسم"/>
      </div>
        <div class="col-md-3"style="float:right;">
           <label for="mat_invoice_date" class="control-label "style="float:right;">التاريخ</label>
          <input type="text" style="float:right;" class="form-control" id="mat_invoice_date" name="mat_invoice_date" onchange="Gdate('mat_invoice_date')" placeholder="تاريخ الفاتورة"/>
          <script>
            $(function() {
              var calendar = $.calendars.instance('');
              $('#mat_invoice_date').calendarsPicker({calendar: calendar});
              });
          </script>
        </div>
        <div class="col-md-2"style="float:right;">
        <label for="store_id" class="control-label "style="float:right;">المخزن</label>
         <select class="form-control" id="store_id" name="store_id">
              <?php
                // include "../actions/dbPDO.php";
               require_once "../magic/DataBase.php";

$dataBase = new DataBase();
$conn = $dataBase->getConnection();
 
              $sql =$conn->prepare("SELECT store_id , store_name from store") ;
              $sql->execute();
               if($sql->rowCount()>0)
              {
                 while($row = $sql->fetch()){
                        $store_id   = $row["store_id"];
                        $store_name = $row["store_name"];
                 echo "<option value= $store_id >$store_name</option>";
                      }} ?>
                    </select>
      </div>
  </div>
  <div class="row" style="padding:2px;">
      <div class="col-xs-2"style="padding:2px; float:right;">
        <label for="net_cost" class="control-label"style="float:right;">الإجمالي بعد الضريبة</label>
        <input type="text" class="form-control" style="float:right;" id="net_cost" name="net_cost" placeholder="بعد الخصم"/>
      </div>
      <div class="col-xs-1"style="padding:2px; float:right;">
         <label for="vat_out" class="control-label "style="float:right;">الضريبة</label>
        <input type="number" style="float:right;" onchange="vat_kind()" class="form-control" id="vat_out" name="vat_out" placeholder="VAT"/>
      </div>
      <div class="col-xs-2"style="padding:2px; float:right;">
      <label for="vat" class="control-label "style="float:right;">نوع الضريبة</label>
      <select class="form-control" style="float:right; padding:3px;" id="vat" name="vat">
          <option value="1" >ضريبية</option>
          <option value="2" >صفرية</option>
          <option selected value="3" >بدون ضريبة</option>
      </select>
    </div>
    <div class="col-xs-5"style="padding:2px; float:right;">
       <label for="comm" class="control-label "style="float:right;">تعليق</label>
      <input type="text" style="float:right;" class="form-control" id="comm" name="comm" placeholder="ملاحظات"/>
    </div>
    <div class="col-xs-1 col-md-2"style="padding:2px; float:right;">
    <input style="float:right ; margin-top: 23px;" type="hidden" id="add_material_in" name="add_material_in" class="btn btn-success btn-block" value="حفظ"/>
    <input style="float:right ;margin-top: 23px;" type="hidden" id="update_material_in" name="update_material_in" class="btn btn-info" value="تحديث بيانات "/>
  </div>
</div><!-- second row material invoice -->
</form>
</div>
</div>
      <div  class="panel panel-primary"id="item_line_add" style="display:none;">
      <div class="panel-heading" style="background-color:#0f4f0f; font-size:1.1em;">إضافة سطر للفاتورة</div>
      <div class="panel-body"style="background-color:#202820; color:#cce5ff;">
    <form method="post" class="form-horizontal " style="float:center; font-size:0.9rem; " id="new_invoice_line">
      <div class="row" style="padding:2px;">
        <div class="col-xs-2" style="float:right;padding:2px;">
          <button id="new_add" class="btn btn-primary btn-block" style="display:none;" >إضافة فاتورة</button>
        </div>
        <div class="col-xs-10" id="line_msg" style="float:right;padding:2px;">
      </div>
      </div>
      <div class="row" style="padding:2px;">
        <input type="hidden" class="form-control" id="invoice_id" name="invoice_id" />
        <div class="col-xs-1" style="float:right;padding:2px;">
          <label for="add" class="control-label"style="float:right;">تحرير</label>
          <input type="button" class="btn btn-success btn-sm add" style="float:right;" value="اضافة سجل"/>
        </div>
          <div class="col-xs-1"style="float:right;padding:2px;">
            <label for="tot_price" class="control-label"style="float:right;">إجمالي</label>
            <input type="number" class="form-control tot_price" style="float:right;"name="tot_price[]"/>
          </div>
          <div class="col-xs-4"style="float:right;padding:2px;">
            <label for="item_search" class="control-label"style="float:right;">اسم القطعة</label>
            <input type="text" class="form-control item_search" style="float:right;" name="item_search[]" placeholder="البيان"/>
          </div>
          <div class="col-xs-1"style="padding:2px; float:right;">
            <label for="qty" class="control-label"style="float:right;">الكمية</label>
            <input type="number" class="form-control qty" style="float:right;"name = "qty[]" value="1.0"/>
          </div>
          <div class="col-xs-1"style="padding:2px; float:right;">
            <label for="unit" class="control-label"style="float:right;">الوحدة</label>
            <input type="text" class="form-control unit" style="float:right;" name="unit[]" value="حبة"/>
          </div>
          <div class="col-xs-1"style="padding:2px; float:right;">
            <label for="location" class="control-label"style="float:right;">مكان التخزين</label>
            <input type="text" class="form-control location" style="float:right;" name="location[]" placeholder="في المخزن" />
          </div>
          <div class="col-xs-3"style="padding:2px; float:right;">
            <label for="comm" class="control-label"style="float:right;">التعليق</label>
            <input type="text" class="form-control comm" style="float:right;" name="comm[]" placeholder="ملاحظات"/>
          </div>
      </div>

  <section id="mass"></section>
  <div class="row" style="padding:2px;">
    <div class="col-xs-6" style="float:right;"></div>
    <div class="col-xs-2"style="float:right;">
      <input type="button" id="add_line" name="add_line" class="btn btn-success btn-block" value="حفظ"/>
      <input type="hidden" id="update_line" name="update_line" class="btn btn-primary" value="تعديل"/>
    </div>
  </div>
    </form>
  </div>
</div>

<?php
$supp = array();
$sql =$conn->prepare("SELECT mat_supp_id , mat_name from material_supp ORDER BY mat_name") ;
$sql->execute();
    $n=0;
    if($sql->rowCount()>0)
   {
      while($row = $sql->fetch()){
             $supp[$n] = array(
                           "value" => $row["mat_name"],
                           "data" => $row["mat_supp_id"]
                         );  $n++ ;
           } }
    else {  $supp[0]= 'لا يوجد '; }

        $output = array();
            $item_sr = $conn->prepare("SELECT item_id , item_name FROM items");
            $item_sr->execute();
            $i=0;
            if($item_sr->rowCount()>0)
              {
                 while($row = $item_sr->fetch())
                 {
                     $output[$i] = array(
                                   "value" => $row["item_name"],
                                   "data" => $row["item_id"]
                                   );  $i++ ;
                   } }
            else {  $output[0]= 'لا يوجد '; }

            // print_r($supp);
        ?>
        </table>
          </div>
        </div><!-- end of avilable invoice items -->
      </div>
<script>
$(document).ready(function(){
  var arr = <?php echo json_encode($output, JSON_UNESCAPED_UNICODE); ?>;
 var supp = <?php echo json_encode($supp, JSON_UNESCAPED_UNICODE); ?>;
  $('.item_search').autocomplete({
	 lookup: arr,
   maxHeight : 200,
   onSelect: function(suggestion) {
         }
})
$('#mat_supp_sr').autocomplete({
 lookup: supp,
 maxHeight : 200,
 onSelect: function(suggestion) {
   $("#mat_supp_id").val(suggestion.data);
   $('#add_material_in').attr("type","button"); 
  }
})

$("#add_material_in").click(function(event){
  event.preventDefault();
  $.ajax({
    url     : "wh/new43mat_in.php",
    method  : "POST",
    data    :$("#new_mat_invoice").serialize(),
    success : function(data){
      if($.isNumeric(data))
        {  $("#mat_in_idd").val(data);
           $("#invoice_id").val(data);
           $('#item_line_add').css('display','block');
           $('#update_material_in').attr("type","button");
           $('#add_material_in').attr("type","hidden");
            }
      else{ $("#signup_msg").html(data);
    }

  },
    error   :function(result){
      alert(result.statusText);
      }
  })
}); //end of add matrial invoice ajax code
$("#add_line").click(function(event){
event.preventDefault();
  var error = '';
  $('.tot_price').each(function(){
          var count = 1;
          if($(this).val() < 1)
          {
            error += "<p> من فضلك راجع اجمالى السعر </p>";
            return false;
          }
          count = count + 1;
          });

$('.item_search').each(function(){
        var count = 1;
        if($(this).val() == '')
        {
        error += "<p> من فضلك راجع اسم القطعة </p>";
        return false;
        }
        count = count + 1;
        });

$('.qty').each(function(){
          var count = 1;
          if($(this).val() <= 0)
          {
            error += "<p>  من فضلك راجع الكمية </p>";
            return false;
          }
          count = count + 1;
          });
if(error == ''){
  $.ajax({
    url     : "wh/new43line_in.php",
    method  : "POST",
    data    : $("#new_invoice_line").serialize(),
    success : function(inv){
       $("#line_msg").html(inv);
       $('#new_add').css("display","block");
      },error   :function(result){alert(result.statusText);}
  })}
  else{ $('#line_msg').html('<div class="alert alert-danger">'+error+'</div>');}
}); //end of add matrial invoice ajax code

function one_item(line_id){
  $.ajax({
  url:"wh/line_single.php",
  method:"POST",
  data:{line_id:line_id},
  dataType:"json",
  success:function(data)
  {
    $('#line_id').val(data.line_id);
    $('#invoic_id').val(data.mat_in_id);
    $('#tot_price').val(data.tot_price);
    $('#item_search').val(data.item_name);
    $('#qty').val(data.qty);
    $('#unit').val(data.unit);
    $('#unit_price').val(data.unit_price);
    $('#location').val(data.location);
    $('#comm').val(data.comm);
    $('#add_line').attr("type","hidden");
    $('#update_line').attr("type","button");
  }
});
}

$("#update_material_in").click(function(event){
  event.preventDefault();
  $.ajax({
    url     : "wh/mat_in_update.php",
    method  : "POST",
    data    :$("#new_mat_invoice").serialize(),
    success : function(data){
    $("#signup_msg").html(data);
  },
    error   :function(result){
      alert(result.statusText);
      }
  })
}); //end of update_branch ajax code ?act='update'

$(".add").click(function(event){
  event.preventDefault();
  var html = '';
      html += '<section class="row" style="padding:2px;"><div class="col-xs-1" style="float:right;padding:2px;"><input type="button" class="btn btn-danger btn-sm remove" style="float:right;" value="حذف سجل"/></div>';
      html += '<div class="col-xs-1"style="float:right;padding:2px;"><input type="number" class="form-control tot_price" style="float:right;"name="tot_price[]"/></div>';
      html += '<div class="col-xs-4"style="float:right;padding:2px;"><input type="text" class="form-control item_search" style="float:right;" name="item_search[]" placeholder="البيان"/></div>';
      html += '<div class="col-xs-1"style="padding:2px; float:right;"><input type="number" class="form-control qty" style="float:right;" name = "qty[]" value="1.0"/></div>';
      html += '<div class="col-xs-1"style="padding:2px; float:right;"><input type="text" class="form-control unit" style="float:right;" name="unit[]" value="حبة"/></div>';
      html += '<div class="col-xs-1"style="padding:2px; float:right;"><input type="text" class="form-control location" style="float:right;" name="location[]" placeholder="في المخزن" /></div>';
      html += '<div class="col-xs-3"style="padding:2px; float:right;"><input type="text" class="form-control comm" style="float:right;" name="comm[]" placeholder="ملاحظات"/></div></section>';
  $('#mass').append(html);
  $('.item_search').autocomplete({
  lookup: arr,
  maxHeight : 200,
  onSelect: function(suggestion) {
       }
});
 });
 $(document).on('click', '.remove', function(){
  $(this).closest('section').remove();
 });
 $("#new_add").click(function(event){
   event.preventDefault();
  $('#site').load("wh/newMaterial_in.php");
});

});//end of document ready
function uint_cost(){
  var tot = $("#tot_price").val();
  var qt = $("#qty").val();
  var a = tot / qt ;
  var unit_price = parseFloat(a.toFixed(2));
  $("#unit_price").val(unit_price);
}
function Gdate(ss){
  var date = $('#' + ss).val().split('/');
  var year = parseInt(date[2], 10);
    if (year < 100){ year = year + 2000 ;}
  var month = parseInt(date[1], 10);
  if(month > 12){month = 12 ;}else if(month < 1){month = 1 ;}
  var day = parseInt(date[0], 10);
  if(day > 31){day = 31 ;}else if(day < 1){day = 1 ;}
  $('#' + ss).val(year + "/" + month + "/" + day );
}
function vat_kind(){
  var vat_out = $('#vat_out').val();
  if(vat_out < 0){
    $('#vat_out').val(0);
  }
  if(vat_out > 0){
    $("#vat").val(1);
    if(vat_out < 5 ){$('#vat_out').val(5);vat_out = $('#vat_out').val();}
  }else{$("#vat").val(3);}
  if(vat_out == 15){ $("#vat_out").css( "background-color", "#42a5f5");}
					else 	if(vat_out == 5){ $("#vat_out").css( "background-color", "#ccffcc");}
          else { $("#vat_out").css( "background-color", "#ff8a80");}
}
</script>
