@extends('layouts.app')
@section('title')查询-城市@endsection

@section('content')
    <span id="nav2">
        @component('maintenance.menu')@endcomponent
        @component('maintenance.city.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>
                    </tr>
                    <tr v-for="(city,i) in cities" @click="selectTr===i+1?selectTr=0:selectTr=i+1" :class="selectTr===i+1?'focusing' : ''">
                        <td class="text-muted">@{{city.id}}</td>
                        <td>
                            @{{ city.province }}
                        </td>
                        <td>@{{city.name}}</td>
                        <td class="text-muted">@{{city.created_at}}</td>
                        <td>
                            @can('省份-编辑')
                                <button class="btn btn-sm btn-outline-primary" @click="edit(city.id)">改</button> @endcan
                            @can('省份-删除')
                                <button class="btn btn-sm btn-outline-dark" @click="destroy(city)">删</button> @endcan
                        </td>
                    </tr>
                </table>
                {{$cities->links()}}
            </div>
        </div>
    </div>
@endsection

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