
@extends('layouts.app')
@section('title')查询-承运商@endsection

@section('content')
    <span id="nav2">
        @component('maintenance.menu')@endcomponent
        @component('maintenance.carrier.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>
                        <th>备注</th>
                        <th>录入时间</th>
                        <th>操作</th>
                    </tr>
                    <tr v-for="carrier in carriers" @click="selectTr===carrier.id?selectTr=0:selectTr=carrier.id" :class="selectTr===carrier.id ? 'focusing' : ''">
                        <td class="text-muted">@{{carrier.id}}</td>
                        <td>@{{carrier.name}}</td>
                        <td>@{{carrier.mobile}}</td>
                        <td>@{{carrier.delivery_fee}}</td>
                        <td>@{{carrier.remark}}</td>
                        <td class="text-muted">@{{carrier.created_at}}</td>
                        <td>
                            @can('承运商-编辑')
                                <button class="btn btn-sm btn-outline-primary" @click="edit(carrier.id)">改</button> @endcan
                            @can('承运商-删除')
                                <button class="btn btn-sm btn-outline-dark" @click="destroy(carrier)">删</button> @endcan
                        </td>
                    </tr>
                </table>
                {{$carriers->links()}}
            </div>
        </div>
    </div>
@endsection

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