@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,i) in owners" @click="selectTr===i+1?selectTr=0:selectTr=i+1" :class="selectTr===i+1?'focusing' : ''">
                        <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-danger" @click="restoreSelected(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
                ],
                selectTr:0
            },
            methods:{
                restoreSelected:function(owner){
                    let _this=this;
                    confirm('确定要恢复停用货主“' + owner.name + '”吗？');
                    let ajaxUrl='{{url("maintenance/owners/restoreSelected")}}';
                    axios.post(ajaxUrl,{id:owner.id}).then(function(response){
                        if(response.data.success){
                            tempTip.setDuration(1000);
                            tempTip.showSuccess('恢复货主'+owner.name+'成功');
                            window.location.reload();
                        }else{
                            tempTip.setDuration(2500);
                            tempTip.show('恢复货主失败，错误：'+response.data.fail_info);
                        }
                    }).catch(function (e) {
                        alert('网络连接错误：'+e);
                        tempTip.setDuration(2500);
                        tempTip.show('恢复货主失败，网络连接错误：'+e);
                    })
                },
            }
        });
    </script>
@endsection
