<?php $__env->startSection("content"); ?>
<?php
function DateThai($strDate)
{
  $strYear = date("Y",strtotime($strDate))+543;
  $strMonth= date("n",strtotime($strDate));
  $strWeek= date("w",strtotime($strDate));
  $strDay= date("j",strtotime($strDate));
  $strMonthCut = Array("","ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");
  $thai_day_arr = Array("อา","จ","อ","พ","พฤ","ศ","ส");
  $strMonthThai=$strMonthCut[$strMonth];
  $strWeekThai=$thai_day_arr[$strWeek];
  return "$strWeekThai $strDay $strMonthThai $strYear";
}

function Datefor($strDate)
{
  $strYear = date("Y",strtotime($strDate));
  $strMonth= date("m",strtotime($strDate));
  $strDay= date("d",strtotime($strDate));
  $datemat = $strDay."/".$strMonth."/".$strYear;
  //19/06/2021
  return "$datemat";
}


$datetime1 = date("d/m/Y");
$idpo = "";
$idsub = "";
$SupName = "";
$no_posub = "";
$ipDoc = "";
$vadd = "";
$idpaychek = "";
$vpay = "";
$inputtimeStart = "";
$inputNote ="" ;
$price_total ="" ;
$price_vat ="" ;
$price_totalNetT ="" ;


if(!empty($mt[0]->id_po)){$idpo = $mt[0]->id_po;}else{$idpo = "";}
if(!empty($mt[0]->id_sub)){$idsub = $mt[0]->id_sub;}else{$idsub = "";}
if(!empty($mt[0]->SupName)){$SupName = $mt[0]->SupName;}else{$SupName = "";}
if(!empty($mt[0]->no_posub)){$no_posub = $mt[0]->no_posub;}else{$no_posub = "";}
if(!empty($mt[0]->DocCodePo)){$ipDoc = $mt[0]->DocCodePo;}else{$ipDoc = "";}
if(!empty($mt[0]->AddressText)){$vadd = $mt[0]->AddressText;}else{$vadd = "ที่อยู่ Subpplier";}
if(!empty($mt[0]->id_pay)){$idpaychek = $mt[0]->id_pay;}else{$idpaychek = "";}
if(!empty($mt[0]->v_pay)){$vpay = $mt[0]->v_pay;}else{$vpay = "การจ่ายเงิน";}
if(!empty($mt[0]->date_require)){$inputtimeStart = $mt[0]->date_require;}else{$inputtimeStart = $datetime1;}
if(!empty($mt[0]->note)){$inputNote = $mt[0]->note;}else{$inputNote = "";}
if(!empty($mt[0]->price)){$price_total = $mt[0]->price;}else{$price_total = "0";}
if(!empty($mt[0]->vat)){$price_vat = $mt[0]->vat;}else{$price_vat = "0";}
if(!empty($mt[0]->price_total)){$price_totalNetT = $mt[0]->price_total;}else{$price_totalNetT = "0";}

?>
<nav aria-label="breadcrumb">
<!-- <img src="assets/file/img/Loading.gif" class="rounded"></td>
<img src="assets/file/img/Loading.gif" class="rounded-circle"> -->
  <ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="#">สั่งซื้อ</a></li>
    <li class="breadcrumb-item"><a href="#">ใบสั่งซื้อ</a></li>
    <?php if($param == "add"): ?>
    <li class="breadcrumb-item active" aria-current="page">สร้างใบสั่งซื้อ</li>
     <?php else: ?>
     <li class="breadcrumb-item active" aria-current="page">แก้ไขใบสั่งซื้อ</li>
    <?php endif; ?>
  </ol>
