<template>
    <v-card class="mb-1 pa-1">
        <v-layout row>
            <v-flex xs6>
                <v-layout>
                    <v-flex xs3>
                        <one-date-picker class="ma-1" label="Tgl. Awal" :date="sdate" class="mydate" data="0"
                        @change="changeDate"></one-date-picker>
                    </v-flex>
                    <v-flex xs3>
                        <one-edate-picker class="ma-1" label="Tgl. Akhir" :date="edate" class="mydate" data="0"
                        @change="echangeDate"></one-edate-picker>
                    </v-flex>
                    <v-flex xs3>
                        <v-text-field class="flex ma-1 mt-2" label="" placeholder="No Reg / Nama" single-line solo hide-details
                        v-model="search" @keyup.native="keySearch"></v-text-field>
                    </v-flex>
                    <v-flex xs3>
                        <v-select :items="companies" class="ma-1 mt-2" v-model="company" item-text="CorporateName"
                        item-value="CorporateID" label="Kelompok Pelanggan" return-object solo hide-details
                        clearable></v-select>
                    </v-flex>
                   
                        
                  <!--  <v-autocomplete v-model="company" :items="companies" :loading="loading_company"
                        :search-input.sync="search_company" class="ma-1 ml-1 mr-1" hide-no-data filled
                        item-text="CorporateName" item-value="CorporateID" label=""
                        placeholder="Cari Kelompok Pelanggan" return-object solo hide-details></v-autocomplete>
                        
                      <v-select :items="groups" class="ma-1" v-model="selected_group" item-text="group_name"
                        item-value="group_id" label="Grup Pemeriksaan" return-object solo hide-details
                        clearable></v-select> -->
                </v-layout>
            </v-flex>
            <v-flex xs1>
                <v-layout>
                    <v-btn class="btn-search one-btn-icon ma-1 mt-2" color="primary" @click="searchs">
                        <span class="icon-search"></span>
                    </v-btn>
                </v-layout>
            </v-flex>

            <v-flex xs5 class="text-xs-right pt-2 ">
                <!-- <img :src="'../../../libs/image/' + img_lang" height="36" style="float:left" alt="" /> -->

                <v-btn v-show="false" v-for="(lang, n) in langs" v-bind:key="n" color="grey lighten-1"
                    class="one-btn-icon pl-2 pr-2 ml-0 mr-1" depressed :outline="!lang_selected(lang)"
                    @click="lang_change(lang)">
                    <img :src="'../../../libs/image/' + img_lang_2(lang.code, lang.is_si)" height="40" alt="" />
                </v-btn>

                <v-btn v-if="show_re_age" class="btn-search one-btn-icon ma-1" color="brown" @click="re_calc_age" dark>
                    Re. Age
                </v-btn>
                <v-btn style="height: 40px;" small class="ma-1" color="red" @click="downloadLocal()" dark>
                   LOKAL
                </v-btn>

                <!--  <v-btn v-show="!info_req_perfect" class="btn-search one-btn-icon ma-1" color="red" @click="info" dark>
                    <v-icon>info</v-icon>
                </v-btn>

                <v-btn class="btn-search one-btn-icon ma-1" color="red" @click="note" dark>
                    <v-icon>library_books</v-icon>
                </v-btn>

                <v-btn color="blue-grey" class="white--text ma-1" @click="histories" v-show="btn_history_show">
                    Histori
                </v-btn> -->

                <v-btn style="height: 40px;" small @click="doPrintKultur()" class="white--text ma-1 " color="red darken-2" dark v-show="btn_print_kultur_show">Print out kultur </v-btn>
                <v-btn style="height: 40px;" small @click="doPrintPapsmear()" class="white--text ma-1" color="brown" dark v-show="btn_print_papsmear_show">Print out papsmear </v-btn>
                <v-btn style="height: 40px;" small @click="doPrintPatient()" class="white--text ma-1" color="purple darken-2" dark>History </v-btn>
                <v-btn style="height: 40px;" small @click="doPrint()" class="white--text ma-1" color="orange" dark>Cetak </v-btn>

                <v-btn style="height: 40px;" small color="blue" class="white--text ma-1" @click="save_result">
                    Simpan
                </v-btn>
              <!--  <v-btn color="green" class="white--text ma-1" @click="save_validate" :disabled="!btn_validation_enable"
                    :dark="btn_validation_enable">
                    Validasi
                    <v-icon right dark>save_alt</v-icon>
                </v-btn>
                <v-btn color="red" class="white--text ma-1" @click="unvalidate" dark>
                    UnValidasi
                    <v-icon right dark>save_alt</v-icon> 
                </v-btn> -->
            </v-flex>
        </v-layout>
        <v-dialog
            v-model="dialog_error"
            width="500"
            >
            

            <v-card>
                <v-card-title
                class="headline red lighten-2"
                primary-title
                >
                Gagal mengunduh data lokal
                </v-card-title>

                <v-card-text>
                   <p>{{error_message}}</p>
                </v-card-text>

                <v-divider></v-divider>

                <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn
                    color="primary"
                    flat
                    @click="dialog_error = false"
                >
                    Tutup
                </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>

        <v-dialog
            v-model="dialog_success"
            width="500"
            >
            

            <v-card>
                <v-card-title
                class="headline green lighten-2"
                primary-title
                >
                Berhasil mengunduh data ke lokal
                </v-card-title>

                <v-card-text>
                   <p>Silahkan periksa data pemeriksaan pada aplikasi lokal</p>
                </v-card-text>

                <v-divider></v-divider>

                <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn
                    color="primary"
                    flat
                    @click="closeSuccess()"
                >
                    Tutup
                </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>

        <one-dialog-print :title="printtitle" :width="printwidth" :height="700" :status="openprint" :urlprint="urlprint"
            @close-dialog-print="openprint = false"></one-dialog-print>

        <v-dialog v-model="dialog_confirm_local"
            max-width="1000px"
            persistent>
            <v-card>
                <v-card-title  primary-title class="grey lighten-3">
                    <h4 class="grey--text text--darken-2">Konfirmasi Daftar Pemeriksaan</h4>
                </v-card-title>
                <v-divider></v-divider>
                <v-progress-linear 
                    v-show="waiting_progress"
                    indeterminate
                    color="red darken-2"
                    height="4"
                ></v-progress-linear>
                <v-card-text>
               
                    <p class="mb-1">Pastikan pemeriksaan berikut yang akan diunduh:</p>
                    <p class="warning--text caption">Jika tidak sesuai, silahkan klik tombol <b>Tutup</b> dan periksa ulang seting pemeriksaan yang dikerjakan lokal terlebih dahulu.</p>
                    <v-layout row wrap>
                        <v-flex xs12>
                            <v-data-table
                                :headers="headers_test_refers"
                                :items="test_refers"
                                class="elevation-1"
                                :items-per-page="20"
                            >
                                <template v-slot:items="props">
                                <td>{{ props.item.T_OrderDetailT_TestSasCode }}</td>
                                <td>{{ props.item.T_OrderDetailT_TestName }}</td>
                                </template>
                            </v-data-table>
                        </v-flex>
                    </v-layout>
                </v-card-text>

                <v-card-actions>
                <v-spacer></v-spacer>
                    <v-btn dark @click="dialog_confirm_local = false">Tutup</v-btn>
                    <v-btn v-if="!waiting_progress" color="success" @click="doDownloadLocal()">Download</v-btn>
                    <v-btn v-if="waiting_progress" disabled>Proses...</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>

        <v-dialog v-model="dialog_print_new"
            max-width="1000px"
            persistent>
            <v-card>
                <v-card-text>
                    <v-layout row wrap>
                        <v-flex xs12>
                        <object style="overflow: hidden;" width="100%" :height="700" :data="urlprint"></object>
                        </v-flex>
                    </v-layout>
                </v-card-text>

                <v-card-actions>
                <v-spacer></v-spacer>
                    <v-btn color="primary" @click="closePrint">Tutup</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>

        <v-dialog v-model="dialog_status"
        max-width="500px"
        persistent>
        <v-card>
            <v-card-text>
                <v-layout row wrap>
                    <v-flex xs12>{{status_print}}}
                    </v-flex>
                </v-layout>
            </v-card-text>

            <v-card-actions>
                <v-btn color="primary" flat @click="dialog_status=!dialog_status">Abaikan</v-btn>
                <v-spacer></v-spacer>
                <v-btn color="primary" @click="add_log_print">Ya, mengerti</v-btn>
            </v-card-actions>
        </v-card>
    </v-dialog>
    </v-card>
