@extends('layouts.app')
@section('title')查询-仓库@endsection

@section('content')
    <span id="nav2">
        @component('maintenance.menu')@endcomponent
        @component('maintenance.warehouse.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>
                        <th>SKU减产系数</th>
                        <th>联系人</th>
                        <th>联系电话</th>
                        <th>地址</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                    <tr v-for="(warehouse,i) in warehouses"  @click="selectTr===i+1?selectTr=0:selectTr=i+1" :class="selectTr===i+1?'focusing' : ''">
                        <td class="text-muted">@{{warehouse.id}}</td>
                        <td>@{{warehouse.name}}</td>
                        <td>@{{warehouse.code}}</td>
                        <td>@{{warehouse.production_capacity}}</td>
                        <td>@{{warehouse.reduced_production_capacity_coefficient}}%</td>
                        <td>@{{warehouse.principal}}</td>
                        <td>@{{warehouse.phone}}</td>
                        <td>@{{warehouse.address}}</td>
                        <td class="text-muted">@{{warehouse.created_at}}</td>
                        <td>
                            @can('仓库-编辑')
                            <button class="btn btn-sm btn-outline-primary" @click="edit(warehouse.id)">改</button> @endcan
                            @can('仓库-删除')
                            <button class="btn btn-sm btn-outline-dark" @click="destroy(warehouse)">删</button> @endcan
                        </td>
                    </tr>
                </table>
                {{$warehouses->links()}}
            </div>
        </div>
    </div>
@endsection

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