@extends('core::layouts.master')
@section('title')
    {{ trans_choice('core::general.edit',1) }} {{ trans_choice('savings::general.savings',1) }}
@endsection
@section('content')
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>
                        {{ trans_choice('core::general.edit',1) }} {{ trans_choice('savings::general.savings',1) }}
                        <a href="#" onclick="window.history.back()"
                           class="btn btn-outline-light bg-white d-none d-sm-inline-flex">
                            <em class="icon ni ni-arrow-left"></em><span>{{ trans_choice('core::general.back',1) }}</span>
                        </a>
                    </h1>

                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a
                                    href="{{url('dashboard')}}">{{ trans_choice('dashboard::general.dashboard',1) }}</a>
                        </li>
                        <li class="breadcrumb-item"><a
                                    href="{{url('savings')}}">{{ trans_choice('savings::general.savings',2) }}</a>
                        </li>
                        <li class="breadcrumb-item active">{{ trans_choice('core::general.edit',1) }} {{ trans_choice('savings::general.savings',1) }}</li>
                    </ol>
                </div>
            </div>
        </div><!-- /.container-fluid -->
    </section>
    <section class="content" id="app">
        <form method="post" action="{{ url('savings/'.$savings->id.'/update') }}">
            {{csrf_field()}}
            <div class="card card-bordered card-preview">
                <div class="card-body">
                    <input type="hidden" name="client_id"
                           v-model="client_id">
                    <div class="row gy-4">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="savings_product_id"
                                       class="control-label">{{trans_choice('savings::general.savings',1)}} {{trans_choice('savings::general.product',1)}}</label>
                                <v-select label="name" :options="savings_products"
                                          :reduce="savings_product => savings_product.id"
                                          v-on:input="change_product"
                                          v-model="savings_product_id">
                                    <template #search="{attributes, events}">
                                        <input
                                                autocomplete="off"
                                                class="vs__search @error('savings_product_id') is-invalid @enderror"
                                                v-bind="attributes"
                                                id="savings_product_id"
                                                v-bind:required="!savings_product_id"
                                                v-on="events"
                                        />
                                    </template>
                                </v-select>
                                <input type="hidden" name="savings_product_id"
                                       v-model="savings_product_id">
                                @error('savings_product_id')
                                <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                    </div>
                    <div class="row gy-4">
                        <div class="col-md-4">
                            <div class="form-group">
                                <label for="savings_officer_id"
                                       class="control-label"> {{trans_choice('savings::general.savings_officer',1)}}</label>
                                <v-select label="full_name" :options="users"
                                          :reduce="user => user.id"
                                          v-model="savings_officer_id">
                                    <template #search="{attributes, events}">
                                        <input
                                                autocomplete="off"
                                                class="vs__search @error('savings_officer_id') is-invalid @enderror"
                                                v-bind="attributes"
                                                v-bind:required="!savings_officer_id"
                                                id="savings_officer_id"
                                                v-on="events"
                                        />
                                    </template>
                                </v-select>
                                <input type="hidden" name="savings_officer_id"
                                       v-model="savings_officer_id">
                                @error('savings_officer_id')
                                <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label for="account_number"
                                       class="control-label">{{trans_choice('savings::general.account_number',1)}}</label>
                                <input type="text" name="account_number" v-model="account_number"
                                       id="account_number"
                                       class="form-control @error('account_number') is-invalid @enderror">
                                @error('account_number')
                                <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label for="external_id"
                                       class="control-label">{{trans_choice('savings::general.external_id',1)}}</label>
                                <input type="text" name="external_id" v-model="external_id"
                                       id="external_id"
                                       class="form-control @error('external_id') is-invalid @enderror">
                                @error('external_id')
                                <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                    </div>
                    <div class="row gy-4">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="interest_rate"
                                       class="control-label">{{trans_choice('savings::general.interest_rate',1)}}</label>
                                <input type="text" name="interest_rate" v-model="interest_rate"
                                       id="interest_rate" required
                                       class="form-control @error('interest_rate') is-invalid @enderror numeric">
                                @error('interest_rate')
                                <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="automatic_opening_balance"
                                       class="control-label">{{trans_choice('savings::general.automatic_opening_balance',1)}}</label>
                                <input type="text" name="automatic_opening_balance"
                                       id="automatic_opening_balance"
                                       class="form-control @error('automatic_opening_balance') is-invalid @enderror numeric"
                                       v-model="automatic_opening_balance" required>
                                @error('automatic_opening_balance')
                                <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                    </div>
                    <div class="row gy-4">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="lockin_period"
                                       class="control-label">{{trans_choice('savings::general.lockin_period',1)}}</label>
                                <input type="text" name="lockin_period"
                                       id="lockin_period" v-model="lockin_period"
                                       class="form-control @error('lockin_period') is-invalid @enderror numeric"
                                       required>
                                @error('lockin_period')
                                <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="lockin_type"
                                       class="control-label">{{trans_choice('savings::general.lockin_type',1)}}</label>
                                <select class="form-control @error('lockin_type') is-invalid @enderror "
                                        name="lockin_type" v-model="lockin_type"
                                        id="lockin_type"
                                        required>
                                    <option value=""></option>
                                    <option value="days">{{trans_choice('savings::general.day',2)}}</option>
                                    <option value="weeks">{{trans_choice('savings::general.week',2)}}</option>
                                    <option value="months">{{trans_choice('savings::general.month',2)}}</option>
                                    <option value="years">{{trans_choice('savings::general.year',2)}}</option>
                                </select>
                                @error('lockin_type')
                                <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                    </div>
                    <h3>{{trans_choice('savings::general.charge',2)}}</h3>
                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th>{{trans_choice('core::general.name',1)}}</th>
                            <th>{{trans_choice('core::general.type',1)}}</th>
                            <th>{{trans_choice('core::general.amount',1)}}</th>
                            <th>{{trans_choice('savings::general.collected_on',1)}}</th>
                            <th></th>
                        </tr>
                        </thead>
                        <tbody id="charges_table_body">
                        <tr v-for="(charge,index) in selected_charges" v-bind:id="charge.id">
                            <td>@{{ charge.name }}</td>
                            <td>@{{ charge.savings_charge_option_id }}</td>
                            <td>
                                <span v-if="charge.allow_override=='0'">
                                    <input v-bind:name="'charges['+charge.id+']'" type="hidden"
                                           v-bind:value="charge.amount">
                                    @{{ charge.amount }}
                                </span>
                                <span v-if="charge.allow_override=='1'">
                                    <input v-bind:name="'charges['+charge.id+']'" type="text"
                                           class="form-control numeric" v-bind:value="charge.amount" required>
                                </span>
                            </td>
                            <td>
                                <span v-if="charge.savings_charge_type_id==1">{{trans_choice('savings::general.savings_activation', 1)}}</span>
                                <span v-if="charge.savings_charge_type_id==2">{{trans_choice('savings::general.specified_due_date', 1)}}</span>
                                <span v-if="charge.savings_charge_type_id==3">{{trans_choice('savings::general.withdrawal_fee', 1)}}</span>
                                <span v-if="charge.savings_charge_type_id==4">{{trans_choice('savings::general.annual_fee', 1) }}</span>
                                <span v-if="charge.savings_charge_type_id==5">{{trans_choice('savings::general.monthly_fee', 1)}}</span>
                                <span v-if="charge.savings_charge_type_id==6">{{trans_choice('savings::general.inactivity_fee', 1)}}</span>
                                <span v-if="charge.savings_charge_type_id==7">{{trans_choice('savings::general.quarterly_fee', 1)}}</span>
                            </td>
                            <td><i class="fa fa-remove" v-on:click="remove_charge" v-bind:data-id="index"></i></td>
                        </tr>
                        </tbody>
                    </table>
                    <div class="row gy-4">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="savings_charges"
                                       class="control-label">{{trans_choice('savings::general.charge',2)}}</label>
                                <select class="form-control" name="savings_charges"
                                        id="savings_charges" v-model="selected_charge">
                                    <option value=""></option>
                                    <option v-for="(charge,index) in savings_product_charges" v-bind:value="index">
                                        @{{charge.charge.name }}
                                    </option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <label class="control-label"></label>
                            <button type="button" v-on:click="add_charge"
                                    class="btn btn-info"
                                    style="margin-top:20px">{{trans_choice('core::general.add',1)}} {{trans_choice('core::general.to',1)}} {{trans_choice('savings::general.product',1)}}</button>
                        </div>
                    </div>
                    <div class="row gy-4">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="submitted_on_date"
                                       class="control-label">{{trans_choice('savings::general.submitted_on',1)}}</label>
                                <flat-pickr
                                        v-model="submitted_on_date"
                                        class="form-control  @error('submitted_on_date') is-invalid @enderror"
                                        name="submitted_on_date" id="submitted_on_date" required>
                                </flat-pickr>
                                @error('submitted_on_date')
                                <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                    </div>
                    @foreach($custom_fields as $custom_field)
                        <?php
                        $field = custom_field_build_form_field($custom_field);
                        ?>
                        <div class="row gy-4">
                            <div class="col-md-12">
                                <div class="form-group">
                                    @if($custom_field->type=='radio')
                                        <label class="control-label"
                                               for="field_{{$custom_field->id}}">{{$field['label']}}</label>
                                        {!! $field['html'] !!}
                                    @else
                                        <label class="control-label"
                                               for="field_{{$custom_field->id}}">{{$field['label']}}</label>
                                        {!! $field['html'] !!}
                                    @endif
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
                <div class="card-footer border-top ">
                    <button type="submit"
                            class="btn btn-primary  float-right">{{trans_choice('core::general.save',1)}}</button>
                </div>
            </div>
        </form>
    </section>
