<template>
    <div class="row">
        <div class="col">
            <div class="row no-margin">
                <div class="col p-b-15 p-l-0 p-r-0">
                    <div class="row align-items-center m-b-10">
                        <div class="col-auto p-r-0"><div class="icon-thumbnail icon-35 mr-0 btn-rounded text-white light animate__animated animate__infinite" :class="[{'bg-danger': !submitted}, {'animate__pulse': !submitted}, {'bg-success': submitted}]"><span v-if="data.status !==3">2</span><i class='fa fa-check' v-if="data.status === 3"></i></div></div>
                        <div class="col">
                            <div class="fs-14 bold all-caps" :class="[{'text-success': submitted}]">Purchase Order</div>
                            <p class="m-b-0 text-danger fs-12" v-if="!submitted">In order for us to process your order, you will need to provide us with your purchase order information.</p>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col">
                            <div class="row" v-for="(product, index) in products">
                                <div class="col p-b-10 p-t-10 " :class="[{'b-grey' : index !== Object.keys(products).length - 1}, {'b-b' : index !== Object.keys(products).length - 1}]">
                                    <purchase-order-item-form-component :data="product" :index="index" :currency="data.fixed_currency.short_code" :editable="!submitted" :section="section" @change="updateProduct($event, index)" v-on:remove="removeProduct(index)"></purchase-order-item-form-component>
                                </div>
                            </div>
                            <div class="row m-t-20" v-if="!submitted">
                                <div class="col">
                                    <div class="row">
                                        <div class="col-sm-12 col-md-4 pr-md-1">
                                            <div class="form-group form-group-default b-rad-none">
                                                <label>Stock Code</label>
                                                <input id="stock_code" name="stock_code" type="text" class="form-control b-rad-none" v-model="product.stockCode" />
                                            </div>
                                        </div>
                                        <div class="col-sm-12 col-md pl-md-1">
                                            <div class="form-group form-group-default b-rad-none required">
                                                <label>description</label>
                                                <input id="desc" name="desc" class="form-control b-rad-none" rows="1" @keyup="onlyEnglish($event)" v-model="product.description">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row align-items-center">
                                        <div class="col-sm-12 col-md-6 pr-md-1">
                                            <div class="form-group form-group-default b-rad-none required">
                                                <label>Unit Price</label>
                                                <input id="unit_price" name="unit_price" type="text" class="form-control b-rad-none" placeholder="Unit Price" v-model.lazy="product.unit_price" v-money="productPrice" />
                                            </div>
                                        </div>
                                        <div class="col-sm-12 col-md-6 pl-md-1">
                                            <div class="form-group form-group-default b-rad-none no-padding no-border">
                                                <div class="row no-margin">
                                                    <div  id="minus-quantity" name="minus-quantity" class="col-auto bg-master-lightest pointer" @mousedown="updateQuantity" @mouseleave="clearInterval" @mouseup="clearInterval" @touchstart="updateQuantity" @touchend="clearInterval" @touchcancel="clearInterval">
                                                        <div class="row h-100 align-items-center">
                                                            <div class="col">
                                                                <i class="fa fa-minus"></i>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="col no-padding">
                                                        <label class="p-t-5 p-l-5 text-center">Quantity</label>
                                                        <input  id="quantity" name="quantity" type="text" class="form-control m-b-5 b-rad-none no-border text-center" v-model.lazy="product.quantity" v-mask="'#########'"/>
                                                    </div>
                                                    <div id="add-quantity" name="add-quantity" class="col-auto bg-master-lightest pointer" @mousedown="updateQuantity('add')" @mouseleave="clearInterval" @mouseup="clearInterval" @touchstart="updateQuantity('add')" @touchend="clearInterval" @touchcancel="clearInterval">
                                                        <div class="row h-100 align-items-center">
                                                            <div class="col">
                                                                <i class="fa fa-plus"></i>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-sm-12 col-md-4 p-b-15 pb-md-0">
                                            <p class="m-b-0 small">Total</p>
                                            <h6 class="no-margin bold text-complete">{{data.fixed_currency.short_code}} {{productTotal.toFixed(3)}}</h6>
                                        </div>
                                        <div class="col-sm-12 col-md">
                                            <button id="add-product" name="add-product" class="btn btn-sm btn-block btn-complete b-rad-none" @click="addProduct()">Add Product</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col">
                    <div class="row" v-if="(Math.round((poTotal + Number.EPSILON) * 1000) / 1000).toFixed(3) > (Math.round((data.amount + Number.EPSILON) * 1000) / 1000).toFixed(3)">
                        <div class="col">
                            <div class="alert alert-warning padding-15" role="alert" v-if="false">
                                <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-12 col-md-8" v-if="data.company.address">
                    <div class="row align-items-end m-r-0 p-b-20">
                        <div class="col"></div>
                        <div class="col-auto b-t b-grey p-t-10 p-l-0">
                            <h6 class="font-heading all-caps bold">Total:</h6>
                        </div>
                        <div class="col-auto b-t b-grey p-t-10  p-r-0">
                            <h6><span v-if="!submitted" class="bold m-r-5" :class="[{'text-danger' : (Math.round((poTotal + Number.EPSILON) * 1000) / 1000).toFixed(3) !== (Math.round((data.amount + Number.EPSILON) * 1000) / 1000).toFixed(3)}, {'text-success' : (Math.round((this.poTotal  + Number.EPSILON) * 1000) / 1000).toFixed(3) === (Math.round((this.data.amount + Number.EPSILON) * 1000) / 1000).toFixed(3)}]">{{(Math.round(( poTotal + Number.EPSILON) * 1000) / 1000).toFixed(3)}}/</span><span class="text-primary bold m-l-5">{{(Math.round((data.amount + Number.EPSILON) * 1000) / 1000).toFixed(3)}} {{data.fixed_currency.short_code}}</span></h6>
                        </div>
                    </div>
                    <div class="row" v-if="poTotal > 0 && !submitted">
                        <div class="col">
                            <div class="row m-b-10">
                                <div class="col">
                                    <button id="save-purchase-order" name="save-purchase-order" class="btn btn-xs all-caps b-rad-none btn-primary btn-block" @click="submitForm()">{{(Math.round((poTotal + Number.EPSILON) * 1000) / 1000).toFixed(3) !== (Math.round((data.amount + Number.EPSILON) * 1000) / 1000).toFixed(3) ? 'Save Purchase Order' : 'Save & Confirm'}}</button>
                                </div>
                            </div>
                            <div class="row" v-if="(Math.round((poTotal + Number.EPSILON) * 1000) / 1000).toFixed(3) !== 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-12 col-md-5 pr-md-0">
                    <div class="row">
                        <div class="col-12">
                            <document-file-viewer-component :file="data.documents.proforma_invoice.files[0]" v-if="data.documents.proforma_invoice">
                                <template slot="button">
                                    <div class="text-center bg-master-lightest padding-20 pointer m-b-10">
                                        <div class="m-b-10">
                                            <svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px"
                                                    width="45" height="45"
                                                    viewBox="0 0 172 172"
                                                    style=" fill:#000000;"><defs><linearGradient x1="118.25" y1="63.41425" x2="118.25" y2="88.92669" gradientUnits="userSpaceOnUse" id="color-1_Ya7GVcPz6Vqb_gr1"><stop offset="0" stop-color="#4ec9ff"></stop><stop offset="1" stop-color="#2bffe6"></stop></linearGradient><linearGradient x1="72.5625" y1="20.87113" x2="72.5625" y2="158.79631" gradientUnits="userSpaceOnUse" id="color-2_Ya7GVcPz6Vqb_gr2"><stop offset="0" stop-color="#009add"></stop><stop offset="1" stop-color="#00baa4"></stop></linearGradient><linearGradient x1="56.4375" y1="20.87113" x2="56.4375" y2="158.79631" gradientUnits="userSpaceOnUse" id="color-3_Ya7GVcPz6Vqb_gr3"><stop offset="0" stop-color="#009add"></stop><stop offset="1" stop-color="#00baa4"></stop></linearGradient><linearGradient x1="45.6875" y1="20.87113" x2="45.6875" y2="158.79631" gradientUnits="userSpaceOnUse" id="color-4_Ya7GVcPz6Vqb_gr4"><stop offset="0" stop-color="#009add"></stop><stop offset="1" stop-color="#00baa4"></stop></linearGradient><linearGradient x1="69.875" y1="20.87113" x2="69.875" y2="158.79631" gradientUnits="userSpaceOnUse" id="color-5_Ya7GVcPz6Vqb_gr5"><stop offset="0" stop-color="#009add"></stop><stop offset="1" stop-color="#00baa4"></stop></linearGradient><linearGradient x1="80.625" y1="20.87113" x2="80.625" y2="158.79631" gradientUnits="userSpaceOnUse" id="color-6_Ya7GVcPz6Vqb_gr6"><stop offset="0" stop-color="#009add"></stop><stop offset="1" stop-color="#00baa4"></stop></linearGradient><linearGradient x1="91.375" y1="20.87113" x2="91.375" y2="158.79631" gradientUnits="userSpaceOnUse" id="color-7_Ya7GVcPz6Vqb_gr7"><stop offset="0" stop-color="#009add"></stop><stop offset="1" stop-color="#00baa4"></stop></linearGradient><linearGradient x1="86" y1="20.87113" x2="86" y2="158.79631" gradientUnits="userSpaceOnUse" id="color-8_Ya7GVcPz6Vqb_gr8"><stop offset="0" stop-color="#009add"></stop><stop offset="1" stop-color="#00baa4"></stop></linearGradient><linearGradient x1="52.40625" y1="20.87113" x2="52.40625" y2="158.79631" gradientUnits="userSpaceOnUse" id="color-9_Ya7GVcPz6Vqb_gr9"><stop offset="0" stop-color="#009add"></stop><stop offset="1" stop-color="#00baa4"></stop></linearGradient></defs><g fill="none" fill-rule="nonzero" stroke="none" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" stroke-dasharray="" stroke-dashoffset="0" font-family="none" font-weight="none" font-size="none" text-anchor="none" style="mix-blend-mode: normal"><path d="M0,172v-172h172v172z" fill="none"></path><g id="Layer_1"><circle cx="44" cy="30" transform="scale(2.6875,2.6875)" r="4" fill="url(#color-1_Ya7GVcPz6Vqb_gr1)"></circle><rect x="12" y="14" transform="scale(2.6875,2.6875)" width="30" height="2" fill="url(#color-2_Ya7GVcPz6Vqb_gr2)"></rect><path d="M80.625,102.125v-43c0,-2.95625 -2.41875,-5.375 -5.375,-5.375h-37.625c-2.95625,0 -5.375,2.41875 -5.375,5.375v43c0,2.95625 2.41875,5.375 5.375,5.375h37.625c2.95625,0 5.375,-2.41875 5.375,-5.375zM53.75,102.125h-16.125v-10.75h16.125zM53.75,86h-16.125v-10.75h16.125zM53.75,69.875h-16.125v-10.75h16.125zM75.25,102.125h-16.125v-10.75h16.125zM75.25,86h-16.125v-10.75h16.125zM75.25,69.875h-16.125v-10.75h16.125z" fill="url(#color-3_Ya7GVcPz6Vqb_gr3)"></path><rect x="12" y="47" transform="scale(2.6875,2.6875)" width="10" height="2" fill="url(#color-4_Ya7GVcPz6Vqb_gr4)"></rect><rect x="25" y="47" transform="scale(2.6875,2.6875)" width="2" height="2" fill="url(#color-5_Ya7GVcPz6Vqb_gr5)"></rect><rect x="29" y="47" transform="scale(2.6875,2.6875)" width="2" height="2" fill="url(#color-6_Ya7GVcPz6Vqb_gr6)"></rect><rect x="33" y="47" transform="scale(2.6875,2.6875)" width="2" height="2" fill="url(#color-7_Ya7GVcPz6Vqb_gr7)"></rect><path d="M141.3625,73.90625c-1.88125,-6.45 -6.45,-11.825 -12.3625,-14.78125v-34.9375c0,-4.56875 -3.49375,-8.0625 -8.0625,-8.0625h-96.75c-4.56875,0 -8.0625,3.49375 -8.0625,8.0625v110.1875v2.6875v10.75c0,4.56875 3.49375,8.0625 8.0625,8.0625h96.75c4.56875,0 8.0625,-3.49375 8.0625,-8.0625v-10.75v-22.30625c1.6125,0.5375 3.49375,0.80625 5.375,0.80625c11.825,0 21.5,-9.675 21.5,-21.5c0,-9.1375 -5.9125,-17.2 -14.5125,-20.15625zM137.0625,80.625c0,10.48125 -8.33125,18.8125 -18.8125,18.8125c-10.48125,0 -18.8125,-8.33125 -18.8125,-18.8125c0,-10.48125 8.33125,-18.8125 18.8125,-18.8125c10.48125,0 18.8125,8.33125 18.8125,18.8125zM123.625,147.8125c0,1.6125 -1.075,2.6875 -2.6875,2.6875h-96.75c-1.6125,0 -2.6875,-1.075 -2.6875,-2.6875v-3.225c0.80625,0.26875 1.6125,0.5375 2.6875,0.5375h96.75c1.075,0 1.88125,-0.26875 2.6875,-0.5375zM123.625,134.375v2.6875c0,1.6125 -1.075,2.6875 -2.6875,2.6875h-96.75c-1.6125,0 -2.6875,-1.075 -2.6875,-2.6875v-2.6875v-110.1875c0,-1.6125 1.075,-2.6875 2.6875,-2.6875h96.75c1.6125,0 2.6875,1.075 2.6875,2.6875v32.7875c-1.6125,-0.26875 -3.49375,-0.5375 -5.375,-0.5375c-13.4375,0 -24.1875,10.75 -24.1875,24.1875c0,12.3625 9.40625,22.84375 21.76875,24.1875c1.88125,3.49375 4.56875,6.18125 7.79375,8.0625zM134.375,110.1875c-4.8375,0 -9.1375,-2.15 -12.3625,-5.64375c11.55625,-1.88125 20.425,-11.825 20.425,-23.91875c0,-0.26875 0,-0.26875 0,-0.5375c4.8375,2.95625 8.0625,8.0625 8.0625,13.975c0,8.86875 -7.25625,16.125 -16.125,16.125z" fill="url(#color-8_Ya7GVcPz6Vqb_gr8)"></path><rect x="12" y="43" transform="scale(2.6875,2.6875)" width="15" height="2" fill="url(#color-9_Ya7GVcPz6Vqb_gr9)"></rect></g></g></svg>
                                        </div>
                                        <p class="no-margin fs-12">Proforma Invoice</p>
                                    </div>
                                </template>
                            </document-file-viewer-component>
                            <button class="btn btn-sm btn-danger b-rad-none btn-block" v-if="data.documents.proforma_invoice && data.outstanding_amount > 0" @click="submit(route('api.booking.proforma.create', data.id), 'post', section, true, true)">Regenerate Proforma Invoice</button>
                        </div>
                    </div>
                    <div class="row m-b-15" v-if="$store.getters.isAdmin && data.purchase_order.status === 1" >
                        <div class="col">
                            <button class="btn btn-sm btn-block btn-success b-rad-none" @click="submit(route('api.booking.po.approval', data.id), 'post', section, true, true)">Approve Purchase Order</button>
                        </div>
                    </div>
                </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 w-100" @click="submitted = false">Edit Purchase Order</button>
                        <button class="btn btn-xs all-caps b-rad-none btn-complete w-100 m-t-5" v-if="!data.documents.proforma_invoice && data.outstanding_amount != 0" @click="submit(route('api.booking.proforma.create', data.id), 'post', section, true, true)">Generate Proforma Invoice</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    import formHandler from '../../../general/mixins/formHandler';
    export default {
        data(){
            return {
                interval:false,
                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().replaceAll(',', ''));
            },
            poTotal(){
                return this.products.reduce(function(last, product) {
                    return last + product.total;
                }, 0);
            }
        },
        methods: {
            updateQuantity(type){
                if(!this.interval){
                    this.interval = setInterval(() => type === 'add' ? this.product.quantity++ : this.product.quantity > 1 ? this.product.quantity--:null , 80)
                }
            },
            clearInterval(){
                clearInterval(this.interval);
                this.interval = false;
            },
            onlyEnglish(event){
                let value = event.target.value,
                    regex = /^[^~`!@#$%^&*()_+=[\]\{}|;':",.\/<>?a-zA-Z0-9-]+$/;

                event.preventDefault();

                if(regex.test(value)){
                    this.product.description = value.replace(regex, '');
                }

            },
            submitForm(){

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

                this.submit(route('api.transaction.po.create', this.data.id), 'post', this.section, true, true);
            },
            successHandler(){
                if((Math.round((this.poTotal  + Number.EPSILON) * 1000) / 1000).toFixed(3) === (Math.round((this.data.amount + Number.EPSILON) * 1000) / 1000).toFixed(3)){
                    this.submitted = true;
                }
                this.updateList()
            },
            addProduct(){
              this.products.push({
                  stockCode: this.product.stockCode,
                  description: this.product.description,
                  quantity: this.product.quantity,
                  unit_price: parseFloat((this.product.unit_price).toString().replace(',', '')),
                  total: this.product.quantity * parseFloat((this.product.unit_price).toString().replace(',', ''))
              });

              this.product = {
                  stockCode: '',
                  description: '',
                  quantity: 1,
                  unit_price: 0,
              };
            },
            updateProduct(product, index){
                this.$set(this.products, index, {
                    stockCode: product.stockCode,
                    description: product.description,
                    quantity: product.quantity,
                    unit_price: parseFloat((product.unit_price).toString().replace(',', '')),
                    total: product.quantity * parseFloat((product.unit_price).toString().replace(',', ''))
                });
            },
            removeProduct(index){
                this.products.splice(index, 1);
            }
        },
        mixins: [formHandler]
    }
</script>
