<?php $__env->startSection("content"); ?>
<nav aria-label="breadcrumb">
  <ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="#">Customer</a></li>
    <li class="breadcrumb-item active" aria-current="page">เพิ่มที่อยู่ใหม่</li>
  </ol>
</nav>
<div class="row">
  <div class="col-lg-6 mb-12">
    <div class="row">
      <form action="<?php echo url('savetarn'); ?>" method="post" id="form1" class="col-lg-12 mb-12">
        <?php echo csrf_field(); ?>
        <!-- เพิ่มสินค้าใหม่ -->
        <div class="card shadow mb-4">
          <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">ข้อมูล</h6>
          </div>
          <div class="card-body">
            <div class="form-row">


              <div class="form-group col-md-12">
                <p class="font-weight-bold" for="inputProvince" style="text-align: left; color: #00897B; ">เพิ่มที่อยู่</p>
                <div class="form-group col-md-12">

                  <div class="form-row">
                    <div class="col-md-6 form-group">
                      <label>ชื่อลูกค้า
                        <span class="form-required">*</span>
                      </label>
                      <div class="row gutters-xs">
                        <div class="col">
                          <input type="hidden" class="form-control col-md-7" id="idpsuser" name="idpsuser">
                          <input type="text" name="" id="username" class="form-control" placeholder="ขื่อลูกค้า" readonly>
                        </div>
                        <span class="col-auto">
                          <button class="btn btn-secondary" id="userfide" type="button"  data-toggle="modal" data-target="#personModal"><i class="fas fa-user-tag"></i></button>
                        </span>
                      </div>
                    </div>

                  </div>
                  <div class="form-group">
                    <label for="vAdd">ที่อยู่จัดส่ง</label>
                    <input type="text" class="form-control" name="vAdd" id="vAdd" readonly="">
                  </div>
                  <div class="form-group">
                    <label for="inputAdd">บ้านเลขที่</label>
                    <span class="form-required" style="color: #CC0000">*</span>
                    <input type="text" class="form-control" name="inputAdd" id="inputAdd" onkeyup="readd()" placeholder="">
                  </div>
                  <div class="form-row">
                    <div class="form-group col-md-4">
                      <label for="inputProvince">จังหวัด</label>
                      <span class="form-required" style="color: #CC0000">*</span>
                      <select id="inputProvince" name="inputProvince" class="form-control">
                        <option value="">-- กรุณาเลือก --</option>
                        <?php $__currentLoopData = $province; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $x): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($x->PROVINCE_ID); ?>"><?php echo e($x->PROVINCE_NAME); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                      </select>
                    </div>
                    <div class="form-group col-md-4">
                      <label >อำเภอ</label>
                      <span class="form-required" style="color: #CC0000">*</span>
                      <select id="inputAmphur" name="inputAmphur" class="form-control">
                        <option value="">-- กรุณาเลือกจังหวัดก่อน --</option>

                      </select>
                    </div>
                    <div class="form-group col-md-4">
                      <label for="inputDistrict">ตำบล</label>
                      <span class="form-required" style="color: #CC0000">*</span>
                      <select id="inputDistrict" name="inputDistrict" class="form-control">
                        <option value="">-- กรุณาเลือกอำเภอก่อน --</option>
                      </select>
                    </div>
                    <div class="form-group col-md-4">
                      <label for="inputposcode">รหัสไปรษณีย์</label>
                      <input type="text" class="form-control" name="inputposcode" id="inputposcode" readonly="">
                    </div>
                  </div>


                </div>
              </div>
            </div>
            <div style="text-align: right;">
             <button type="button" id="btSm" class="btn btn-primary">บันทึกร้านค้า</button>
           </div>       
         </div>
       </div>


     </form>
   </div>
 </div>
</div>


<!-- Modal -->
<div class="modal fade" id="personModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">ลูกค้า</h5>
        <button type="button" data-dismiss="modal">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="input-group no-border col-8 mt-2 ml-auto pr-1">
          <input type="text" value="" class="form-control"  id="myInputNewper"  onkeyup="allSearch()" placeholder="Search...">
          <div class="input-group-append">
            <div class="input-group-text">
              <i class="dropdown-icon fe fe-search"></i>
            </div>
          </div>
        </div>
        <table class="table card-table table-sm">
          <thead>
            <tr>
              <th style="text-align: center; width: 30px;">ลำดับ</th>
              <th>ชื่อ-สกุล</th>
              <th style="text-align: center; width: 100px;">ตัวเลือก</th>
            </tr>
          </thead>
          <tbody id="persontd">

          </tbody>
        </table>
      </div>

    </div>
  </div>
</div>


