<template>
    <v-card class="mb-1 pa-1">
        <v-layout row>
            <v-flex xs8>
                <v-layout>
                    <one-date-picker label="Tgl. Awal" :date="sdate" class="mydate" data="0"
                        @change="changeDate"></one-date-picker>
                        <one-edate-picker label="Tgl. Akhir" :date="edate" class="mydate" data="0"
                        @change="echangeDate"></one-edate-picker>
                    <v-text-field class="flex ma-1" label="" placeholder="No Reg / Nama" single-line solo hide-details
                        v-model="search" @keyup.native="keySearch"></v-text-field>
                        <v-select :items="companies" class="ma-1" v-model="company" item-text="CorporateName"
                        item-value="CorporateID" label="Kelompok Pelanggan" return-object solo hide-details
                        clearable></v-select>
                  <!--  <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 xs2>
                <v-layout>
                    <v-btn class="btn-search one-btn-icon ma-1" color="success" @click="searchs">
                        <span class="icon-search"><span>
                    </v-btn>
                </v-layout>
            </v-flex>

            <v-flex xs6 class="text-xs-right">
                <!-- <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 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 @click="doPrintKultur()" class="white--text ma-1" color="red darken-2" dark v-show="btn_print_kultur_show">Print out kultur <v-icon right
                    dark>print</v-icon></v-btn>
                <v-btn @click="doPrintPapsmear()" class="white--text ma-1" color="brown" dark v-show="btn_print_papsmear_show">Print out papsmear <v-icon right
                        dark>print</v-icon></v-btn>
                <v-btn @click="doPrintPatient()" class="white--text ma-1" color="purple darken-2" dark>Cetak History <v-icon right
                        dark>print</v-icon></v-btn>
                <v-btn @click="doPrint()" class="white--text ma-1" color="orange" dark>Cetak <v-icon right
                        dark>print</v-icon></v-btn>

                <v-btn color="blue" class="white--text ma-1" @click="save_result">
                    Simpan
                    <v-icon right dark>save_alt</v-icon>
                </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>

        <one-dialog-print :title="printtitle" :width="printwidth" :height="700" :status="openprint" :urlprint="urlprint"
            @close-dialog-print="openprint = false"></one-dialog-print>
    </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: '',
            urlprint: "",
            printtitle: '',
            printwidth: '100%',
        }
    },
    watch: {
        search_company(n, o) {
            this.$store.dispatch('company/search', n)
        }
    },
    computed: {
        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) }
        },
        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)
            }
        }
    },

    methods: {
        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)
        },
        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_open_print", 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)
        }
    },

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