 @extends('adminlte::page') @section('content')
<?php Fun::lang()?>
@include('administrator.construction.brand.create',compact('categories'))
@include('administrator.construction.brand.edit')
<div class="title-page">
    <div class="page-bar">
        <ul class="page-breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Dashboard</a></li>
            <li><a href="#">Construction</a></li>
            <li><a href="#">Set Up Information</a></li>
            <li><a href="#">Sub Category Product</a></li>
        </ul>
    </div>
</div>
    <div class="page">
        <div class="top-content">
            <div class="row">
                <div class="col-sm-12 col-xs-12">
                    <div class="title-page">
                        <div class="pull-left">
                            <h3>Sub Cagatery List</h3>
                        </div>
                    </div>
                    <div class="pull-right">
                        <a class="button_sm button_info" href="javascript:void(0)" id="filter-toggle-search"><span class="fa fa-plus"></span>  Filter Toggle</a>
                        @can('Construction-Product-Sub-Category-Create')
                            <a class="button_sm button_primary" href="#create-brand" data-toggle="modal"><span class="fa fa-plus"></span>  Create New</a>
                        @endcan
                    </div>
                </div>
            </div>
        </div>
        <div class="box-body table-responsive">
            <div class="row" id="toggle-seaching">
                <div class="col-md-6">
                    <div class="form-group">
                        <strong >Name :</strong>
                        {!! Form::text('sub_name',null, array('class' => 'form-control')) !!}
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <strong >Category :</strong>
                        {!! Form::text('category_name',null, array('class' => 'form-control')) !!}
                    </div>
                </div>
                <div class="col-md-12" style="text-align:right;margin-bottom:40px !important;">
                    <a class="button_sm button_success" href="javascript:void(0)" id="search-submit" onclick="searching()"><span class="fa fa-search"></span> Search</a>
                </div>
            </div>
            <table id="datatable_brand" class="table table-striped table-bordered table-hover nowrap">
                <thead>
                    <tr>
                        <th>NO</th>
                        <th>Name</th>
                        <th>Category</th>
                        <th>Description</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                   
                </tbody>
            </table>

        </div>
    </div>
    @endsection
    @section('js')
    <script type="text/javascript">
            function edit(obj)
            {
                $('#main-spinner').show();
                var id = $(obj).data("id");
                var route = "{{route('edit_data_brand',':id')}}";
                route = route.replace(":id",id);
                $.ajax({
                    url: route,
                    type: 'get',
                    dataType: 'JSON',
                })
                .done(function(data) {
                    $('#main-spinner').hide();
                    $("#add-brand").modal("show");
                    $("#name").val(data['brand'].name);
                    $("#description").val(data['brand'].description);
                    $("#id").val(data['brand'].id);
                    var opt=`<option value="">Please select</option>`;
                    $.each(data['categories'],function(index,value){
                        if(data['brand'].category_id==value.id){
                            opt += `<option selected value="${value.id}">${value.name}</option>`;
                        }
                        else{
                            opt += `<option value="${value.id}">${value.name}</option>`;
                        }
                    });
                    $("#input-select-category-edit").empty();
                    $("#input-select-category-edit").append(opt);
                })
                .fail(function() {
                    console.log("error");
                })
                .always(function() {
                    console.log("complete");
                });
                
            }
            $(document).ready(function() {
                $('#main-spinner').show();
                $('#datatable_brand').DataTable({
                    ordering: false,
                    "serverSide": true,
                    "scrollY":"600px",
                    "scrollCollapse": true,
                    "lengthMenu": [
                        [-1,20, 25, 50, 100, 500],
                        ["All",20, 25, 50, 100, 500]
                    ],
                    ajax:{
                        url:"{{ route('data.brand') }}",
                        type:"GET",
                        data:function(d){
                            var name = $("input[name=sub_name]").val();
                            var category_name = $("input[name=category_name]").val();
                            d.name = name;
                            d.category_name = category_name;
                        }
                    },
                    columns: [{
                        data: 'DT_RowIndex'
                    }, {
                        data: 'name'
                    },
                    {
                        data:'category_name'
                    },
                    {
                        data: 'description'
                    }
                    , {
                        data: 'action'
                    }, ],
                    "initComplete": function(settings, json) {
                        $('#main-spinner').hide();
                    }
                });

                $.fn.dataTable.ext.errMode = 'throw';
            });

            function searching(){
                $('#datatable_brand').DataTable().draw(true);
            }
    </script>
    @endsection