@extends('layouts.app')
@section('title')查询-工作组@endsection

@section('content')
    <span id="nav2">
        @component('maintenance.menu')@endcomponent
        @component('maintenance.userWorkgroup.menu')@endcomponent
    </span>
    <div class="container-fluid mt-3">
        <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>
                        <th>进组时需要备注</th>
                        <th>操作</th>
                    </tr>
                    <tr v-for="(userWorkgroup,i) in userWorkgroups"  @click="selectTr===i+1?selectTr=0:selectTr=i+1" :class="selectTr===i+1?'focusing' : ''">
                        <td class="text-muted">@{{userWorkgroup.id}}</td>
                        <td ><span v-if="userWorkgroup.warehouse">@{{userWorkgroup.warehouseName}}</span></td>
                        <td>@{{userWorkgroup.name}}</td>
                        <td class="text-muted">@{{userWorkgroup.created_at}}</td>
                        <td >@{{userWorkgroup.remark}}</td>
                        <td>
                            @can('工作组-编辑')
                                <button class="btn btn-sm btn-outline-primary" @click="edit(userWorkgroup.id)">改</button> @endcan
                            @can('工作组-删除')
                                <button class="btn btn-sm btn-outline-dark" @click="destroy(userWorkgroup)">删</button> @endcan
                        </td>
                    </tr>
                </table>
                {{$userWorkgroups->links()}}
            </div>
        </div>
    </div>
@endsection

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