<template>
    <div style="width: 100%;">
        <v-toolbar dark color="primary">
            <v-toolbar-title class="white--text">RECEIVE ITEM</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" style="white-space: nowrap;">
                                <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"
                                            >
                                                <v-icon small color="green" v-on="on">done_all</v-icon>
                                            </v-btn>
                                        </template>
                                        <span>konfirmasi</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"
                                            >
                                                <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"
                                            >
                                                <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="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>
                                    <v-tooltip bottom>
                                        <template v-slot:activator="{ on }">
                                            <v-btn 
                                                style="min-width: 30px; height: 30px; margin: 0;"
                                                small @click="printout(props.item)" color="primary"
                                            >
                                                <v-icon small color="white" v-on="on">print</v-icon>
                                            </v-btn>
                                        </template>
                                        <span>Cetak PDF</span>
                                    </v-tooltip>
                                </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-print
            :title="printtitle"
            :width="printwidth"
            :height="600"
            :status="openprint"
            :urlprint="urlprint"
            @close-dialog-print="openprint = false"
        ></one-dialog-print>

        <!-- 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-print': httpVueLoader('../../common/oneDialogPrintX.vue'),
    },
    mounted() {
        this.$store.dispatch("ro/search")
    },
    data() {
        return {
            urlprint: "",
            printtitle: "",
            printwidth: "60%",
            openprint: false,
            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: "15%", 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.dialog_form_receive_item = true;
        },
        konfirmRO(data) {
            this.$store.commit("ro/update_selected_ro", data);
            this.$store.commit("ro/update_dialog_confirmation", 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.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.commit("form/update_dialog_form_receive_item_edit", true);
        },
        printout(val) {
            this.printwidth = "60%";
            this.printtitle = "";
            let user = one_user();
            let username = user && user.M_StaffName ? user.M_StaffName : "";
            let tm = Date.now();
            this.urlprint = BASE_URL + "/one-api/report/rpt_receive_item_po/pdf?id=" + val.ReceiveOrderPoID + "&username=" + username + "&tm=" + tm;
            this.openprint = true;
        }
    },
    watch: {
        filter: {
            handler(newVal, oldVal) {
                this.$store.dispatch("ro/search")
            },
            deep: true
        }
    },
}
</script>