<template>
    <v-layout>
        <!-- Alert dialog -->
        <v-dialog v-model="dialogsaveamount" 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 color="primary" flat @click="dialogsaveamount = false">
                        Tutup
                    </v-btn>
                    <v-btn color="primary" flat @click="saveConfirm()">
                        Yakin
                    </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- End alert 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 -->

        <!-- 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 -->

        <v-flex xs12>
            <v-card>
                <v-toolbar color="blue lighten-3" dark height="50px">
                    <v-toolbar-title>Email Confirmation</v-toolbar-title>
                    <v-spacer></v-spacer>
                </v-toolbar>
                <v-layout row style="background:white;padding-top:5px;" justify-left>
                    <v-layout row>
                        <v-flex xs3>
                            <v-menu
                                v-model="menufilterdatestart"
                                :close-on-content-click="false"
                                :nudge-right="40"
                                lazy
                                transition="scale-transition"
                                offset-y
                                full-width
                                max-width="290px" min-width="290px">
                                <template v-slot:activator="{ on }">
                                    <v-text-field
                                        class="ma-2"
                                        v-model="filterComputedDateFormattedStart"
                                        label="Tanggal Awal" 
                                        outline 
                                        hide-details 
                                        readonly 
                                        v-on="on"
                                        @blur="date = deFormatedDate(filterComputedDateFormattedStart)"
                                        ></v-text-field>
                                </template>
                                    <v-date-picker no-title
                                        v-model="xdatestart" @input="menufilterdatestart = false"></v-date-picker>
                            </v-menu>
                        </v-flex>
                        <v-flex xs3>
                            <v-menu
                                v-model="menufilterdateend"
                                :close-on-content-click="false"
                                :nudge-right="40" 
                                lazy 
                                transition="scale-transition" 
                                offset-y 
                                full-width
                                max-width="290px" min-width="290px">
                                <template v-slot:activator="{ on }">
                                    <v-text-field 
                                        class="ma-2"
                                        v-model="filterComputedDateFormattedEnd"
                                        label="Tanggal Akhir"
                                        outline 
                                        readonly 
                                        hide-details 
                                        v-on="on"
                                        @blur="date = deFormatedDate(filterComputedDateFormattedEnd)"
                                        ></v-text-field>
                                </template>
                                <v-date-picker no-title
                                    v-model="xdateend" @input="menufilterdateend = false"></v-date-picker>
                            </v-menu>
                        </v-flex>
                        <v-flex xs3>
                            <v-text-field v-model="xsearch" class="ma-2" label="No Reg / Nama" 
                                outline hide-details></v-text-field>
                        </v-flex>
                    </v-layout>
                </v-layout>
                <v-divider></v-divider>
                <div>

                    <v-layout row class="scroll-container" style="max-height:600px;overflow: auto;">
                        <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                            <v-data-table :headers="headers" :items="vpatient" hide-actions class="elevation-1">               
                                <template v-slot:items="props">
                                    <td class="text-xs-center pa-2" v-bind:class="{'blue lighten-5':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.rownumber }}</td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'blue lighten-5':isSelected(props.item)}" @click="selectMe(props.item)">
                                        <span class="font-weight-bold" style="color:#000000">{{ props.item.T_OrderHeaderLabNumber}}</span>
                                        <br />
                                        <span class="font-weight-bold" style="color:#800000">
                                            {{props.item.external_numbering}}
                                        </span>
                                    </td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'blue lighten-5':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.patient_fullname }}</td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'blue lighten-5':isSelected(props.item)}" @click="selectMe(props.item)"><span>{{ props.item.M_CompanyName }}</span> : 
                                        <span class="caption">{{ props.item.M_MouName}}</span></td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'blue lighten-5':isSelected(props.item)}" @click="selectMe(props.item)" v-html="props.item.doctor_fullname"></td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'blue lighten-5':isSelected(props.item)}" @click="selectMe(props.item)" v-html="splittest(props.item.test)"></td>
                                    <td class="text-xs-center pa-2" v-bind:class="{'blue lighten-5':isSelected(props.item)}" @click="selectMe(props.item)">
                                        <v-btn @click="openConfirm(props.item)" v-if="props.item.emailstatus === 'X'" small color="primary" dark>Confirm {{ props.item.M_DeliveryName }} </v-btn>
                                        <v-chip v-if="props.item.emailstatus === 'N' || props.item.emailstatus === 'R'" small label dark>PENDING</v-chip>
                                        <v-chip v-if="props.item.emailstatus === 'Y'" small label dark>SENT</v-chip>
                                        <v-btn @click="openConfirm(props.item)" v-if="props.item.emailstatus === 'Y'" small color="orange" dark>ReSend {{ props.item.M_DeliveryName }} </v-btn>                                       
                                    </td>
                                </template>
                            </v-data-table>
                        </v-flex>
                    </v-layout>

                    <v-divider></v-divider>
                    <v-flex xs12 class="text-xs-left pt-3 pb-3">
                        <v-pagination v-model="curr_page" :length="xtotal_page"></v-pagination>
                    </v-flex>
                </div>

            </v-card>
        </v-flex>
    </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;
    }

    .textinput {
        -webkit-transition: width 0.4s ease-in-out;
        transition: width 0.4s ease-in-out;
        background-color: white;
        background-position: 10px 10px;
        background-repeat: no-repeat;
        padding-left: 40px;
        width: 100%;
        padding: 8px 10px;
        margin-bottom: 5px;
        box-sizing: border-box;
        border: 1px solid #607d8b;

    }

    .textinput:focus {
        width: 100%;
    }

    .textinput:focus::-webkit-input-placeholder {
        color: transparent;
    }

    .textinput:focus::-moz-placeholder {
        color: transparent;
    }

    .textinput:-moz-placeholder {
        color: transparent;
    }

    .boxoutline {
        color: red;
        border: 1px solid red;
        justify-content: center;
        height: 45px;
        line-height: 45px;
        padding-left: 10px;
        background: #ffffff;
        font-size: 14px;
        font-weight: 500;
        border-radius: 1px
    }

    .boxoutline:hover {
        background: rgba(0, 0, 0, 0.07) !important;
        font-size: 15px;
        font-weight: 700;
    }

    .boxsolid {
        color: #ffffff;
        border: 1px solid #ffffff;
        justify-content: center;
        height: 45px;
        line-height: 45px;
        padding-left: 10px;
        background: #f44336;
        font-size: 14px;
        font-weight: 500;
        border-radius: 1px
    }

    .boxsolid:hover {
        background: #f44336de;
        font-size: 15px;
        font-weight: 700;
    }

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

    /* if both the vertical and the horizontal bars appear, then perhaps the right bottom corner also needs to be styled */
