@extends('layouts.app')
@section('title')商品@endsection

@section('content')
    <span id="nav2">
        @component('maintenance.menu')@endcomponent
        @component('maintenance.commodity.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>SKU</th>
                        <th>商品名</th>
                        <th>条码</th>
                        <th>货主</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                    <tr v-for="commodity in commodities">
                        <td class="text-muted">@{{commodity.id}}</td>
                        <td>@{{commodity.sku}}</td>
                        <td>@{{commodity.name}}</td>
                        <td>@{{commodity.barcode}}</td>
                        <td>@{{commodity.owner_name}}</td>
                        <td class="text-muted">@{{commodity.created_at}}</td>
                        <td>
                            @can('商品信息-编辑')
                            <button class="btn btn-sm btn-outline-primary" @click="edit(commodity.id)">改</button> @endcan
                            @can('商品信息-删除')
                            <button class="btn btn-sm btn-outline-dark" @click="destroy(commodity)">删</button> @endcan
                        </td>
                    </tr>
                </table>
                {{$commodities->links()}}
            </div>
        </div>
    </div>
@endsection

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