<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.msg }}
            <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>

        <!-- form -->
        <v-layout row wrap class="mt-2">
            <!-- 1st row -->
            <v-flex xs6 class="pr-1">
                <v-autocomplete
                    v-model="s_suratjalan.i_tftype" label="Jenis Transfer" outline 
                    hide-details :items="['Persediaan', 'Non Persediaan']" disabled
                ></v-autocomplete>
            </v-flex>
            <v-flex xs6 class="pl-1">
                <v-autocomplete
                    v-model="s_suratjalan.i_tfrequest" label="Transfer Request Number" 
                    outline hide-details  return-object disabled
                    :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>

            <!-- 2nd row -->
            <v-flex xs6 class="pr-1 pt-2">
                <v-text-field
                    v-model="sjDateFormated" label="Tanggal"
                    readonly outline hide-details disabled
                ></v-text-field>
            </v-flex>
            <v-flex xs6 class="pl-1 pt-2">
                <v-autocomplete
                    v-model="s_suratjalan.i_isInternal" :items="['Internal', 'Eksternal']" 
                    outline hide-details label="Jenis Kurir" disabled
                ></v-autocomplete>
            </v-flex>

            <!-- 3rd row -->
            <v-flex xs4 class="pr-1 pt-2">
                <v-autocomplete
                    v-model="s_suratjalan.i_ekspedisi" :items="l_ekspedisi" label="Ekspedisi"
                    outline hide-details return-object item-text="ExpeditionName" disabled
                ></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"
                    outline hide-details return-object item-text="ExpeditionStaffName" disabled
                ></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
                ></v-text-field>
            </v-flex>

            <!-- 4th row -->
            <v-flex xs12 class="pt-2">
                <v-textarea
                    v-model="s_suratjalan.i_notes" label="Catatan" 
                    outline hide-details disabled
                ></v-textarea>
            </v-flex>
            <v-flex xs12 class="pt-2">
              <v-autocomplete
                v-model="s_suratjalan.i_receivewarehouse" :items="l_warehouse" return-object
                label="Gudang Penerima" class="mini-select" item-text="WarehouseName"
                :disabled="isVerif()" flat outline hide-no-data hide-details
              ></v-autocomplete>
            </v-flex>
            <v-flex xs12 class="pt-2">
                <v-textarea
                    v-model="s_suratjalan.i_receivenotes" label="Catatan Penerima" 
                    outline hide-details :disabled="isVerif()"
                ></v-textarea>
            </v-flex>
        </v-layout>

        <div>
            <v-data-table
                :headers="headers" :items="s_suratjalan.detail" 
                hide-actions class="elevation-1 mb-2 mt-4"
            >
                <template v-slot:items="props">
                    <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">
                        <div v-if="s_suratjalan.i_tftype === 'Persediaan'">
                            <p v-for="obj in props.item.ItemRequest" class="body-1 mb-0">
                                Batch: {{ obj.StockBatchNo }}, Jumlah: {{ obj.QtyReq }}  {{ obj.ItemUnitName }}
                            </p>
                        </div>
                        <div v-else>
                            <p v-for="obj in props.item.ItemRequest" class="body-1 mb-0">
                               Nomor: {{ obj.StockStockNumber }}, Jumlah: {{ obj.QtyReq }}  {{ obj.ItemUnitName }}
                            </p>
                        </div>
                    </td> 
                    <td class="text-xs-center pa-2">{{ props.item.SuratJalanDetailQty }}  {{ props.item.ItemUnitName ?? '-' }}</td>
                    <td class="text-xs-center pa-2">
                        <span class="body-2">{{ props.item.SuratJalanDetailQty }} / {{ props.item.SuratJalanDetailQtyReceived }}</span>
                        <v-btn small icon @click="editBatch(props.item)" :disabled="isdisabled()" class="pl-2">
                            <v-icon small color="blue">edit</v-icon>
                        </v-btn>
                    </td> 
                    <!-- <td class="text-xs-center pa-2">{{ props.item.ItemUnitName ?? '-' }}</td> -->
                </template>
            </v-data-table>
            <v-divider></v-divider>
        </div>

        <v-layout row wrap>
            <v-flex grow class="pr-1 mt-2">
                <v-btn 
                    outline color="primary" class="white--text" @click="confirmSJ()" 
                    :disabled="isdisabled()"
                >
                    Confirm
                    <v-icon v-show="s_suratjalan.i_isConfirmed == 'Y'" 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()">Batal</v-btn> -->
                <v-btn 
                    color="primary" :disabled="isdisabled()" 
                    class="white--text" @click="simpanDetailSJ()"
                >
                    Simpan
                </v-btn>
            </v-flex>
        </v-layout>
    </v-card>
