<template>
<div>
    <v-toolbar color="primary" dark>
        <v-toolbar-title class="white--text">RECEIVE ORDER JASA</v-toolbar-title>
        <v-spacer></v-spacer>
        <v-btn icon @click="openFormRO()">
            <v-icon large>add_box</v-icon>
        </v-btn>
    </v-toolbar>

    <v-card class="pa-2">
        <v-layout row align-center>
            <v-flex xs3>
                <v-menu
                    v-model="menuStartdateFilter" offset-y lazy min-width="290px"
                    transition="scale-transition" max-width="290px" full-width
                    :nudge-right="40" :close-on-content-click="false"
                >
                    <template v-slot:activator="{on}">
                        <v-text-field
                            v-model="formattedStartdate" label="Tanggal Awal"
                            hide-details readonly v-on="on" outline
                        ></v-text-field>
                    </template>
                    <v-date-picker
                        v-model="filterjasa.startdate" no-title
                        @input="menuStartdateFilter = false"
                    ></v-date-picker>
                </v-menu>
            </v-flex>
            <v-flex xs3 pl-2>
                <v-menu
                    v-model="menuEnddateFilter" offset-y lazy min-width="290px"
                    transition="scale-transition" max-width="290px" full-width
                    :nudge-right="40" :close-on-content-click="false"
                >
                    <template v-slot:activator="{on}">
                        <v-text-field
                            v-model="formattedEnddate" label="Tanggal Akhir"
                            hide-details readonly v-on="on" outline
                        ></v-text-field>
                    </template>
                    <v-date-picker
                        v-model="filterjasa.enddate" no-title
                        @input="menuEnddateFilter = false"
                    ></v-date-picker>
                </v-menu>
            </v-flex>
            <v-flex xs3 pl-2>
                <v-autocomplete
                    :items="liststatus" hide-details outline
                    v-model="filterjasa.status" label="Status"
                ></v-autocomplete>
            </v-flex>
            <v-flex xs3 pl-2>
                <v-text-field
                    v-model="filterjasa.search" hide-details outline
                    label="Cari" placeholder="cai nomor po..."
                ></v-text-field>
            </v-flex>
        </v-layout>
    </v-card>

    <v-card class="pa-2 mt-2">
        <v-data-table
            :headers="headers" :items="list_rojasa.records"
            class="elevation-1" hide-actions
        >
            <template v-slot:items="props">
            <tr>
                <td class="text-xs-center pa-2">
                    {{ props.item.ReceiveOrderPoIDate }}
                </td>
                <td class="text-xs-center pa-2">
                    {{ props.item.ReceiveOrderPoNumber }}
                </td>
                <td class="text-xs-center pa-2">
                    {{ props.item.ReceiveOrderPoRefNumber }}
                </td>
                <td class="text-xs-center pa-2">
                    {{ props.item.SupplierName }}
                </td>
                <td class="text-xs-center pa-2">
                    {{ props.item.ReceiveOrderPoNote }}
                </td>
                <td class="text-xs-center pa-2">
                    <div v-if="props.item.ReceiveOrderPoConfirmed == 'Y'">
                        <v-chip small color="primary" text-color="white">Confirmed</v-chip>
                    </div>
                    <div v-else>
                        <v-chip small color="warning" text-color="white">Unconfirmed</v-chip>
                    </div>

                </td>
                <td class="text-xs-center pa-2">
                    <div v-show="props.item.ReceiveOrderPoConfirmed != 'Y'">
                        <v-tooltip bottom pr-2>
                            <template v-slot:activator="{ on }">
                                <v-btn
                                    color="green" dark @click="addAttachment(props.item)"
                                    style="min-width: 40px; margin: 0;" small v-on="on"
                                >
                                    <v-icon small>attachment</v-icon>
                                </v-btn>
                            </template>
                            <span>Upload Nota</span>
                        </v-tooltip>
                        <v-tooltip bottom pr-2>
                            <template v-slot:activator="{ on }">
                                <v-btn
                                    color="warning" dark @click="confirmRO(props.item)"
                                    style="min-width: 40px; margin: 0;" small v-on="on"
                                >
                                    <v-icon small>send</v-icon>
                                </v-btn>
                            </template>
                            <span>Konfirmasi Order</span>
                        </v-tooltip>
                        <v-tooltip bottom pr-2>
                            <template v-slot:activator="{ on }">
                                <v-btn
                                    color="info" dark @click="editRO(props.item)" v-on="on"
                                    style="min-width: 40px; margin: 0;" small
                                >
                                    <v-icon small>edit</v-icon>
                                </v-btn>
                            </template>
                            <span>Edit Order</span>
                        </v-tooltip>
                        <v-tooltip bottom>
                            <template v-slot:activator="{ on }">
                                <v-btn
                                    color="red" dark @click="deleteRO(props.item)"
                                    style="min-width: 40px; margin: 0;" small v-on="on"
                                >
                                    <v-icon small>delete</v-icon>
                                </v-btn>
                            </template>
                            <span>Hapus Order</span>
                        </v-tooltip>
                    </div>
                    <div v-show="props.item.ReceiveOrderPoConfirmed == 'Y'">
                        <v-tooltip bottom pr-2>
                            <template v-slot:activator="{ on }">
                                <v-btn
                                    color="info" dark @click="confirmRO(props.item)"
                                    style="min-width: 40px; margin: 0;" small v-on="on"
                                >
                                    <v-icon small>visibility</v-icon>
                                </v-btn>
                            </template>
                            <span>Detail Order</span>
                        </v-tooltip>
                        <v-tooltip bottom>
                            <template v-slot:activator="{ on }">
                                <v-btn
                                    color="green" dark @click="addAttachment(props.item)"
                                    style="min-width: 40px; margin: 0;" small v-on="on"
                                >
                                    <v-icon small>attach_file</v-icon>
                                </v-btn>
                            </template>
                            <span>View Nota</span>
                        </v-tooltip>
                    </div>
                </td>
            </tr>
            </template>
        </v-data-table>
        <v-divider class="my-1"></v-divider>
        <div class="pa-2 mt-1 text-xs-center">
            <v-pagination v-model="currpage_rojasa" :length="pagelength" :total-visible="10"></v-pagination>
        </div>
    </v-card>

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

    <dialog-form-ro></dialog-form-ro>
    <dialog-add-jasa></dialog-add-jasa>
    <dialog-inspeksi></dialog-inspeksi>
    <dialog-add-attachment></dialog-add-attachment>
    <dialog-delete-ro></dialog-delete-ro>
    <dialog-confirm-ro></dialog-confirm-ro>
    <dialog-edit-ro></dialog-edit-ro>
