<%- include ('../../partials/header.ejs') %>
<%- include ('../../partials/navbar.ejs') %>
<%- include ('../../partials/sidebar.ejs') %>

<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Tambah</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="/">Home</a></li>
              <li class="breadcrumb-item"><a href="/category">Nominal</a></li>
              <li class="breadcrumb-item active">Tambah</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-12">
            <div class="card card-primary">
                <div class="card-header">
                  <h3 class="card-title">Form Ubah Voucher <%= voucher.name %></h3>
                </div>
                <!-- /.card-header -->
                <!-- form start -->
                <form action="/voucher/edit/<%= voucher._id %>?_method=PUT" method="POST" enctype="multipart/form-data">
                  <div class="card-body">
                      <div class="form-group">
                        <label for="name">Nama Game</label>
                        <input type="text" name="name" value="<%= voucher.name %>" class="form-control" id="name" placeholder="Masukan nama game" required>
                      </div>
                    <div class="form-group">
                      <label>Kategori</label>
                      <select class="form-control select2" style="width: 100%;" name="category" required>
                        <option selected="selected">Pilih Kategori</option>
                        <% category.forEach(category => { %>
                            <option value="<%= category._id %>"
                                <% if(category._id.toString() === voucher?.category?._id.toString()) { %>
                                    selected
                                <% } %>
                            ><%= category.name %></option>
                        <% }) %>
                      </select>
                    </div>
                    <div class="form-group">
                      <label>Nominal</label>
                      <div class="select2-purple">
                          <select 
                            class="form-control select2" 
                            multiple="multiple" 
                            data-placeholder="Pilih Nominal"
                            style="width: 100%;" 
                            name="nominal" 
                            required
                          >
                            <% nominal.forEach(nominal => { %>
                                <option value="<%= nominal._id %>"
                                    <% voucher.nominals.forEach(voucherNominal => { %>
                                        <% if(nominal._id.toString() === voucherNominal._id.toString()) { %>
                                            selected
                                        <% } %>
                                    <% }) %>
                                >
                                <%= nominal.coinName %> <%= nominal.coinQuantity %></option>
                            <% }) %>
                          </select>
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="image">Gambar</label>
                      <input type="file" name="image" class="form-control" id="image" placeholder="Masukan gambar">
                      <br>
                      <img src="https://bwa-storegg-fatih.herokuapp.com/uploads/<%= voucher.thumbnail %>" id="preview">
                    </div>
                  </div>
                  <div class="card-footer">
                    <button type="submit" class="btn btn-primary">Simpan</button>
                  </div>
                </form>
              </div>
            <!-- /.card -->
            <!-- /.card -->
          </div>
          <!-- /.col -->
        </div>
        <!-- /.row -->
      </div>
      <!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>

  <%- include ('../../partials/footer.ejs') %>
  <%- include ('../../partials/js.ejs') %>

  <script>
    function readURL (input) {
      if(input.files && input.files[0]) {
        const reader = new FileReader();

        reader.onload = function(e) {
          $('#preview').attr('src', e.target.result);
          $('#preview').attr('width', '20%');
          $('#preview').attr('height', '20%');
        }

        reader.readAsDataURL(input.files[0])
      }
    }

    $('#image').change(function() {
      readURL(this);
    })
  </script>