@extends('layouts.app')
@section('title')计费模型-仓储计费@endsection

@section('content')
    <div id="nav2">
        @component('maintenance.menu')@endcomponent
        @component('maintenance.priceModel.storage.menu')
            @if(isset($model))
            <li class="nav-item">
                <a class="nav-link" href="{{URL::current()}}" :class="{active:isActive('edit',5)}">编辑</a>
            </li>
            @endif
        @endcomponent
    </div>
    <div class="container-fluid mt-2 card" id="container">
        <form method="POST" :action="model.id ? '{{url('maintenance/priceModel/storage/update')}}' : '{{url('maintenance/priceModel/storage/store')}}'" class="card-body offset-3">
            @csrf
            <label hidden><input :value="model.id" name="id"></label>
            <div class="row">
                <label for="name" class="col-2">名称</label>
                <input id="name" type="text" name="name" class="col-6 form-control"
                       :class="errors.name ? 'is-invalid' : ''" v-model="model.name">
                <span class="invalid-feedback mt-0 offset-2" role="alert" v-if="errors.name">
                    <strong>@{{ errors.name[0] }}</strong>
                </span>
            </div>
            <div class="row mt-3">
                <label class="col-2" for="owner_id">项目</label>
                <select id="owner_id" class="selectpicker" multiple data-live-search="true" title="项目(多选)" v-model="model.owner_id">
                    <option v-for="owner in owners" :value="owner.id">@{{ owner.name }}</option>
                </select>
                <label hidden><input :value="model.owner_id" name="owner_id"></label>
            </div>
            <div class="row mt-3">
                <label for="counting_type" class="col-2 text-info">计费类型</label>
                <select id="counting_type" name="counting_type" class="col-3 form-control"
                        :class="errors.counting_type ? 'is-invalid' : ''" v-model="model.counting_type">
                    <option v-for="type in counting_type" :value="type">@{{ type }}</option>
                </select>
                <span class="invalid-feedback mt-0 offset-2" role="alert" v-if="errors.counting_type">
                    <strong>@{{ errors.counting_type[0] }}</strong>
                </span>
            </div>
            <div class="row mt-3">
                <label for="using_type" class="col-2 text-info">用仓类型</label>
                <select id="using_type" name="using_type" class="col-3 form-control"
                        :class="errors.using_type ? 'is-invalid' : ''" v-model="model.using_type">
                    <option v-for="type in using_type" :value="type">@{{ type }}</option>
                </select>
                <span class="invalid-feedback mt-0 offset-2" role="alert" v-if="errors.using_type">
                    <strong>@{{ errors.using_type[0] }}</strong>
                </span>
            </div>
            <div class="row mt-3">
                <label for="minimum_area" class="col-2">最低起租面积</label>
                <input id="minimum_area" type="number" min="0" step="0.001" name="minimum_area" class="col-6 form-control"
                       :class="errors.minimum_area ? 'is-invalid' : ''" v-model="model.minimum_area">
                <span class="invalid-feedback mt-0 offset-2" role="alert" v-if="errors.minimum_area">
                    <strong>@{{ errors.minimum_area[0] }}</strong>
                </span>
            </div>
            <div class="row mt-3">
                <div class="col-5 row">
                    <label for="price" class="col-4 text-info">单价</label>
                    <input id="price" type="number" min="0" step="0.001" name="price" class="col-7 offset-1 form-control" required
                           :class="errors.price ? 'is-invalid' : ''" v-model="model.price">
                    <span class="invalid-feedback mt-0 offset-2" role="alert" v-if="errors.price">
                        <strong>@{{ errors.price[0] }}</strong>
                    </span>
                </div>
                <div class="col-3 row">
                    <label for="unit_id" class="col-4 text-info">单位</label>
                    <select id="unit_id" name="unit_id" class="col-8 form-control" required
                            :class="errors.unit_id ? 'is-invalid' : ''" v-model="model.unit_id">
                        <option v-for="unit in units" :value="unit.id" v-if="unit.name == 'm²' || unit.name == '㎡' || unit.name == 'm³'">@{{ unit.name }}</option>
                    </select>
                    <span class="invalid-feedback mt-0 offset-2" role="alert" v-if="errors.unit_id">
                        <strong>@{{ errors.unit_id[0] }}</strong>
                    </span>
                </div>
                <div class="col-3 row">
                    <label for="time_unit_id" class="col-4 text-info">计时</label>
                    <select id="time_unit_id" name="time_unit_id" class="col-8 form-control" required
                            :class="errors.time_unit_id ? 'is-invalid' : ''" v-model="model.time_unit_id">
                        <option v-for="unit in units" :value="unit.id" v-if="unit.name == '日' || unit.name == '月'">@{{ unit.name }}</option>
                    </select>
                    <span class="invalid-feedback mt-0 offset-2" role="alert" v-if="errors.unit_id">
                        <strong>@{{ errors.unit_id[0] }}</strong>
                    </span>
                </div>
            </div>
            <div class="row mt-3">
                <label for="discount_type" class="col-2 text-info">减免类型</label>
                <select id="discount_type" name="discount_type" class="col-3 form-control"
                        :class="errors.discount_type ? 'is-invalid' : ''" v-model="model.discount_type">
                    <option v-for="type in discount_type" :value="type">@{{ type }}</option>
                </select>
                <span class="invalid-feedback mt-0 offset-2" role="alert" v-if="errors.discount_type">
                    <strong>@{{ errors.discount_type[0] }}</strong>
                </span>
            </div>
            <div class="row mt-3">
                <label for="discount_value" class="col-2">减免值</label>
                <input id="discount_value" type="number" min="0" step="0.001" name="discount_value" class="col-6 form-control"
                       :class="errors.discount_value ? 'is-invalid' : ''" v-model="model.discount_value">
                <span class="invalid-feedback mt-0 offset-2" role="alert" v-if="errors.discount_value">
                    <strong>@{{ errors.discount_value[0] }}</strong>
                </span>
            </div>
            <div class="row mt-3 offset-1">
                <button type="submit" class="btn btn-success col-7">提交</button>
            </div>
        </form>
    </div>
