<!-- 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 </b> <b>({{ $kategori->nama_kategori }})</b> > </a> Sub Category
                    </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> Add</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">Sub Category 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 ($sub_kategori as $sub)
                            <tr>
                                <td>{{ $loop->iteration }}</td>
                                <td>{{ $sub->nama_sub_kategori }}</td>
                                <td>{{ $sub->nomor_sub_kategori }}</td>
                                {{-- <td>{{ $sub->sub_kategori_portion }}</td>
                                <td>{{ $sub->sub_kategori_portion_from }}</td>
                                <td>{{ $sub->sub_kategori_portion_to }}</td> --}}
                                <td>
                                    <button type="button" onclick="onClickEdit({{ $sub }})"
                                        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('item.index', [$kategori->id_kategori, $sub->id_sub_kategori]) }}"
                                        class="btn btn-primary btn-sm rounded mb-3" title="Lihat"><i
                                            class="fa fa-info"></i> View Sub</a>
                                    <form action="{{ route('delete.subcategory', $sub->id_sub_kategori) }}" 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('store.subcategory') }}" method="post">
            @csrf
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Add Sub Category</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> --}}
                        <input type="text" name="id_kategori" value="{{ $kategori->id_kategori }}" hidden>
                        <div class="form-group">
                            <label for="">Sub Category Name</label>
                            <input type="text" name="nama_sub_kategori" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="">Part Number</label>
                            <input type="text" name="nomor_sub_kategori" class="form-control">
                        </div>
                        <div class="form-group" hidden>
                            <label for="">Portion (%)</label>
                            <input type="text" value="0" name="sub_kategori_portion" class="form-control">
                        </div>
                        <div class="form-group" hidden>
                            <label for="">Portion From (%)</label>
                            <input type="text" value="0" name="sub_kategori_portion_from" class="form-control">
                        </div>
                        <div class="form-group" hidden>
                            <label for="">Portion To (%)</label>
                            <input type="text" value="0" name="sub_kategori_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="" id="form_edit" method="post">
            @csrf
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Ubah Sub Kategori</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> --}}
                        {{-- <input type="text" id="id_kategori" name="id_kategori"> --}}
                        <div class="form-group">
                            <label for="">Sub Category Name</label>
                            <input type="text" id="nama_sub_kategori" name="nama_sub_kategori" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="">Part Number</label>
                            <input type="text" id="nomor_sub_kategori" name="nomor_sub_kategori"
                                class="form-control">
                        </div>
                        <div class="form-group" hidden>
                            <label for="">Portion (%)</label>
                            <input type="text" value="0" id="sub_kategori_portion" name="sub_kategori_portion"
                                class="form-control">
                        </div>
                        <div class="form-group" hidden>
                            <label for="">Portion From (%)</label>
                            <input type="text" value="0" id="sub_kategori_portion_from" name="sub_kategori_portion_from"
                                class="form-control">
                        </div>
                        <div class="form-group" hidden>
                            <label for="">Portion To (%)</label>
                            <input type="text" value="0" id="sub_kategori_portion_to" name="sub_kategori_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) {
        console.log(val);
        var formMethod = document.getElementById('form_edit');
        var id_kategori = document.getElementById('id_kategori');
        var nama_sub_kategori = document.getElementById('nama_sub_kategori');
        var nomor_sub_kategori = document.getElementById('nomor_sub_kategori');
        var sub_kategori_portion = document.getElementById('sub_kategori_portion');
        var sub_kategori_portion_from = document.getElementById('sub_kategori_portion_from');
        var sub_kategori_portion_to = document.getElementById('sub_kategori_portion_to');
        // id_kategori.value = val.id_kategori;
        nama_sub_kategori.value = val.nama_sub_kategori;
        nomor_sub_kategori.value = val.nomor_sub_kategori;
        sub_kategori_portion.value = val.sub_kategori_portion;
        sub_kategori_portion_from.value = val.sub_kategori_portion_from;
        sub_kategori_portion_to.value = val.sub_kategori_portion_to;
        formMethod.action = "/admin/subcategory/" + val.id_sub_kategori;
    }
</script>