</nav>
<div class="row" style="font-size: 12px;">
  <div class="col-lg-6 mb-12">
    <div class="row"> 
      
      <form id="form1" class="col-lg-12 mb-12">
        <?php echo csrf_field(); ?>
        <!-- เพิ่มรายการขาย -->
        <div class="card shadow mb-4">
          <div class="card-header py-3">
            <?php if($param == "add"): ?>
            <h6 class="m-0 font-weight-bold text-primary">สร้างใบสั่งซื้อ </h6>
            <?php else: ?>
            <h6 class="m-0 font-weight-bold text-primary">แก้ไขใบสั่งซื้อ </h6>
            <?php endif; ?>
          </div>
          <div class="card-body">
            <div class="form-row">
              <div class="form-group col-12 col-md-12" id="cusold">
                <div class="form-row">
                  <?php if($param == "edit"): ?>
                  <div class="col-md-4 form-group">
                    <div class="row gutters-xs">
                      <div class="col">
                        <input type="text" class="form-control form-control-sm" id="ipDoc" name="ipDoc" placeholder="เลขที่รายการ" value="<?php echo $ipDoc; ?>" readonly>
                        
                      </div>
                    </div>
                  </div>
                  <?php endif; ?>
                <div class="col-md-4 form-group">
                    <div class="row gutters-xs">
                      <div class="col">
                        <input type="text" name="vnumsup" id="vnumsup" class="form-control form-control-sm" placeholder="รหัส Subpplier" value="<?php echo $no_posub; ?>" readonly>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-8 form-group">
                    <div class="row gutters-xs">
                      <div class="col">
                        <input type="hidden" class="form-control col-md-7" id="idpo" name="idpo" value="<?php echo $idpo; ?>">
                        <input type="hidden" class="form-control col-md-7" id="idpsuser" name="idpsuser" value="<?php echo $idsub; ?>">
                        <input type="text" name="username" id="username" class="form-control form-control-sm" placeholder="ชื่อ Subpplier"  value="<?php echo $SupName; ?>" readonly>
                      </div>
                      <span class="col-auto">
                        <button class="btn btn-outline-primary btn-sm" id="supfide" type="button"  data-toggle="modal" data-target="#personModal"><i class="fas fa-user-plus"></i> ค้นหา</button>
                      </span>
                    </div>
                  </div>
                  <div class="col-md-12 form-group">
                    <div class="row gutters-xs">
                      <div class="col"> 
                        <textarea type="text" name="vAddold" id="vAddold" class="form-control form-control-sm"   readonly><?php echo $vadd; ?></textarea>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-12 form-group">
                    <div class="row gutters-xs">
                      <div class="col">
                        <input type="hidden" class="form-control col-md-7" id="idpaychek" name="idpaychek" value="<?php echo $idpaychek; ?>">
                        <textarea type="text" name="vpay" id="vpay" class="form-control form-control-sm" placeholder="การจ่ายเงิน"  readonly><?php echo $vpay; ?></textarea>
                      </div>
                      <span class="col-auto">
                        <button class="btn btn-outline-primary btn-sm" id="payfide" type="button"  data-toggle="modal" data-target="#payModal"><i class="far fa-credit-card"></i> การจ่ายเงิน</button>
                      </span>
                      <span class="col-auto">
                        <button class="btn btn btn-outline-info btn-sm" id="" type="button"  data-toggle="modal" data-target="#AddPayModalCenter"><i class="fas fa-plus-circle"></i> สร้างการจ่ายเงิน</button>
                      </span>
                    </div>
                  </div>
                  <div class="col-md-5 form-group">
                    <div class="row gutters-xs">
                        <div class="input-group input-group-sm mb-8" style="padding-left: 5%">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="inputGroup-sizing-sm">วันที่ต้องการ</span>
                            </div>
                            <input type="text" class="form-control" id="inputtimeStart" name="inputtimeStart" value="<?php echo Datefor($inputtimeStart); ?>" aria-label="Small" aria-describedby="inputGroup-sizing-sm">
                        </div>
                    </div>
                  </div>
                  
                </div>
              </div>
              <div class="form-group col-12 col-md-12">
              <div class="form-row">
                <div class="form-group col-md-12">
                  <label for="inputNote" style="font-size: 14x;">หมายเหตุ: </label>
                  <textarea type="text" class="form-control form-control-sm" name="inputNote" id="inputNote" rows="3" ><?php echo $inputNote; ?></textarea>
                </div>
                </div>
              </div>
            </div>
        </div>

    <!-- ตารางสินค้า -->
    <div class="form-group col-md-12"style="font-size: 12px;" >
    <div class="col-lg-12 mb-12" >
        <div class="row">
          <div class="col-lg-6 mb-6"> <p class="font-weight-bold" for="inputProvince" style="text-align: left; color: #043F8E;font-size: 14px; ">กำหนดสินค้าที่ต้องการทำใบสั่งซื้อ</p></div>
          <div class="col-lg-6 mb-6" style="text-align: right;padding-bottom: 25px;"><button type="button" class="btn btn-outline-primary btn-sm"data-toggle="modal" data-target="#AddPDModalCenter" ><i class="fas fa-plus-circle"></i> เพิ่มสินค้า</button></div>
        </div>
      </div>

      <div class="col-lg-12 mb-12">
        <div class="table-responsive">
          <table class="table table-sm" >
            <thead>
              <tr>
                <th class="text-nowrap" scope="col" style="text-align: center;">#</th>
                <th class="text-nowrap" scope="col" style="text-align: center;">รายการสินค้า</th>
                <th class="text-nowrap" scope="col" style="text-align: center;">จำนวน</th>
                <th class="text-nowrap" scope="col" style="text-align: center;">ราคา</th>
                <th class="text-nowrap" scope="col" style="text-align: center;">รวมเป็นเงิน</th>
                <th class="text-nowrap" scope="col" style="text-align: center;">ตัวเลือก</th>
              </tr>
            </thead>
            <tbody id="trpdlot">
            <?php if($param == "add"): ?>
            <?php else: ?>
            <?php $i=1;
            ?>
            <?php $__currentLoopData = $dt; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $x): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
              <tr>
                <td scope="row" style="text-align: center;"><input type="hidden" name="idunit[]" value="<?php echo e($x->id_unit); ?>"><input type="hidden" name="idPd[]" value="<?php echo e($x->id_material); ?>"><input type="hidden" name="priceT[]" value="<?php echo e($x->price); ?>"></td>
                <td class="text-nowrap" style="text-align: left; color: #00897B;"><input type="hidden" name="namepdT[]" value="<?php echo e($x->name_material); ?>"><?php echo e($x->name_material); ?></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" onchange="SumPrice(this)" value="<?php echo e(number_format($x->Amount_material), 2); ?>"></td>
                <td style="text-align: right;"><input style="text-align: right; width:100px;" type="number" onchange="SumPrice(this)" class="form-control form-control-sm" maxlength="9"  name="inputpice[]" id="inputpice" value="<?php echo e($x->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;width:100px;"  value="<?php echo e(number_format($x->price_total), 2); ?>" readonly=""></td>
                <td style="text-align: center; color: #C2185B;"><input type="hidden" name="inputnoteadd[]" value="<?php echo e($x->note); ?>">
                <a onclick="deleteRow(this)"><font color="#D50000"> ลบ</font></a> <input type="hidden" name="num_mtrT[]"value="<?php echo e($x->price_total); ?>"></td>
              </tr>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>

      <div class="col-lg-12 mb-12" style="margin-top: 120px;">
        <div class="row">
          <div class="col-lg-12 mb-12">
          <div class="row">
            <div class="col-lg-12 mb-12">
            <div class="row">
              <!-- <div class="col-lg-6 mb-6"> <p class="font-weight-bold" for="inputProvince" style="text-align: left; color: #0C4973;font-size: 16px; ">กำหนดโปรโมชั่นที่ต้องการใช้งาน</p></div>
              <div class="col-lg-6 mb-6" style="text-align: right;"><button type="button" class="btn btn-outline-info btn-sm" onClick="showpomo()"><i class="fas fa-plus-circle"></i> เพิ่มโปรโมชั่น</button></div> -->
            </div>
          </div>
          <div class="col-lg-8 mb-8" id="carddic">
          </div>
        </div>
      </div>
      <div class="col-lg-4 mb-4"></div>
      <div class="col-lg-8 mb-8" style="text-align: right;"> <div class="table-responsive"  style="margin-top: 20px;">
        <table class="table">
          <tbody>
            <tr>
              <th style="width: 40%; color: #00897B;text-align: right; padding-top: 25px">จำนวนเงิน :</th>
              <td ><input type="text" class="form-control" name="price_total" id="price_total" style="font-size: 14px; text-align: right;" min="0.01" step="0.01" readonly="" value="<?php echo e(number_format($price_total)); ?>"></td>
            </tr>
            <tr>
              <th style="width: 40%; color: #00897B;padding-top: 25px">ภาษี มูลค่าเพิ่ม 7%:</th>
              <td ><input type="text" class="form-control" name="price_vat" id="price_vat" style="font-size: 14px; text-align: right;" readonly="" value="<?php echo $price_vat; ?>"></td>
            </tr>
            <tr>
              <th style="width: 40%; color: #00897B;padding-top: 27px">เงินสุทธิ :</th>
              <td><input type="text" class="form-control font-weight-bold " name="price_totalNet" id="price_totalNet" style="font-size: 18px; text-align: right; color: #00796B;" min="0.01" step="0.01" readonly="" value="<?php echo e(number_format($price_totalNetT)); ?>">
                <input type="hidden" name="price_totalT" id="price_totalT"  value="<?php echo $price_total; ?>">
                <input type="hidden" name="price_totalNetT" id="price_totalNetT" value="<?php echo $price_totalNetT; ?>">
                <input type="hidden" name="price_vatT" id="price_vatT" value="<?php echo $price_vat; ?>">
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div> 
    </div>
      </div>  
          </div>
              <div style="text-align: right;">
              <?php if($param == "add"): ?>
              <button type="button" id="btSm" class="btn btn-primary">บันทึกรายการ</button>
              <?php else: ?>
              <button type="button" id="bteditpo" class="btn btn-info">บันทึกแก้ไข</button>
              <?php endif; ?>
              </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">รายชื่อ Supplier</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <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"  style="padding-bottom: 20px">
          <input type="text" value="" class="form-control"  id="myInputCus"  onkeyup="CusAllSearch()" 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" style="color: #00897B;">
          <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>

