<template>
    <div style="width: 100%;">
        <v-toolbar dark color="primary">
            <v-toolbar-title class="white--text">RECEIVE ITEM INVENTARIS</v-toolbar-title>
            <v-spacer></v-spacer>
            <v-btn flat icon color="white" @click="openDialogReceiveItem()">
                <v-icon>add_box</v-icon>
            </v-btn>
        </v-toolbar>

        <!-- Filter Listing -->
        <v-card class="pa-2">
            <v-layout row wrap>
                <v-flex xs4>
                    <v-menu
                        v-model="menuStartDateFilter" lazy offset-y
                        transition="scale-transition" max-width="290px"
                        min-width="290px" :nudge-right="40"
                        :close-on-content-click="false"
                    >
                        <template v-slot:activator="{ on }">
                            <v-text-field
                                :value="startDateFilter" label="Tanggal Mulai"
                                hide-details readonly outline v-on="on" class="mr-2"
                            ></v-text-field>
                        </template>
                        <v-date-picker
                            no-title hide-details v-model="filter.startdate"
                            @input="menuStartDateFilter = false"
                        ></v-date-picker>
                    </v-menu>
                </v-flex>
                <v-flex xs4>
                    <v-menu
                        v-model="menuEndDateFilter" lazy offset-y
                        transition="scale-transition" max-width="290px"
                        min-width="290px" :nudge-right="40"
                        :close-on-content-click="false"
                    >
                        <template v-slot:activator="{ on }">
                            <v-text-field
                                :value="endDateFilter" label="Tanggal Akhir"
                                hide-details readonly outline v-on="on" class="mr-2"
                            ></v-text-field>
                        </template>
                        <v-date-picker
                            no-title hide-details v-model="filter.enddate"
                            @input="menuEndDateFilter = false"
                        ></v-date-picker>
                    </v-menu>
                </v-flex>
                <v-flex xs4>
                    <v-text-field
                        label="Cari nomor GR, nomor DO" v-model="filter.search" hide-details outline
                    ></v-text-field>
                </v-flex>
            </v-layout>
        </v-card>

        <!-- Table Listing -->
        <v-card class="pa-2 mt-2">
            <div style="overflow-y: scroll;">
                <v-data-table
                    :items="list_data_ro.records" :headers="headers" :loading="false"
                    hide-actions class="elevation-1"
                >
                    <template v-slot:items="props">
                        <tr>
                            <td class="text-xs-center">{{ props.item.ReceiveOrderPoNumber }}</td>
                            <td class="text-xs-center"> {{ props.item.ReceiveOrderPoIDate }} </td>
                            <td class="text-xs-center">{{ props.item.ReceiveOrderPoDONumber }}</td>
                            <td class="text-xs-center">{{ props.item.SupplierName }}</td>
                            <td class="text-xs-center">{{ props.item.WarehouseName }}</td>
                            <td class="text-xs-center">{{ props.item.ReceiveOrderPoNote }}</td>
                            <td class="text-xs-center">
                                <div v-if="props.item.ReceiveOrderPoConfirmed === 'Y'">
                                    <v-chip color="green" text-color="white">
                                        KONFIRMASI
                                    </v-chip>
                                    <kbd>{{ props.item.ReceiveOrderPoConfirmedDate }}</kbd>
                                </div>
                                <v-chip v-else color="red" text-color="white">
                                    BELUM KONFIRMASI
                                </v-chip>
                            </td>
                            <td class="text-xs-center">
                                <div v-show="props.item.ReceiveOrderPoConfirmed === 'N'">
                                    <v-tooltip bottom>
                                        <template v-slot:activator="{ on }">
                                            <v-btn
                                                style="min-width: 30px; height: 30px; margin: 0;" outline
                                                small @click="konfirmRO(props.item)" color="green lighten-2"
                                                class="mt-1"
                                            >
                                                <v-icon small color="green" v-on="on">done_all</v-icon>
                                            </v-btn>
                                        </template>
                                        <span>konfirmasi</span>
                                    </v-tooltip>
                                    <span v-if="props.item.hasDocuments == true">
                                        <v-tooltip bottom>
                                            <template v-slot:activator="{ on }">
                                                <v-btn
                                                    style="min-width: 30px; height: 30px; margin: 0;" outline
                                                    small @click="openViewDoc(props.item)" color="grey lighten-2"
                                                    class="mt-1"
                                                >
                                                    <v-icon small color="grey" v-on="on">description</v-icon>
                                                </v-btn>
                                            </template>
                                            <span>Lihat surat jalan supplier</span>
                                        </v-tooltip>
                                    </span>
                                    <v-tooltip bottom>
                                        <template v-slot:activator="{ on }">
                                            <v-btn
                                                style="min-width: 30px; height: 30px; margin: 0;" outline
                                                small @click="uploadDoct(props.item)" color="orange lighten-2"
                                                class="mt-1"
                                            >
                                                <v-icon small color="orange" v-on="on">attachment</v-icon>
                                            </v-btn>
                                        </template>
                                        <span>Upload surat jalan supplier</span>
                                    </v-tooltip>
                                    <v-tooltip bottom>
                                        <template v-slot:activator="{ on }">
                                            <v-btn
                                                style="min-width: 30px; height: 30px; margin: 0;" outline
                                                small @click="openBarcode(props.item)" color="black lighten-2"
                                                class="mt-1 mb-1"
                                            >
                                                <v-icon small color="black" v-on="on">print</v-icon>
                                            </v-btn>
                                        </template>
                                        <span>Barcode</span>
                                    </v-tooltip>
                                    <v-tooltip bottom>
                                        <template v-slot:activator="{ on }">
                                            <v-btn
                                                style="min-width: 30px; height: 30px; margin: 0;" outline
                                                small @click="editRO(props.item)" color="blue lighten-2"
                                                class="mt-1 mb-1"
                                            >
                                                <v-icon small color="blue" v-on="on">edit</v-icon>
                                            </v-btn>
                                        </template>
                                        <span>Edit</span>
                                    </v-tooltip>
                                    <v-tooltip bottom>
                                        <template v-slot:activator="{ on }">
                                            <v-btn
                                                style="min-width: 30px; height: 30px; margin: 0;" outline
                                                small @click="deleteRO(props.item)" color="red lighten-2"
                                                class="mt-1 mb-1"
                                            >
                                                <v-icon small color="red" v-on="on">delete</v-icon>
                                            </v-btn>
                                        </template>
                                        <span>Hapus</span>
                                    </v-tooltip>
                                </div>
                                <div v-show="props.item.ReceiveOrderPoConfirmed === 'Y'">
                                    <v-tooltip bottom>
                                        <template v-slot:activator="{ on }">
                                            <v-btn
                                                style="min-width: 30px; height: 30px; margin: 0;" outline
                                                small @click="openBarcode(props.item)" color="black lighten-2"
                                            >
                                                <v-icon small color="black" v-on="on">print</v-icon>
                                            </v-btn>
                                        </template>
                                        <span>Barcode</span>
                                    </v-tooltip>
                                    <v-tooltip bottom>
                                        <template v-slot:activator="{ on }">
                                            <v-btn
                                                style="min-width: 30px; height: 30px; margin: 0;" outline
                                                small @click="detailView(props.item)" color="blue lighten-2"
                                            >
                                                <v-icon small color="blue" v-on="on">visibility</v-icon>
                                            </v-btn>
                                        </template>
                                        <span>Lihat detail</span>
                                    </v-tooltip>
                                    <span v-show="props.item.ReceiveOrderPoIsHandover === 'N'">
                                        <v-tooltip bottom>
                                            <template v-slot:activator="{ on }">
                                                <v-btn
                                                    style="min-width: 30px; height: 30px; margin: 0;" outline
                                                    small @click="handOverRO(props.item)" color="green lighten-2"
                                                >
                                                    <v-icon small color="green" v-on="on">move_to_inbox</v-icon>
                                                </v-btn>
                                            </template>
                                            <span>Serah Terima</span>
                                        </v-tooltip>
                                    </span>
                                </div>
                            </td>
                        </tr>
                    </template>
                </v-data-table>
            </div>
            <v-divider></v-divider>
            <div class="text-xs-center mt-2">
                <v-pagination v-model="curr_page" :length="list_data_ro.total"></v-pagination>
            </div>
        </v-card>

        <!-- dialog -->
        <one-dialog-form-inspeksi></one-dialog-form-inspeksi>
        <one-dialog-form-receive></one-dialog-form-receive>
        <one-dialog-form-receive-edit></one-dialog-form-receive-edit>
        <one-dialog-item-receive></one-dialog-item-receive>
        <one-dialog-item-batch></one-dialog-item-batch>
        <one-dialog-ro-layout></one-dialog-ro-layout>
        <one-dialog-ro-view></one-dialog-ro-view>
        <one-dialog-ro-handover></one-dialog-ro-handover>
        <one-dialog-form-document></one-dialog-form-document>
        <one-dialog-print-barcode></one-dialog-print-barcode>

        <!-- snackbar -->
        <v-snackbar
            :color="snackbar.color" v-model="snackbar.isOpen"
            :timeout="3000" multi-line top
        >
            {{ snackbar.msg }}
            <v-btn class="ml-2" flat @click="snackbar.isOpen = false">Tutup</v-btn>
        </v-snackbar>
    </div>
