<template>
    <div>
        <v-toolbar color="primary" dark>
            <v-toolbar-title class="white--text">PURCHASE ORDER ASET</v-toolbar-title>
            <v-spacer></v-spacer>
            <v-btn icon @click="createNewOrder()">
                <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="filterpo.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="filterpo.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="filterpo.status"
                        label="Status"></v-autocomplete>
                </v-flex>
                <v-flex xs3 pl-2>
                    <v-text-field v-model="filterpo.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" class="elevation-1" :items="list_asetpo.records" hide-actions>
                <template v-slot:items="props">
                    <tr>
                        <td class="text-xs-center pa-2">{{ props.item.PurchaseOrderNumber }}</td>
                        <td class="text-xs-center pa-2">{{ props.item.PurchaseOrderDate }}</td>
                        <td class="text-xs-center pa-2">{{ props.item.SupplierName }}</td>
                        <td class="text-xs-center pa-2">{{ props.item.M_BranchName }}</td>
                        <td class="text-xs-center pa-2">{{ props.item.PurchaseOrderNote }}</td>
                        <td class="text-xs-center pa-2">
                            Rp. {{ formatCurrency(props.item.PurchaseOrderGrandTotal) }}
                        </td>
                        <td class="text-xs-center pa-2">{{ props.item.PurchaseOrderStatus }}</td>
                        <td class="text-xs-center pa-2">
                            <div v-show="props.item.PurchaseOrderStatus == 'Draft'">
                                <v-tooltip bottom>
                                    <template v-slot:activator="{ on }">
                                        <v-btn color="green" dark v-on="on" @click="openDialogAttachment(props.item)"
                                            small style="min-width: 40px; margin: 0;">
                                            <v-icon small>attachment</v-icon>
                                        </v-btn>
                                    </template>
                                    <span>Upload Attachment</span>
                                </v-tooltip>
                                <v-tooltip bottom>
                                    <template v-slot:activator="{ on }">
                                        <v-btn color="warning" dark v-on="on" @click="sendRequestPO(props.item)"
                                            small style="min-width: 40px; margin: 0;">
                                            <v-icon small>send</v-icon>
                                        </v-btn>
                                    </template>
                                    <span>Buat Request PO</span>
                                </v-tooltip>
                                <v-tooltip bottom>
                                    <template v-slot:activator="{ on }">
                                        <v-btn color="info" dark v-on="on" @click="openEditFormOrder(props.item)"
                                            style="min-width: 40px; margin: 0;" small>
                                            <v-icon small>edit</v-icon>
                                        </v-btn>
                                    </template>
                                    <span>Edit Request PO</span>
                                </v-tooltip>
                                <v-tooltip bottom>
                                    <template v-slot:activator="{ on }">
                                        <v-btn color="error" dark v-on="on" @click="openDialogDeleteOrder(props.item)"
                                            small style="min-width: 40px; margin: 0;">
                                            <v-icon small>delete</v-icon>
                                        </v-btn>
                                    </template>
                                    <span>Delete Request PO</span>
                                </v-tooltip>
                            </div>
                            <div v-show="props.item.PurchaseOrderStatus != 'Draft'">
                                <v-tooltip bottom>
                                    <template v-slot:activator="{ on }">
                                        <v-btn color="green" dark v-on="on" @click="openDialogAttachment(props.item)"
                                            small style="min-width: 40px; margin: 0;">
                                            <v-icon small>attachment</v-icon>
                                        </v-btn>
                                    </template>
                                    <span>Upload Attachment</span>
                                </v-tooltip>
                                <v-tooltip bottom>
                                    <template v-slot:activator="{ on }">
                                        <v-btn color="warning" dark v-on="on" @click="viewDetailPO(props.item)"
                                        small style="min-width: 40px; margin: 0;">
                                            <v-icon small>visibility</v-icon>
                                        </v-btn>
                                    </template>
                                        <span>Detail PO</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-add-attchment></dialog-add-attchment>
        <dialog-delete-order></dialog-delete-order>
        <dialog-create-order></dialog-create-order>
    </div>
</template>

