@extends('layouts.app')
@section('title')查询-劳务所@endsection

@section('content')
    <span id="nav2">
        @component('maintenance.menu')@endcomponent
        @component('maintenance.laborCompany.menu')@endcomponent
    </span>
    <div class="container-fluid">
        <div class="card">
            <div class="card-body">
                @if(Session::has('successTip'))
                    <div class="alert alert-success h1">{{Session::get('successTip')}}</div>
                @endif
                <table class="table table-striped table-sm" id="list">
                    <tr>
                        <th>ID</th>
                        <th>劳务所名</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                    <tr v-for="(laborCompany,i) in laborCompanys"  @click="selectTr===i+1?selectTr=0:selectTr=i+1" :class="selectTr===i+1?'focusing' : ''">
                        <td class="text-muted">@{{laborCompany.id}}</td>
                        <td>@{{laborCompany.name}}</td>
                        <td class="text-muted">@{{laborCompany.created_at}}</td>
                        <td>
                            @can('劳务所-编辑')
                            <button class="btn btn-sm btn-outline-primary" @click="edit(laborCompany.id)">改</button> @endcan
                            @can('劳务所-删除')
                            <button class="btn btn-sm btn-outline-dark" @click="destroy(laborCompany)">删</button> @endcan
                        </td>
                    </tr>
                </table>
                {{$laborCompanys->links()}}
            </div>
        </div>
    </div>
@endsection

@section('lastScript')
    <script>
        new Vue({
            el:"#list",
            data:{
                laborCompanys:[
                    @foreach( $laborCompanys as $laborCompany )
                    {id:'{{$laborCompany->id}}',name:'{{$laborCompany->name}}',created_at:'{{$laborCompany->created_at}}'},
                    @endforeach
                ],
                selectTr:0
            },
            methods:{
                edit:function(id){
                    location.href = "{{url('maintenance/laborCompany')}}/"+id+"/edit";
                },
                destroy:function(laborCompany){
                    if(!confirm('确定要删除劳务所“' + laborCompany.name + '”吗？')){return};
                    let data=this;
                    let url = "{{url('maintenance/laborCompany')}}/"+laborCompany.id;
                    axios.delete(url,{id:laborCompany.id})
                        .then(function (response) {
                            if(response.data.success){
                                for (let i = 0; i < data.laborCompanys.length; i++) {
                                    if (data.laborCompanys[i].id===laborCompany.id){
                                        data.laborCompanys.splice(i,1);
                                        break;
                                    }
                                }
                                tempTip.setDuration(1000);
                                tempTip.showSuccess('删除劳务所"'+laborCompany.name+'"成功!')
                            }else{
                                tempTip.setDuration(1000);
                                tempTip.show('删除劳务所"'+laborCompany.name+'"失败!')
                            }
                        })
                        .catch(function (err) {
                            tempTip.setDuration(3000);
                            tempTip.show('删除物流公司失败!'+'网络错误：' + err)
                        });
                },
            }
        });
    </script>
@endsection
