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

@section('content')
    <span id="nav2">
        @component('maintenance.menu')@endcomponent
        @component('maintenance.logistic.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>
                        <th>备注</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                    <tr v-for="(logistic,i) in logistics" @click="selectTr===i+1?selectTr=0:selectTr=i+1" :class="selectTr===i+1?'focusing' : ''">
                        <td class="text-muted">@{{logistic.id}}</td>
                        <td>@{{logistic.name}}&nbsp;<span class="badge badge-success" v-if="logistic.is_bunched=='Y'">子母单</span></td>
                        <td>@{{logistic.english_name}}</td>
                        <td>@{{logistic.code}}</td>
                        <td>@{{logistic.mobile}}</td>
                        <td>@{{logistic.type}}</td>
                        <td>@{{logistic.delivery_fee}}</td>
                        <td>@{{logistic.remark}}</td>
                        <td class="text-muted">@{{logistic.created_at}}</td>
                        <td>
                            @can('物流公司-编辑')
                            <button class="btn btn-sm btn-outline-primary" @click="edit(logistic.id)">改</button> @endcan
                            @can('物流公司-删除')
                            <button class="btn btn-sm btn-outline-dark" @click="destroy(logistic)">删</button> @endcan
                        </td>
                    </tr>
                </table>
                {{$logistics->links()}}
            </div>
        </div>
    </div>
@endsection

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