<?php $__env->stopSection(); ?>
<?php $__env->startSection("script"); ?>
<script type="text/javascript">

  $(function(){
    $("#inputProvince").change(function(){
      $.ajax({
        url:'<?php echo e(url("fineamphur")); ?>',
        data:{idpv:$('#inputProvince').val()},
        type:'GET',
        dataType:'json',
        cache:false,
        success:function(ret){
          $("#inputAmphur").empty().append('<option value="">-- กรุณาเลือก --</option>');  
          sc = ret["amphur"];
          for(a in sc){
            $("#inputAmphur").append('<option value="'+sc[a].AMPHUR_ID+'">'+sc[a].AMPHUR_NAME+'</option>');  
          }
        },
        error:function(err){
          alert('Error');
        }
      });
    })
    $("#inputAmphur").change(function(){
      $.ajax({
        url:'<?php echo e(url("finedist")); ?>',
        data:{idAmp:$('#inputAmphur').val()},
        type:'GET',
        dataType:'json',
        cache:false,
        success:function(ret){
          $("#inputDistrict").empty().append('<option value="">-- กรุณาเลือก --</option>');  
          sc = ret["dist"];
          ps = ret["poscode"];
          
          $("#inputposcode").val(ps[0].POSTCODE);
          for(a in sc){
            $("#inputDistrict").append('<option value="'+sc[a].DISTRICT_ID+'">'+sc[a].DISTRICT_NAME+'</option>');  
          }
        },
        error:function(err){
          alert('Error');
        }
      });
    })
    $("#inputDistrict").change(function(){
      $("#vAdd").empty()
      $("#vAdd").val($("#inputAdd").val()+" ตำบล"+ $("#inputDistrict option:selected").text() +"อำเภอ"+ $("#inputAmphur option:selected").text() +"จังหวัด"+ $("#inputProvince option:selected").text() +""+ $("#inputposcode").val());
    })

  })

  $('#btSm').click(function(){
    row = $("#trpdlot tr").length;
    if(($("#inputAdd").val() == "")){
      swal({
        title: "ไม่สามารถบันทึกได้ !!!",
        text: "กรุณากรอกข้อมูลบ้านเลขที่",
        icon: "info",
        button: "ตกลง",
      });

      return false;
    }else if(($("#inputProvince").val() == "")){
      swal({
        title: "ไม่สามารถบันทึกได้ !!!",
        text: "กรุณาเลือกจังหวัดก่อน",
        icon: "info",
        button: "ตกลง",
      });

      return false;
    }else if(($("#inputAmphur").val() == "")){
      swal({
        title: "ไม่สามารถบันทึกได้ !!!",
        text: "กรุณาเลือกอำเภอก่อน",
        icon: "info",
        button: "ตกลง",
      });

      return false;
    }else if(($("#inputDistrict").val() == "")){
      swal({
        title: "ไม่สามารถบันทึกได้ !!!",
        text: "กรุณาเลือกตำบลก่อน",
        icon: "info",
        button: "ตกลง",
      });

      return false;
    }else{
     swal({
      title: "การบันทึกข้อมูล",
      text: "คุณต้องการบันทึกรายการหรือไม่",
      icon: "warning",
      buttons: true,
      dangerMode: true,
    })
     .then((willDelete) => {
      if (willDelete) {
        swal("บันทึกสำเร็จ", {
          icon: "success",
        });
        $("#form1").submit();
      } else {
        swal("ยกเลิกการบันทึก");
      }
    });

   }
 })
  function readd() {
    $("#vAdd").val($("#inputAdd").val()+" ตำบล"+ $("#inputDistrict option:selected").text() +"อำเภอ"+ $("#inputAmphur option:selected").text() +"จังหวัด"+ $("#inputProvince option:selected").text() +""+ $("#inputposcode").val());
  }

  function show(){

    $('#ShowPdModal').modal('show');
  }

  function deTailTranGB(id,e,s){

    $.ajax({
      url:'<?php echo e(url("finepdstock")); ?>/'+id,
      type:'GET',
      dataType:'json',
      cache:false,
      success:function(ret){
        console.log(ret);

        var showpd = ret["showpdStock"];

        $("."+e).empty();
        $("."+s).empty();
        var i= 1;
        var amonuse = 0;
        for(x in showpd){
         amonuse = showpd[x]["Amount"] - showpd[x]["AmountUse"];
         strGB ='<tr style="background: #E0F2F1">'
         +'<td style="width: 3px;">->['+i+']</td>'
         +'<td style="width: 5px;text-align: left;"><a href="#" class="btn btn-success btn-circle btn-sm" onClick="addpd2chart('+showpd[x].idPdLot+',\''+showpd[x].LotNum+'\',\''+showpd[x].PdName+'\',\''+showpd[x].Price+'\',\''+showpd[x].idPd+'\')"><i class="fas fa-cart-arrow-down"></i></a></td>'
         +'<td style="width: 150px;text-align: left;">'+showpd[x]["LotNum"]+'</td>'
         +'<td style="width: 50px;text-align: left;">'+showpd[x]["datePdCer"]+'</td>'
         +'<td style="width: 50px;text-align: right; color: #00796B; font-weight: bold;">'+amonuse+'</td>'
         +'<td style="width: 50px;text-align: right; color: #E65100; font-weight: bold;">'+showpd[x]["AmountUse"]+'</td>'
         +'<td style="width: 50px;text-align: right; color: #6D4C41; font-weight: bold;">'+showpd[x]["Amount"]+'</td>'
         +'<td style="width: 200px;text-align: center;">'+showpd[x]["Note"]+'</td>'

         +'</tr>';
         $("."+e).append(strGB);
         i++;
       }

       $('.tranGB').show();
     },
     error:function(err){
      console.log(err);
    }
  });
  }

  function PdSearch() {
    var input, filter, tbody, tr, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    tbody = document.getElementById("ListPdDetail");
    tr = tbody.getElementsByTagName("tr");
    for (i = 0; i < tr.length; i++) {
      a = tr[i].getElementsByTagName("td")[0];
      if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";

      }
    }
  }
  var rcount = 1;
  function addpd2chart(idlot,numlot,namepd,price,idPd){


    $("#trpdlot").append(''
      +'<tr>'
      +'<td scope="row" style="text-align: center;"><input type="hidden" name="idlotT[]" value="'+idlot+'"><input type="hidden" name="idPd[]" value="'+idPd+'">'
      +rcount
      +'. '
      +'</td>'
      +'<td style="text-align: left;"><input type="hidden" name="numlotT[]" value="'+numlot+'">'
      +numlot
      +'</td>'
      +'<td style="text-align: left; color: #00897B;"><input type="hidden" name="namepdT[]" value="\''+namepd+'\'">'
      +namepd
      +'</td>'
      +'<td style="text-align: right;"><input type="hidden" name="priceT[]" value="\''+price+'\'">'
      +price
      +'</td>'
      +'<td class="text-nowrap"style="text-align: right;"><input style="text-align: right; width:100px;" type="number" class="form-control form-control-sm" maxlength="9"  name="amoun[]" id="amoun" onkeyup="SumPrice(this)" value="0"></td>'
      +'<td style="text-align: right;"><input style="text-align: right; width:100px;" type="number" onkeyup="SumPrice(this)" class="form-control form-control-sm" maxlength="9"  name="inputpice[]" id="inputpice" value="'+price+'"></td>'
      +'<td class="font-weight-bold"><input class="form-control form-control-sm font-weight-bold price_mtr" type="text" name="num_mtr[]" style="text-align: right; color: #1E88E5;"  value="0" readonly=""></td>'
      +'<td style="text-align: center; color: #C2185B;">'
      +'<a onclick="deleteRow(this)"><font color="#D50000"> ลบ</font></a> <input type="hidden" name="num_mtrT[]">'
      +'</td>'
      +'</tr>'
      );

    rcount++;
    $("#ShowPdModal").modal('toggle');
  }

  function SumPrice(e){

   var tr = $(e).parent().parent();

   price_unit_mtr = tr.find('input[name="amoun[]"]').val();
   price_mtr = tr.find('input[name="inputpice[]"]').val();
   totalrow = numeral(price_mtr).value() * numeral(price_unit_mtr).value();
   tr.find('input[name="num_mtr[]"]').val(numeral(totalrow).format('0,0'));
   tr.find('input[name="num_mtrT[]"]').val(totalrow);
   totalPrice();
   totalNet();
 }

 function totalPrice(){
  var total = 0;
  $(".price_mtr").each(function( index ) {
    total += numeral($(this).val()).value();
  });
  $('#price_total').val(numeral(total).format('0,0'));
  $('#price_totalT').val(total);
}

