<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-dialog v-model="dialogviewresult" persistent max-width="1050px">
                            <v-card>
                                <v-card-title>
                                    <span class="headline">DETAIL HASIL</span><span> &nbsp;[ <span style="color:#2196F3;font-weight: 900;">{{xtestname}} </span>&nbsp;]
                                </v-card-title>
                                <v-layout row wrap>
                                    <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                                        <v-data-table :headers="headerviewresults" :items="deliveryorderdetailresults" :loading="isLoading" hide-actions class="elevation-1">
                                            <template slot="items" slot-scope="props">
                                                <td class="text-xs-left pa-2" v-bind:class="props.item.T_RefDeliveryOrderDetailStage === 'VALIDATION' ? 'green lighten-3' : 'white'"
                                                    @click="selectMe(props.item)" v-html="props.item.T_OrderHeaderLabNumber">
                                                </td>
                                                <td class="text-xs-left pa-2" v-bind:class="props.item.T_RefDeliveryOrderDetailStage === 'VALIDATION' ? 'green lighten-3' : 'white'"
                                                    v-html="props.item.M_PatientName">
                                                </td>
                                                <td class="text-xs-left pa-2" v-bind:class="props.item.T_RefDeliveryOrderDetailStage === 'VALIDATION' ? 'green lighten-3' : 'white'"
                                                    v-html="props.item.T_TestName">
                                                </td>
                                                <td class="text-xs-left pa-2" v-bind:class="props.item.T_RefDeliveryOrderDetailStage === 'VALIDATION' ? 'green lighten-3' : 'white'"
                                                    v-html="props.item.T_RefDeliveryOrderChildResult">
                                                </td>
                                                <td class="text-xs-left pa-2" v-bind:class="props.item.T_RefDeliveryOrderDetailStage === 'VALIDATION' ? 'green lighten-3' : 'white'"
                                                    v-html="props.item.T_RefDeliveryOrderDetailNote">
                                                </td>
                                            </template>
                                        </v-data-table>
                                    </v-flex>
                                </v-layout>
                                <v-card-actions>
                        <v-spacer></v-spacer>
                        <v-btn color="primary" flat @click="closeViewResult()">
                            Tutup
                        </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-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-text-field class="xs4 ma-1" label="No. Surat Jalan / No.Reg / Nama Pasien" outline v-model="name"></v-text-field>         
                       
            </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 SUDAH SURAT JALAN
                    </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="vdeliveryorders" :loading="isLoading" hide-actions class="elevation-1">
                                <template slot="items" slot-scope="props">
                                    <td class="text-xs-left pa-1" v-bind:class="props.item.T_RefDeliveryOrderDetailStage === 'VALIDATION' ? 'green lighten-3' : 'white'"
                                        @click="selectMe(props.item)">
                                        <span v-if="props.item.iskonfirmasi === 'N'" style="text-decoration:line-through">{{props.item.T_RefDeliveryOrderNumber}}</span>
                                        <span v-if="props.item.iskonfirmasi === 'Y'">{{props.item.T_RefDeliveryOrderNumber}}</span>
                                        <br>{{props.item.tanggal_suratjalan}}</br>
                                    </td>
                                    <td class="text-xs-left pa-1" v-bind:class="props.item.T_RefDeliveryOrderDetailStage === 'VALIDATION' ? 'green lighten-3' : 'white'"
                                        @click="selectMe(props.item)">
                                        <span v-if="props.item.iskonfirmasi === 'N'" style="text-decoration:line-through">{{props.item.destination}}</span>
                                        <span v-if="props.item.iskonfirmasi === 'Y'">{{props.item.destination}}</span>
                                    </td>
                                    <td class="text-xs-left pa-1" v-bind:class="props.item.T_RefDeliveryOrderDetailStage === 'VALIDATION' ? 'green lighten-3' : 'white'"
                                        @click="selectMe(props.item)">
                                        <span v-if="props.item.iskonfirmasi === 'N'" style="text-decoration:line-through">{{props.item.T_OrderHeaderLabNumber}}</span>
                                        <span v-if="props.item.iskonfirmasi === 'Y'">{{props.item.T_OrderHeaderLabNumber}}</span>
                                        <br>{{props.item.tanggal_order}}</br>
                                    </td>
                                    <td class="text-xs-left pa-1" v-bind:class="props.item.T_RefDeliveryOrderDetailStage === 'VALIDATION' ? 'green lighten-3' : 'white'"
                                        @click="selectMe(props.item)">
                                        <span v-if="props.item.iskonfirmasi === 'N'" style="text-decoration:line-through">{{props.item.M_PatientName}}</span>
                                        <span v-if="props.item.iskonfirmasi === 'Y'">{{props.item.M_PatientName}}</span>
                                    </td>
                                    <td class="text-xs-left pa-1" v-bind:class="props.item.T_RefDeliveryOrderDetailStage === 'VALIDATION' ? 'green lighten-3' : 'white'"
                                        @click="selectMe(props.item)">
                                        <span v-if="props.item.iskonfirmasi === 'N'" style="text-decoration:line-through">{{props.item.T_TestName}}</span>
                                        <span v-if="props.item.iskonfirmasi === 'Y'">{{props.item.T_TestName}}</span>
                                    </td>
                                    <td class="text-xs-left pa-2" v-bind:class="props.item.T_RefDeliveryOrderDetailStage === 'VALIDATION' ? 'green lighten-3' : 'white'"
                                        @click="selectMe(props.item)">
                                        <span v-if="props.item.T_RefDeliveryOrderDetailResult !== '...'">{{props.item.T_RefDeliveryOrderDetailResult}}</span>
                                        <v-icon v-if="props.item.T_RefDeliveryOrderDetailResult === '...'" small class="ml-1" color="blue" @click="viewResult(props.item)">view_list</v-icon>
                                    </td>
                                    <td class="text-xs-left pa-1" v-bind:class="props.item.T_RefDeliveryOrderDetailStage === 'VALIDATION' ? 'green lighten-3' : 'white'"
                                        @click="selectMe(props.item)">
                                        <span v-if="props.item.iskonfirmasi === 'N'" style="text-decoration:line-through">{{props.item.T_RefDeliveryOrderDetailNote}}</span>
                                        <span v-if="props.item.iskonfirmasi === 'Y'">{{props.item.T_RefDeliveryOrderDetailNote}}</span>
                                    </td>
                                    <td class="text-xs-left pa-1" v-bind:class="props.item.T_RefDeliveryOrderDetailStage === 'VALIDATION' ? 'green lighten-3' : 'white'"
                                        @click="selectMe(props.item)">
                                        <span v-if="props.item.iskonfirmasi === 'N'" style="text-decoration:line-through">{{props.item.status}}</span>
                                        <span v-if="props.item.iskonfirmasi === 'Y'">{{props.item.status}}</span>
                                    </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("trace/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.trace.selected_doctor.id
            },
            searchDoctor() {
                this.$store.dispatch("trace/searchtrace", {
                    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("trace/update_current_page", 1)
            },
            thr_search: _.debounce(function () {
                    this.$store.dispatch("trace/searchtrace", {
                    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.trace.no_save == 0) {
                    this.$store.commit("trace/update_selected_doctor", doc)
                    this.$store.commit("trace/update_preffix", doc.M_DoctorPrefix)
                    this.$store.commit("trace/update_doctor_name", doc.M_DoctorName)
                    this.$store.commit("trace/update_suffix", doc.M_DoctorSufix)
                    this.$store.commit("trace/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("trace/update_selected_branch", {
                        M_BranchCode: doc.M_BranchCode,
                        M_BranchName: doc.M_BranchName
                    })
                    this.$store.commit("trace/update_selected_religion", {
                        M_ReligionID: doc.M_DoctorM_ReligionID,
                        M_ReligionName: doc.M_ReligionName
                    })
                    this.$store.commit("trace/update_phone", doc.M_DoctorPhone)
                    this.$store.commit("trace/update_email", doc.M_DoctorEmail)
                    this.$store.commit("trace/update_hp", doc.M_DoctorHP)
                    this.$store.commit("trace/update_note", doc.M_DoctorNote)
                    this.$store.commit("trace/update_pj", doc.M_DoctorIsPJ)
                    this.$store.commit("trace/update_dpj", doc.M_DoctorIsDefaultPJ)
                    this.$store.commit("trace/update_clinic", doc.M_DoctorIsClinic)
                    this.$store.commit("trace/update_marketing_confirm", doc.M_DoctorIsMarketingConfirm)
                    this.$store.commit("trace/update_is_default", doc.M_DoctorIsDefault)
                    this.$store.commit("trace/update_is_default_email", doc.M_DoctorEmailIsDefault)



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

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

                this.$store.commit("trace/update_act", 'new')
            },
            saveFormDoctor() {
                if (this.in_saving) return;
                this.in_saving = true;
                this.$store.dispatch("trace/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.trace.vdeliveryorders
                var idx = _.findIndex(arrdoctor, item => item.M_DoctorID === this.$store.state.trace.last_id)
                console.log(idx)
                this.$store.dispatch("trace/searchtrace", {
                    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("trace/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("trace/update_dialog_form_doctor", true)
            },
            updateDialogFormDoctor() {
                this.$store.commit("trace/update_dialog_form_doctor", false)
            },
            updateDialogViewResult() {
                this.$store.commit("trace/update_dialog_form_viewresult", false)
            },
            viewResult(data) {
                this.xid = data.id
                this.xtestname = data.T_TestName
                this.$store.dispatch("trace/lookupresultbyid", {
                    id: data.id
                })
                this.$store.commit("trace/update_dialog_form_viewresult", true)
            },
            closeViewResult() {
                this.$store.commit("trace/update_dialog_form_viewresult", false)
            },
        },
        computed: {
            dialogdoctor() {
                return this.$store.state.trace.dialog_form_doctor
            },
            filterComputedDateFormattedStart() {
                return this.formatDate(this.xstartdate)
            },
            deliveryorderdetailresults() {
                return this.$store.state.trace.deliveryorderdetailresults
            },
            dialogviewresult() {
                return this.$store.state.trace.dialog_form_viewresult
            },
            xstartdate: {
                get() {
                    return this.$store.state.trace.startdate
                },
                set(val) {
                    this.$store.commit("trace/update_startdate", val)
                    this.searchDoctor()
                }
            },
            in_saving: {
                get() {
                    return this.$store.state.trace.in_saving
                },
                set(val) {
                    this.$store.commit("trace/update_in_saving", val)
                }
            },
            selected_filter_branch: {
                get() {
                    return this.$store.state.trace.selected_filter_branch
                },
                set(val) {
                    this.$store.commit("trace/update_selected_filter_branch", val)
                    this.searchDoctor()
                }
            },
            branchs: {
                get() {
                    return this.$store.state.trace.branchs
                },
                set(val) {
                    this.$store.commit("trace/update_branchs", val)
                }
            },
            dialogsuccess: {
                get() {
                    return this.$store.state.trace.dialog_success
                },
                set(val) {
                    this.$store.commit("trace/update_dialog_success", val)
                }
            },
            msgsuccess() {
                return this.$store.state.trace.msg_success
            },
            snackbar: {
                get() {
                    return this.$store.state.trace.alert_success
                },
                set(val) {
                    this.$store.commit("trace/update_alert_success", val)
                }
            },
            isLoading() {
                return this.$store.state.trace.search_status == 1
            },
            xstatuses() {
                return this.$store.state.trace.statuses
            },
            xselectedstatus: {
                get() {
                    return this.$store.state.trace.selected_status
                },
                set(val) {
                    this.$store.commit("trace/update_selected_status", val)
                    this.searchDoctor()
                }
            },
            vdeliveryorders() {
                return this.$store.state.trace.vdeliveryorders
            },
            openalertconfirmation: {
                get() {
                    return this.$store.state.trace.open_alert_confirmation
                },
                set(val) {
                    this.$store.commit("trace/update_open_alert_confirmation", val)
                }
            },
            totalvdeliveryorders() {
                return this.$store.state.trace.total_vdeliveryorders
            },
            totalvdeliveryordersfilter() {
                return this.$store.state.trace.total_vdeliveryordersfilter
            },
            curr_page: {
                get() {
                    return this.$store.state.trace.current_page
                },
                set(val) {
                    this.$store.commit("trace/update_current_page", val)
                    this.$store.dispatch("trace/searchtrace", {
                        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.trace.newcode
                },
                set(val) {
                    this.$store.commit("trace/update_newcode", val)
                }
            },
            xtotal_page: {
                get() {
                    return this.$store.state.trace.total_vdeliveryorders
                },
                set(val) {
                    this.$store.commit("trace/update_total_vdeliveryorders", val)
                }
            },
            xtotal_filter: {
                get() {
                    return this.$store.state.trace.total_filterdoctor
                },
                set(val) {
                    this.$store.commit("trace/update_total_filterdoctor", val)
                }
            },
            opendialoginfo: {
                get() {
                    return this.$store.state.trace.open_dialog_info
                },
                set(val) {
                    this.$store.commit("trace/update_open_dialog_info", val)
                }
            },
            msginfo() {
                return this.$store.state.trace.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. SURAT JALAN",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "LAB RUJUKAN",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "NO. REG",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "NAMA",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "PEMERIKSAAN",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "HASIL",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "CATATAN",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "STATUS",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    }
                ],
                headerviewresults: [{
                        text: "NO. REG",
                        align: "left",
                        sortable: false,
                        value: "lab",
                        width: "10%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "NAMA",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "25%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "TEST",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "15%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "HASIL",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "10%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "PETUGAS",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "20%",
                        class: "blue lighten-3 white--text"
                    }
                ],
                pagination: {
                    descending: false,
                    page: 1,
                    rowsPerPage: 5,
                    sortBy: 'M_DoctorName',
                    totalItems: this.$store.state.trace.total_vdeliveryorders
                }
            };
        }
    }
</script>
