<template>
    <div class="row">
        <div class="col">
            <div class="row">
                <div class="col">
                    <div class="row m-b-20">
                        <div class="col">
                            <div class="row" v-if="!submitted">
                                <div class="col p-b-10 p-t-10 b-b b-grey">
                                    <div class="row align-items-center m-b-15">
                                        <div class="col p-r-5">
                                            <validation-wrapper-component :validator="$v.product.description">
                                                <label class="text-primary">Description</label>
                                                <input type="text" class="form-control fs-12" v-model="product.description">
                                            </validation-wrapper-component>
                                        </div>
                                        <div class="col p-l-5">
                                            <validation-wrapper-component :validator="$v.product.quantity">
                                                <label class="text-primary">Quantity</label>
                                                <input type="text" class="form-control fs-12" v-model="product.quantity">
                                            </validation-wrapper-component>
                                        </div>
                                    </div>
                                    <div class="row m-b-15">
                                        <div class="col p-r-5">
                                            <validation-wrapper-component :validator="$v.product.width">
                                                <label class="text-primary">Width</label>
                                                <input type="text" class="form-control fs-12" v-model="product.width">
                                            </validation-wrapper-component>
                                        </div>
                                        <div class="col p-r-5 p-l-5">
                                            <validation-wrapper-component :validator="$v.product.height">
                                                <label class="text-primary">Height</label>
                                                <input type="text" class="form-control fs-12" v-model="product.height">
                                            </validation-wrapper-component>
                                        </div>
                                        <div class="col p-l-5">
                                            <validation-wrapper-component :validator="$v.product.length">
                                                <label class="text-primary">Length</label>
                                                <input type="text" class="form-control fs-12" v-model="product.length">
                                            </validation-wrapper-component>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col">
                                            <button class="btn btn-success btn-block b-rad-none" @click="addPackage()">Add Package</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row" v-for="(item, index) in packages">
                                <div class="col p-b-10 p-t-10 b-b b-grey">
                                    <div class="row">
                                        <div class="col">
                                            <div class="row align-items-center m-b-15">
                                                <div class="col-auto">
                                                    <div @click="removePackage(index)" class="pointer">
                                                        <svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px"
                                                             width="25" height="25"
                                                             viewBox="0 0 172 172"
                                                             style=" fill:#000000;"><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 fill="#e74c3c"><path d="M18.87987,153.12013c2.23887,2.23819 5.86807,2.23819 8.10693,0l59.0132,-59.0132l59.0132,59.0132c2.24964,2.17277 5.82555,2.1417 8.03709,-0.06984c2.21154,-2.21154 2.24261,-5.78745 0.06984,-8.03709l-59.0132,-59.0132l59.0132,-59.0132c1.49042,-1.43949 2.08815,-3.57117 1.56346,-5.57571c-0.52469,-2.00454 -2.09015,-3.57 -4.09469,-4.09469c-2.00454,-0.52469 -4.13622,0.07305 -5.57571,1.56346l-59.0132,59.0132l-59.0132,-59.0132c-2.24964,-2.17277 -5.82555,-2.1417 -8.03709,0.06984c-2.21154,2.21154 -2.24261,5.78745 -0.06984,8.03709l59.0132,59.0132l-59.0132,59.0132c-2.23819,2.23887 -2.23819,5.86807 0,8.10693z"></path></g></g></svg>
                                                    </div>
                                                </div>
                                                <div class="col">
                                                    <div class="row">
                                                        <div class="col-3">
                                                            <p class="m-b-0 small muted">Description</p>
                                                            <p class="m-b-0 bold">{{item.description}}</p>
                                                        </div>
                                                        <div class="col-auto">
                                                            <p class="m-b-0 small muted">Quantity</p>
                                                            <p class="m-b-0 bold">{{item.quantity}}</p>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-12">
                                                    <div class="row align-items-center">
                                                        <div class="col-auto">
                                                            <p class="m-b-0 small muted">Width</p>
                                                            <p class="m-b-0 bold">{{item.width}}</p>
                                                        </div>
                                                        <div class="col-auto text-center">
                                                            <p class="m-b-0 small muted">Height</p>
                                                            <p class="m-b-0 bold">{{item.height}}</p>
                                                        </div>
                                                        <div class="col-auto text-center">
                                                            <p class="m-b-0 small muted">Length</p>
                                                            <p class="m-b-0 bold">{{item.length}}</p>
                                                        </div>
                                                        <div class="col text-right">
                                                            <p class="m-b-0 small muted">Total CBM</p>
                                                            <p class="m-b-0 bold text-success">{{(Math.round((((item.width / 100) * (item.height / 100) * (item.length / 100) * item.quantity)) * 1000) / 1000).toFixed(3)}}</p>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
           
        </div>
    </div>
</template>
<script>
    import { required, numeric } from "vuelidate/lib/validators";
    import formHandler from '../../../general/mixins/formHandler';
    export default {
        props: {
            packages: {
                type: Array,
                required: true
            },
        },
        data(){
          return {
              submitted: false,
              product: {
                  type: 0,
                  description: '',
                  width: 0,
                  height: 0,
                  length: 0,
                  weight: 0,
                  quantity: 1,
                  reference: '',
              },
          }
        },
        validations: {
            product: {
                type: {
                    required
                },
                description: {
                    required
                },
                width: {
                    required,
                    numeric
                },
                height: {
                    required,
                    numeric
                },
                length: {
                    required,
                    numeric
                },
                weight: {
                    required,
                    numeric
                },
                quantity: {
                    required,
                    numeric
                },
                reference: {
                    required
                }
            }
        },
        methods: {
            addPackage(){
              this.packages.push({
                  type: this.product.type,
                  description: this.product.description,
                  width: this.product.width,
                  height: this.product.height,
                  length: this.product.length,
                  weight: this.product.weight,
                  quantity: this.product.quantity,
                  reference: this.product.reference,
              });

              this.resetProduct();
            },
            resetProduct(){
                this.product = {
                    type: '',
                    description: '',
                    width: 0,
                    height: 0,
                    length: 0,
                    weight: 0,
                    quantity: 1,
                    reference: 0,
                };
            },
            removePackage(index){
                this.packages.splice(index, 1);
            }
        },
        mixins: [formHandler]
    }
</script>