@endsection
@section('scripts')
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                client_id: parseInt("{{old('client_id',$savings->client_id)}}"),
                savings_officer_id: parseInt("{{old('savings_officer_id',$savings->savings_officer_id)}}"),
                savings_product_id: parseInt("{{old('savings_product_id',$savings->savings_product_id)}}"),
                account_number: "{{old('account_number',$savings->account_number)}}",
                external_id: "{{old('external_id',$savings->external_id)}}",
                interest_rate: "{{old('interest_rate',$savings->interest_rate)}}",
                lockin_period: "{{old('lockin_period',$savings->lockin_period)}}",
                lockin_type: "{{old('lockin_type',$savings->lockin_type)}}",
                automatic_opening_balance: "{{old('automatic_opening_balance',$savings->automatic_opening_balance)}}",
                submitted_on_date: "{{old('submitted_on_date',$savings->submitted_on_date)}}",
                overdraft_limit: "{{old('overdraft_limit',$savings->overdraft_limit)}}",
                overdraft_interest_rate: "{{old('overdraft_interest_rate',$savings->overdraft_interest_rate)}}",
                minimum_overdraft_for_interest: "{{old('minimum_overdraft_for_interest',$savings->minimum_overdraft_for_interest)}}",
                savings_products: savings_products,
                savings_product_charges: {!! json_encode($savings->savings_product->charges) !!},
                savings_charges: savings_charges,
                clients: [],
                users: users,
                selected_charge: '',
                savings_product: {!! json_encode($savings->savings_product) !!},
                selected_charges: charges_list,
            },
            mounted() {

            },
            methods: {
                onSearch(search, loading) {
                    if (search.length) {
                        loading(true);
                        this.search(loading, search, this);
                    }
                },
                search: _.debounce((loading, search, vm) => {
                    axios.get('{{url('client/search')}}?s=' + search).then(function (response) {
                        vm.clients = response.data
                        loading(false);
                    }).catch(function (error) {

                    });
                }, 350),
                change_client() {
                    this.savings_officer_id = "";
                    if (this.client_id != "") {
                        this.clients.forEach(item => {
                            if (item.id == this.client_id) {
                                this.savings_officer_id = item.loan_officer_id;
                            }
                        })
                    }
                },
                change_product() {
                    if (this.savings_product_id != "") {
                        this.savings_products.forEach(item => {
                            if (item.id == this.savings_product_id) {
                                this.savings_product = item;
                                this.interest_rate = item.default_interest_rate;
                                this.automatic_opening_balance = item.automatic_opening_balance;
                                this.lockin_period = item.lockin_period;
                                this.lockin_type = item.lockin_type;
                                this.savings_product_charges = item.charges
                            }
                        })
                    }

                },
                onSubmit() {

                },
                add_charge(event) {
                    this.selected_charges.push(savings_charges[this.selected_charge]);
                },
                remove_charge(event) {
                    var id = event.currentTarget.getAttribute('data-id');
                    this.selected_charges.splice(id, 1);
                    //charges.push(original_charges[id]);
                },
            }
        })
    </script>
@endsection