(async function () {


    Vue.component('stitched-item-table', {
        props: ['items'],
        methods: {
            getFormattedDateTime: function (dateTime) {
                if (!!dateTime) {
                    return dateTime.split('T')[0] + ' ' + dateTime.split('T')[1];
                }
                return luxon.DateTime.now().toFormat('yyyy-MM-dd HH:mm:ss');
            },
            removeItem: function (index) {
                this.$emit('remove-item', index)
            }
        },
        template: `
            <table class="table table-bordered bg-white col-sm-12">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Item ID</th>
                        <th>Sku</th>
                        <th>Created By</th>
                        <th>Created At</th>
                        <th>Job Card ID</th>
                        <th>Barcode</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item,index) in items">
                        <td>
                           <input hidden="hidden" v-bind:name="'items[' + index + '].id'" v-bind:value="item.id">
                           <input hidden="hidden" v-bind:name="'items[' + index + '].itemId'" v-bind:value="item.itemId">
                           <input hidden="hidden" v-bind:name="'items[' + index + '].sku'" v-bind:value="item.sku">
                           <input hidden="hidden" v-bind:name="'items[' + index + '].createdBy'" v-bind:value="item.createdBy">
                           <input hidden="hidden" v-bind:name="'items[' + index + '].createdAt'" v-bind:value="getFormattedDateTime(item.createdAt)">
                           <input hidden="hidden" v-bind:name="'items[' + index + '].jobCardId'" v-bind:value="item.jobCardId">
                           <input hidden="hidden" v-bind:name="'items[' + index + '].barcode'" v-bind:value="item.barcode" >
                           <input hidden="hidden" v-bind:name="'items[' + index + '].bundleId'" v-bind:value="item.bundleId" >
                           <input hidden="hidden" v-bind:name="'items[' + index + '].isQa'" v-bind:value="item.isQa">
                           <span> {{item.id}} </span> 
                        </td>
                        <td> {{item.itemId}} </td>
                        <td> {{item.sku}} </td>
                        <td> {{item.createdBy}} </td>
                        <td> {{ getFormattedDateTime( item.createdAt) }} </td>
                        <td> {{item.jobCardId}}</td>
                        <td> {{item.barcode}} </td>
                        <td>
                               <span v-if="!item.qaStatus" class="badge badge-danger">NOT PERFORMED</span>
                               <span v-else-if="item.qaStatus === 'APPROVED'" class="font-lg badge badge-success">{{ item.qaStatus }}</span>
                               <span v-else-if="item.qaStatus === 'REJECT'" class="font-lg badge badge-danger">{{ item.qaStatus }}</span>
                        </td>
                        <td> 
                         <button type="button" title="Remove" class="btn btn-light text-left" v-on:click="removeItem(index)">
                                <i class="bi bi-trash"></i>
                            </button>
                        </td>
                    </tr>
                </tbody>
            </table>
        `
    });


    let app = new Vue({
        el: '#qcForm',
        data: {
            items: [],
            QaStatus: 'APPROVED'
        },
        methods: {
            onItemSelect: function (id, item) {
                this.items.push(item);

            },
            removeItem: function (index) {
                this.items.splice(index, 1);
            },
            hasDuplicates: function () {
                const ids = this.items.map(item => item.id);
                const uniqueIds = new Set(ids);
                return ids.length !== uniqueIds.size;
            },
            submitWithQaStatus: function (status) {
                this.QaStatus = status;
                this.$nextTick(() => {
                    const form = document.getElementById('qcForm');
                    if (form.checkValidity()) {
                        form.submit(); 
                    } else {
                        form.reportValidity();
                    }
                });
            }
        },
        mounted: function () {

        }
    })
})(jQuery);