<template>
<div>
    <v-toolbar color="primary" dark>
        <v-toolbar-title class="white--text">PURCHASE ORDER JASA</v-toolbar-title>
        <v-spacer></v-spacer>
        <v-btn icon @click="openFormPO()">
            <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_pojasa.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" style="white-space: nowrap;">
                    <div v-show="props.item.PurchaseOrderStatus == 'Draft'">
                        <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>Request PO</span>
                        </v-tooltip>
                        <v-tooltip bottom>
                            <template v-slot:activator="{ on }">
                                <v-btn
                                    color="info" dark v-on="on" small 
                                    @click="editRequestPO(props.item)" 
                                    style="min-width: 40px; margin: 0;"
                                >
                                    <v-icon small>edit</v-icon>
                                </v-btn>
                            </template>
                            <span>Edit PO</span>
                        </v-tooltip>
                        <v-tooltip bottom>
                            <template v-slot:activator="{ on }">
                                <v-btn
                                    color="green" dark v-on="on" small 
                                    @click="addAttachmentPO(props.item)" 
                                    style="min-width: 40px; margin: 0;"
                                >
                                    <v-icon small>attachment</v-icon>
                                </v-btn>
                            </template>
                            <span>Upload Berita Acara</span>
                        </v-tooltip>
                        <v-tooltip bottom>
                            <template v-slot:activator="{ on }">
                                <v-btn
                                    color="red" dark small v-on="on"
                                    @click="deleteRequestPO(props.item)" 
                                    style="min-width: 40px; margin: 0;"
                                >
                                    <v-icon small>delete</v-icon>
                                </v-btn>
                            </template>
                            <span>Delete PO</span>
                        </v-tooltip>
                    </div>
                    <div v-show="props.item.PurchaseOrderStatus != 'Draft'">
                        <v-tooltip bottom>
                            <template v-slot:activator="{ on }">
                                <v-btn
                                    color="warning" dark v-on="on" 
                                    @click="viewPO(props.item)" small 
                                    style="min-width: 40px; margin: 0;"
                                >
                                    <v-icon small>visibility</v-icon>
                                </v-btn>
                            </template>
                            <span>Detail PO</span>
                        </v-tooltip>
                        <v-tooltip bottom>
                            <template v-slot:activator="{ on }">
                                <v-btn
                                    color="primary" dark v-on="on" small 
                                    @click="printout(props.item)" 
                                    style="min-width: 40px; margin: 0;"
                                >
                                    <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="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-form-po></dialog-form-po>
    <dialog-item-request></dialog-item-request>
    <dialog-form-edit></dialog-form-edit>
    <dialog-jasa-delete></dialog-jasa-delete>
    <dialog-jasa-attach></dialog-jasa-attach>
    <dialog-view-jasa></dialog-view-jasa>
    <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-form-po': httpVueLoader('./dialogFormPoJasa.vue'),
        'dialog-item-request': httpVueLoader('./dialogSelectItemJasa.vue'),
        'dialog-form-edit': httpVueLoader('./dialogEditFormPoJasa.vue'),
        'dialog-jasa-delete': httpVueLoader('./dialogDeletePoJasa.vue'),
        'dialog-jasa-attach': httpVueLoader('./dialogAddAttachment.vue'),
        'dialog-view-jasa': httpVueLoader('./dialogViewPoJasa.vue'),
        'one-dialog-print': httpVueLoader('../../common/oneDialogPrintX.vue'),
    },
    mounted() {
        this.$store.dispatch("poservice/lookupVendor")
        this.$store.dispatch("poservice/lookupCabang")
        this.$store.dispatch("poservice/lookupPOJasa")
    },
    data() {
        return {
            urlprint: "",
            printtitle: "",
            printwidth: "60%",
            openprint: false,
            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: {
        filterjasa: {
            get() {
                return this.$store.state.poservice.filterjasa;
            },
            set(val) {
                this.$store.commit("poservice/update_filterjasa", val);
            }
        },
        formattedStartdate() {
            return this.formatDate(this.filterjasa.startdate)
        },
        formattedEnddate() {
            return this.formatDate(this.filterjasa.enddate)
        },
        liststatus() {
            return this.$store.state.poservice.liststatus
        },
        list_pojasa() {
            return this.$store.state.poservice.list_pojasa
        },
        pagelength() {
            let total = this.list_pojasa.total
            if (total === undefined || total < 1) return 1
            return Math.ceil(total/10)
        },
        loading_process() {
            return this.$store.state.poservice.loading_process
        },
        currpage: {
            get() {
                return this.$store.state.poservice.currpage
            },
            set(val) {
                this.$store.commit("poservice/update_currpage", val)
                this.$store.dispatch("poservice/lookupPOJasa")
            }
        },
        snackbar: {
            get() {
                return this.$store.state.poservice.snackbar
            },
            set(val) {
                this.$store.commit("poservice/update_snackbar", val)
            }
        }
    },
    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, ".");
        },
        sendRequestPO(item) {
            if (!this.loading_process) {
                this.$store.dispatch("poservice/sendRequestPO", item.PurchaseOrderID)
            } else {
                let snac = {color: 'warning', msg: 'proses request masih berjalan', state: true}
                this.$store.commit('poservice/update_snackbar', snac)
            }
        },
        editRequestPO(item) {
            this.$store.dispatch("poservice/lookupDataPOJasa", item.PurchaseOrderID)
            this.$store.commit("poservice/update_dialog_editformpo", true)
        },
        addAttachmentPO(item) {
            let param = {
                TJasaID: item.T_KontrakJasaID, 
                poID: item.PurchaseOrderID
            }

            this.$store.dispatch("poservice/lookupAttachment", param)
            this.$store.commit("poservice/update_pojasa_attachment", item)
            this.$store.commit("poservice/update_dialog_attachment", true)
        },
        deleteRequestPO(item) {
            this.$store.commit("poservice/update_deleted_pojasa", item);
            this.$store.commit("poservice/update_dialog_deletepojasa", true);
        },
        openFormPO() {
            this.$store.commit("poservice/update_dialog_formpo", true);
        },
        viewPO(item) {
            this.$store.dispatch("poservice/lookupDataPOJasa", item.PurchaseOrderID)
            this.$store.commit("poservice/update_dialog_viewpo", 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_purchase_order_jasa/pdf?id=" + val.PurchaseOrderID + "&username=" + username + "&tm=" + tm;
            this.openprint = true;
        }
    },
    watch: {
        filterjasa: {
            handler(val) {
                this.$store.dispatch("poservice/lookupPOJasa")
            },
            deep: true
        }
    }
}
</script>