@extends('layouts.app')
@section('title')查询-省份@endsection

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

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