</div>
</template>

<script>
module.exports = {
    components: {
        'dialog-form-ro': httpVueLoader('./dialog-form-ro.vue'),
        'dialog-inspeksi': httpVueLoader('./dialog-inspeksi.vue'),
        'dialog-add-attachment': httpVueLoader('./dialog-add-attachment.vue'),
        'dialog-add-jasa': httpVueLoader('./dialog-add-jasa.vue'),
        'dialog-delete-ro': httpVueLoader('./dialog-delete-ro.vue'),
        'dialog-confirm-ro': httpVueLoader('./dialog-confirm-ro.vue'),
        'dialog-edit-ro': httpVueLoader('./dialog-edit-ro.vue'),
    },
    mounted() {
        this.$store.dispatch('roservice/lookupCabang');
        this.$store.dispatch('roservice/lookupVendor');
        this.$store.dispatch('roservice/lookupStaff');
        this.$store.dispatch('roservice/lookupReceiveOrder');
    },
    data() {
        return {
            menuStartdateFilter: false,
            menuEnddateFilter: false,
            headers: [
                {
                    text: "TANGGAL RO", align: "center", sortable: false,
                    value: "date", width: "10%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "NOMOR RO", align: "center", sortable: false,
                    value: "nopo", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "NOMOR PO", align: "center", sortable: false,
                    value: "noro", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "SUPPLIER", align: "center", sortable: false,
                    value: "supp", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "CATATAN", align: "center", sortable: false,
                    value: "note", width: "20%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "STATUS", align: "center", sortable: false,
                    value: "status", width: "10%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "AKSI", align: "center", sortable: false,
                    value: "acts", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
            ]
        }
    },
    computed: {
        filterjasa: {
            get() {
                return this.$store.state.roservice.filterjasa
            },
            set(val) {
                this.$store.commit("roservice/update_filterjasa", val)
            }
        },
        snackbar: {
            get() {
                return this.$store.state.roservice.snackbar
            },
            set(val) {
                this.$store.commit("roservice/update_snackbar", val)
            }
        },
        currpage_rojasa: {
            get() {
                return this.$store.state.roservice.currpage_rojasa
            },
            set(val) {
                this.$store.commit("roservice/update_currpage_rojasa", val)
                this.$store.dispatch('roservice/lookupReceiveOrder')
            }
        },
        list_rojasa() {
            return this.$store.state.roservice.list_rojasa
        },
        pagelength() {
            let total = this.list_rojasa.total
            if (total === undefined || total < 1) return 1
            return Math.ceil(total/10)
        },
        liststatus() {
            return this.$store.state.roservice.liststatus
        },
        formattedStartdate() {
            return this.formatDate(this.filterjasa.startdate)
        },
        formattedEnddate() {
            return this.formatDate(this.filterjasa.enddate)
        }
    },
    methods: {
        formatDate(date) {
            if (!date) return null;
            const [year, month, day] = date.split('-');
            return `${day}-${month}-${year}`;
        },
        openFormRO() {
            this.$store.commit("roservice/update_dialog_formro", true)
        },
        addAttachment(item) {
            this.$store.commit("roservice/update_selected_rojasa", item)
            this.$store.dispatch("roservice/lookupAttachment")
            this.$store.commit("roservice/update_dialog_attachment", true)
        },
        confirmRO(item) {
            this.$store.commit("roservice/update_selected_rojasa", item)
            this.$store.dispatch("roservice/lookupAttachment")
            this.$store.dispatch("roservice/lookupDetailReceiveOrder")
            this.$store.commit("roservice/update_dialog_confirm_ro", true)
        },
        editRO(item) {
            this.$store.commit("roservice/update_selected_rojasa", item)
            this.$store.dispatch("roservice/lookupDetailReceiveOrder")
            this.$store.commit("roservice/update_dialog_editro", true)
        },
        deleteRO(item) {
            if (item.ReceiveOrderPoConfirmed === 'N') {
                this.$store.commit("roservice/update_selected_rojasa", item)
                this.$store.commit("roservice/update_dialog_deletero", true)
            }
        }
    },
    watch: {
        filterjasa: {
            handler(val) {
                setTimeout(() => {
                    this.$store.dispatch('roservice/lookupReceiveOrder');
                }, 500);
            },
            deep: true
        }
    }
}
</script>