<script>
module.exports = {
    mounted() {
        this.$store.dispatch('orderAsset/listingCabang');
        this.$store.dispatch('orderAsset/listingSupplier');
        this.$store.dispatch('common/getListingPOAsset');
    },
    data() {
        return {
            menuStartdateFilter: false,
            menuEnddateFilter: false,
            headers: [
                {
                    text: "No PO", align: "center", sortable: false,
                    value: "action", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "TANGGAL", align: "center", sortable: false,
                    value: "action", width: "10%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "VENDOR", align: "center", sortable: false,
                    value: "action", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "CABANG", align: "center", sortable: false,
                    value: "action", width: "10%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "KETERANGAN", align: "center", sortable: false,
                    value: "action", width: "20%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "TOTAL", align: "center", sortable: false,
                    value: "action", width: "10%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "STATUS", align: "center", sortable: false,
                    value: "action", width: "5%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "AKSI", align: "center", sortable: false,
                    value: "action", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
            ]
        }
    },
    components: {
        'dialog-add-attchment': httpVueLoader('./dialog-add-attachment.vue'),
        'dialog-delete-order': httpVueLoader('./dialog-delete-order.vue'),
        'dialog-create-order': httpVueLoader('./dialog-create-order.vue'),
    },
    computed: {
        snackbar: {
            get() {
                return this.$store.state.common.snackbar;
            },
            set(val) {
                return this.$store.commit("common/update_snackbar", val);
            }
        },
        filterpo: {
            get() {
                return this.$store.state.common.filterpo;
            },
            set(val) {
                this.$store.commit("common/update_filterpo", val);
                this.$store.dispatch('common/getListingPOAsset');
            }
        },
        currpage: {
            get() {
                return this.$store.state.common.currpage;
            },
            set(val) {
                this.$store.commit("common/update_currpage", val);
            }
        },
        formattedStartdate() {
            return this.formatDate(this.filterpo.startdate);
        },
        formattedEnddate() {
            return this.formatDate(this.filterpo.enddate);
        },
        list_status() {
            return this.$store.state.common.list_status;
        },
        list_asetpo() {
            return this.$store.state.common.list_asetpo;
        },
        pagelength() {
            const total = this.list_asetpo.total;
            if (total === undefined || total < 1) return 1;
            return Math.ceil(total / 10);
        },
        loading_process() {
            return this.$store.state.common.loading_process;
        }
    },
    methods: {
        formatDate(date) {
            if (!date) return null;
            const [year, month, day] = date.split('-');
            return `${day}-${month}-${year}`;
        },
        formatCurrency(value) {
            const num = parseFloat(value);
            if (isNaN(num)) return 0;
            return num.toFixed(2).replace('.', ',').replace(/\B(?=(\d{3})+(?!\d))/g, ".");
        },
        createNewOrder() {
            this.$store.commit("orderAsset/update_dialog_create_order", true);
        },
        openDialogAttachment(item) {
            let param = {
                contractID: item.contractID,
                poID: item.PurchaseOrderID
            };

            if (item.PurchaseOrderStatus != 'Draft') {
                this.$store.commit("common/update_viewonlymode", true);
            }

            this.$store.dispatch('common/getAssetAttachment', param);
            this.$store.commit("common/update_selected_orderaset", item);
            this.$store.commit("common/update_dialog_attachment", true);
        },
        openDialogDeleteOrder(item) {
            if (!this.loading_process) {
                this.$store.commit("common/update_selected_orderaset", item)
                this.$store.commit("common/update_dialog_deleteorder", true);
            } else {
                const snac = { color: 'warning', msg: 'terdapat proses lain yang masih berjalan', state: true };
                this.snackbar = snac;
            }
        },
        sendRequestPO(item) {
            if (item.AttachmentCount < 1) {
                const snac = { color: 'warning', msg: 'belum upload attachment', state: true };
                this.snackbar = snac;
            } else if (!this.loading_process) {
                this.$store.dispatch('kontrakAsset/sendRequestPO', item.PurchaseOrderID);
            } else {
                const snac = { color: 'warning', msg: 'terdapat proses lain yang masih berjalan', state: true };
                this.snackbar = snac;
            }
        },
        openEditFormOrder(item) {
            this.$store.dispatch("kontrakAsset/getDetailDataPO", item.PurchaseOrderID);
        },
        viewDetailPO(item) {
            this.$store.commit("common/update_viewonlymode", true);
            this.$store.dispatch("kontrakAsset/getDetailDataPO", item.PurchaseOrderID);
        }
    },
    watch: {
        filterpo: {
            handler(val) {
                this.$store.dispatch('common/getListingPOAsset');
            },
            deep: true
        }
    },
}
</script>
