<template>
    <v-layout class="fill-height" column>
        <v-toolbar color="primary" dark>
            <v-toolbar-title class="white--text">LISTING SURAT JALAN</v-toolbar-title>
            <v-spacer></v-spacer>
            <v-btn icon @click="newForm()">
                <v-icon>add_box</v-icon>
            </v-btn>
        </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 Awal"
                                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.records" :loading="false"
                hide-actions class="elevation-1 mb-2"
            >
                <template v-slot:items="props">
                    <tr @click="selectSuratJalan(props.item)" :class="{'yellow lighten-4':isSelected(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.ExpeditionStaffName }}</td>
                        <td class="text-xs-center pa-2">
                            <v-chip small :color="checkColor(props.item.SuratJalanStatus)">
                                {{ props.item.SuratJalanStatus }}
                            </v-chip>
                        </td>
                        <td class="text-xs-center pa-2">
                            <v-icon
                                small @click="deleteSJ(props.item)" color="red"
                                :disabled="props.item.SuratJalanStatus != 'Draft'"
                            >
                                delete
                            </v-icon>
                        </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>
    </v-layout>
</template>

<script>
    module.exports = {
        components: {},
        mounted() {
            this.$store.dispatch("suratjalan/getBranches");
            this.$store.dispatch("suratjalan/listingSuratJalan");
        },
        data() {
            return {
                menuStartDate: false,
                menuEndDate: false,
                headers: [
                    {
                        text: "SURAT JALAN",
                        align: "center",
                        sortable: false,
                        value: "requestDate",
                        width: "20%",
                        class: "blue lighten-3 white--text",
                    },
                    {
                        text: "CABANG",
                        align: "center",
                        sortable: false,
                        value: "requestDate",
                        width: "20%",
                        class: "blue lighten-3 white--text",
                    },
                    {
                        text: "PETUGAS",
                        align: "center",
                        sortable: false,
                        value: "requestDate",
                        width: "20%",
                        class: "blue lighten-3 white--text",
                    },
                    {
                        text: "STATUS",
                        align: "center",
                        sortable: false,
                        value: "requestDate",
                        width: "20%",
                        class: "blue lighten-3 white--text",
                    },
                    {
                        text: "AKSI",
                        align: "center",
                        sortable: false,
                        value: "requestDate",
                        width: "20%",
                        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)
                }
            },
            startDateFormated() {
                return this.formatDate(this.f_startdate)
            },
            f_enddate: {
                get() {
                    return this.$store.state.suratjalan.f_enddate
                },
                set(val) {
                    this.$store.commit("suratjalan/update_f_enddate", val)
                }
            },
            endDateFormated() {
                return this.formatDate(this.f_enddate)
            },
            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)
                }
            },
            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
            },
            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.l_suratjalan.total
                if (total == undefined || total == 0) {
                    return 1
                }
                return Math.ceil(total/10)
            },
            i_phone() {
                return this.$store.state.suratjalan.i_phone
            },
            s_suratjalan() {
                return this.$store.state.suratjalan.s_suratjalan
            }
        },
        methods: {
            formatDate(date) {
                if (!date) return null
                const [year, month, day] = date.split("-")
                return `${day}-${month}-${year}`
            },
            searchSuratJalan() {
                this.$store.dispatch("suratjalan/listingSuratJalan");
            },
            selectSuratJalan(item) {
                this.$store.dispatch("suratjalan/mapDetailSuratJalan", {SuratJalanID: item.SuratJalanID, page: 1})
            },
            isSelected(item) {
                return item.SuratJalanID === this.s_suratjalan.SuratJalanID
            },
            confirmSJ(item) {
                if (item.SuratJalanID == '' || item.SuratJalanID == null) {
                    return
                }

                if (!this.i_phone || !item.SuratJalanEkspedisiID || !item.SuratJalanEkspedisiStaffID) {
                    let x = { isOpen: true, color: "error", message: "Ekpedisi/Staff/No Hp Masih kosong"}
                    this.$store.commit("suratjalan/update_snackbar", x)
                    return
                }

                this.$store.dispatch("suratjalan/generateJurnal", item)
            },
            deleteSJ(item) {
                if (item.SuratJalanID == '' || item.SuratJalanID == null) {
                    return
                }

                this.$store.dispatch("suratjalan/deleteSuratJalan", { 
                    SJalanID: item.SuratJalanID,
                    TGoodID: item.SuratJalanT_GoodsTransferID
                })
            },
            checkColor(status) {
                switch (status) {
                    case 'Process':
                        return 'orange'
                    case 'Completed':
                        return 'green'
                    default:
                        return ""
                }
            },
            newForm() {
                this.$store.dispatch("suratjalan/newForm")
            }
        },
        watch: {
            
        },
    }
</script>