<template>
    <v-layout class="fill-height" column>
        <v-toolbar color="primary" dark>
            <v-toolbar-title class="white--text">RECEIVE SURAT JALAN</v-toolbar-title>
            <v-spacer></v-spacer>
        </v-toolbar>

        <v-card class="pa-2">
            <v-layout row wrap>
                <v-flex xs2 class="pr-1">
                    <v-menu
                        v-model="menuStartDate" lazy offset-y full-width
                        :close-on-content-click="false" :nudge-right="40"
                        transition="scale-transition" max-width="290px" min-width="290px"
                    >
                        <template v-slot:activator="{on}">
                            <v-text-field
                                v-model="startDateFormated" label="Tanggal Akhir"
                                readonly outline hide-details v-on="on"
                            ></v-text-field>
                        </template>
                        <v-date-picker no-title v-model="f_startdate" @input="menuStartDate = false"></v-date-picker>
                    </v-menu>
                </v-flex>
                <v-flex xs2 class="px-1">
                    <v-menu
                        v-model="menuEndDate" lazy offset-y full-width
                        :close-on-content-click="false" :nudge-right="40"
                        transition="scale-transition" max-width="290px" min-width="290px"
                    >
                        <template v-slot:activator="{on}">
                            <v-text-field
                                v-model="endDateFormated" label="Tanggal Akhir"
                                readonly outline hide-details v-on="on"
                            ></v-text-field>
                        </template>
                        <v-date-picker no-title v-model="f_enddate" @input="menuEndDate = false"></v-date-picker>
                    </v-menu>
                </v-flex>
                <v-flex xs3 class="px-1">
                    <v-autocomplete
                        v-model="f_cabang" :items="l_cabang" label="Cabang"
                        outline hide-details return-object item-text="M_BranchName"
                    ></v-autocomplete>
                </v-flex>
                <v-flex xs2 class="px-1">
                    <v-autocomplete
                        v-model="f_status" :items="l_status"
                        outline hide-details label="status"
                    ></v-autocomplete>
                </v-flex>
                <v-flex xs2 class="px-1">
                    <v-text-field 
                        v-model="f_search" label="No Surat Jalan" 
                        outline hide-details
                    ></v-text-field>
                </v-flex>
                <v-flex xs1 class="pl-1">
                    <v-btn
                        dark color="primary" @click="searchSuratJalan()"
                        style="min-width: 50px; height: 100%; margin: 0;"
                    >
                        <v-icon>search</v-icon>
                    </v-btn>
                </v-flex>
            </v-layout>
        </v-card>

        <v-card class="pa-2 mt-2">
            <v-data-table :headers="headers" :items="l_suratjalan" hide-actions class="elevation-1 mb-2">
                <template v-slot:items="props">
                    <tr @click="selectSJ(props.item)" :class="{'yellow lighten-4':selected(props.item)}">
                        <td class="text-xs-center pa-2">{{ props.item.SuratJalanNumber }}</td>
                        <td class="text-xs-center pa-2">{{ props.item.M_BranchName }}</td>
                        <td class="text-xs-center pa-2">{{ props.item.M_StaffName }}</td>
                        <td class="text-xs-center pa-2">{{ props.item.SuratJalanReceivedDate }}</td>
                        <td class="text-xs-center pa-2">
                            <span v-if="props.item.SuratJalanReceivedConfirmed == 'N'">Belum Konfirmasi</span>
                            <span v-else="props.item.SuratJalanReceivedConfirmed == 'Y'">Sudah Konfirmasi</span>
                        </td>
                        <td class="text-xs-center pa-2">
                            <v-chip small text-color="white" :color="statusColor(props.item)">
                                {{ props.item.SuratJalanStatus }}
                            </v-chip>
                        </td>
                        <td class="text-xs-center pa-2" style="white-space: nowrap;">
                            <v-btn
                                style="min-width: 25px; height: 25px; margin: 0;"
                                @click="printout(props.item)"
                                small title="Cetak PDF"
                                color="primary"
                            >
                                <v-icon small color="white">print</v-icon>
                            </v-btn>
                        </td>
                    </tr>
                </template>
            </v-data-table>
            <v-divider></v-divider>
            <div class="pa-2 mt-2 text-xs-center">
                <v-pagination v-model="paginat" :length="paginat_len" :total-visible="5"></v-pagination>
            </div>
        </v-card>

        <one-dialog-print
            :title="printtitle"
            :width="printwidth"
            :height="600"
            :status="openprint"
            :urlprint="urlprint"
            @close-dialog-print="openprint = false"
        ></one-dialog-print>
    </v-layout>
