<template>
    <v-card class="pa-2">
        <v-snackbar 
            v-model="snackbar.isOpen" :timeout="3000" :multi-line="false" 
            :vertical="false" :top="true" :color="snackbar.color"
        >
            {{ snackbar.message }}
            <v-btn flat @click="closeSnackbar()">Tutup</v-btn>
        </v-snackbar>

        <h3>NOMOR : {{ this.s_suratjalan.SuratJalanNumber ?? '-' }}</h3>
        <h3>STATUS : {{ this.s_suratjalan.i_status }}</h3>
        <v-layout row wrap class="mt-2">
            <v-flex xs6 class="pr-1">
                <v-autocomplete
                    v-model="s_suratjalan.i_tftype" label="Jenis Transfer" outline @input="getTFReq"
                    hide-details :disabled="isDraft()" :items="['Persediaan', 'Non Persediaan']"
                ></v-autocomplete>
            </v-flex>
            <v-flex xs6 class="pl-1">
                <v-autocomplete
                    v-model="s_suratjalan.i_tfrequest" label="Transfer Request Number" return-object 
                    outline hide-details :disabled="isDraft()" @input="getTFDetail"
                    :items="l_tfrequest" item-text="T_GoodsTransferNum"
                >
                    <template v-slot:item="data">
                        <v-list-tile-content>
                            <v-list-tile-title>
                                {{ data.item.T_GoodsTransferNum }}
                            </v-list-tile-title>
                            <v-list-tile-sub-title>
                                Type: {{ data.item.T_GoodsTransferType == 'P' ? 'Persediaan' : 'Non Persediaan'}}, 
                                Tanggal: {{ data.item.T_GoodsTransferDate }}
                            </v-list-tile-sub-title>
                        </v-list-tile-content>
                    </template>
                </v-autocomplete>
            </v-flex>

            <v-flex xs6 class="pr-1 pt-2">
                <v-text-field
                    v-model="sjDateFormated" label="Tanggal"
                    readonly outline hide-details :disabled="isDraft()"
                ></v-text-field>
            </v-flex>
            <v-flex xs6 class="pl-1 pt-2">
                <v-autocomplete
                    v-model="s_suratjalan.i_isInternal" :items="['Internal', 'Eksternal']" :disabled="isDraft()"
                    outline hide-details label="Jenis Kurir" @input="getEkspedisi"
                ></v-autocomplete>
            </v-flex>
            
            <v-flex xs4 class="pr-1 pt-2">
                <v-autocomplete
                    v-model="s_suratjalan.i_ekspedisi" :items="l_ekspedisi" label="Ekspedisi" :disabled="isDraft()"
                    outline hide-details return-object item-text="ExpeditionName" @input="getStaff"
                ></v-autocomplete>
            </v-flex>
            <v-flex xs4 class="px-1 pt-2">
                <v-autocomplete
                    v-model="s_suratjalan.i_staff" :items="l_staff" label="Staff" @input="setPhone" :disabled="isDraft()"
                    outline hide-details return-object item-text="ExpeditionStaffName"
                ></v-autocomplete>
            </v-flex>
            <v-flex xs4 class="pl-1 pt-2">
                <v-text-field 
                    v-model="s_suratjalan.i_phone" label="Nomor HP" 
                    outline hide-details :disabled="isDraft()"
                ></v-text-field>
            </v-flex>

            <v-flex xs12 class="pt-2">
                <v-textarea
                    v-model="s_suratjalan.i_notes" label="Catatan" 
                    outline hide-details :disabled="isDraft()"
                ></v-textarea>
            </v-flex>
        </v-layout>

        <div>
            <v-data-table
                :headers="header" :items="s_suratjalan.detail" :loading="false"
                hide-actions class="elevation-1 mb-2 mt-4"
            >
                <template v-slot:items="props">
                    <tr>
                        <td class="text-xs-center pa-2">{{ (props.index + 1) }}</td> 
                        <td class="text-xs-center pa-2">{{ props.item.M_ItemDesc }}</td> 
                        <td class="text-xs-center pa-2">{{ props.item.Quantity }}</td> 
                        <td class="text-xs-center pa-2">{{ props.item.ItemUnitName ?? '-' }}</td> 
                    </tr>
                </template>
            </v-data-table>
            <v-divider></v-divider>
            <div class="mt-2 pa-2 text-xs-center">
                <v-pagination v-model="d_page" :length="detpage_len" :total-visible="5"></v-pagination>
            </div>
        </div>

        <v-layout row wrap>
            <v-flex grow class="pr-1 mt-2">
                <v-btn outline color="primary" class="white--text" @click="kirimSJ()" :disabled="isDraft()">
                    Simpan & Kirim
                    <v-icon v-show="isDraft()" right>check_circle</v-icon>
                </v-btn>
            </v-flex>
            <v-flex shrink class="pl-1 mt-2">
                <v-btn outline color="primary" class="white--text" @click="cancelSJ()" :disabled="isDraft()">Batal</v-btn>
                <v-btn color="primary" class="white--text" @click="simpanSJ()" :disabled="isDraft()">Simpan</v-btn>
            </v-flex>
        </v-layout>
    </v-card>
