@extends(branchMenu())
@section('content')
    <?php Fun::lang()?>
    <div class="page">
        <div class=container-fluid>
            <div class="top-content">
                <div class="row">
                    <div class="col-sm-12 col-xs-12">
                        <div class="title-add">
                            <div class="pull-left">
                                <h3>{{__('administrator.warehouse')}}</h3>
                            </div>
                            <div class="pull-right">
                                @can('Warehouse Create')
                                    <a onclick="getHtmlForm('{{route('warehouse.create')}}','{{__('administrator.create')}} {{__('administrator.warehouse')}}','500px')" class=" button_info">
                                        <span class="fa fa-plus"></span> {{__('administrator.new_warehouse')}}
                                    </a>
                                @endcan
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="wrapper-table">
                <table id="datatable" width="100%" class="table table-striped table-hover ">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>{{__('administrator.branch')}}</th>
                        <th>{{__('administrator.warehouse_name')}}</th>
                        <th>{{__('administrator.warehouse_type')}}</th>
                        <th>{{__('administrator.tailor')}}</th>
                        <th>{{__('administrator.description')}}</th>
                        <th>{{__('administrator.action')}}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($data as $key => $value)
                        @php
                            $br = findBranch($value->branch_id);
                            $bg = '';
                            if($value->warehouse_type=='item'){
                                $bg = 'bg-success';
                            }elseif($value->warehouse_type=='material'){
                                $bg = 'bg-warning';
                            }elseif($value->warehouse_type=='fabric'){
                                $bg = 'bg-info';
                            }elseif($value->warehouse_type=='cut_piece'){
                                $bg = '';
                            }else{
                                 $bg = 'bg-danger';
                            }
                            $tailor = findTailor($value->tailor_id);
                            $check_wh = \App\Model\stock_inventory\StockModel::where('warehouse_id',$value->id)->first();
                        @endphp
                        <tr>
                            <td class="{{$bg}}">{{$key+1}}</td>
                            <td class="{{$bg}}">{{$br?$br->name:''}}</td>
                            <td class="{{$bg}}"><span class="wh_name">{{$value->warehouse_name}}</span></td>
                            <td class="{{$bg}}">{{$value->warehouse_type?getWarehouseType($value->warehouse_type):''}}</td>
                            <td class="{{$bg}}">{{$tailor?$tailor->name:''}}</td>
                            <td class="{{$bg}}">{{$value->description}}</td>
                            <td style="width: 200px;" class="{{$bg}}">
                                @php
                                    $user_role = Auth::user()->branch_id;
                                    $user_role = findBranch($user_role);
                                 @endphp
                                @can('Warehouse Edit')
                                    <a data-href="{{route('warehouse.edit',$value->id)}}"
                                       class="button_warning btn-edit">{{__('administrator.edit')}}</a>
                                @endcan
                                @can('Warehouse Set-main')
                                        @if($user_role->main===1 || 1)

                                        @if($value->warehouse_type !== 'tailor' && $value->warehouse_type !== 'cut_piece')
                                        <a herf="#"
                                           class="
                                @if($value->main==1)
                                               button_primary
                                        @else
                                               button_info
                                        @endif"
                                           @if($value->main==0)
                                           onclick="set_main('{{$value->id}}')"
                                            @endif >
                                            @if($value->main==1)
                                                {{__('administrator.main')}}
                                            @else
                                                {{__('administrator.set')}}
                                            @endif
                                        </a>
                                    @endif
                                    @endif
                                @endcan
                                @can('Delete Warehouse')
                                    @if(!$check_wh)
                                        <a class="button_danger btn_delete"
                                           data-href="{{route('warehouse.delete')}}?id={{$value->id}}">{{__('administrator.delete')}}</a>
                                    @endif
                                @endcan
                            </td>
                        </tr>
                    @endforeach()
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection

@section('script')

    <script type="text/javascript">
        let table_ =  $('#datatable');
        table_.DataTable({
            "lengthMenu": [20, 30, 50,100]
        });
        table_.on('click', '.btn_delete', function () {
            let text = $(this).closest('tr').find('.wh_name').text();
            let url = $(this).data('href');
            $.confirm({
                theme: 'modern',
                title: '{{__('administrator.are_you_sure')}}',
                content: 'Warehouse Name :' + text,
                buttons: {
                    delete: {
                        btnClass: 'button_danger',
                        text: '{{__('administrator.delete')}}',
                        action: function () {
                            $.ajax({
                                method: 'get',
                                url: url,
                                success: function (res) {
                                    if (res) {
                                        window.location.reload();
                                    }
                                },
                                error: function (xhr) {
                                    alertErrorFormController(xhr);
                                }
                            });
                        }
                    },
                    cancel: {
                        btnClass: 'button_warning',
                        text: '{{__('administrator.cancel')}}',
                        action: function () {

                        }
                    },
                }
            });
        });
        table_.on('click', '.btn-edit', function () {
            let url = $(this).data('href');
            getHtmlForm(url, '{{__('administrator.edit')}} {{__('administrator.warehouse')}}', '500px')
        });
        function set_main(id) {
            if ($.confirm({
                title: '<strong style="color:darkcyan;">{{__('administrator.change_status')}}</strong>',
                content: "<b>Want to Change Main Warehouse ? "
                    + " </b>Then Click OK",

                buttons: {
                    '{{__('administrator.cancel')}}': {
                        btnClass: 'btn-red',
                        action: function () {
                        }

                    },
                    '{{__('administrator.ok')}}': {
                        btnClass: 'btn-blue',
                        action: function () {
                            $.ajax({
                                type: "GET",
                                dataType: "json",
                                url: '{{route("warehouse_changeStatus")}}',
                                data: {'id': id},
                                success: function (data) {
                                    location.reload();
                                }
                            });
                        }
                    }
                }
            }))

                return false;
        }
    </script>
@stop