</template>

<script>
    module.exports = {
        components: {
            "one-dialog-print": httpVueLoader("../../common/oneDialogPrintX.vue"),
        },
        mounted() {
            this.$store.dispatch("suratjalan/LookupBranches")
            this.$store.dispatch("suratjalan/LookupEkpedisi")
            this.$store.dispatch("suratjalan/LookupEkspedisiStaff")
            this.$store.dispatch("suratjalan/LookupWarehouse")
            // this.$store.dispatch("suratjalan/LookupSuratJalan")
        },
        data() {
            return {
                urlprint: "",
                printtitle: "",
                printwidth: "60%",
                openprint: false,
                menuStartDate: false,
                menuEndDate: false,
                headers: [
                    {
                        text: "SURAT JALAN", align: "center", sortable: false,
                        value: "requestDate", width: "15%", class: "blue lighten-3 white--text",
                    },
                    {
                        text: "CABANG", align: "center", sortable: false,
                        value: "requestDate", width: "20%", class: "blue lighten-3 white--text",
                    },
                    {
                        text: "STAFF PENERIMA", align: "center", sortable: false,
                        value: "requestDate", width: "20%", class: "blue lighten-3 white--text",
                    },
                    {
                        text: "TANGGAL TERIMA", align: "center", sortable: false,
                        value: "requestDate", width: "15%", class: "blue lighten-3 white--text",
                    },
                    {
                        text: "KONFIRMASI", align: "center", sortable: false,
                        value: "requestDate", width: "15%", class: "blue lighten-3 white--text",
                    },
                    {
                        text: "STATUS", align: "center", sortable: false,
                        value: "requestDate", width: "10%", class: "blue lighten-3 white--text",
                    },
                    {
                        text: "AKSI", align: "center", sortable: false,
                        value: "action", width: "10%", class: "blue lighten-3 white--text",
                    }
                ]
            }
        },
        computed: {
            f_startdate: {
                get() {
                    return this.$store.state.suratjalan.f_startdate
                },
                set(val) {
                    this.$store.commit("suratjalan/update_f_startdate", val)
                }
            },
            f_enddate: {
                get() {
                    return this.$store.state.suratjalan.f_enddate
                },
                set(val) {
                    this.$store.commit("suratjalan/update_f_enddate", val)
                }
            },
            f_cabang: {
                get() {
                    return this.$store.state.suratjalan.f_cabang
                },
                set(val) {
                    this.$store.commit("suratjalan/update_f_cabang", val)
                }
            },
            f_status: {
                get() {
                    return this.$store.state.suratjalan.f_status
                },
                set(val) {
                    this.$store.commit("suratjalan/update_f_status", val)
                }
            },
            f_search: {
                get() {
                    return this.$store.state.suratjalan.f_search
                },
                set(val) {
                    this.$store.commit("suratjalan/update_f_search", val)
                }
            },
            paginat: {
                get() {
                    return this.$store.state.suratjalan.paginat
                },
                set(val) {
                    this.$store.commit("suratjalan/update_paginat", val)
                    this.$store.dispatch("suratjalan/listingSuratJalan");
                }
            },
            paginat_len() {
                let total = this.$store.state.suratjalan.l_total_page
                if (total === undefined || total == 0) {
                    return 1
                }
                return Math.ceil(total/10)
            },
            startDateFormated() {
                return this.formatDate(this.f_startdate)
            },
            endDateFormated() {
                return this.formatDate(this.f_enddate)
            },
            l_suratjalan() {
                return this.$store.state.suratjalan.l_suratjalan
            },
            l_cabang() {
                return this.$store.state.suratjalan.l_cabang
            },
            l_status() {
                return this.$store.state.suratjalan.l_status
            },
        },
        methods: {
            formatDate(date) {
                if (!date) return null
                const [year, month, day] = date.split("-")
                return `${day}-${month}-${year}`
            },
            searchSuratJalan() {
                this.$store.dispatch("suratjalan/LookupSuratJalan")
            },
            selectSJ(item) {
                this.$store.dispatch("suratjalan/LookupDetailSuratJalan", {id: item.SuratJalanID})
            },
            selected(item) {
                return item.SuratJalanID == this.$store.state.suratjalan.s_suratjalan.SuratJalanID
            },
            statusColor(item) {
                switch (item.SuratJalanStatus) {
                    case "Process":
                        return "warning"
                    case "Completed":
                        return "success"
                    default:
                        return "gray"
                }
            },
            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_transfer/pdf?id=" + val.SuratJalanID + "&username=" + username + "&tm=" + tm;
                this.openprint = true;
            }
        },
    }
</script>