<template>
<div>
    <v-toolbar color="primary" dark>
        <v-toolbar-title class="white--text">APPROVE PURCHASE ORDER ASET</v-toolbar-title>
        <v-spacer></v-spacer>
    </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="filter.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="filter.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="filter.status"
                    label="Status"></v-autocomplete>
            </v-flex>
            <v-flex xs3 pl-2>
                <v-text-field v-model="filter.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_order.records"
            class="elevation-1" 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">
                    <v-layout
                        v-if="props.item.PurchaseOrderStatus != 'Draft'"
                        align-center justify-center row
                    >
                        <v-flex shrink pr-1>
                            <v-btn
                                color="warning" @click="viewdetail(props.item)" small
                            >
                                detail
                                <v-icon small right>list_alt</v-icon>
                            </v-btn>
                            <v-chip
                                v-if="props.item.approvedby != '0'"
                                label color="success" text-color="white"
                            >
                                APPROVED
                            </v-chip>
                        </v-flex>
                        <v-flex pl-1 v-if="props.item.approvedby == '0'">
                            <v-btn
                                color="info" @click="updateStatusPO(props.item)" small
                                :disabled="user.M_UserM_ApproveLevelID != '1'"
                                :outline="props.item.verifiedby == '0'"
                            >
                                verifikasi
                                <v-icon small right>done</v-icon>
                            </v-btn>
                            <v-btn
                                color="success" @click="updateStatusPO(props.item)" small
                                :disabled="user.M_UserM_ApproveLevelID != '2'"
                                :outline="props.item.PurchaseOrderStatus != 'Approved'"
                            >
                                approve
                                <v-icon small right>done_all</v-icon>
                            </v-btn>
                        </v-flex>
                    </v-layout>
                </td>
            </tr>
            </template>
        </v-data-table>
        <v-divider class="my-1"></v-divider>
        <div class="po-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-confirm></dialog-confirm>
    <dialog-attachment></dialog-attachment>
</div>
</template>

<script>
module.exports = {
    components: {
        'dialog-confirm': httpVueLoader('./dialog-confirm.vue'),
        'dialog-attachment': httpVueLoader('./dialog-attachment.vue')
    },
    mounted() {
        this.$store.dispatch("orderAsset/getListingPOAsset")
        this.$store.dispatch("orderAsset/getUserApproveLevel")
        this.$store.dispatch("orderAsset/listingCabang")
        this.$store.dispatch("orderAsset/listingSupplier")
    },
    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",
                },
            ]
        }
    },
    computed: {
        snackbar: {
            get() {
                return this.$store.state.orderAsset.snackbar;
            },
            set(val) {
                this.$store.commit("orderAsset/update_snackbar", val);
            }
        },
        filter: {
            get() {
                return this.$store.state.orderAsset.filter;
            },
            set(val) {
                this.$store.commit("orderAsset/update_filter", val);
            }
        },
        currpage: {
            get() {
                return this.$store.state.orderAsset.currpage;
            },
            set(val) {
                this.$store.commit("orderAsset/update_currpage", val);
                this.$store.dispatch("orderAsset/getListingPOAsset");
            }
        },
        list_order: {
            get() {
                return this.$store.state.orderAsset.list_order;
            },
            set(val) {
                this.$store.commit("orderAsset/update_list_order", val);
            }
        },
        pagelength() {
            let total = this.list_order.total
            if (total === undefined || total < 1) return 1
            return Math.ceil(total/10)
        },
        formattedStartdate() {
            return this.formatDate(this.filter.startdate)
        },
        formattedEnddate() {
            return this.formatDate(this.filter.enddate)
        },
        list_status() {
            return this.$store.state.orderAsset.list_status
        },
        user() {
            return this.$store.state.orderAsset.user
        }
    },
    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, ".");
        },
        updateStatusPO(item) {
            const userlvl = this.user.M_UserM_ApproveLevelID
            if (item.verifiedby == '0' || userlvl != '1') {
                this.$store.dispatch("orderAsset/getAssetAttachment", {
                    contractID: item.contractID,
                    poID: item.PurchaseOrderID
                });

                this.$store.dispatch("formView/getDetailDataPO", item.PurchaseOrderID);

                this.$store.commit("orderAsset/update_selected_order", item)
                this.$store.commit("orderAsset/update_dialog_confirm_order", true)
            } else {
                const snac = { color: "info", msg: "PO sudah diverifikasi", state: true }
                this.snackbar = snac
            }
        },
        viewdetail(item) {
            this.$store.dispatch("orderAsset/getAssetAttachment", {
                contractID: item.contractID,
                poID: item.PurchaseOrderID
            });
            this.$store.dispatch("formView/getDetailDataPO", item.PurchaseOrderID);

            this.$store.commit("orderAsset/update_view_detail_mode", true);
            this.$store.commit("orderAsset/update_dialog_confirm_order", true);
        }
    },
    watch: {
        filter: {
            handler(val) {
                this.$store.dispatch("orderAsset/getListingPOAsset")
            },
            deep: true
        }
    }
}
</script>
