<template>
    <v-layout class="fill-height" column>
        <v-dialog v-model="dialogsuccess" persistent max-width="290">
            <v-card>
                <v-card-title color="success" class="headline">Berhasil !</v-card-title>
                <v-card-text>
                    {{msgsuccess}}
                </v-card-text>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="green darken-1" flat @click="closeDialogSuccess">OK</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <v-card class="mb-2 pa-2 searchbox">
            <v-layout>
                <v-menu class="xs3 pr-2" 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="mt-1" v-model="filterComputedDateFormattedStart" label="Filter Tanggal Pengiriman" outline readonly v-on="on" @blur="date = deFormatedDate(filterComputedDateFormattedStart)"></v-text-field>
                    </template>
                    <v-date-picker v-model="xstartdate" no-title @input="menufilterdatestart = false"></v-date-picker>
                </v-menu>
                <v-text-field class="xs4 ma-1" label="No.Reg / Nama Pasien" outline v-model="name"></v-text-field>         
                <v-select class="xs4 mini-select ma-1" :items="xstatuses" style="font-size:14px" item-text="label" return-object v-model="xselectedstatus"
                    label="Status" outline hide-details></v-select>          
            </v-layout>
        </v-card>
        <v-card>
            <v-subheader red--text text--lighten-1>
                <v-layout row>
                    <v-flex class="text-sm-left" xs6>
                        DAFTAR STATUS WHATSAPP
                    </v-flex>
                </v-layout>
            </v-subheader>
            <v-layout row>
                <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                    <v-data-table :headers="headers" :items="doctors" :loading="isLoading" hide-actions class="elevation-1">
                        <template slot="items" slot-scope="props">
                            <td class="text-xs-left pa-2" >{{ props.item.T_OrderHeaderLabNumber}}</td>
                            <td class="text-xs-left pa-2" >{{ props.item.M_PatientName}}</td>
                            <td class="text-xs-left pa-2" >{{ props.item.M_DoctorName}}</td>
                            <td class="text-xs-left pa-2" >{{ props.item.validasi}}</td>
                            <td class="text-xs-left pa-2" >{{ props.item.Last_StatusPaymentIsLunas}}</td>
                            <td class="text-xs-left pa-2" ><v-btn small :color="props.item.K == 'N' ? 'grey' : props.item.K == 'P' ? 'yellow' : 'green'">K</v-btn>
                            <v-btn small :color="props.item.T == 'N' ? 'grey' : props.item.T == 'P' ? 'yellow' : 'green'">T</v-btn>
                            <v-btn small :color="props.item.B == 'N' ? 'grey' : props.item.B == 'P' ? 'yellow' : 'green'">B</v-btn></td>
                            <td class="text-xs-left pa-2" >{{ props.item.M_DeliveryName}} | {{props.item.T_OrderDeliveryDestination}}</td>

                            <td class="text-xs-left pa-2" v-if="props.item.Tx_WhatsappGroup_ResultID != -1">{{ props.item.Group_ResultName}}{{ props.item.temail}}</td>
                            <td class="text-xs-left pa-2" v-if="props.item.Tx_WhatsappGroup_ResultID != -1">{{ props.item.wastatus}}</td>

                            <td class="text-xs-left pa-2" v-if="props.item.Tx_WhatsappGroup_ResultID == -1">Greeting</td>
                            <td class="text-xs-left pa-2" v-if="props.item.Tx_WhatsappGroup_ResultID == -1">{{ props.item.wastatus}}</td>
                        </template>
                    </v-data-table>
                    <v-divider></v-divider>
                    <v-pagination style="margin-top:10px;margin-bottom:10px" v-model="curr_page" :length="xtotal_page"></v-pagination>
                </v-flex>
            </v-layout>
        </v-card>
        <one-dialog-info :status="opendialoginfo" :msg="msginfo" @close-dialog-info="closeDialogInfo()"></one-dialog-info>
        <one-dialog-alert :status="openalertconfirmation" :msg="msgalertconfirmation" @forget-dialog-alert="forgetAlertConfirmation()"
            @close-dialog-alert="closeAlertConfirmation()"></one-dialog-alert>
        <v-dialog v-model="dialogdoctor" persistent max-width="600px">
            <v-card>
                <v-card-title>
                    <span class="headline">Anda akan mem-verifikasi</span>
                </v-card-title>
                <v-card-text class="pt-0 pb-0">
                    <v-form ref="formdoctor">
                        <v-layout wrap>
                            <v-flex xs12>
                            <span>Nama : {{this.tname}}</span>
                            </v-flex>
                            <v-flex xs12>
                            <span>No. HP : {{this.thp}}</span>
                            </v-flex>
                            <v-flex xs12>
                            <span>Cabang : {{this.tbranch}}</span>
                            </v-flex>
                </v-card-text>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="blue darken-1" :disabled="in_saving" flat @click="saveFormDoctor()">Verifikasi</v-btn>
                    <v-btn color="blue darken-1" flat @click="updateDialogFormDoctor()">Tutup</v-btn>

                </v-card-actions>
                </v-form>
            </v-card>
        </v-dialog>
        </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;
    }
