<?php Fun::lang() ?>
@extends('layouts.app-accounting')
@section('content')
    <div class="page">
        <div>
            <div class="row">
                <div class="col-sm-12 col-xs-12">
                    <div class="title-add">
                        <div class="pull-left">
                            <h3>{{__('administrator.account_chart')}}</h3>
                        </div>
                        <div class="pull-right">
                            <a class="button_sm button_primary" href="{{route('account-chart.create')}}"><span class="fa fa-plus"></span>  {{__('administrator.create_new')}}</a>

                        </div>
                    </div>
                </div>
            </div>
        </div>
        <br>
        <div class="box-body table-responsive">
            <table id="datatable_account_chart" class="table table-bordered table-hover nowrap">
                <thead>
                <tr>
                    <th>{{__('administrator.account_code')}}</th>
                    <th>{{__('administrator.account_name')}}</th>
                    <th>{{__('administrator.account_namekh')}}</th>
                    <th>{{__('administrator.account_type')}}</th>
                    <th>{{__('administrator.description')}}</th>
                    <th>{{__('administrator.action')}}</th>
                </tr>
                </thead>
                <tbody>
                @if(isset($account_chart) && count($account_chart) > 0)
                    @foreach($account_chart as $key => $acc_chart)
                        <tr id="{{$acc_chart->id}}">
                            <td>{{$acc_chart->id}}</td>
                            <td>{{$acc_chart->acc_name}}</td>
                            <td>{{$acc_chart->acc_namekh}}</td>
                            <td>{{$acc_chart->acctype_name}}</td>
                            <td>{{$acc_chart->acc_des}}</td>
                            <td>@if($acc_chart->acc_status != 0)
                                    <a href="{{route('account-chart.edit',$acc_chart->id)}}" class="btn btn-success btn-xs" style="margin-right: 5px"><i class="fa fa-edit" title="Edit"></i></a>

                                    <a href="#" data-id = "{{$acc_chart->id}}" data-toggle="modal" data-target="#form-delete" class="btn-xs btn btn-danger confirm_delete" data-toggle="tooltip" data-placement="top" title="Delete"><i class="fa fa-trash"></i></a>
                                @endif
                            </td>
                        </tr>
                    @endforeach
                @endif
                </tbody>
            </table>
        </div>
    </div>
    @include('Accounting.account-chart.confirm-delete')
@endsection
@section('js')
    <script type="text/javascript">
        $(document).ready(function() {
            var check_edit = "<?php echo Gate::check('Account-Chart-Edit'); ?>";
            var check_delete = "<?php echo Gate::check('Account-Chart-Delete'); ?> ";
            var sub_acc_cart = <?php echo json_encode($sub_account_chart)?>;
            $.each(sub_acc_cart,function (index,val) {
                var route_edit = "{{route('account-chart.edit',':id')}}";
                route_edit = route_edit.replace(':id', val.id);
                var btn_edit = '';
                var btn_delete = '';
                if(check_edit == 1)
                {
                    btn_edit=`<a href="${route_edit}" class="btn btn-success btn-xs" style="margin-right: 5px"><i class="fa fa-edit" title="Edit"></i></a>`;
                }
                if(check_delete == 1)
                {
                    btn_delete = `<a href="#" data-id = "${val.id}" data-toggle="modal" data-target="#form-delete" class="btn-xs btn btn-danger confirm_delete" data-toggle="tooltip" data-placement="top" title="Delete"><i class="fa fa-trash"></i></a>`;
                }
                $("tbody").find("tr#"+val.acctype).after(`<tr class="bg-sub">
            <td class="pl-4">&#9830; ${val.id}</td>
            <td>${val.acc_name}</td>
            <td>${val.acc_namekh}</td>
            <td>${val.acctype_name != null ? val.acctype_name : ''}</td>
            <td>${val.acc_des !=null ? val.acc_des.substring(0,30) : ''}</td>
            <td>${btn_edit} ${btn_delete}</td>
        </tr>`);
            });

           $('#datatable_account_chart').DataTable({
                ordering:false,
                info:false,
                "lengthMenu": [[20, 30, 50, -1], [20, 30, 50, "All"]]
            });
            $(document).on('click','.confirm_delete',function(){
                var id = $(this).data('id');
                $('#delete-id').val(id);
                $('#form-delete').modal('show');
            });
        });
    </script>
@endsection