@stop

@section("lastScript")
    <script>
        new Vue({
            el:"#container",
            data:{
                model : {
                    'id' : "{{isset($model) ? $model->id : ''}}",
                    'counting_type' : "{{old('counting_type') ?? (isset($model) ? $model->counting_type : '')}}",
                    name : "{{old('name') ?? (isset($model) ? $model->name : '')}}",
                    'using_type' : "{{old('using_type') ?? (isset($model) ? $model->using_type : '')}}",
                    'minimum_area' : "{{old('minimum_area') ?? (isset($model) ? $model->minimum_area : '')}}",
                    'discount_value' : "{{old('discount_value') ?? (isset($model) ? $model->discount_value : '')}}",
                    'price' : "{{old('price') ?? (isset($model) ? $model->price : '')}}",
                    'discount_type' : "{{old('discount_type') ?? (isset($model) ? $model->discount_type : '')}}",
                    'unit_id' : "{{old('unit_id') ?? (isset($model) ? $model->unit_id : '')}}",
                    'time_unit_id' : "{{old('time_unit_id') ?? (isset($model) ? $model->time_unit_id : '')}}",
                    owner_id:{!! old('owner_id') ? json_encode(old('owner_id')) : (isset($model) ? json_encode(array_column($model->owners->toArray(),"id")) : '[]') !!},
                },
                counting_type : ['包仓','灵活用仓','统单价'],
                using_type : ['常温','恒温'],
                discount_type : ['无减免','按单减免','固定减免'],
                units : [
                    @foreach($units as $unit)
                    {id:"{{$unit->id}}", name:"{{$unit->name}}"},
                    @endforeach
                ],
                errors:{!! $errors !!},
                owners:[
                    @foreach($owners as $model)
                    {id:"{{$model->id}}",name:"{{$model->name}}"},
                    @endforeach
                ],
            },
        });
    </script>
@stop