<template>
    <div>
        <v-toolbar color="primary" dark>
            <v-toolbar-title class="white--text">RECEIVE ORDER ASET</v-toolbar-title>
            <v-spacer></v-spacer>
            <v-btn icon @click="openDialogReceive()">
                <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 label="Tanggal Awal" hide-details readonly v-on="on" outline
                                v-model="formattedStartdate"></v-text-field>
                        </template>
                        <v-date-picker v-model="filterasset.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="filterasset.enddate" no-title
                            @input="menuEnddateFilter = false"></v-date-picker>
                    </v-menu>
                </v-flex>
                <v-flex xs3 pl-2>
                    <v-autocomplete :items="list_status" hide-details outline v-model="filterasset.status"
                        label="Status"></v-autocomplete>
                </v-flex>
                <v-flex xs3 pl-2>
                    <v-text-field v-model="filterasset.search" hide-details outline label="Cari"
                        placeholder="cari 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_roasset.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 }} <br>
                            {{ props.item.PurchaseOrderNumber }}
                        </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" style="white-space: nowrap;">
                            <div v-show="props.item.ReceiveOrderPoConfirmed != 'Y'">
                                <v-tooltip bottom pr-2>
                                    <template v-slot:activator="{ on }">
                                        <v-btn color="green" dark @click="uploadAttachment(props.item)"
                                            style="min-width: 40px; margin: 0;" small v-on="on">
                                            <v-icon small>attachment</v-icon>
                                        </v-btn>
                                    </template>
                                    <span>Upload Bukti</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 Receive</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 Receive</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 Receive</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="viewDetail(props.item)"
                                            style="min-width: 40px; margin: 0;" small v-on="on">
                                            <v-icon small>visibility</v-icon>
                                        </v-btn>
                                    </template>
                                    <span>View Detail</span>
                                </v-tooltip>
                                <v-tooltip bottom pr-2>
                                    <template v-slot:activator="{ on }">
                                        <v-btn color="green" dark @click="uploadAttachment(props.item)"
                                            style="min-width: 40px; margin: 0;" small v-on="on">
                                            <v-icon small>attach_file</v-icon>
                                        </v-btn>
                                    </template>
                                    <span>View Document</span>
                                </v-tooltip>
                                <v-tooltip bottom>
                                    <template v-slot:activator="{ on }">
                                        <v-btn color="primary" dark @click="printout(props.item)"
                                            style="min-width: 40px; margin: 0;" small v-on="on">
                                            <v-icon small>print</v-icon>
                                        </v-btn>
                                    </template>
                                    <span>Cetak PDF</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" :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-receive-aset></dialog-receive-aset>
        <dialog-add-attachment></dialog-add-attachment>
        <dialog-delete-order-asset></dialog-delete-order-asset>
        <dialog-confirm-order-asset></dialog-confirm-order-asset>
        <dialog-pick-contract></dialog-pick-contract>
        <dialog-inspeksi></dialog-inspeksi>
        <one-dialog-print
            :title="printtitle"
            :width="printwidth"
            :height="600"
            :status="openprint"
            :urlprint="urlprint"
            @close-dialog-print="openprint = false"
        ></one-dialog-print>
    </div>
</template>

<script>
module.exports = {
    components: {
        'dialog-receive-aset': httpVueLoader('./dialog-receive-order-asset.vue'),
        'dialog-add-attachment': httpVueLoader('./dialog-add-attachment.vue'),
        'dialog-delete-order-asset': httpVueLoader('./dialog-delete-order-asset.vue'),
        'dialog-confirm-order-asset': httpVueLoader('./dialog-confirm-receive.vue'),
        'dialog-pick-contract': httpVueLoader('./dialog-pick-contract.vue'),
        'dialog-inspeksi': httpVueLoader('./dialog-inspeksi.vue'),
        'one-dialog-print': httpVueLoader('../../common/oneDialogPrintX.vue'),
    },
    mounted() {
        this.$store.dispatch("orderAsset/listingCabang");
        this.$store.dispatch("orderAsset/listingSupplier");
        this.$store.dispatch("roasset/lookupOrderAssetReceived");
    },
    data() {
        return {
            urlprint: "",
            printtitle: "",
            printwidth: "60%",
            openprint: false,
            menuStartdateFilter: false,
            menuEnddateFilter: false,
            headers: [
                {
                    text: "TANGGAL RECEIVE", align: "center", sortable: false, value: "date",
                    width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "NOMOR RO/PO", align: "start", sortable: false, value: "noropo",
                    width: "20%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "SUPPLIER", align: "center", sortable: false, value: "supplier",
                    width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "CATATAN", align: "center", sortable: false, value: "note",
                    width: "25%", 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: {
        snackbar: {
            get() {
                return this.$store.state.roasset.snackbar
            },
            set(val) {
                this.$store.commit("roasset/update_snackbar", val)
            }
        },
        filterasset: {
            get() {
                return this.$store.state.roasset.filterasset
            },
            set(val) {
                this.$store.commit("roasset/update_filterasset", val)
            }
        },
        currpage: {
            get() {
                return this.$store.state.roasset.currpage
            },
            set(val) {
                this.$store.commit("roasset/update_currpage", val)
                this.$store.dispatch("roasset/lookupOrderAssetReceived")
            }
        },
        list_status() {
            return this.$store.state.roasset.list_status
        },
        list_roasset() {
            return this.$store.state.roasset.list_roasset
        },
        pagelength() {
            let total = this.list_roasset.total
            if (total === undefined || total < 1) return 1
            return Math.ceil(total / 10)
        },
        formattedStartdate() {
            return this.formatDate(this.filterasset.startdate)
        },
        formattedEnddate() {
            return this.formatDate(this.filterasset.enddate)
        }
    },
    methods: {
        formatDate(date) {
            if (!date) return null;
            const [year, month, day] = date.split('-');
            return `${day}-${month}-${year}`;
        },
        openDialogReceive() {
            this.$store.commit("orderAsset/update_dialog_receive_aset", true)
        },
        uploadAttachment(item) {
            this.$store.dispatch("roasset/lookupAttachment", item.ReceiveOrderPoID);
            this.$store.commit("roasset/update_selected_roasset", item);
            this.$store.commit("roasset/update_dialog_attachment", true);
        },
        editRO(item) {
            this.$store.dispatch("roasset/getDetailDataROAsset", item.ReceiveOrderPoID)
            this.$store.commit("roasset/update_selected_roasset", item)
            this.$store.commit("orderAsset/update_dialog_receive_aset", true)
        },
        viewDetail(item) {
            this.$store.dispatch("roasset/getDetailDataROAsset", item.ReceiveOrderPoID)
            this.$store.commit("roasset/update_selected_roasset", item)

            this.$store.commit("orderAsset/update_readonly_mode", true)
            this.$store.commit("orderAsset/update_dialog_receive_aset", true)
        },
        confirmRO(item) {
            this.$store.dispatch("roasset/lookupAttachment", item.ReceiveOrderPoID);
            this.$store.commit("roasset/update_selected_roasset", item)
            this.$store.commit("roasset/update_dialog_confirm", true)
        },
        deleteRO(item) {
            this.$store.commit("roasset/update_selected_roasset", item)
            this.$store.commit("roasset/update_dialog_delete", 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_order_asset/pdf?id=" + val.ReceiveOrderPoID + "&username=" + username + "&tm=" + tm;
            this.openprint = true;
        }
    },
    watch: {
        filterasset: {
            handler(val) {
                setTimeout(() => {
                    this.$store.dispatch("roasset/lookupOrderAssetReceived")
                }, 500)
            },
            deep: true
        }
    }
}
</script>
