<!-- Page Heading -->
@extends('template.index')
@section('content')
    <!-- Content Row -->
    <div class="card">
        <div class="card-body">
            <div class="clearfix mb-3">
                <div class="float-left">
                    <h5><a href="{{ url('admin/category') }}"><b> Category ({{ $kategori->nama_kategori }})</b> </a> > <a href="{{ url('admin/subcategory/'.$sub_kategori->id_sub_kategori ) }}"><b> Sub Category  ({{ $sub_kategori->nama_sub_kategori }}) </b> </a> > Item </h5>
                </div>
                <div class="float-right">
                    <a href="" data-target="#add" data-toggle="modal" class="btn btn-primary text-white"><i
                            class="fa fa-plus"></i> Tambah</a>
                </div>
            </div>
            <div class="table-responsive">
                <table class="table" id="myTable">
                    <thead>
                        <tr class="bg-primary text-white">
                            <th scope="col">#</th>
                            <th scope="col">Item Name</th>
                            <th scope="col">Part Number</th>
                            {{-- <th scope="col">Portion</th>
                            <th scope="col">Portion From</th>
                            <th scope="col">Portion To</th> --}}
                            <th scope="col">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($barang as $barang)
                            <tr>
                                <td>{{ $loop->iteration }}</td>
                                <td>{{ $barang->nama_barang }}</td>
                                <td>{{ $barang->nomor_barang }}</td>
                                {{-- <td>{{ $barang->barang_portion }}</td>
                                <td>{{ $barang->barang_portion_from }}</td>
                                <td>{{ $barang->barang_portion_to }}</td> --}}
                                <td>
                                    <button onclick="onClickEdit({{ $barang }})"
                                        class="btn btn-warning btn-sm rounded mb-3" title="Ubah" data-target="#edit"
                                        data-toggle="modal"><i class="fa fa-edit"></i> Edit</button>
                                    <a href="{{ route('component.index', [$kategori->id_kategori, $sub_kategori->id_sub_kategori, $barang->id_barang]) }}"
                                        class="btn btn-primary btn-sm rounded mb-3" title="Lihat"><i
                                            class="fa fa-info"></i> View Sub</a>
                                    <form action="{{ route('delete.item', $barang->id_barang) }}" method="POST"
                                        onsubmit="return confirm('Are you sure you want to delete this data ?');">
                                        @csrf
                                        <button type="submit" class="btn mb-2 btn-danger btn-sm rounded mb-3"
                                            title="Hapus"><i class="fa fa-trash"></i> Delete</button>
                                    </form>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="modal fade" id="add" tabindex="-1">
        <form action="{{ route('item.create', $sub_kategori->id_sub_kategori) }}" method="POST">
            @csrf
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Add Item</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="form-group">
                    <label for="">Pilih Kategori</label>
                    <select name="" id="" class="form-control select2" style="width: 100%">
                        <option value="">Kategori 1</option>
                        <option value="">Kategori 2</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="">Pilih Sub Kategori</label>
                    <select name="" id="" class="form-control select2" style="width: 100%">
                        <option value="">Kategori 1</option>
                        <option value="">Kategori 2</option>
                    </select>
                </div> --}}
                        <div class="form-group">
                            <label for="">Item Name</label>
                            <input type="text" name="nama_barang" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="">Part Number</label>
                            <input type="text" name="nomor_barang" class="form-control">
                        </div>
                        <div class="form-group" hidden>
                            <label for="">Portion (%)</label>
                            <input type="text" value="0" name="barang_portion" class="form-control">
                        </div>
                        <div class="form-group" hidden>
                            <label for="">Portion From (%)</label>
                            <input type="text" value="0" name="barang_portion_from" class="form-control">
                        </div>
                        <div class="form-group" hidden>
                            <label for="">Portion To (%)</label>
                            <input type="text" value="0" name="barang_portion_to" class="form-control">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-success">Save</button>
                    </div>
                </div>
            </div>
        </form>
    </div>

    <div class="modal fade" id="edit" tabindex="-1">
        <form action="/" method="post" id="form_edit">
            @csrf
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Ubah Barang</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="form-group">
                    <label for="">Pilih Kategori</label>
                    <select name="" id="" class="form-control select2" style="width: 100%">
                        <option value="">Kategori 1</option>
                        <option value="">Kategori 2</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="">Pilih Sub Kategori</label>
                    <select name="" id="" class="form-control select2" style="width: 100%">
                        <option value="">Kategori 1</option>
                        <option value="">Kategori 2</option>
                    </select>
                </div> --}}
                        <div class="form-group">
                            <label for="">Item Name</label>
                            <input type="text" id="nama_barang" name="nama_barang" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="">Part Number</label>
                            <input type="text" id="nomor_barang" name="nomor_barang" class="form-control">
                        </div>
                        <div class="form-group" hidden>
                            <label for="">Portion (%)</label>
                            <input type="text" value="0" id="barang_portion" name="barang_portion" class="form-control">
                        </div>
                        <div class="form-group" hidden>
                            <label for="">Portion From (%)</label>
                            <input type="text" value="0" id="barang_portion_from" name="barang_portion_from"
                                class="form-control">
                        </div>
                        <div class="form-group" hidden>
                            <label for="">Portion To (%)</label>
                            <input type="text" value="0" id="barang_portion_to" name="barang_portion_to" class="form-control">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-success">Save</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
@endsection
<script>
    function onClickEdit(val) {
        var formMethod = document.getElementById('form_edit');
        var id_barang = document.getElementById('id_barang');
        var nama_barang = document.getElementById('nama_barang');
        var nomor_barang = document.getElementById('nomor_barang');
        var barang_portion = document.getElementById('barang_portion');
        var barang_portion_from = document.getElementById('barang_portion_from');
        var barang_portion_to = document.getElementById('barang_portion_to');
        // id_kategori.value = val.id_kategori;
        nama_barang.value = val.nama_barang;
        nomor_barang.value = val.nomor_barang;
        barang_portion.value = val.barang_portion;
        barang_portion_from.value = val.barang_portion_from;
        barang_portion_to.value = val.barang_portion_to;
        formMethod.action = "/admin/item/" + val.id_barang;
    }
</script>