</template>

<script>
    module.exports = {
        component: {},
        mounted() {},
        data() {
            return {
                header: [
                    {
                        text: "No.", align: "center", sortable: false,
                        value: "requestDate", width: "10%", class: "blue lighten-3 white--text",
                    },
                    {
                        text: "ITEM", align: "center", sortable: false,
                        value: "requestDate", width: "30%", class: "blue lighten-3 white--text",
                    },
                    {
                        text: "KUANTITAS", align: "center", sortable: false,
                        value: "requestDate", width: "20%", class: "blue lighten-3 white--text",
                    },
                    {
                        text: "SATUAN", align: "center", sortable: false,
                        value: "requestDate", width: "20%", class: "blue lighten-3 white--text",
                    },
                ]
            }
        },
        computed: {
            i_tftype: {
                get() {
                    return this.$store.state.suratjalan.i_tftype
                },
                set(val) {
                    this.$store.commit("suratjalan/update_i_tftype", val);
                }
            },
            i_tfrequest: {
                get() {
                    return this.$store.state.suratjalan.i_tfrequest
                },
                set(val) {
                    this.$store.commit("suratjalan/update_i_tfrequest", val);
                }
            },
            i_isInternal: {
                get() {
                    return this.$store.state.suratjalan.i_isInternal
                },
                set(val) {
                    this.$store.commit("suratjalan/update_i_isInternal", val)
                }
            },
            i_ekspedisi: {
                get() {
                    return this.$store.state.suratjalan.i_ekspedisi
                },
                set(val) {
                    this.$store.commit("suratjalan/update_i_ekspedisi", val)
                }
            },
            i_staff: {
                get() {
                    return this.$store.state.suratjalan.i_staff
                },
                set(val) {
                    this.$store.commit("suratjalan/update_i_staff", val)
                }
            },
            i_phone: {
                get() {
                    return this.$store.state.suratjalan.i_phone
                },
                set(val) {
                    this.$store.commit("suratjalan/update_i_phone", val)
                }
            },
            i_notes: {
                get() {
                    return this.$store.state.suratjalan.i_notes
                },
                set(val) {
                    this.$store.commit("suratjalan/update_i_notes", val)
                }
            },
            i_date: {
                get() {
                    return this.$store.state.suratjalan.i_date
                },
                set(val) {
                    this.$store.commit("suratjalan/update_i_date", val)
                }
            },
            sjDateFormated() {
                return this.formatDate(this.s_suratjalan.i_date)
            },
            l_ekspedisi() {
                return this.$store.state.suratjalan.l_ekspedisi
            },
            l_staff() {
                return this.$store.state.suratjalan.l_staff
            },
            l_tfrequest() {
                return this.$store.state.suratjalan.l_tfrequest
            },
            s_suratjalan: {
                get() {
                    return this.$store.state.suratjalan.s_suratjalan;
                },
                set(val) {
                    this.$store.commit("suratjalan/update_s_suratjalan", val);
                }
            },
            detpage_len() {
                let total = parseInt(this.s_suratjalan.detailtotal)
                if (!total || total == 0) {
                    return 1
                }
                return Math.ceil(total/5)
            },
            d_page: {
                get() {
                    return this.$store.state.suratjalan.d_page
                },
                set(val) {
                    this.$store.commit("suratjalan/update_d_page", val)
                    this.$store.dispatch("suratjalan/detailSuratJalan", {
                        SuratJalanID: this.s_suratjalan.SuratJalanID, 
                        page: this.d_page}
                    )
                }
            },
            snackbar: {
                get() {
                    return this.$store.state.suratjalan.snackbar
                },
                set(val) {
                    this.$store.commit("suratjalan/update_snackbar", val)
                }
            }
        },
        methods: {
            formatDate(date) {
                if (!date) return null
                const [year, month, day] = date.split("-")
                return `${day}-${month}-${year}`
            },
            async getTFReq() {
                this.$store.dispatch("suratjalan/listingTransferRequest");
            },
            async getTFDetail() {
                this.$store.dispatch("suratjalan/listingTFdetail");
            },
            async getEkspedisi() {
                this.$store.dispatch("suratjalan/listingEkspedisi");
                this.s_suratjalan.i_ekspedisi = {}
                this.s_suratjalan.i_staff = {}
                this.s_suratjalan.i_phone = ""
            },
            async getStaff() {
                this.$store.dispatch("suratjalan/listingEkspedisiStaff");
                this.s_suratjalan.i_staff = {}
                this.s_suratjalan.i_phone = ""
            },
            async setPhone() {
                this.s_suratjalan.i_phone = this.s_suratjalan.i_staff.ExpeditionStaffMobile
            },
            async simpanSJ() {
                const sj = this.s_suratjalan
                let kurir = Object.keys(sj.i_ekspedisi).length
                let staff = Object.keys(sj.i_staff).length
                let tfreq = Object.keys(sj.i_tfrequest).length
                let nohpn = sj.i_phone
                let typet = sj.i_tftype
                if (kurir <= 0 || staff <= 0 || tfreq <= 0 || nohpn == "" || typet == "") {
                    let x = {isOpen: true, color: "error", message: "form belum lengkap"}
                    this.snackbar = x
                    return                    
                }

                if (this.s_suratjalan.i_status == 'BARU') {
                    this.$store.dispatch("suratjalan/createSuratJalan")
                } else {
                    this.$store.dispatch("suratjalan/updateSuratJalan")
                }
            },
            cancelSJ() {
                this.$store.dispatch("suratjalan/newForm")
                this.$store.dispatch("suratjalan/listingSuratJalan")
                // let id = this.s_suratjalan.SuratJalanID
                // this.$store.dispatch("suratjalan/detailSuratJalan", {SuratJalanID: id, page: this.d_page})
            },
            closeSnackbar() {
                let x = {isOpen: false, color: "", message: ""}
                this.snackbar = x
            },
            isDraft() {
                let status = this.s_suratjalan.i_status
                if (status === 'Draft' || status === 'BARU') {
                    return false
                }
                return true
            },
            async kirimSJ() {
                let sj = this.s_suratjalan
                let kurir = Object.keys(sj.i_ekspedisi).length
                let staff = Object.keys(sj.i_staff).length
                let tfreq = Object.keys(sj.i_tfrequest).length
                let nohpn = sj.i_phone
                let typet = sj.i_tftype

                if (kurir <= 0 || staff <= 0 || tfreq <= 0 || nohpn == "" || typet == "") {
                    let x = { isOpen: true, color: "error", message: "Tedapat form yang masih kosong"}
                    this.$store.commit("suratjalan/update_snackbar", x)
                    return
                }

                if (sj.i_status == 'BARU') {
                    sj.i_status = 'Process'
                    await this.$store.commit("suratjalan/update_s_suratjalan", sj)
                    await this.$store.dispatch("suratjalan/createSuratJalan")
                } 
                if (sj.i_status == 'Draft') {
                    sj.i_status = 'Process'
                    await this.$store.commit("suratjalan/update_s_suratjalan", sj)
                    await this.$store.dispatch("suratjalan/updateSuratJalan")
                }

                // await this.$store.dispatch("suratjalan/updateSuratJalan")
                // await this.$store.dispatch("suratjalan/generateJurnal", this.s_suratjalan)
            },
        },
        watch: {

        }
    }
</script>