@extends('layouts.app')
@section('title')查询-测量设备@endsection

@section('content')
    <span id="nav2">
        @component('maintenance.menu')@endcomponent
        @component('maintenance.measuringMachine.menu')@endcomponent
    </span>
    <div class="container-fluid" id="list">
        <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="(measuringMachine,i) in measuringMachines" @click="selectTr===i+1?selectTr=0:selectTr=i+1" :class="selectTr===i+1?'focusing' : ''">
                        <td class="text-muted">@{{measuringMachine.id}}</td>
                        <td>@{{measuringMachine.name}}</td>
                        <td class="text-muted">@{{measuringMachine.code}}</td>
                        <td class="text-muted">@{{measuringMachine.status}}</td>
                        <td>
                            @can('测量设备-编辑')
                                <button class="btn btn-sm btn-outline-primary" @click="edit(measuringMachine.id)">改</button> @endcan
                            @can('测量设备-删除')
                                <button class="btn btn-sm btn-outline-dark" @click="destroy(measuringMachine)">删</button> @endcan
                        </td>
                    </tr>
                </table>
                    {{$measuringMachines->links()}}
            </div>
        </div>
    </div>
@endsection

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