<template>
    <v-layout class="fill-height" column>
        <!-- Alert dialog -->
        <v-dialog v-model="dialogdeletealert" max-width="30%">
            <v-card>
                <v-card-title class="headline grey lighten-2 pt-2 pb-2" primary-title>
                    Peringatan !
                </v-card-title>
                <v-card-text class="pt-2 pb-2">
                    <v-layout row>
                        <v-flex xs12 d-flex>
                            <v-layout row>
                                <v-flex pb-1 xs12>
                                    <v-layout row>
                                        <v-flex pt-2 pr-2 xs12>
                                            {{ msgalert }}
                                        </v-flex>
                                    </v-layout>
                                </v-flex>
                            </v-layout>
                        </v-flex>
                    </v-layout>
                </v-card-text>
                <v-divider></v-divider>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn v-if="xact_notreset === 'notreset' || xact === 'reset' || xact === 'save'" color="primary" flat @click="dialogdeletealert = false">
                        Tutup
                    </v-btn>
                    <v-btn v-if="xact === 'reset'" color="primary" flat
                        @click="resetAll()">
                        Yakin lah
                    </v-btn>
                    <v-btn v-if="xact === 'save'" color="primary" flat
                        @click="saveConsumableAll()">
                        Yakin lah
                    </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- End alert dialog -->
        <!-- ERROR DIALOG -->
        <v-dialog v-model="dialog_error" max-width="500px">
            <v-card>
                <v-card-title>
                    <span>ERROR !</span>
                    <v-spacer></v-spacer>
                </v-card-title>
                <v-divider></v-divider>
                <div class="ma-3 red--text">{{ msgError }}</div>
                <v-divider></v-divider>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="primary" flat @click="dialog_error = false">Close</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- END ERROR DIALOG -->
        <!-- Snackbar -->
        <v-snackbar v-model="snackbar" :timeout="5000" :multi-line="false" :vertical="false" :top="true" :color="clr"
            :value="snackbar">
            {{ msgsnackbar }}
            <v-btn flat @click="snackbar = false"> Tutup </v-btn>
        </v-snackbar>
        <!-- End Snackbar -->
        <!-- Form Dialog -->
        <v-dialog v-model="dialog_form" persistent width="50%">
            <v-card>
                <v-card-title>
                    <span class="title">Tambah Consumable</span>
                </v-card-title> 
                <v-divider></v-divider>
                <v-card-text class="pt-3 pb-0">
                    <v-layout row wrap>
                        <v-flex xs12>
                            <v-select v-model="xgetitemconsumable" :items="vitems" item-text="M_ConsumableName"
                                item-value="consumableid" :error="itemconsumable_error_message !== ''" :error-messages="itemconsumable_error_message"
                                @blur="itemconsumableValidation"
                                return-object label="Pilih Item" required>
                            </v-select>
                        </v-flex>
                    </v-layout>
                </v-card-text>
                <v-divider></v-divider>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="primary" flat @click="dialog_form = false">Tutup</v-btn>
                    <v-btn color="primary" flat
                        @click="saveAdd()">Simpan</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- End Form Dialog -->

        <v-card class="mb-2 pa-2">
            <v-card-title>
                <span class="title font-weight-light">PEMERIKSAAN :</span>
            </v-card-title>
            <v-layout row wrap class="scroll-container" style="max-height:200px;overflow: auto;">
                <v-flex class="border-bottom-dashed" xs12 pt-2 pl-2 pr-2 pb-4>
                    <v-layout xs12 row wrap>
                        <v-flex xs6 v-for="(vst,idx) in vtests" :key="`6${idx}`" class="pr-2 pb-1">
                            <v-layout row>
                                <v-chip
                                color="orange"
                                label
                                text-color="white"
                                style="white-space: normal; display: block;"
                                >{{ vst.testname }}</v-chip>
                            </v-layout>
                        </v-flex>
                    </v-layout>
                </v-flex>
            </v-layout>
        </v-card>
        <v-card>
            <v-layout row>
                <v-flex pt-2 pl-2>
                    <div class="text-md-right">
                        <v-btn
                            v-if="this.selected_consumable.isHidden == true"
                            color="primary"
                            @click="addConsumable()"
                        >
                        Tambah Consumable
                        </v-btn>
                    </div>
                </v-flex>
            </v-layout>
            <v-layout row style="max-height:500px;overflow: auto;">
                <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                    <v-data-table :headers="headers" :items="xconsumables" :isLoading="isLoading"  hide-actions class="elevation-1 fixed_header">
                        <template slot="items" slot-scope="props">
                            <td class="text-xs-left px-2">
                                {{ props.item.M_ConsumableName }}
                            </td>
                            <td class="text-xs-center px-2">
                                <div v-if="props.item.isHidden === true">
                                    <v-text-field class="mt-3"
                                        v-model="props.item.T_ConsumableQty"
                                        item-value="T_ConsumableQty"
                                        solo single-line label="" type="number"></v-text-field>
                                </div>
                                <div v-if="props.item.isHidden === false">
                                    <v-text-field class="mt-3"
                                        v-model="props.item.M_ConsumableDefaultQty"
                                        item-value="M_ConsumableDefaultQty"
                                        solo single-line label="" type="number"></v-text-field>
                                </div>
                            </td>
                            <td class="text-xs-center px-2">
                                <div v-if="props.item.isHidden === true">
                                    <v-layout row>
                                        <v-flex>
                                            <v-select
                                            :items="props.item.units"
                                            v-model="props.item.M_SatuanID"
                                            return-object
                                            item-value="M_SatuanID"
                                            item-text="M_SatuanName"
                                            solo
                                            class="mt-3"
                                            ></v-select>
                                        </v-flex>
                                    </v-layout>
                                </div>
                                <div v-if="props.item.isHidden === false">
                                    <v-layout row>
                                        <v-flex>
                                            <v-select
                                            :items="props.item.units"
                                            v-model="props.item.M_SatuanID"
                                            return-object
                                            item-value="M_SatuanID"
                                            item-text="M_SatuanName"
                                            solo
                                            class="mt-3"
                                            ></v-select>
                                        </v-flex>
                                    </v-layout>
                                </div>

                            </td>
                        </template>
                    </v-data-table>

                    <v-layout row>
                        <v-flex pt-2 pl-2>
                            <div class="text-md-right pt-2 pb-2">
                                <v-btn
                                    color="error"
                                    @click="confirmResetAll()"
                                    >
                                    Reset
                                </v-btn>

                                <v-btn
                                    color="primary"
                                    @click="openSaveConsumableAll()"
                                    >
                                    Simpan
                                </v-btn>
                            </div>
                        </v-flex>
                    </v-layout>
                </v-flex>
            </v-layout>
        </v-card>

        </v-layout>
