 <!-- Main content -->
<section class="content">
  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header">
            <div class="row">
              <div class="col-sm-6">
                <h3 class="card-title">Data Invoice</h3>
              </div>
              <div class="col-sm-6">
                <ol class="float-sm-right mb-0">
                  <a type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#staticBackdrop">Tambah</a>
                </ol>
              </div>
              <!-- Modal Tambah -->
              <div class="modal fade" id="staticBackdrop" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                <div class="modal-dialog">
                  <div class="modal-content">
                    <form method="post" enctype="multipart/form-data">
                      <div class="modal-header bg-primary">
                        <h5 class="modal-title" id="staticBackdropLabel">Tambah Invoice</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="card-body pb-0">
                            <div class="form-group">
                              <label>Customer</label>
                              <select class="form-control select2bs4" name="id_customer" required="required">
                                <option value="">Pilih</option>
                                <?php 
                                $List = new Listdata();
                                $List->set_list("customer", $SqlDataWHId." ORDER BY nama");
                                foreach ($List->get_list() as $key => $value): ?>
                                <option value="<?php echo $value['id']; ?>"><?php echo $value['nama']; ?></option>
                                <?php endforeach ?>
                              </select>
                            </div>
                            <div class="form-group">
                              <label>Tanggal Invoice</label>
                              <input type="date" class="form-control" name="tanggal_invoice" required="required">
                            </div>
                            <div class="form-group">
                              <label>Limit Invoice</label>
                              <input type="date" class="form-control" name="limit_invoice" required="required">
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <input type="submit" name="tambah" class="btn btn-primary" value="Tambah">
                      </div>
                    </form>
                    <?php
                    if (isset($_POST['tambah'])) 
                    {
                      $db2 = mysqli_query($con,"SELECT id_data_server FROM customer WHERE id = '$_POST[id_customer]'");
                      $pc2 = mysqli_fetch_assoc($db2);
                      $db1 = mysqli_query($con,"SELECT SUBSTRING(nomor_invoice,-4) AS num FROM invoice WHERE id_data_server = '$pc2[id_data_server]' ORDER BY nomor_invoice DESC Limit 1");
                      $pc1 = mysqli_fetch_assoc($db1);
                      $urutan = $pc1['num'] + 1;
                      $num = date('ym').'0000' + $urutan;
                      $nomorinv = "INV".$num;
                      $input = mysqli_query($con,"INSERT INTO invoice (id_data_server, id_customer, nomor_invoice, tanggal_invoice, limit_invoice) 
                        VALUES ('$pc2[id_data_server]', '$_POST[id_customer]', '$nomorinv', '$_POST[tanggal_invoice]', '$_POST[limit_invoice]')");
                      if ($input) 
                      {
                        sweetAlert("Invoice Telah di buat", "success");
                      }
                    }
                    ?>
                  </div>
                </div>
              </div>
              <!-- END Modal Tambah -->
            </div>
          </div>
          <!-- /.card-header -->
          <div class="card-body">
            <table id="example2" class="table table-bordered table-striped">
              <thead>
              <tr>
                <th>No</th>
                <th>Nomor Invoice</th>
                <th>Nama</th>
                <th>Alamat</th>
                <th>Nomor WhatsApp</th>
                <th>Tanggal Invoice</th>
                <th>Limit Invoice</th>
                <th>Jumlah Item</th>
                <th>Total Nominal</th>
                <th>Action</th>
              </tr>
              </thead>
              <tbody>
              <?php
                $no = 1;
                $aserver = mysqli_query($con,"SELECT * FROM v_invoice WHERE status = '0' $SqlDataId ORDER BY id,nomor_invoice DESC");
                while ($server = mysqli_fetch_assoc($aserver)) 
                {
              ?>
              <tr>
                <td style="max-width: 30px;"><?php echo $no; ?></td>
                <td><?php echo $server['nomor_invoice']; ?></td>
                <td><?php echo $server['nama']; ?></td>
                <td><?php echo $server['alamat']; ?></td>
                <td><?php echo $server['nomor_whatsapp']; ?></td>
                <td><?php echo substr(formatIndo($server['tanggal_invoice']),3); ?></td>
                <td><?php echo $server['limit_invoice']; ?></td>
                <td><?php echo $server['jumlah_list']; ?></td>
                <td><?php echo number_format($server['total_list']); ?></td>
                <td>
                  <form method="post">
                    <a href="?<?php echo encrypt_url('invoice_detail|'.$server['id']);?>" class="btn btn-info btn-sm m-1">Detail</a>
                    <a type="button" class="btn btn-secondary btn-sm m-1" data-toggle="modal" data-target="#modal-edit<?php echo $server['id'];?>">Edit</a>
                    <input type="submit" name="hapus<?php echo $server['id'];?>" value="Hapus" class="btn btn-danger btn-sm m-1" />
                    <input type="submit" name="arsip<?php echo $server['id'];?>" value="Arsipkan" class="btn btn-warning btn-sm m-1" />
                  </form>
                </td>
              </tr>
              <!-- Modal Edit -->
              <div class="modal fade" id="modal-edit<?php echo $server['id'];?>" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                <div class="modal-dialog modal-md">
                  <div class="modal-content">
                    <form method="post" enctype="multipart/form-data">
                      <div class="modal-header bg-secondary">
                        <h5 class="modal-title" id="staticBackdropLabel">Edit Invoice : <?php echo $server['nomor_invoice']; ?></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="card-body pb-0">
                            <div class="form-group">
                              <label>Nama Server</label>
                              <select class="form-control" name="ids">
                                <?php 
                                $List = new Listdata();
                                $List->set_list("customer", $SqlDataWHId." ORDER BY nama");
                                foreach ($List->get_list() as $key => $value): ?>
                                <option value="<?php echo $value['id']; ?>" <?php if($value['id'] == $server['id_customer']) { echo "selected";} ?> ><?php echo $value['nama']; ?></option>
                                <?php endforeach ?>
                              </select>
                            </div>
                            <div class="form-group">
                              <label>Tanggal Invoice</label>
                              <input type="text" class="form-control" value="<?php echo $server['tanggal_invoice']; ?>" name="tanggal_invoice" required="required">
                            </div>
                            <div class="form-group">
                              <label>Limit Invoice</label>
                              <input type="text" class="form-control" value="<?php echo $server['limit_invoice']; ?>" name="limit_invoice" required="required">
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <input type="submit" name="edit<?php echo $server['id']; ?>" class="btn btn-warning" value="Edit">
                      </div>
                    </form>
                    <?php
                    if (isset($_POST['edit'.$server['id']])) 
                    {
                      $db2 = mysqli_query($con,"SELECT id_data_server FROM customer WHERE id = '$_POST[id_customer]'");
                      $pc2 = mysqli_fetch_assoc($db2);
                      $update = mysqli_query($con,"UPDATE customer SET
                        id_data_server  = '$pc2[id_data_server]',
                        id_customer     = '$_POST[id_customer]',
                        tanggal_invoice = '$_POST[tanggal_invoice]',
                        limit_invoice   = '$_POST[limit_invoice]'
                        WHERE id        = '$server[id]' ");
                      if ($update) 
                      {
                        sweetAlert("Invoice ".$server['nomor_invoice']." telah di edit", "success");
                      }
                    }
                    
                    if (isset($_POST['arsip'.$server['id']]))
                    {
                      $update = mysqli_query($con,"UPDATE invoice SET
                        status      = '1'
                        WHERE id    = '$server[id]' ");
                      if ($update) 
                      {
                        sweetAlert("Invoice ".$server['nomor_invoice']." telah di Arsipkan", "success");
                      }
                    }
                    
                    if (isset($_POST['hapus'.$server['id']]))
                    {
                      $iddata = $server['id'];
                      deleteAlert("invoice",$iddata );
                    }
                    ?>
                  </div>
                </div>
              </div>
              <!-- END Modal Edit -->
            <?php $no++; } ?>
              </tbody>
            </table>
          </div>
          <!-- /.card-body -->
        </div>
        <!-- /.card -->
      </div>
      <!-- /.col -->
    </div>
    <!-- /.row -->
  </div>
  <!-- /.container-fluid -->
</section>
<!-- /.content -->
<!-- <script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2').select2()

    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })
</script> -->