@extends('layouts.app')

@section('content')
    <div id="nav2">
        @component('transport.waybill.menu')
            <li class="nav-item">
                <a class="nav-link" href="{{URL::current()}}" :class="{active:isActive('waybillEdit',2)}">修改</a>
            </li>
        @endcomponent
    </div>
    <div class="container-fluid mt-3" id="container">
        <div class="card">
            <div class="card-body">
                <form method="POST" action='{{url("transport/waybill/waybillUpdate/{$waybill->id}")}}'>
                    @csrf
                    <div class="form-group row">
                        <label for="type" class="col-2 col-form-label text-right text-primary">运单类型 *</label>
                        <div class="h5 mb-3 col-8">
                            <button type="button" @click="model.type='专线'" class="btn" :class="model.type=='专线'?'btn-primary':'btn-outline-primary'">专线</button>
                            <button type="button" @click="model.type='直发车'" class="btn ml-2" :class="model.type!='专线'?'btn-primary':'btn-outline-primary'">直发车</button>
                        </div>
                    </div>
                    <label hidden><input type="text" name="type" :value="model.type"></label>
                    <div class="form-group row">
                        <label for="wms_bill_number" class="col-2 col-form-label text-right text-muted">WMS单号</label>
                        <div class="col-8">
                            <input id="wms_bill_number" type="text" class="form-control @error('wms_bill_number') is-invalid @enderror"
                                   name="wms_bill_number" autocomplete="off" value="@if(old('wms_bill_number')){{ old('wms_bill_number') }}@else{{$waybill->wms_bill_number}}@endif"
                                   @change="bindOrder($event)">
                            @error('wms_bill_number')
                            <span class="invalid-feedback" role="alert"><strong>{{ $message }}</strong></span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="owner_id" class="col-2 col-form-label text-right text-primary">货主 *</label>
                        <div class="col-8">
                            <select id="owner_id" class="form-control" style="width: 30%;" v-model="model.owner_id"
                                    :name="model.order_id ? '' : 'owner_id'" :disabled="!!model.order_id">
                                @foreach($owners as $owner)
                                    @if($owner->id==$waybill->owner_id)
                                        <option value="{{$owner->id}}" selected>{{$owner->name}}</option>
                                    @else
                                        <option value="{{$owner->id}}">{{$owner->name}}</option>
                                    @endif
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <label hidden><input :value="model.order_id" name="order_id"></label>
                    <div class="form-group row">
                        <label for="origination" class="col-2 col-form-label text-right text-primary">始发地 *</label>
                        <div class="col-8">
                            <input id="origination" type="text" class="form-control @error('origination') is-invalid @enderror"
                                   name="origination" autocomplete="off" value="@if(old('origination')){{ old('origination') }}@else{{$waybill->origination}}@endif"  >
                            @error('origination')
                            <span class="invalid-feedback" role="alert"><strong>{{ $message }}</strong></span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="destination" class="col-2 col-form-label text-right text-primary">目的地 *</label>
                        <div class="col-8">
                            <input id="destination" type="text" class="form-control @error('destination') is-invalid @enderror" autocomplete="off" required
                                   :readonly="!!model.order_id" :name="model.order_id ? '' : 'destination'"  :value="model.order_id ? order.address : '{{old('destination') ?? $waybill->destination}}'"  >
                            @error('origination')
                            <span class="invalid-feedback" role="alert"><strong>{{ $message }}</strong></span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="recipient" class="col-2 col-form-label text-right text-primary">收件人 *</label>
                        <div class="col-8">
                            <input id="recipient" type="text" class="form-control @error('recipient') is-invalid @enderror" autocomplete="off" required
                                   :readonly="!!model.order_id" :name="model.order_id ? '' : 'recipient'"  :value="model.order_id ? order.consignee_name : '{{old('recipient') ?? $waybill->recipient}}'">
                            @error('recipient')
                            <span class="invalid-feedback" role="alert"><strong>{{ $message }}</strong></span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="recipient_mobile" class="col-2 col-form-label text-right text-primary">收件人电话 *</label>
                        <div class="col-8">
                            <input id="recipient_mobile" type="text" class="form-control @error('recipient_mobile') is-invalid @enderror" autocomplete="off" required
                                   :readonly="!!model.order_id" :name="model.order_id ? '' : 'recipient_mobile'"  :value="model.order_id ? order.consignee_phone : '{{old('recipient_mobile') ?? $waybill->recipient_mobile}}'">
                            @error('recipient_mobile')
                            <span class="invalid-feedback" role="alert"><strong>{{ $message }}</strong></span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="charge" class="col-2 col-form-label text-right text-muted">运输收费(元)</label>
                        <div class="col-8">
                            <input id="charge" type="text" class="form-control @error('charge') is-invalid @enderror"
                                   name="charge" autocomplete="off" value="@if(old('charge')){{ old('charge') }}@else{{$waybill->charge}}@endif"  >
                            @error('charge')
                            <span class="invalid-feedback" role="alert"><strong>{{ $message }}</strong></span>
                            @enderror
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="other_charge" class="col-2 col-form-label text-right text-muted">其他收费(元)</label>
                        <div class="col-8">
                            <input id="other_charge" type="text" class="form-control @error('other_charge') is-invalid @enderror"
                                   name="other_charge" autocomplete="off" value="@if(old('other_charge')){{ old('other_charge') }}@else{{$waybill->other_charge}}@endif"  >
                            @error('other_charge')
                            <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="other_charge_remark" class="col-2 col-form-label text-right text-muted">其他收费备注</label>
                        <div class="col-8">
                            <input id="other_charge_remark" type="text" class="form-control @error('other_charge') is-invalid @enderror"
                                   name="other_charge_remark" autocomplete="off" value="@if(old('other_charge_remark')){{ old('other_charge_remark') }}@else{{$waybill->other_charge_remark}}@endif"  >
                            @error('other_charge_remark')
                            <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>·
                            @enderror
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="collect_fee" class="col-2 col-form-label text-right text-muted">到付金额(元)</label>
                        <div class="col-8">
                            <input id="collect_fee" type="text" class="form-control @error('collect_fee') is-invalid @enderror"
                                   name="collect_fee" autocomplete="off" value="@if(old('collect_fee')){{ old('collect_fee') }}@else{{$waybill->collect_fee}}@endif"  >
                            @error('collect_fee')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="ordering_remark" class="col-2 col-form-label text-right text-muted">下单备注</label>
                        <div class="col-8">
                            <textarea id="ordering_remark" class="form-control @error('ordering_remark') is-invalid @enderror"
                                      name="ordering_remark" autocomplete="off"  >@if(old('ordering_remark')){{ old('ordering_remark') }}@else{{$waybill->ordering_remark}}@endif</textarea>
                            @error('ordering_remark')
                            <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group row">
                        <div class="col-8 offset-2">
                            <input type="submit" class="btn btn-outline-dark form-control">
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection

@section('lastScript')
<script>
    new Vue({
        el:"#container",
        data:{
            model:{
                order_id : "{{old('order_id') ?? $waybill->order_id}}",
                owner_id : "{{old('owner_id') ?? $waybill->owner_id}}",
                type:"{{old('type') ?? $waybill->type}}",
            },
            order:{},
        },
        mounted(){
            if (this.model.order_id)this._codeGetOrder("{{ old('wms_bill_number') ?? $waybill->wms_bill_number }}");
        },
        methods:{
            bindOrder(e){
                this.model.order_id = "";
                this.model.owner_id = "";
                let val = e.target.value;
                this._codeGetOrder(val);
            },
            _codeGetOrder(val){
                let url = "{{url('transport/waybill/seekOrder')}}";
                window.tempTip.postBasicRequest(url,{code:val},res=>{
                    this.order = res;
                    this.model.order_id = res.id;
                    this.model.owner_id = res.owner_id;
                });
            },
        },
    });
</script>
@endsection