<!-- Modal addpay -->
<div class="modal fade" id="AddPayModalCenter" tabindex="-1" role="dialog" aria-labelledby="AddPayModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="AddPayModalLongTitle">สร้างการชำระเงินใหม่</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-lg-12 mb-12">
            <div class="row">
              <form action="<?php echo url('savetarn'); ?>" method="post" id="form133355" 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">
                        <div class="form-group col-md-12">
                          <div class="form-row">
                            <div class="col-md-12 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="idpssuppay" name="idpssuppay" value="<?php echo $idsub; ?>">
                                  <input type="text" name="suppayname" id="suppayname" class="form-control" placeholder="ชื่อบัญชี">
                                </div>  
                              </div>
                            </div>
        
                          </div>
                          <div class="form-row" >
                            <div class="form-group col-md-12">
                              <label for="inputPay">การชำระเงิน</label>
                              <span class="form-required" style="color: #CC0000">*</span>
                              <select id="inputPay" name="inputPay" class="form-control">
                                <option value="">กรุณาเลือก</option>
                              <?php $__currentLoopData = $dpay; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $x): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($x->idPay); ?>"><?php echo e($x->NamePay); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                              </select>
                            </div>
                            </div>
                            <div class="form-row" id="divPay2"  style="display: none;">
                              <div class="form-group col-md-12">
                                <label for="inputBank">ธนาคาร</label>
                                <span class="form-required" style="color: #CC0000">*</span>
                                <select id="inputBank" name="inputBank" class="form-control">
                                  <?php $__currentLoopData = $dbank; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $x): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                  <option value="<?php echo e($x->id_bank); ?>"><?php echo e($x->namebank); ?></option>
                                  <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                              </div>               
                            </div>
                            <div class="form-group" id="divPay"  style="display: none;">
                              <label for="inputnoAcc">หมายเลขบัญชี</label>
                              <span class="form-required" style="color: #CC0000">*</span>
                              <input type="text" class="form-control" name="inputnoAcc" id="inputnoAcc" placeholder="000-0000-0000">
                            </div>
                            <div class="form-group">
                              <label for="inputnotacc">เงื่อนไขการชำระเงิน</label>                         
                              <textarea  type="text" class="form-control" name="inputnotacc" id="inputnotacc"  placeholder=""></textarea>
                            </div>
                          </div>
                          
                      </div>
                    </div>
                    <div style="text-align: right;">
                     
                   </div>       
                 </div>
               </div>
        
        
             </form>
           </div>
         </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" id="Addaccpay">Save changes</button>
      </div>
    </div>
  </div>
