@extends('layouts.app')
@section('title')查询-车辆@endsection

@section('content')
    <span id="nav2">
        @component('maintenance.menu')@endcomponent
        @component('maintenance.carType.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>
                    </tr>
                    <tr v-for="carType in carTypes" @click="selectTr===carType.id?selectTr=carType.id:selectTr=carType.id" :class="selectTr===carType.id?'focusing':''">
                        <td class="text-muted">@{{carType.id}}</td>
                        <td>@{{carType.name}}</td>
                        <td>@{{carType.model}}</td>
                        <td>@{{carType.length}}</td>
                        <td>@{{carType.load}}</td>
                        <td>@{{carType.remark}}</td>
                        <td class="text-muted">@{{carType.created_at}}</td>
                        <td>
                            @can('车型-编辑')
                                <button class="btn btn-sm btn-outline-primary" @click="edit(carType.id)">改</button> @endcan
                            @can('车型-删除')
                                <button class="btn btn-sm btn-outline-dark" @click="destroy(carType)">删</button> @endcan
                        </td>
                    </tr>
                </table>
                {{$carTypes->links()}}
            </div>
        </div>
    </div>
@endsection

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