</template>

<script>
    module.exports = {
        data() {
            return {
                headers: [
                    {
                        text: "No.", align: "center", sortable: false,
                        value: "requestDate", width: "5%", class: "blue lighten-3 white--text",
                    },
                    {
                        text: "ITEM", align: "center", sortable: false,
                        value: "requestDate", width: "30%", class: "blue lighten-3 white--text",
                    },
                    {
                        text: "DETAIL", align: "center", sortable: false,
                        value: "requestDate", width: "30%", class: "blue lighten-3 white--text",
                    },
                    {
                        text: "KUANTITAS", align: "center", sortable: false,
                        value: "requestDate", width: "15%", class: "blue lighten-3 white--text",
                    },
                    {
                        text: "KUANTITAS DITERIMA", align: "center", sortable: false,
                        value: "requestDate", width: "20%", class: "blue lighten-3 white--text",
                    },
                ]
            }
        },
        computed: {
            snackbar: {
                get() {
                    return this.$store.state.suratjalan.snackbar
                },
                set(val) {
                    this.$store.commit("suratjalan/update_snackbar", val)
                }
            },
            s_suratjalan: {
                get() {
                    return this.$store.state.suratjalan.s_suratjalan
                },
                set(val) {
                    this.$store.commit("suratjalan/update_s_suratjalan", val)
                }
            },
            dialog_receive: {
                get() {
                    return this.$store.state.suratjalan.dialog_receive
                },
                set(val) {
                    this.$store.commit("suratjalan/update_dialog_receive", 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
            },
            l_warehouse() {
                return this.$store.state.suratjalan.l_warehouse
            },
        },
        methods: {
            closeSnackbar() {
                let x = {isOpen: false, color: "", message: ""}
                this.snackbar = x
            },
            formatDate(date) {
                if (!date) return null
                const [year, month, day] = date.split("-")
                return `${day}-${month}-${year}`
            },
            isVerif() {},
            isdisabled() {
                const sj = this.s_suratjalan
                let len = Object.keys(sj).length
                let status = sj.i_status
                let confirmed = sj.i_isConfirmed

                if (len <= 0 || status !== 'Process' || confirmed === 'Y') {
                    return true
                } else {
                    return false
                }
            },
            editBatch(item) {
                if (Object.keys(item).length <= 0) {
                    return
                }

                let copy = JSON.parse(JSON.stringify(item))
                this.$store.commit("suratjalan/update_receive_items", copy.ItemRequest)
                this.$store.commit("suratjalan/update_buffer_item", copy)
                this.dialog_receive = true
            },
            simpanDetailSJ() {
                let sj = this.s_suratjalan
                if (Object.keys(sj.i_receivewarehouse).length <= 0) {
                    return
                }

                this.$store.dispatch("suratjalan/saveReceveiSJ")
            },
            confirmSJ() {
                let sj = this.s_suratjalan
                if (Object.keys(sj.i_receivewarehouse).length <= 0) {
                    return
                }
                
                sj.i_status = "Completed"
                this.$store.dispatch("suratjalan/saveReceveiSJ")
                // this.$store.dispatch("suratjalan/testinput")
            }
        },
    }
</script>