</template>

<style scoped>
    .searchbox .v-input.v-text-field .v-input__slot {
        min-height: 60px;
    }

    .searchbox .v-btn {
        min-height: 60px;
    }

    table.v-table tbody td,
    table.v-table tbody th {
        height: 40px;
    }

    table.v-table thead tr {
        height: 40px;
    }

    .scroll-container {
        scroll-padding: 50px 0 0 50px;
    }

    ::-webkit-scrollbar {
        width: 7px;
    }

    /* this targets the default scrollbar (compulsory) */

    ::-webkit-scrollbar-track {
        background-color: #73baf3;
    }

    /* the new scrollbar will have a flat appearance with the set background color */

    ::-webkit-scrollbar-thumb {
        background-color: #2196f3;
    }

    /* this will style the thumb, ignoring the track */

    ::-webkit-scrollbar-button {
        background-color: #0079da;
    }

    /* optionally, you can style the top and the bottom buttons (left and right for horizontal bars) */

    ::-webkit-scrollbar-corner {
        background-color: black;
    }
</style>

<script>
    module.exports = {
        data() {
            return {
                xgetitemconsumable: 0,
                itemconsumable_error_message: "",
                clr: "success",
                msgalert: "",
                dialogdeletealert: false,
                headers: [{
                        text: "ITEM",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 teal lighten-3 white--text"
                    },
                    {
                        text: "QTY",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 teal lighten-3 white--text"
                    },
                    {
                        text: "UNIT",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 teal lighten-3 white--text"
                    },
                ],
            };
        },
        mounted() {

        },
        computed: {
            vtests() {
                return this.$store.state.consumable.tests
            },
            selected_orderheaderid() {
                return this.$store.state.consumable.selected_orderheaderid
            },
            xconsumables() {
                return this.$store.state.consumable.consumables
            },
            isLoading() {
                return this.$store.state.consumable.search_consumable == 1
            },
            dialog_form: {
                get() {
                    return this.$store.state.consumable.dialog_form
                },
                set(val) {
                    this.$store.commit("consumable/update_dialog_form", val)
                    this.$store.dispatch("consumable/get_item")
                } 
            },
            units() {
                return this.$store.state.consumable.satuans
            },
            selected_consumable: {
                get() {
                    return this.$store.state.consumable.selected_consumable
                },
                set(val) {
                    this.$store.commit("consumable/update_selected_consumable", val)
                }
            },
            vitems() {
                return this.$store.state.consumable.items
            },
            snackbar: {
                get() {
                    return this.$store.state.consumable.alert_success
                },
                set(val) {
                    this.$store.commit("consumable/update_alert_success", val)
                }
            },
            msgsnackbar: {
                get() {
                    return this.$store.state.consumable.msg_success
                },
                set(val) {
                    this.$store.commit("consumable/update_msg_success", val)
                }
            },
            dialog_error: {
                get() {
                    return this.$store.state.consumable.alert_error
                },
                set(val) {
                    this.$store.commit("consumable/update_alert_error", val)
                },
            },
            msgError() {
                return this.$store.state.consumable.error_message
            },
            xact: {
                get() {
                    return this.$store.state.consumable.act
                },
                set(val) {
                    this.$store.commit("consumable/update_act", val)
                }
            },
            xact_notreset: {
                get() {
                    return this.$store.state.consumable.act_notreset
                },
                set(val) {
                    this.$store.commit("consumable/update_act_notreset", val)
                }
            }
        },
        methods: {
            thr_search_orderheader: _.debounce(function () {
                this.$store.dispatch("consumable/gettest")
                this.$store.dispatch("consumable/searchconsumable")
                this.$store.dispatch("consumable/get_item")
                // this.$store.dispatch("consumable/get_satuan")
            }, 1000),
            // isSelected(p) {
            //     return p.M_ConsumableID == this.$store.state.consumable.selected_consumable.M_ConsumableID
            // },
            selectMe(selected) {
                this.$store.commit("consumable/update_selected_consumable", selected)
                // this.$store.commit("consumable/update_last_id", selected.M_ConsumableID)
            },
            //validation
            itemconsumableValidation() {
                if (this.xgetitemconsumable.consumableid < 1 || !this.xgetitemconsumable.consumableid) {
                    this.itemconsumable_error_message = "Pilih salah satu item"
                }
                if (this.xgetitemconsumable.consumableid > 0) {
                    this.itemconsumable_error_message = ""
                }
            },
            resetAllInput() {
                this.xgetitemconsumable.consumableid = 0
                this.itemconsumable_error_message = ""
            },
            // tambah consumable
            addConsumable() {
                this.dialog_form = true
                this.xgetitemconsumable = ""
                this.resetAllInput()
            },
            saveAdd() {
                this.itemconsumableValidation()
                if (this.itemconsumable_error_message === "") {
                    let prm = {
                        orderheaderid: this.$store.state.consumable.selected_orderheaderid,
                        consumableid: this.xgetitemconsumable.consumableid,
                        satuanid: this.xgetitemconsumable.M_ConsumableDefaultM_SatuanID,
                        qty: this.xgetitemconsumable.M_ConsumableDefaultQty,
                        name: this.xgetitemconsumable.M_ConsumableName
                    }
                    this.$store.dispatch("consumable/addconsumable", prm)
                }
            },
            // reset
            confirmResetAll() {
                if (this.selected_consumable.isHidden === false) {
                    this.$store.commit("consumable/update_act_notreset", "notreset")
                    this.$store.commit("consumable/update_act", "")
                    this.msgalert = "Tidak bisa di reset, Pasien belum memiliki item consumable"
                    this.dialogdeletealert = true
                } else if (this.selected_consumable.isHidden === true) {
                    this.$store.commit("consumable/update_act", "reset")
                    this.msgalert = "Yakin, mau reset semua consumable ?"
                    this.dialogdeletealert = true
                }
            },
            resetAll() {
                let prm = {
                    orderheaderid: this.$store.state.consumable.selected_orderheaderid
                }
                this.$store.dispatch("consumable/reset", prm)
                this.dialogdeletealert = false
            },
            // save consumable
            openSaveConsumableAll() {
                this.$store.commit("consumable/update_act", "save")
                this.msgalert = "Yakin, mau simpan CONSUMABLE ?"

                let datas = []
                for (let i = 0; i < this.xconsumables.length; i++) {
                    const item = this.xconsumables[i];
                    // console.log(item)
                    var unitx = item.units
                    // console.log(unitx)
                    for (let j = 0; j < unitx.length; j++) {
                        const itemUnit = unitx[j];
                        var unit = itemUnit.M_SatuanID
                        // console.log(unit)
                    }

                    if (item.M_ConsumableDefaultQty === null || 
                        item.M_ConsumableDefaultQty === "" || 
                        item.T_ConsumableQty === null || 
                        item.T_ConsumableQty === "")
                    {
                        this.clr = "black";
                        this.msgsnackbar = "QTY ada yang kosong"
                        this.snackbar = true;
                        this.dialogdeletealert = false
                        break;
                    } else if (item.M_ConsumableDefaultQty !== null &&
                        item.M_ConsumableDefaultQty !== "" &&
                        item.T_ConsumableQty !== null && 
                        item.T_ConsumableQty !== "") 
                    {
                        this.dialogdeletealert = true
                    }
                }
            },
            saveConsumableAll() {

                if (this.selected_consumable.isHidden === false) {
                    let datas = []
                    for (let i = 0; i < this.xconsumables.length; i++) {
                        const item = this.xconsumables[i];
                        // console.log(item)
                        var unitx = item.units
                        // console.log(unitx)
                        for (let j = 0; j < unitx.length; j++) {
                            const itemUnit = unitx[j];
                            var unit = itemUnit.M_SatuanID
                        }

                        if (item.M_ConsumableDefaultQty === null || 
                        item.M_ConsumableDefaultQty === "" ||
                        item.M_ConsumableDefaultQty === "0" ||
                        item.M_ConsumableDefaultQty === 0) 
                        {
                            this.clr = "black";
                            this.msgsnackbar = "QTY ada yang kosong"
                            this.snackbar = true;
                            this.dialogdeletealert = true
                            break;
                        } else if (item.M_ConsumableDefaultQty !== null &&
                            item.M_ConsumableDefaultQty !== "") 
                        {
                            this.dialogdeletealert = false
                            const prm = {
                                "orderheaderId": this.$store.state.consumable.selected_orderheaderid,
                                "satuanId": item.M_SatuanID,
                                "consumableId": item.M_ConsumableID,
                                "consumableQty": item.M_ConsumableDefaultQty,
                                "type": item.isHidden
                            }
                            datas.push(prm)
                        }
                    }
                    // console.log("prm" + JSON.stringify(datas))
                    if (datas.length > 0) {
                        this.$store.dispatch("consumable/saveAllConsumable", {"data": datas})
                    }
                } else if (this.selected_consumable.isHidden === true) {
                    let datas = []
                    for (let i = 0; i < this.xconsumables.length; i++) {
                        const item = this.xconsumables[i];
                        // console.log(item)
                        var unitx = item.units
                        // console.log(unitx)
                        for (let j = 0; j < unitx.length; j++) {
                            const itemUnit = unitx[j];
                            var unit = itemUnit.M_SatuanID
                        }

                        if (item.T_ConsumableQty === null || 
                        item.T_ConsumableQty === "") 
                        {
                            this.clr = "black";
                            this.msgsnackbar = "QTY ada yang kosong"
                            this.snackbar = true;
                            this.dialogdeletealert = true
                            break;
                        } else if (item.T_ConsumableQty !== null &&
                            item.T_ConsumableQty !== "") 
                        {
                            this.dialogdeletealert = false
                            const prm = {
                                "orderheaderId": this.$store.state.consumable.selected_orderheaderid,
                                "satuanId": item.M_SatuanID,
                                "consumableId": item.M_ConsumableID,
                                "consumableQty": item.T_ConsumableQty,
                                "type": item.isHidden
                            }
                            datas.push(prm)
                        }
                    }
                    // console.log("prm" + JSON.stringify(datas))
                    if (datas.length > 0) {
                        this.$store.dispatch("consumable/saveAllConsumable", {"data": datas})
                    }
                }
            }
        },
        watch: {
            selected_orderheaderid(val, old) {
                this.thr_search_orderheader()
            },
        }
    }
</script>