@extends('layouts.app')
@section('title')货主@endsection

@section('content')
    <span id="nav2">
        @component('maintenance.menu')@endcomponent
        @component('maintenance.owner.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 table-hover" id="list">
                    <tr>
                        <th>ID</th>
                        <th>货主编码</th>
                        <th>货主名</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                    <tr v-for="owner in owners">
                        <td class="text-muted">@{{owner.id}}</td>
                        <td>@{{owner.code}}</td>
                        <td>@{{owner.name}}</td>
                        <td class="text-muted">@{{owner.created_at}}</td>
                        <td>
                            @can('货主-编辑')
                            <button class="btn btn-sm btn-outline-primary" @click="edit(owner.id)">改</button> @endcan
                            @can('货主-删除')
                            <button class="btn btn-sm btn-outline-danger" @click="destroy(owner)">停用</button> @endcan
                        </td>
                    </tr>
                </table>
                {{$owners->links()}}
            </div>
        </div>
    </div>
@endsection

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