</div>

<!-- Modal -->
<div class="modal fade" id="payModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">การชำระเงินของ Subpplier</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body" id="modalcard">    
        <table class="table card-table table-sm" cellspacing="0" cellpadding="0" style="border: none;" >
          <tbody id="addresstd">
          </tbody>
        </table>
      </div>

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

<!-- Modal addpd -->
<div class="modal fade" id="AddPDModalCenter" tabindex="-1" role="dialog" aria-labelledby="AddPDModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="AddPDModalLongTitle">เพิ่มรายการสินค้า</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-lg-12 mb-12">
            <div class="row">
              <form action="<?php echo url('savetarn'); ?>" method="post" id="form133" 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">
                        <div class="form-group col-md-12">
                          <div class="form-row">
                            <div class="col-md-12 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="idpdnameadd" name="idpdnameadd">
                                  <input type="text" name="pdnameadd" id="pdnameadd" class="form-control" placeholder="ชื่อสินค้า">
                                </div>  
                              </div>
                            </div>
                            <div class="col-md-12 form-group">
                              <label>ราคาต่อหน่วย
                                <span class="form-required">*</span>
                              </label>
                              <div class="row gutters-xs">
                                <div class="col">
                                  
                                  <input type="number" name="priceadd" id="priceadd" class="form-control" placeholder="ราคาต่อหน่วย">
                                </div>  
                              </div>
                            </div>
        
                          </div>
                          <div class="form-row" >
                            <div class="form-group col-md-12">
                              <label for="inputUnitadd">หน่วย</label>
                              <span class="form-required" style="color: #CC0000">*</span>
                              <select id="select-testing" name="inputUnitadd"  class="form-control selectpicker" data-live-search="true" required>
                              <?php $__currentLoopData = $dunit; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $x): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($x->idUnit); ?>"}}><?php echo e($x->UnitName); ?></option>
                              <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                              </select>
                            </div>
                            </div>
                            <div class="form-group">
                              <label for="inputnoteadd">หมายเหตุ</label>                         
                              <textarea  type="text" class="form-control" name="inputnoteadd" id="inputnoteadd"  placeholder=""></textarea>
                            </div>
                          </div>
                          
                      </div>
                    </div>     
                 </div>
               </div>            
             </form>
           </div>
         </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary"  id="Addpd2cart" >เพิ่มลงตาราง</button>
      </div>
    </div>
  </div>
