<template>
    <div class="row">
        <div class="col">
            <loading-component style="height: 50px; top: 0;" key="1" color="success" v-show="$store.getters.isLoading(section)"></loading-component>
            <div class="row" v-show="!$store.getters.isLoading(section)">
                <div class="col">
                    <div class="row m-b-20">
                        <div class="col">
                            <h6 class="all-caps m-b-5 bold no-margin">Create Service</h6>
                            <div class="font-heading fs-10 muted">Create a new exchange service</div>
                        </div>
                    </div>
                    <div class="row m-b-5 animate__animated animate__fadeInUpBig animate__fast" v-if="error">
                        <div class="col">
                            <small class="bold fs-10 text-danger">{{error}}</small>
                        </div>
                    </div>
                    <div class="row m-b-20">
                        <div class="col-5 p-r-10">
                            <validation-wrapper-component :validator="$v.parameters.name">
                                <label>Service Name</label>
                                <input type="text" class="form-control" v-model="parameters.name">
                            </validation-wrapper-component>
                        </div>
                        <div class="col-auto pointer bg-master-lightest m-r-10 d-none" @click="parameters.configurations.billable = !parameters.configurations.billable">
                            <div class="row h-100 align-items-center">
                                <div class="col-auto p-r-0">
                                    <div class="icon-thumbnail fs-12 icon-25 btn-rounded" :class="[{'bg-success': parameters.configurations.billable}, {'bg-white': !parameters.configurations.billable}]">
                                        <i class="fa fa-check fs-12 text-white"></i>
                                    </div>
                                </div>
                                <div class="col p-l-0 p-r-25">
                                    <div class="font-heading fs-8 all-caps muted">Billable Service</div>
                                    <div class="font-heading fs-8 all-caps">Include Billing</div>
                                </div>
                            </div>
                        </div>
                        <div class="col-auto pointer bg-master-lightest" v-if="data" @click="parameters.configurations.active = !parameters.configurations.active">
                            <div class="row h-100 align-items-center">
                                <div class="col-auto p-r-0">
                                    <div class="icon-thumbnail fs-12 icon-25 btn-rounded" :class="[{'bg-success': parameters.configurations.active}, {'bg-white': !parameters.configurations.active}]">
                                        <i class="fa fa-check fs-12 text-white"></i>
                                    </div>
                                </div>
                                <div class="col p-l-0 p-r-25">
                                    <div class="font-heading fs-8 all-caps muted">Active For Default</div>
                                    <div class="font-heading fs-8 all-caps">Customer segment</div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <div class="font-heading all-caps fs-10 hint-text muted">
                                Service Configurations
                            </div>
                        </div>
                    </div>
                    <div class="row m-b-20">
                        <div class="col">
                            <div class="row m-b-10">
                                <!-- <div class="col p-r-5">
                                    <validation-wrapper-component :validator="$v.parameters.configurations.service_charge.value">
                                        <label>Service Charge</label>
                                        <input type="text" class="form-control" v-model.lazy="parameters.configurations.service_charge.value"  v-money="percentage">
                                    </validation-wrapper-component>
                                </div> -->
                                <div class="col p-r-5">
                                    <validation-wrapper-component :validator="$v.parameters.configurations.service_charge.value">
                                        <label>Price Per CBM</label>
                                        <input type="text" class="form-control" v-model.lazy="parameters.configurations.service_charge.value"  v-money="percentage">
                                    </validation-wrapper-component>
                                </div>
                                <div class="col p-l-5 p-r-20">
                                    <div class="row">
                                        <div class="col p-r-0">
                                            <validation-wrapper-component :validator="$v.parameters.configurations.minimum_charge.value">
                                                <label>Minimum Charge</label>
                                                <input type="text" class="form-control" v-model.lazy="parameters.configurations.minimum_charge.value"  v-money="money">
                                            </validation-wrapper-component>
                                        </div>
                                        <div class="col-auto bg-white b-t b-r b-b b-grey">
                                            <div class="row h-100 align-items-center">
                                                <div class="col">
                                                    <div class="font-heading fs-10 muted">MYR</div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col p-l-5 d-none" v-show="parameters.configurations.billable">
                                    <validation-wrapper-component :validator="$v.parameters.configurations.tax.value">
                                        <label>Tax</label>
                                        <input type="text" class="form-control" v-model.lazy="parameters.configurations.tax.value"  v-money="percentage">
                                    </validation-wrapper-component>
                                </div>
                            </div>
                            <div class="row m-b-5 d-none">
                                <div class="col-5 p-r-0" v-show="parameters.configurations.billable">
                                    <validation-wrapper-component :validator="$v.parameters.configurations.po_limit.value">
                                        <label>Abandoned PO Limit **</label>
                                        <input type="text" class="form-control" v-model.lazy="parameters.configurations.po_limit.value" >
                                    </validation-wrapper-component>
                                </div>
                                <div class="col">
                                    <validation-wrapper-component selectable :validator="$v.parameters.configurations.bank_id">
                                        <label>Receivable Bank Account</label>
                                        <selectable-component :endpoint="route('api.bank.list')+'?filters={%22company_id%22:1}'" section="BankAccountListSection" valueColumn="id" :labelColumn="['bank_name', 'account_no']" v-model="parameters.configurations.bank_id"></selectable-component>
                                    </validation-wrapper-component>
                                </div>
                            </div>
                            <div class="row m-b-10 d-none" v-show="parameters.configurations.billable">
                                <div class="col">
                                    <div class="fs-8 muted">** Abandoned PO's limit is only applicable to transfers that have been paid in full.</div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <div class="font-heading all-caps fs-10 hint-text muted">
                                Currencies Settings
                            </div>
                        </div>
                    </div>
                    <div class="row m-l-0 m-r-0 m-b-10">
                        <div class="col p-r-0">
                            <currencies-list-component :data="parameters.configurations" :section="section" v-model="parameters.configurations.currencies"></currencies-list-component>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col p-r-5">
                            <div class="btn btn-sm btn-default bg-master-lightest btn-block b-rad-none" data-dismiss="modal">Cancel</div>
                        </div>
                        <div class="col p-l-5">
                            <div class="btn btn-sm btn-success btn-block b-rad-none" @click="submitForm()">{{this.data ? 'Update' : 'Create'}} Service</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    import ModalFormHandler from '../../../general/mixins/modalFormHandler';
    import { required, requiredIf, minValue, decimal} from "vuelidate/lib/validators";
    export default {
        data(){
            return {
                parameters: {
                    name: '',
                    configurations: {
                        bank_id: '',
                        service_charge: {
                            type: 'percentage',
                            value: 0
                        },
                        minimum_charge: {
                            type: 'fixed_amount',
                            value: 0
                        },
                        tax: {
                            type: 'percentage',
                            value: 0
                        },
                        po_limit: {
                            type: 'fixed_amount',
                            value: 1
                        },
                        billable: true,
                        currencies: []
                    },
                }
            }
        },
        validations: {
            parameters: {
                name: {
                    required
                },
                configurations: {
                    bank_id: {
                        required
                    },
                    service_charge: {
                        value:{
                            required
                        }
                    },
                    minimum_charge: {
                        value:{
                            required
                        }
                    },
                    tax: {
                        value:{
                            required
                        }
                    },
                    po_limit: {
                        value:{
                            required,
                            minValue: minValue(1)
                        }
                    }
                }
            }
        },
        methods: {
            submitForm(){
                this.submit((this.data ? this.route('api.service_type.update', this.data.id) : this.route('api.service_type.create')), (this.data ? 'put' : 'post'), this.section, true, true)
            }
        },
        mixins: [ModalFormHandler],


    }
</script>