<template>
    <div class="row">
        <div class="col">
            <div class="row no-margin">
                <div class="col p-t-25 p-t-25">
                    <div class="row">
                        <div class="col p-b-10 b-b b-grey">
                            <div class="row align-items-center">
                                <div class="col-auto p-r-0" style="min-width: 40px;">
                                    <div class="font-heading all-caps fs-10"></div>
                                </div>
                                <div class="col p-r-10">
                                    <div class="font-heading all-caps fs-10 muted">Stock Code</div>
                                </div>
                                <div class="col-4 p-r-5 p-l-10">
                                    <div class="font-heading all-caps fs-10 muted">description</div>
                                </div>
                                <div class="col text-center p-r-5 p-l-10">
                                    <div class="font-heading all-caps fs-10 muted">quantity</div>
                                </div>
                                <div class="col text-center p-r-5 p-l-10">
                                    <div class="font-heading all-caps fs-10 muted">Unit Price</div>
                                </div>
                                <div class="col-1 text-center p-r-5 p-l-10">
                                    <div class="font-heading all-caps fs-10 muted"></div>
                                </div>
                                <div class="col-1 text-right p-r-5 p-l-10">
                                    <div class="font-heading all-caps fs-10 muted">Total</div>
                                </div>
                                <div class="col-auto text-center">
                                    <button class="btn btn-xs btn-outline-success b-rad-none invisible"><i class="fa fa-check"></i></button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row m-b-20">
                        <div class="col">
                            <div class="row" v-if="!submitted">
                                <div class="col p-b-5 p-t-5 b-b b-grey">
                                    <div class="row align-items-center">
                                        <div class="col-auto p-r-0" style="min-width: 40px;">
                                            <div class="font-heading all-caps fs-10"></div>
                                        </div>
                                        <div class="col p-r-5">
                                            <input type="text" class="form-control fs-10 b-rad-none" placeholder="Stock Code" v-model="product.stockCode" />
                                        </div>
                                        <div class="col-4 p-r-5 p-l-5">
                                            <textarea class="form-control fs-10 b-rad-none" placeholder="Description" rows="1" v-model="product.description"></textarea>
                                        </div>
                                        <div class="col text-center p-r-5 p-l-5">
                                            <input type="text" class="form-control fs-10 b-rad-none text-center" placeholder="Quantity" v-model.lazy="product.quantity" v-mask="'#########'"/>
                                        </div>
                                        <div class="col text-center p-r-5 p-l-5">
                                            <input type="text" class="form-control fs-10 b-rad-none text-center" placeholder="Unit Price" v-model.lazy="product.unit_price" v-money="money" />
                                        </div>
                                        <div class="col-1 text-center p-r-5 p-l-5">
                                            <div class="font-heading all-caps fs-10">{{data.fixed_currency.short_code}}</div>
                                        </div>
                                        <div class="col-1 text-right p-r-5 p-l-5">
                                            <div class="font-heading all-caps fs-10">{{(Math.round((productTotal + Number.EPSILON) * 100) / 100).toFixed(2) }}</div>
                                        </div>
                                        <div class="col-auto">
                                            <button class="btn btn-xs btn-outline-success b-rad-none" @click="addProduct()"><i class="fa fa-check"></i></button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row" v-for="(product, index) in products">
                                <div class="col p-b-10 p-t-10 b-b b-grey">
                                    <div class="row  align-items-center">
                                        <div class="col-auto p-r-0" style="min-width: 40px;">
                                            <div class="font-heading all-caps fs-10">{{index + 1}}</div>
                                        </div>
                                        <div class="col p-r-5">
                                            <div class="font-heading all-caps fs-10">{{product.stockCode}}</div>
                                        </div>
                                        <div class="col-4 p-r-5 p-l-5">
                                            <div class="font-heading all-caps fs-10">{{product.description}}</div>
                                        </div>
                                        <div class="col text-center p-r-5 p-l-5">
                                            <div class="font-heading all-caps fs-10">{{product.quantity}}</div>
                                        </div>
                                        <div class="col text-center p-r-5 p-l-5">
                                            <div class="font-heading all-caps fs-10">{{product.unit_price}}</div>
                                        </div>
                                        <div class="col-1 text-center p-r-5 p-l-5">
                                            <div class="font-heading all-caps fs-10">{{data.fixed_currency.short_code}}</div>
                                        </div>
                                        <div class="col-1 text-right p-r-5 p-l-5">
                                            <div class="font-heading all-caps fs-10">{{(Math.round((product.total + Number.EPSILON) * 100) / 100).toFixed(2) }}</div>
                                        </div>
                                        <div class="col-auto">
                                            <button class="btn btn-xs btn-outline-danger b-rad-none" v-if="!submitted" @click="removeProduct(index)"><i class="fa fa-times"></i></button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-7">
                    <div class="row" v-if="poTotal > data.amount">
                        <div class="col">
                            <div class="alert alert-warning padding-15" role="alert">
                                <div class="font-heading fs-12 all-caps bold m-b-15">Can't Complete Your Purchase Order ?</div>
                                <div class="row m-b-15">
                                    <div class="col">
                                        <div class="font-heading fs-10 m-b-10">If your Purchase Order total amount doesn't match your transfer total amount we are unable to confirm your purchase order and generate the invoice for you, this usually happen when you pay for your purchase order with multiple transfer orders.</div>
                                        <div class="font-heading fs-10">But don't worry you can always merge your transfer orders into one to complete your purchase order successfully and generate your invoice.</div>
                                    </div>
                                </div>
                                <!--link to merge: route('booking.merge',this.data.marking) -->
                                <a class="btn btn-xs all-caps b-rad-none btn-warning" href="#">Merge Transfer Orders</a>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-5" v-if="data.company.address">
                    <div class="row align-items-end p-t-10 p-b-10">
                        <div class="col">
                            <div class="font-heading all-caps fs-12 bold">Total:</div>
                        </div>
                        <div class="col-auto text-right">
                            <div class="font-heading muted"><span v-if="!submitted" class="bold m-r-5" :class="[{'text-danger' : poTotal !== data.amount}, {'text-success' : poTotal === data.amount}]">{{(Math.round(( poTotal + Number.EPSILON) * 100) / 100).toFixed(2)}}/</span><span class="text-primary bold m-l-5">{{(Math.round((data.amount + Number.EPSILON) * 100) / 100).toFixed(2)}} {{data.fixed_currency.short_code}}</span></div>
                        </div>
                    </div>
                    <div class="row" v-if="poTotal > 0 && !submitted">
                        <div class="col">
                            <div class="row m-b-10">
                                <div class="col">
                                    <button class="btn btn-xs all-caps b-rad-none btn-primary btn-block" @click="submitForm()">{{poTotal !== data.amount ? 'Save Purchase Order' : 'Save & Confirm'}}</button>
                                </div>
                            </div>
                            <div class="row" v-if="poTotal !== data.amount">
                                <div class="col">
                                    <div class="fs-10">** you purchase order will be saved but wont be approved until your <span class="text-danger bold all-caps">purchase order's total</span> matches your <span class="text-success bold all-caps">transfer order's total</span>.</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row" v-if="submitted && data.status !== 3">
                <div class="col-7">
                    <button class="btn btn-xs btn-success b-rad-none" v-if="$store.getters.isAdmin && data.purchase_order.status === 1" @click="submit(route('api.booking.po.approval', data.id), 'post', section, true, true)">Approve Purchase Order</button>
                </div>
                <div class="col" v-if="submitted">
                    <div class="alert alert-success padding-15" role="alert">
                        <div class="font-heading fs-12 all-caps bold m-b-15">Your Purchase Order {{data.purchase_order.status === 1 ? 'is Being Processed for Verification' : 'has been Approved' }}</div>
                        <div class="row m-b-15">
                            <div class="col">
                                <div class="font-heading fs-10">If you would like to still edit your Purchase Order you can do that by clicking on the edit button below, but your purchase order verification request will be reset.</div>
                            </div>
                        </div>
                        <button class="btn btn-xs all-caps b-rad-none btn-default bg-master-lightest" @click="submitted = false">Edit Purchase Order</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    import formHandler from '../../../general/mixins/formHandler';
    export default {
        data(){
          return {
              submitted: false,
              product: {
                  stockCode: '',
                  description: '',
                  quantity: 1,
                  unit_price: 0,
              },
              products: []
          }
        },
        created() {
            this.products = this.data.purchase_order ? this.data.purchase_order.details : [];
            this.submitted = this.data.purchase_order ? this.data.purchase_order.status === 1 || this.data.purchase_order.status === 2: false;
        },
        computed: {
            productTotal(){
                return this.product.quantity * parseFloat((this.product.unit_price).toString().replace(',', ''));
            },
            poTotal(){
                return this.products.reduce(function(last, product) {
                    return last + product.total;
                }, 0);
            }
        },
        methods: {
            submitForm(){

                this.parameters = {
                    products: this.products
                };

                this.submit(route('api.transaction.po.create', this.data.id), 'post', this.section, true, true);
            },
            successHandler(){
                if(this.poTotal === this.data.amount){
                    this.submitted = true;
                }
                this.updateList()
            },
            addProduct(){
              this.products.push({
                  stockCode: this.product.stockCode,
                  description: this.product.description,
                  quantity: this.product.quantity,
                  unit_price: this.product.unit_price,
                  total: this.productTotal
              });

              this.product = {
                  stockCode: '',
                  description: '',
                  quantity: 1,
                  unit_price: 0,
              };

            },
            removeProduct(index){
                this.products.splice(index, 1);
            }
        },
        mixins: [formHandler]
    }
</script>