</template>

<script>
module.exports = {
    components: {
        'one-dialog-item-receive': httpVueLoader('./dialogSelectItem.vue'),
        'one-dialog-form-receive': httpVueLoader('./formReceiveItem.vue'),
        'one-dialog-form-receive-edit': httpVueLoader('./formReceiveItemEdit.vue'),
        'one-dialog-form-inspeksi': httpVueLoader('./formInspection.vue'),
        'one-dialog-item-batch': httpVueLoader('./dialogBatchItem.vue'),
        'one-dialog-ro-layout': httpVueLoader('./dialogRoLayout.vue'),
        'one-dialog-ro-view': httpVueLoader('./dialogViewRO.vue'),
        'one-dialog-ro-handover': httpVueLoader('./dialogHandOver.vue'),
        'one-dialog-form-document': httpVueLoader('./formDialogDocument.vue'),
        'one-dialog-print-barcode': httpVueLoader('./dialogPrintBarcode.vue'),
    },
    mounted() {
        this.$store.dispatch("ro/search")
    },
    data() {
        return {
            menuStartDateFilter: false,
            menuEndDateFilter: false,
            headers: [
                {
                    text: "GR NUMBER", align: "center", sortable: false, value: "grnumber",
                    width: "10%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "DATE", align: "center", sortable: false, value: "date",
                    width: "10%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "DO NUMBER", align: "center", sortable: false, value: "do",
                    width: "10%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "SUPPLIER", align: "center", sortable: false, value: "supplier",
                    width: "20%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "WAREHOUSE", align: "center", sortable: false, value: "wh",
                    width: "15%", class: "pa-2  blue lighten-3 white--text",
                },
                {
                    text: "CATATAN", align: "center", sortable: false, value: "note",
                    width: "15%", class: "pa-2  blue lighten-3 white--text",
                },
                {
                    text: "STATUS", align: "center", sortable: false, value: "status",
                    width: "10%", class: "pa-2  blue lighten-3 white--text",
                },
                {
                    text: "AKSI", align: "center", sortable: false, value: "act",
                    width: "10%", class: "pa-2 text-center  blue lighten-3 white--text",
                },
            ]
        }
    },
    computed: {
        filter: {
            get() {
                return this.$store.state.ro.filter;
            },
            set(val) {
                this.$store.commit("ro/update_filter", val);
            }
        },
        snackbar: {
            get() {
                return this.$store.state.ro.snackbar;
            },
            set(val) {
                this.$store.commit("ro/update_snackbar", val);
            }
        },
        startDateFilter() {
            return this.formatDate(this.filter.startdate);
        },
        endDateFilter() {
            return this.formatDate(this.filter.enddate);
        },
        curr_page: {
            get() {
                return this.$store.state.ro.curr_page;
            },
            set(val) {
                this.$store.commit("ro/update_curr_page", val);
                this.$store.dispatch("ro/search");
            }
        },
        list_data_ro() {
            return this.$store.state.ro.list_data_ro;
        },
        dialog_form_receive_item: {
            get() {
                return this.$store.state.form.dialog_form_receive_item;
            },
            set(val) {
                this.$store.commit("form/update_dialog_form_receive_item", val);
            }
        }
    },
    methods: {
        formatDate(date) {
            if (!date) return null;
            const [year, month, day] = date.split("-");
            return `${day}-${month}-${year}`;
        },
        openDialogReceiveItem() {
            this.$store.dispatch("ro/getSupplier");
            this.$store.dispatch("ro/getWarehouse");
            this.$store.dispatch("ro/getRuangan");
            this.dialog_form_receive_item = true;
        },
        konfirmRO(data) {
            if (data.hasDocuments) {
                this.$store.commit("ro/update_selected_ro", data);
                this.$store.commit("ro/update_dialog_confirmation", true);
                return;
            }
            const message = "Belum upload bukti penerimaan";
            this.snackbar = { color: 'warning', msg: message, isOpen: true }
        },
        deleteRO(data) {
            this.$store.commit("ro/update_selected_ro", data);
            this.$store.commit("ro/update_dialog_delete", true);
        },
        detailView(data) {
            this.$store.commit("ro/update_selected_ro", data);
            this.$store.dispatch("ro/getSupplier");
            this.$store.dispatch("ro/getWarehouse");
            this.$store.dispatch("ro/getItemUpdate");
            this.$store.dispatch("ro/getRuangan");
            this.$store.commit("ro/update_dialog_detail_view", true);
        },
        editRO(data) {
            this.$store.commit("ro/update_selected_ro", data);
            this.$store.dispatch("ro/getSupplier");
            this.$store.dispatch("ro/getWarehouse");
            this.$store.dispatch("ro/getItemUpdate");
            this.$store.dispatch("ro/getRuangan");
            this.$store.commit("form/update_dialog_form_receive_item_edit", true);
        },
        handOverRO(data) {
            this.$store.commit("ro/update_selected_ro", data);
            this.$store.dispatch("ro/getRuangan");
            this.$store.dispatch("ro/getListStaff", 'Y');
            this.$store.dispatch("ro/getItemSerahTerima");
            this.$store.commit("ro/update_dialog_handover", true);
        },
        uploadDoct(data) {
            this.$store.commit("ro/update_selected_ro", data);
            this.$store.commit("ro/update_dialog_document", true);
            this.$store.commit("ro/update_imageFiles", []);
            this.$store.commit("ro/update_imagePreviews", []);
            this.$store.commit("ro/update_cekFile", false);
            this.$store.commit("ro/update_act_dialog_doc", 'upload');
        },
        openViewDoc(data) {
            let curryear = data.documents[0].ReceiveOrderPoDocumentDate
            let year = new Date(curryear).getFullYear();
            let fileBaseUrl = BASE_URL + `/one-media/order-inventaris/${year}/`;
            let allImgUrls = [];

            for (let i = 0; i < data.documents.length; i++) {
                allImgUrls.push(fileBaseUrl + data.documents[i].ReceiveOrderPoDocumentFile);
            }

            this.$store.commit("ro/update_selected_ro", data);
            this.$store.commit("ro/update_imagePreviews", allImgUrls);
            this.$store.commit("ro/update_dialog_document", true);
            this.$store.commit("ro/update_cekFile", false);
            this.$store.commit("ro/update_act_dialog_doc", 'preview');
        },
        openBarcode(data) {
            this.$store.commit("ro/update_selected_ro", data);
            this.$store.commit("ro/update_dialog_barcode", true);
            this.$store.commit("ro/update_bar_chx_all", false);
            this.$store.commit("ro/update_indeterminatex", false);
            this.$store.commit("ro/update_selected_barcode", []);
            this.$store.dispatch("ro/getBarcodes");
        }
    },
    watch: {
        filter: {
            handler(newVal, oldVal) {
                this.$store.dispatch("ro/search")
            },
            deep: true
        }
    },
}
</script>
