@extends('layouts.app')
@section('title')单位@endsection

@section('content')
    <span id="nav2">
        @component('maintenance.menu')@endcomponent
        @component('maintenance.unit.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="unit in units">
                        <td class="text-muted">@{{unit.id}}</td>
                        <td>@{{unit.name}}</td>
                        <td class="text-muted">@{{unit.created_at}}</td>
                        <td>
                            @can('计量单位-编辑')
                                <button class="btn btn-sm btn-outline-primary" @click="edit(unit.id)">改</button> @endcan
                            @can('计量单位-删除')
                                <button class="btn btn-sm btn-outline-dark" @click="destroy(unit)">删</button> @endcan
                        </td>
                    </tr>
                </table>
                {{$units->links()}}
            </div>
        </div>
    </div>
@endsection

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