function totalNet(){
  var totalNet = 0;
  totalNet = numeral($("#price_total").val()).value() - numeral($("#price_dis").val()).value();
  $('#price_totalNet').val(numeral(totalNet).format('0,0'));
  $('#price_totalNetT').val(totalNet);
}

function deleteRow(e){
  tag = $(e).parent().parent();
  /*  del_dnameCarplate = tag.find(".dnameCarplate").text();*/
  tag.remove();
}

$(function(){
  $("#userfide").click(function(){
    $.ajax({
      url:'<?php echo e(url("user")); ?>',
      /* data:{CompName:$("#findcompname").val()},*/
      type:'GET',
      dataType:'json',
      cache:false,
      success:function(ret){
        items = ret;
        $("#persontd").empty();
        var rows = 1;
        for(x in items){
                //str = '<tr><td >'+ rows +'</td><td style="text-align:left">'+items[x].CompName+'</td><td ><button type="button" class="btn btn-default" onclick="getCus('+x+')">เลือก</button> </td></tr> ';
                str = '<tr><td >'+ rows +'</td><td style="text-align:left padding: 0px;"><a>'+items[x].CusName+''+items[x].LastName+'</a></td><td style="padding: 0px; text-align: center;"><a class="btn btn-primary btn-sm" style="color:#FFF" onclick="getGetUser('+ret[x].idCus+',\''+ret[x].CusName+'\')">เลือก</a> </td></tr> ';
                $("#persontd").append(str);
                rows++;
              }
            },
            error:function(err){
              alert('Error');
              //console.log(err.responseText);
            }
          });
  });

});

function getGetUser(ids,sname){
  $("#username").val(sname);
  $("#idpsuser").val(ids);
  $("#personModal").modal('toggle');
}


</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make("layout.menu", \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/cp325668/public_html/erp/resources/views/customer/addTarn.blade.php ENDPATH**/ ?>