</template>

<style scoped>
.mydate {
    width: 150px;
}

button {

    height: 48px;
}

.btn-search {
    font-size: 1.5em
}
</style>

<script>
module.exports = {
    components: {
        'one-date-picker': httpVueLoader('./oneDatePicker.vue'),
        'one-edate-picker': httpVueLoader('./oneEndDatePicker.vue'),
        'one-dialog-print': httpVueLoader('../../common/oneDialogPrintX.vue')
    },
    data() {
        return {
            search_company: '',
            headers_test_refers: [
                { text: 'Kode', value: 'T_OrderDetailT_TestSasCode' },
                { text: 'Nama', value: 'T_OrderDetailT_TestName' }
            ]
        }
    },
    watch: {
        search_company(n, o) {
            this.$store.dispatch('company/search', n)
        }
    },
    computed: {
        error_message: {
            get() { return this.$store.state.re_patient.error_message },
            set(v) { this.$store.commit('re_patient/update_error_message', v) }
        },
        dialog_error: {
            get() { return this.$store.state.re_patient.dialog_error },
            set(v) { this.$store.commit('re_patient/update_dialog_error', v) }
        },
        dialog_success: {
            get() { return this.$store.state.re_patient.dialog_success },
            set(v) { this.$store.commit('re_patient/update_dialog_success', v) }
        },
        waiting_progress: {
            get() { return this.$store.state.re_patient.waiting_progress },
            set(v) { this.$store.commit('re_patient/update_waiting_progress', v) }
        },
        dialog_confirm_local: {
            get() { return this.$store.state.re_patient.dialog_confirm_local },
            set(v) { this.$store.commit('re_patient/update_dialog_confirm_local', v) }
        },
        test_refers: {
            get() { return this.$store.state.re_patient.test_refers },
            set(v) { this.$store.commit('re_patient/update_test_refers', v) }
        },
        test_not_yet: {
            get() { return this.$store.state.helper.test_not_yet },
            set(v) { this.$store.commit('helper/update_test_not_yet', v) }
        },
        urlprint: {
            get() { return this.$store.state.helper.urlprint },
            set(v) { this.$store.commit('helper/update_urlprint', v) }
        },
        printtitle: {
            get() { return this.$store.state.helper.printtitle },
            set(v) { this.$store.commit('helper/update_printtitle', v) }
        },
        printwidth: {
            get() { return this.$store.state.helper.printwidth },
            set(v) { this.$store.commit('helper/update_printwidth', v) }
        },
        show_re_age() {
            return this.$store.state.re_patient.selected_patient.T_OrderHeaderAddOnRequestChangeDOB == 'Y'
        },
        company: {
            get() { return this.$store.state.company.company },
            set(v) { this.$store.commit('company/update_company', v) }
        },
        companies() {
            return this.$store.state.company.companies
        },
        loading_company() {
            return this.$store.state.company.loading
        },
        nolab: {
            get() { return this.$store.state.re_patient.nolab },
            set(v) { this.$store.commit('re_patient/update_nolab', v) }
        },
        search: {
            get() { return this.$store.state.re_patient.search },
            set(v) { this.$store.commit('re_patient/update_search', v) }
        },
        status_print: {
            get() { return this.$store.state.helper.status_print },
            set(v) { this.$store.commit('helper/update_status_print', v) }
        },
        dialog_status : {
            get () { return this.$store.state.helper.dialog_status },
            set (v) { this.$store.commit('helper/update_dialog_status', v) }
        },
        groups() {
            return this.$store.state.re_px.groups
        },
        selected_group: {
            get() { return this.$store.state.re_px.selected_group },
            set(v) {
                this.$store.commit('re_px/update_selected_group', v)
                this.$store.dispatch('re_patient/search')
            }
        },
        btn_validation_enable() {
            return true
            let x = this.$store.state.re_px.pxs
            // console.log(x)
            let y = 0, n = 0
            for (let i in x) {
                // console.log(i+' : '+x[i].t_testname+' : '+x[i].is_result+' : '+x[i].validation+' : '+x[i].verification)
                if (x[i].is_result == 'Y') {
                    n++
                    if (x[i].validation == 'Y') { y++; }
                }
            }
            // console.log("n:"+n+" y:"+y)
            if (y < 1) return false
            return true
        },

        btn_validation_show() {
            let x = this.$store.state.re_px.pxs
            let z = 0, n = 0
            for (let i in x) {
                if (x[i].is_result == 'Y') {
                    n++
                    if (x[i].validation_old == 'Y')
                        z++
                }
            }

            if (z == n) return false
            return true
        },
        btn_print_papsmear_show() {
            let x = this.$store.state.re_px.pxs
            for (let i in x) {

                if (x[i].t_testid == '1611') {
                    return true
                }
            }

            // return true
        },
        btn_print_kultur_show() {
            let x = this.$store.state.re_px.pxs
            for (let i in x) {

                if (x[i].ResultGroupName == 'Mikro') {
                    return true
                }
            }

            // return true
        },
        lang() {
            return this.$store.state.re_patient.selected_patient.M_LangCode
        },

        img_lang() {
            if (this.lang == "ID")
                return "inako.png"
            // return "flag-ina_48.png"
            else if (this.lang == "EN")
                return "engko.png"
            // return "flag-us_48.png"
        },

        langs() {
            return this.$store.state.re_patient.langs
        },

        sdate: {
            get() { return this.$store.state.re_patient.s_date },
            set(v) { this.$store.commit('re_patient/update_sdate', v) }
        },
        edate: {
            get() { return this.$store.state.re_patient.e_date },
            set(v) { this.$store.commit('re_patient/update_edate', v) }
        },

        selected_patient() {
            return this.$store.state.re_patient.selected_patient
        },

        btn_history_show() {
            if (!this.selected_patient) return false
            if (!this.selected_patient.is_history) return false
            if (this.selected_patient.is_history == "N") return false

            return true
        },

        info_req_perfect() {
            let x = false
            try {
                x = this.$store.state.re_patient.info_req.is_perfect == 'Y';
            } catch (error) {
            }

            return x
        },

        openprint: {
            get() {
                return this.$store.state.helper.open_print
            },
            set(val) {
                this.$store.commit("helper/update_open_print", false)
            }
        },
        dialog_print_new: {
            get() {
                return this.$store.state.helper.dialog_print_new
            },
            set(val) {
                this.$store.commit("helper/update_dialog_print_new", false)
            }
        }
    },

    methods: {
        doDownloadLocal(){
            this.$store.dispatch('re_patient/sendToLocal')
        },
        downloadLocal(){
            this.$store.commit('re_patient/update_waiting_progress', true)
            
            let orderID = this.$store.state.re_patient.selected_patient.T_OrderHeaderID
            this.$store.dispatch('re_patient/downloadLocal', {id:orderID})
        },
        searchs() {
            this.$store.dispatch('re_patient/search')
        },
        async re_calc_age() {

            let orderID = this.$store.state.re_patient.selected_patient.T_OrderHeaderID
            await this.$store.dispatch('helper/calc_age', orderID)
        },
        add_log_print() {
            id = this.$store.state.re_patient.selected_patient.T_OrderHeaderID
            let tests = this.$store.state.helper.test_not_yet
            this.$store.dispatch('helper/insertlogprint', {orderid : id, tests: tests})
            
           /* 
            tm = Date.now()
            let user = one_user()
            this.printwidth = 1700
            this.printtitle = ""
            let rpt = 'rpt_test'
            this.urlprint = "/birt/run?__report=report/one/lab/" + rpt + ".rptdesign&__format=pdf&PID=" + id + "&username=" + user.M_StaffName + "&tm=" + tm
            this.$store.commit("helper/update_open_print", true)
            */
        },
        closePrint() {
            id = this.$store.state.re_patient.selected_patient.T_OrderHeaderID
            this.$store.dispatch('helper/check_status_print', id)
        },
        doPrint() {
            id = this.$store.state.re_patient.selected_patient.T_OrderHeaderID
            tm = Date.now()
            let user = one_user()
            this.printwidth = 1700
            this.printtitle = ""
            let rpt = 'rpt_test'
            // https://devcpone.aplikasi.web.id/birt/run?__report=report/one/lab/rpt_test.rptdesign&__format=pdf&PID=1&username=adhi&tm=1717726294764
            this.urlprint = "/birt/run?__report=report/one/lab/" + rpt + ".rptdesign&__format=pdf&PID=" + id + "&username=" + user.M_StaffName + "&tm=" + tm
            this.$store.commit("helper/update_dialog_print_new", true)
            
        },
        doPrintPatient() {
            id = this.$store.state.re_patient.selected_patient.T_OrderHeaderID
            tm = Date.now()
            let user = one_user()
            this.printwidth = 1700
            this.printtitle = ""
            let rpt = 'rpt_test_history'
            // https://cpone.aplikasi.web.id/birt/run?__report=report/one/lab/rpt_test_history.rptdesign&__format=pdf&PID=7931&username=adhi&tm=1717726294764
            this.urlprint = "/birt/run?__report=report/one/lab/" + rpt + ".rptdesign&__format=pdf&PID=" + id + "&username=" + user.M_StaffName + "&tm=" + tm
            this.$store.commit("helper/update_open_print", true)
        },
        doPrintPapsmear() {
            id = this.$store.state.re_patient.selected_patient.T_OrderHeaderID
            tm = Date.now()
            let user = one_user()
            this.printwidth = 1700
            this.printtitle = ""
            let rpt = 'rpt_test_papsmear'
            // https://cpone.aplikasi.web.id/birt/run?__report=report/one/lab/rpt_test_papsmear.rptdesign&__format=pdf&PID=323&username=PETUGAS%20SAMPLE%20LAB&tm=1721461717824
            this.urlprint = "/birt/run?__report=report/one/lab/" + rpt + ".rptdesign&__format=pdf&PID=" + id + "&username=" + user.M_StaffName + "&tm=" + tm
            this.$store.commit("helper/update_open_print", true)
        },
        doPrintKultur() {
            id = this.$store.state.re_patient.selected_patient.T_OrderHeaderID
            tm = Date.now()
            let user = one_user()
            this.printwidth = 1700
            this.printtitle = ""
            let rpt = 'rpt_test_mikro'
            // https://cpone.aplikasi.web.id/birt/run?__report=report/one/lab/rpt_test_mikro.rptdesign&__format=pdf&PID=2088&username=PETUGAS%20SAMPLE%20LAB&tm=1721461717824
            this.urlprint = "/birt/run?__report=report/one/lab/" + rpt + ".rptdesign&__format=pdf&PID=" + id + "&username=" + user.M_StaffName + "&tm=" + tm
            this.$store.commit("helper/update_open_print", true)
        },
        keySearch(e) {
            if (e.which == 13) {
                this.searchs()
            }
        },

        search_px() {
            this.$store.dispatch('re_px/search')
        },

        save_result() {
            this.$store.dispatch('re_px/save')
        },
        save_validate() {
            this.$store.dispatch('re_px/confirm')
            let patient = this.$store.state.re_patient.selected_patient
            if (patient.T_OrderHeaderAddOnValidationDone == 'N') {
                patient.T_OrderHeaderAddOnValidationDone = 'P'
            }
            let patients = this.$store.state.re_patient.patients;

            for (let i = 0; i < patients.length; i++) {
                if (patients[i].M_PatientID == patient.M_PatientID) {
                    patients[i] = patient
                }
            }
            let dt = { records: patients, total_page: 1, total: patients.length }
            this.$store.commit("re_patient/update_patients", dt)
            console.log('pat', patients)
        },
        unvalidate() {
            this.$store.dispatch('re_px/unvalidate')
        },
        histories() {
            this.$store.dispatch('re_history/history')
            this.$store.commit('re_history/update_dialog_history', true)
        },

        img_lang_2(lang, si) {

            if (lang == "ID" && si == "N")
                return "inako.png"
            if (lang == "ID" && si == "Y")
                return "inasi.png"
            if (lang == "EN" && si == "N")
                return "engko.png"
            if (lang == "EN" && si == "Y")
                return "engsi.png"
        },

        lang_change(lang) {
            this.$store.commit('re_patient/update_lang', lang)
            this.$store.dispatch('re_px/lang_export')
        },

        lang_selected(lang) {
            if (lang.code == this.$store.state.re_patient.lang_code)
                return true
            return false
        },

        changeDate(x) {
            this.sdate = x.new_date
            this.searchs()
        },
        echangeDate(x) {
            this.edate = x.new_date
            this.searchs()
        },
        note() {
            this.$store.commit('re_patient/update_dialog_note', true)
        },

        info() {
            this.$store.commit('re_patient/update_dialog_req', true)
        },
        closeSuccess() {
            this.$store.commit('re_patient/update_dialog_success', false)
            this.$store.commit('re_patient/update_dialog_confirm_local', false)
        }
    },

    mounted() {
        //this.$store.dispatch('company/search', {search_company: ''})
        this.$store.dispatch('re_px/search_group')
    }
}
</script>