</style>

<script>
    module.exports = {
        components: {
            'one-dialog-info': httpVueLoader('../../common/oneDialogInfo.vue'),
            'one-dialog-alert': httpVueLoader('../../common/oneDialogAlert.vue')
        },
        mounted() {
             this.$store.dispatch("doctor/getsexreg", {
                code: this.code,
                name: this.name,
                date: this.xstartdate,
                status: this.xselectedstatus.value,
                branch: this.selected_filter_branch.M_BranchCode,
                current_page: 1,
                lastid: -1
            })
        },
        methods: {
            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')}`
            },
            isSelected(p) {
                return p.id == this.$store.state.doctor.selected_doctor.id
            },
            searchDoctor() {
                this.$store.dispatch("doctor/search", {
                    name: this.name,
                    date: this.xstartdate,
                    status: this.xselectedstatus.value,
                    branch: this.selected_filter_branch.M_BranchCode,
                    current_page: 1,
                    lastid: -1
                })
                this.$store.commit("doctor/update_current_page", 1)
            },
            thr_search: _.debounce(function () {
                    this.$store.dispatch("doctor/search", {
                    name: this.name,
                    date: this.xstartdate,
                    status: this.xselectedstatus.value,
                    branch: this.selected_filter_branch.M_BranchCode,
                    current_page: 1,
                    lastid: -1
                    })
                }, 1000),
            selectMe(doc) {
                if (this.$store.state.doctor.no_save == 0) {
                    this.$store.commit("doctor/update_selected_doctor", doc)
                    this.$store.commit("doctor/update_preffix", doc.M_DoctorPrefix)
                    this.$store.commit("doctor/update_doctor_name", doc.M_DoctorName)
                    this.$store.commit("doctor/update_suffix", doc.M_DoctorSufix)
                    this.$store.commit("doctor/update_selected_sex", {
                        M_SexID: doc.M_DoctorM_SexID,
                        m_sexname: doc.M_SexName
                    })
                    console.log({
                        M_BranchCode: doc.M_BranchCode,
                        M_BranchName: doc.M_BranchName
                    })
                    this.$store.commit("doctor/update_selected_branch", {
                        M_BranchCode: doc.M_BranchCode,
                        M_BranchName: doc.M_BranchName
                    })
                    this.$store.commit("doctor/update_selected_religion", {
                        M_ReligionID: doc.M_DoctorM_ReligionID,
                        M_ReligionName: doc.M_ReligionName
                    })
                    this.$store.commit("doctor/update_phone", doc.M_DoctorPhone)
                    this.$store.commit("doctor/update_email", doc.M_DoctorEmail)
                    this.$store.commit("doctor/update_hp", doc.M_DoctorHP)
                    this.$store.commit("doctor/update_note", doc.M_DoctorNote)
                    this.$store.commit("doctor/update_pj", doc.M_DoctorIsPJ)
                    this.$store.commit("doctor/update_dpj", doc.M_DoctorIsDefaultPJ)
                    this.$store.commit("doctor/update_clinic", doc.M_DoctorIsClinic)
                    this.$store.commit("doctor/update_marketing_confirm", doc.M_DoctorIsMarketingConfirm)
                    this.$store.commit("doctor/update_is_default", doc.M_DoctorIsDefault)
                    this.$store.commit("doctor/update_is_default_email", doc.M_DoctorEmailIsDefault)



                    this.$store.commit("doctor/update_act", 'edit')
                } else {
                    this.$store.commit("doctor/update_open_alert_confirmation", true)
                }

            },
            closeAlertConfirmation() {
                this.$store.commit("doctor/update_open_alert_confirmation", false)
            },
            forgetAlertConfirmation() {
                this.$store.commit("doctor/update_no_save", 0)
                this.$store.commit("doctor/update_open_alert_confirmation", false)
            },
            updateAlert_success(val) {
                this.$store.commit("doctor/update_alert_success", val)
            },
            setNewDoctor() {
                this.$store.commit("doctor/update_selected_doctor", {})
                this.$store.commit("doctor/update_preffix", '')
                this.$store.commit("doctor/update_doctor_name", '')
                this.$store.commit("doctor/update_suffix", '')
                this.$store.commit("doctor/update_selected_sex", {})
                this.$store.commit("doctor/update_selected_religion", {})
                this.$store.commit("doctor/update_phone", '')
                this.$store.commit("doctor/update_email", '')
                this.$store.commit("doctor/update_hp", '')
                this.$store.commit("doctor/update_note", '')
                this.$store.commit("doctor/update_pj", 'N')
                this.$store.commit("doctor/update_dpj", 'N')
                this.$store.commit("doctor/update_clinic", 'N')
                this.$store.commit("doctor/update_marketing_confirm", 'N')
                this.$store.commit("doctor/update_is_default", 'N')
                this.$store.commit("doctor/update_addresses", [])

                this.$store.commit("doctor/update_act", 'new')
            },
            saveFormDoctor() {
                if (this.in_saving) return;
                this.in_saving = true;
                this.$store.dispatch("doctor/save", {
                    id: this.xid,
                    tname: this.tname,
                    thp: this.thp,
                    tbranch: this.tbranch,
                        name: this.name,                        
                        date: this.xstartdate,
                        status: this.xselectedstatus.value,
                        branch: this.selected_filter_branch.M_BranchCode,
                        current_page: this.curr_page,
                        lastid: -1
                })
            },
            closeDialogSuccess() {
                let arrdoctor = this.$store.state.doctor.doctors
                var idx = _.findIndex(arrdoctor, item => item.M_DoctorID === this.$store.state.doctor.last_id)
                console.log(idx)
                this.$store.dispatch("doctor/search", {
                    code: this.code,
                    name: this.name,
                    date: this.xstartdate,
                    status: this.xselectedstatus.value,
                    branch: this.selected_filter_branch.M_BranchCode,
                    current_page: this.curr_page,
                    lastid: idx
                })
                this.$store.commit("doctor/update_dialog_success", false)
            },
            confirmDoctor(data) {
                this.xid = data.tGramReportID
                this.tname = data.tGramReportName
                this.thp = data.tGramReportHp
                this.tbranch = data.M_BranchName
                this.$store.commit("doctor/update_dialog_form_doctor", true)
            },
            updateDialogFormDoctor() {
                this.$store.commit("doctor/update_dialog_form_doctor", false)
            },
        },
        computed: {
            dialogdoctor() {
                return this.$store.state.doctor.dialog_form_doctor
            },
            filterComputedDateFormattedStart() {
                return this.formatDate(this.xstartdate)
            },
            xstartdate: {
                get() {
                    return this.$store.state.doctor.startdate
                },
                set(val) {
                    this.$store.commit("doctor/update_startdate", val)
                    this.searchDoctor()
                }
            },
            in_saving: {
                get() {
                    return this.$store.state.doctor.in_saving
                },
                set(val) {
                    this.$store.commit("doctor/update_in_saving", val)
                }
            },
            selected_filter_branch: {
                get() {
                    return this.$store.state.doctor.selected_filter_branch
                },
                set(val) {
                    this.$store.commit("doctor/update_selected_filter_branch", val)
                    this.searchDoctor()
                }
            },
            branchs: {
                get() {
                    return this.$store.state.doctor.branchs
                },
                set(val) {
                    this.$store.commit("doctor/update_branchs", val)
                }
            },
            dialogsuccess: {
                get() {
                    return this.$store.state.doctor.dialog_success
                },
                set(val) {
                    this.$store.commit("doctor/update_dialog_success", val)
                }
            },
            msgsuccess() {
                return this.$store.state.doctor.msg_success
            },
            snackbar: {
                get() {
                    return this.$store.state.doctor.alert_success
                },
                set(val) {
                    this.$store.commit("doctor/update_alert_success", val)
                }
            },
            isLoading() {
                return this.$store.state.doctor.search_status == 1
            },
            xstatuses() {
                return this.$store.state.doctor.statuses
            },
            xselectedstatus: {
                get() {
                    return this.$store.state.doctor.selected_status
                },
                set(val) {
                    this.$store.commit("doctor/update_selected_status", val)
                    this.searchDoctor()
                }
            },
            doctors() {
                return this.$store.state.doctor.doctors
            },
            openalertconfirmation: {
                get() {
                    return this.$store.state.doctor.open_alert_confirmation
                },
                set(val) {
                    this.$store.commit("doctor/update_open_alert_confirmation", val)
                }
            },
            totaldoctor() {
                return this.$store.state.doctor.total_doctor
            },
            totaldoctorfilter() {
                return this.$store.state.doctor.total_doctorfilter
            },
            curr_page: {
                get() {
                    return this.$store.state.doctor.current_page
                },
                set(val) {
                    this.$store.commit("doctor/update_current_page", val)
                    this.$store.dispatch("doctor/search", {
                        code: this.code,
                        name: this.name,                        
                        date: this.xstartdate,
                        status: this.xselectedstatus.value,
                branch: this.selected_filter_branch.M_BranchCode,
                        current_page: val,
                        lastid: -1
                    })
                }
            },
            newcode: {
                get() {
                    return this.$store.state.doctor.newcode
                },
                set(val) {
                    this.$store.commit("doctor/update_newcode", val)
                }
            },
            xtotal_page: {
                get() {
                    return this.$store.state.doctor.total_doctor
                },
                set(val) {
                    this.$store.commit("doctor/update_total_doctor", val)
                }
            },
            xtotal_filter: {
                get() {
                    return this.$store.state.doctor.total_filterdoctor
                },
                set(val) {
                    this.$store.commit("doctor/update_total_filterdoctor", val)
                }
            },
            opendialoginfo: {
                get() {
                    return this.$store.state.doctor.open_dialog_info
                },
                set(val) {
                    this.$store.commit("doctor/update_open_dialog_info", val)
                }
            },
            msginfo() {
                return this.$store.state.doctor.msg_info
            }
        },
        watch: {
                name(val, old) {
                    if (val !== old && this.lookupstatus !== 1) {
                        console.log(val)
                        this.name = val
                        this.thr_search()
                    }
                }
            },
        data() {
            return {
                msgalertconfirmation: "Perubahan yang telah dilakukan belum disimpan dong !",
                items: [],
                code: '',
                name: '',
                xid: 0,
                tname: '',
                thp:'',
                tbranch:'',
                oldcode: '',
                menufilterdatestart: false,
                date: new Date().toISOString().substr(0, 10),
                page: 1,
                total_pages: 5,
                headers: [{
                        text: "NO. REG",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "5%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "NAMA PASIEN",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "DOKTER",
                        align: "left",
                        sortable: false,
                        value: "lab",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "VALIDASI",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "LUNAS",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "WHATSAPP",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "25%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "TUJUAN",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "REPORT",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "STATUS",
                        align: "left",
                        sortable: false,
                        value: "status",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    }
                ],
                pagination: {
                    descending: false,
                    page: 1,
                    rowsPerPage: 5,
                    sortBy: 'M_DoctorName',
                    totalItems: this.$store.state.doctor.total_doctor
                }
            };
        }
    }
</script>