</style>

<script>
module.exports = {
    data() {
        return {
            menufilterdatestart:false,
            menufilterdateend:false,
            date: new Date().toISOString().substr(0, 10),
            dialogsaveamount: false,
            msgalert: "",
            orderheaderid: "",
            orderdeliveryid: "",
            destination: "",
            clr: "success",
            headers: [{
                        text: "NO",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "5%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "NO REG",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "NAMA",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "AGREEMENT",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "DOKTER",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "PEMERIKSAAN",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "20%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "AKSI",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "20%",
                        class: "pa-2 blue lighten-3 white--text"
                    }
                ]
        };
    },
    mounted() {
        this.$store.dispatch("patient/search")
    },
    computed: {
        vpatient() {
            return this.$store.state.patient.patient
        },
        isLoading() {
            return this.$store.state.patient.search_status
        },
        xdatestart : {
            get() {
                return this.$store.state.patient.start_date
            },
            set(val) {
                this.$store.commit("patient/update_start_date",val)
            }
        },
        xdateend : {
            get() {
                return this.$store.state.patient.end_date
            },
            set(val) {
                this.$store.commit("patient/update_end_date",val)
            }
        },
        filterComputedDateFormattedStart () {
            return this.formatDate(this.xdatestart)
        },
        filterComputedDateFormattedEnd () {
            return this.formatDate(this.xdateend)
        },
        curr_page: {
            get() {
                return this.$store.state.patient.current_page
            },
            set(val) {
                this.$store.commit("patient/update_current_page", val)
                this.$store.commit("patient/update_last_id", -1)
                this.$store.dispatch("patient/search")
            }
        },
        xtotal_page: {
            get() {
                return this.$store.state.patient.total_patients
            },
            set(val) {
                this.$store.commit("patient/update_total_patients", val)
            }
        },
        xsearch: {
            get() {
                return this.$store.state.patient.x_search
            },
            set(val) {
                this.$store.commit("patient/update_x_search", val)
            },
        },
        snackbar: {
            get() {
                return this.$store.state.patient.alert_success;
            },
            set(val) {
                this.$store.commit("patient/update_alert_success", val);
            }
        },
        msgsnackbar() {
            return this.$store.state.patient.msg_success;
        },
        msgError() {
            return this.$store.state.patient.error_message;
        },
        dialog_error: {
            get() {
                return this.$store.state.patient.alert_error;
            },
            set(val) {
                this.$store.commit("patient/update_alert_error", val);
            },
        }
    },
    methods: {
        isSelected(p) {
            return p.T_OrderHeaderID == this.$store.state.patient.selected_patient.T_OrderHeaderID
        },
        selectMe(selected) {
            this.$store.commit("patient/update_selected_patient", selected)
            this.$store.commit("patient/update_last_id", selected.T_OrderHeaderID)
        },
        formatDate (date) {
            if (!date) return null

            const [year, month, day] = date.split('-')
            return `${day}-${month}-${year}`
        },
        deFormatedDate (date) {
            if (!date) return null

            const [ day,month, year] = date.split('-')
            return `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`
        },
        splittest(inp) {
            if (inp != '') {
               let a_test = inp.split(',')
               let s_test = ''
               for (let i = 0; i < a_test.length; i++) {
                  let test = a_test[i]
                  let a_cito = test.split('^')
                  s_test += '<p class="mb-0 '
                  if (a_cito[1] == 'Y') s_test += 'citoo'
                  s_test += '" >'
                  s_test += a_cito[0] + '</p>'
               }
               return s_test
            } else {
               return ''
            }

         },
        thr_search: _.debounce(function () {
            this.$store.dispatch("patient/search")
        }, 1000),
        openConfirm(item) {
            this.selectMe(item)
            this.orderheaderid = item.T_OrderHeaderID
            this.orderdeliveryid = item.T_OrderDeliveryID
            this.destination = item.T_OrderDeliveryDestination
            if (item.emailstatus === 'X' || item.emailstatus === 'R' ) {
                this.msgalert = "Yakin mau konfirmasi " + item.M_DeliveryName + " ?"
            } else {
                this.msgalert = "Yakin mau resend " + item.M_DeliveryName + " ?"
            }
            this.dialogsaveamount = true 
        },
        saveConfirm() {
            this.dialogsaveamount = false
            let prm = {
                orderheaderid: this.orderheaderid,
                orderdeliveryid: this.orderdeliveryid,
                destination: this.destination,
            }
            this.$store.dispatch("patient/confirm", prm)
            console.log("cek prm", prm)
        }
    },
    watch: {
        xdatestart(val, old) {
            this.xdatestart = val
            this.thr_search()
        },
        xdateend(val, old) {
            this.xdateend = val
            this.thr_search()
        },
        xsearch(val, old) {
            this.xsearch = val
            this.thr_search()
        }
    }
}
</script>