@extends('layouts.app')
@section('title')查询-品质标签@endsection

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

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