</div>

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

$(function() {
  $('.selectpicker').selectpicker();
});

  $("input[name='phase_type']").change(function() {
    if($("#inlineRadio1").prop('checked')==true){
      $("#idpsuser").val('');
      $("#username").val('');
      $("#vAddold").val('');
      $("#idAdd").val('');
      $("#cusnew").show(200);
      $("#cusnew2").show(200);
      $("#cusold").hide(100);
    }
    else if($("#inlineRadio2").prop('checked')==true){
      $("#cusold").show(200);
      $("#cusnew").hide(100);
      $("#cusnew2").hide(100);
    }

  });

  function getGetUser(ids,sname,numbe,add){
    $("#username").val(sname);
    $("#idpsuser").val(ids);
    $("#vnumsup").val(numbe);
    $("#vAddold").val(add);
    $("#idpssuppay").val(ids);
    $("#suppayname").val(sname);
    $("#personModal").modal('toggle');

    $.ajax({
      url:'<?php echo e(url("address")); ?>',
      data:{idcus:ids},
      type:'GET',
      dataType:'json',
      cache:false,
      success:function(ret){

       $("#vAdd").empty()
       sc = ret["ad"];
      // $("#vAddold").val(sc[0].AddressText);
      // $("#idAdd").val(sc[0].idAdd);

     },
     error:function(err){
      alert('Error');
    }
  });
  }

  function getGetAdd(idPay,NamePay,nameacc,detail,namebank,noacc){
    str = "ชำระเงินโดย: "+NamePay+" ผู้รับเงิน: "+nameacc+"  "+namebank+" "+noacc+" "+detail;
    $("#idpaychek").val(idPay);
    $("#vpay").val(str);
    $("#payModal").modal('toggle');
    

  }

  $(function(){

    var rcount = 1;
    $("#Addpd2cart").click(function(){
     
      var namepd = $("#pdnameadd").val();
      var price = $("#priceadd").val();
      var amonuseadd = 0;
      var idunit = $("#select-testing").val();
      var unit = $("#unit").text();
      var inputnoteadd = $("#inputnoteadd").val();

      if(($("#pdnameadd").val() == "")){
        swal({
          title: "ไม่สามารถเพิ่มได้ !!!",
          text: "กรุณาเพิ่มชื่อสินค้า",
          icon: "info",
          button: "ตกลง",
        });

        return false;

      }

      $("#trpdlot").append(''
        +'<tr>'
        +'<td scope="row" style="text-align: center;"><input type="hidden" name="idunit[]" value="'+idunit+'"><input type="hidden" name="idmaterial[]" value=""><input type="hidden" name="inputnoteadd[]" value="'+inputnoteadd+'" readonly="">'
        +'. '
        +'</td>'
        +'<td class="text-nowrap" style="text-align: left; color: #00897B;"><input type="hidden" name="namepdT[]" value="'+namepd+'">'
        +namepd
        +'</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" onchange="SumPrice(this)" value="'+amonuseadd+'"></td>'
        +'<td style="text-align: right;"><input style="text-align: right; width:100px;" type="number" onchange="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;width:100px;"  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++;
      SumPrice();
      $("#AddPDModalCenter").modal('toggle');
        $("#pdnameadd").val("");
        $("#priceadd").val("");
        $("#inputUnitadd").val("");
        $("#unit").text("");
    });

    $("#supfide").click(function(){
      $.ajax({
        url:'<?php echo e(url("finesup")); ?>',
        /* 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].SupName+'</a></td><td style="padding: 0px; text-align: center;"><a class="btn btn-primary btn-sm" style="color:#FFF" onclick="getGetUser('+ret[x].idSup+',\''+ret[x].SupName+'\',\''+ret[x].SupCode+'\',\''+ret[x].AddressText+'\')">เลือก</a> </td></tr> ';
                $("#persontd").append(str);
                rows++;
              }
            },
            error:function(err){
              alert('Error');
              //console.log(err.responseText);
            }
          });
    });

    $("#payfide").click(function(){
      if(($("#idpsuser").val() == "")){
        swal("กรุณาเลือก supplier ก่อน");
        return false;
      }
      $.ajax({
        url:'<?php echo e(url("finesup2pay")); ?>',
        data:{idsup:$("#idpsuser").val()},
        type:'GET',
        dataType:'json',
        cache:false,
        success:function(ret){
          items = ret["dpay"];
          $("#addresstd").empty();
          var rows = 1;
          var img = "";
          var noacc = "";
          for(x in items){
            if(items[x].idPay == 2 || items[x].idPay == 3 || items[x].idPay == 5){
              img =items[x].imgpay;
              noacc="";
            }else{
              if(items[x].img != null){
              img = items[x].img;
              noacc='เลขที่บัญชี : '+items[x].noacc;
              }else{
                  img = items[x].imgpay;
              }
            }
            
            str='<tr>'
              +'<td>'
                +' <a onclick="getGetAdd('+items[x].idPay+',\''+items[x].NamePay+'\',\''+items[x].nameacc+'\',\''+items[x].detail+'\',\''+items[x].namebank+'\',\''+items[x].noacc+'\')"><div class="card" style="width: 29rem;">'
                  +'<div class="card-body">'
                    +'<div class="container">'
                      +'<div class="row">'
                        +' <div class="col-sm-3">'
                          +'<img style="box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);" src="http://erp.natureplus.co.th/'+img+'" alt="..." class="rounded">'
                        +'</div>'
                        +'<div class="col-sm">'
                         +' <h5 class="card-title" >'+items[x].NamePay+'</h5>'
                         +' <h6 class="card-subtitle"><span style="color: #AD0773;"> ถึง :</span> '+items[x].nameacc+'</h6>'
                         +' <p class="card-text" style="color: #086BAC;"> '+items[x].detail+'</p>'
                        +' </div>'
                     +' </div>'
                    +' </div>'
                    +' <a href="#" class="card-link">'+items[x].namebank+'</a>'
                    +' <a href="#" class="card-link">'+noacc+'</a>'
                    +'</div>'
                  +'</div></a>'
                +'</td>'
            +'</tr>' ;
                // str = '<tr><td >'+ rows +'</td><td style="text-align:left padding: 0px;"><a>'+items[x].NamePay+' '+items[x].nameacc+'</a></td><td style="padding: 0px; text-align: center;"><a class="btn btn-primary btn-sm" style="color:#FFF" onclick="getGetAdd('+items[x].id_suppay+',\''+items[x].id_suppay+'\')">เลือก</a> </td></tr> ';
                $("#addresstd").append(str);
                rows++;
              }
            },
            error:function(err){
              alert('Error');
              //console.log(err.responseText);
            }
          });
    });

    $("#Addaccpay").click(function(){
      
      $.ajax({
        url:'<?php echo e(url("addaccpay")); ?>',
        data:{idsup:$("#idpssuppay").val(),name:$("#suppayname").val(),inputPay:$("#inputPay").val(),inputBank:$("#inputBank").val(),inputnoAcc:$("#inputnoAcc").val(),inputnotacc:$("#inputnotacc").val()},
        type:'GET',
        dataType:'json',
        cache:false,
        success:function(ret){
          console.log(ret);
          items = ret["dpay"];
          $("#vpay").empty();
          $("#idpaychek").empty();
          for(x in items){

          str = 'ชำระเงินโดย: '+items[x].NamePay+' ผู้รับเงิน: '+items[x].nameacc+''+items[x].namebank+' '+items[x].noacc+'  '+items[x].detail+' ';
          $("#vpay").val(str);
          $("#idpaychek").val(items[x].id_suppay);
            }
            $("#inputPay").val('');
            $("#inputBank").val('');
            $("#inputnotacc").val('');
            $("#inputnoAcc").val('');
            $('#AddPayModalCenter').modal('hide');
          },
          error:function(err){
            alert('Error');
          }
        });
    });

    

  });

  $(function(){
    $("#inputPay").change(function(){
      if($('#inputPay').val() == 1 || $('#inputPay').val()== 4){
        $("#divPay2").show(200);
        $("#divPay").show(100);
      }else{
        $("#inputnoAcc").val("");
        $("#divPay2").hide(200);
        $("#divPay").hide(100);
      }      
    })
  })

  $('#btSm').click(function(){
    row = $("#trpdlot tr").length;

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

        return false;

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

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

        return false;
        }else if(row == 0){
        swal({
          title: "ไม่สามารถบันทึกได้ !!!",
          text: "กรุณาเพิ่มสินค้า",
          icon: "info",
          button: "ตกลง",
        });

        return false;
        }else{
      swal({
        title: "การบันทึกข้อมูล",
        text: "คุณต้องการบันทึกหรือไม่",
        icon: "warning",
        buttons: true,
        dangerMode: true,
      })
      .then((willSuccess) => {
        if (willSuccess) {
          $(".md-Load").show();
          var form1 = $('#form1')[0];
          var formData = new FormData(form1);
            $.ajax({
              url:'<?php echo e(url('adddatanewpo')); ?>',
                      headers: {
                          'X-CSRF-TOKEN': $('input[name="_token"]').val()
                      },
                      data: formData,
                      type: 'POST',
                      contentType: false,
                      processData: false,
                      cache:false,
                      success:function(ret){
                      $("#loadPage").hide();
                      swal({
                          text: "บันทึกข้อมูลเรียบร้อยแล้ว",
                          icon: "success",
                      })
                      // $("#error").html(ret);
                      window.location.replace("<?php echo e(url('tbpo')); ?>");
                      
                  },
                  error:function(err){
                      console.log(err);
                      $("#error").html(err.responseText);
                      $("#modal_error").modal('show');
                  }
              });
        } else {
          swal("ยกเลิกการบันทึก");
        }
      });

    }
  })


  $('#bteditpo').click(function(){
    row = $("#trpdlot tr").length;

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

        return false;

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

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

        return false;
        }else if(row == 0){
        swal({
          title: "ไม่สามารถบันทึกได้ !!!",
          text: "กรุณาเพิ่มสินค้า",
          icon: "info",
          button: "ตกลง",
        });

        return false;
        }else{
      swal({
        title: "การบันทึกข้อมูล",
        text: "คุณต้องการบันทึกหรือไม่",
        icon: "warning",
        buttons: true,
        dangerMode: true,
      })
      .then((willSuccess) => {
        if (willSuccess) {
          $(".md-Load").show();
          var form1 = $('#form1')[0];
          var formData = new FormData(form1);
            $.ajax({
              url:'<?php echo e(url('editdatanewpo')); ?>',
                      headers: {
                          'X-CSRF-TOKEN': $('input[name="_token"]').val()
                      },
                      data: formData,
                      type: 'POST',
                      contentType: false,
                      processData: false,
                      cache:false,
                      success:function(ret){
                      $("#loadPage").hide();
                      swal({
                          text: "บันทึกข้อมูลเรียบร้อยแล้ว",
                          icon: "success",
                      })
                      // $("#error").html(ret);
                      window.location.replace("<?php echo e(url('tbpo')); ?>");
                      
                  },
                  error:function(err){
                      console.log(err);
                      $("#error").html(err.responseText);
                      $("#modal_error").modal('show');
                  }
              });
        } 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 showpomo(){

    $('#ShowPMModal').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){
         var textname =  addslashes(showpd[x].PdName);
         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(\''+textname+'\',\''+showpd[x].Price+'\',\''+showpd[x].idPd+'\',\''+amonuse+'\')"><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 addslashes(str) {
    str = str.replace(/\\/g, '\\\\');
    str = str.replace(/\'/g, '\\\'');
    str = str.replace(/\"/g, '\\"');
    str = str.replace(/\0/g, '\\0');
    return str;
  }

  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";

      }
    }
  }
  

  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;
  var total = 0;
  var vatt = 0;
  vatt = ((numeral($("#price_total").val()).value() * 7)/100) ;
  totalNet = numeral($("#price_total").val()).value() + numeral($("#price_tarn").val()).value() + vatt;
  $('#price_totalNet').val(numeral(totalNet).format('0,0'));
  $('#price_totalNetT').val(totalNet);
  $('#price_vat').val(numeral(vatt).format('0,0'));
  $('#price_vatT').val(vatt);
}

function deleteRow(e){
  tag = $(e).parent().parent();
  tag.remove();
  totalPrice();
  totalNet(); 
}

function addpromo(idpro,name,typepro,codepro,dic,detailpro,date,fulldic){
 $('#price_dis').val(dic);
 $('#price_disT').val(fulldic);

 totalNet();
 $('#carddic').html('<div class="card"><div class="card-header" style="background: #EC407A; color: #FFF">โปรโมชั่นสำหรับโค้ดนี้ ['+codepro+'] <button type="button" class="close"  onclick="deletePro()"><span aria-hidden="true">&times;</span></div><div class="card-body"><input type="hidden" name="idPro" value="'+idpro+'"><h5 class="card-title font-weight-bold" style="color: #880E4F"> ->>โปรโมชั่น '+name+'</h5><p class="card-text">หมายเหตุ : '+detailpro+'</p></div></div>');
 $("#ShowPMModal").modal('toggle');

}
function deletePro(){
  $( "#carddic" ).empty();
}

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

      }
    }
  }



  $(function(){ 

    $("#inputtimeStart").datepicker({
      format: "dd/mm/yyyy",
      todayBtn: "linked",
      clearBtn: true,
      language: "th",
      daysOfWeekHighlighted: "0",
      autoclose: true,
      todayHighlight: true
    });

    $("#timeStart").click(function(){
      $("#inputtimeStart").focus();
    });

    $("#inputtimeEnd").datepicker({
      format: "dd/mm/yyyy",
      todayBtn: "linked",
      clearBtn: true,
      language: "th",
      daysOfWeekHighlighted: "0",
      autoclose: true,
      todayHighlight: true
    });

    $("#timeEnd").click(function(){
      $("#inputtimeEnd").focus();
    });

  });
</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/except/resources/views/po/addpo.blade.php ENDPATH**/ ?>