<template>
    <v-card   class="mb-1 pa-1">
        <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 dikirim:</p>
                    <p class="warning--text caption">Jika tidak sesuai, silahkan klik tombol <b>Tutup</b> dan validasi 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_TestSasCode }}</td>
                                <td>{{ props.item.t_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="warning" @click="uploadCpone()">Kirim</v-btn>
                    <v-btn v-if="waiting_progress" disabled>Proses...</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <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 mengirim data ke CPONE
                </v-card-title>

                <v-card-text>
                   <p>Silahkan periksa data pemeriksaan pada aplikasi CPONE</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>
        <v-layout row>
            <v-flex xs9>
                <v-layout>
                    <one-date-picker
                        label="Tanggal"
                        :date="sdate"
                        style="width:160px"
                        data="0"
                        class="mt-1"
                        hide-details
                        @change="changeDate"
                    ></one-date-picker>
                    <v-text-field class="flex xs6 ma-1 mt-2"
                          label=""
                          placeholder="No Reg / Nama"
                          solo
              
                          hide-details
                          v-model="search"
                          @keyup.native="keySearch"
                    ></v-text-field>

                    <v-autocomplete
                        v-model="company"
                        :items="companies"
                        :loading="loading_company"
                        :search-input.sync="search_company"
                                class="xs6 ma-1 ml-1 mr-1 mt-2"
                        hide-no-data
                                solo
                        item-text="CorporateName"
                        item-value="CorporateID"
                        label="company"
                        hide-details
                        placeholder="Cari Kelompok Pelanggan"
                        return-object
                    ></v-autocomplete>  
			
			<v-select
			label="status"
			:items="status_validasi"
			style="width:30%"
			item-value="code"
			item-text="name"
            class="mt-2"
            hide-details
            solo
			return-object
			v-model="selected_status_validasi"
			></v-select>
                </v-layout>
            </v-flex>
            <v-flex xs1>
                <v-layout>
                    <v-btn class="btn-search one-btn-icon ma-1 mt-2" color="success" style="height: 46px;" @click="searchs" >
                        <span class="icon-search"><span>
                    </v-btn>
                </v-layout>
            </v-flex>

            <v-flex xs7 class="text-xs-right">
                <v-layout row wrap>
                    <v-flex xs2 class="text-xs-left">
                        &nbsp;
                    </v-flex>
                    <v-flex xs10>
                        <!-- <img v-if="img_lang" :src="'../../../libs/image/' + img_lang_2(lang.code, lang.is_si)" height="36" style="float:left" alt="" class="mt-1" /> -->
                        <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 mt-2" v-if="info_req.validation_internal === '' || !info_req.validation_internal" color="red" @click="note" 
                           title="Catatan"
                           dark >
                           <v-icon>library_books</v-icon>
                        </v-btn>
                        <v-btn class="btn-search one-btn-icon ma-1 mt-2" v-if="info_req.validation_internal !== '' && info_req.validation_internal" color="teal ligthen-2" @click="note" 
                           title="Catatan"
                           dark >
                           <v-icon>library_books</v-icon>
                        </v-btn>

                        <v-btn
                            color="blue-grey"
                            small
                            class="white--text ma-1 mt-2"
                            @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="orange"
                     
                            class="one-btn-icon white--text ma-1 mt-2"
                            @click="preview"
                            title="Preview Hasil"
                            >
                           <v-icon>remove_red_eye</v-icon>
                        </v-btn>
                        

                        <v-btn
                            color="blue"
                            small
                            class="white--text ma-1 mt-2"
                            @click="save_result"
                            :disabled="!btn_validation_enable"
                            :dark="btn_validation_enable"
                            style="height: 36px;"
                        >
                            Validasi
                        </v-btn>

                        <!--<v-btn
                            color="green"
                            small
                            style="height: 36px;"
                            class="white--text ma-1 mt-2"
                            @click="send_to_cpone"
                            :dark="contain_val"
                            v-show="contain_val"
                            >
                            Kirim CPONE 
                     
                        </v-btn>-->

                        <v-btn
                            color="red"
                            small
                            class="white--text ma-1 mt-2"
                            @click="unvalidate_result"
                            :disabled="!btn_unvalidation_enable"
                            :dark="btn_unvalidation_enable"
                            style="height: 36px;"
                        >
                            Unvalidasi
                        </v-btn>
                    </v-flex>
                </v-layout>
                

                
            </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>
.btn-legend {
    height: 25px;
    width: 25px;
    border-radius: 0%;
}



.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_TestSasCode' },
                { text: 'Nama', value: 't_testname' }
            ],
	 status_validasi : [
		{'code': 'ALL', 'name': 'Semua Hasil'},
		{'code': 'BV', 'name': 'Belum Tervalidasi'},
		{'code': 'SV', 'name': 'Tervalidasi'},
		{'code': 'SVS', 'name': 'Tervalidasi dan Serahkan'},
		
	 ],
       }
    },
    watch : {
	search_company(n,o) {
		this.$store.dispatch('company/search',n)
	}
    },
    computed : {
        openprint: {
            get() {
                return this.$store.state.helper.open_print
            },
            set(val) {
                this.$store.commit("helper/update_open_print", false)
            }
        },
        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) }
        },
        waiting_progress: {
            get() { return this.$store.state.re_patient.waiting_progress},
            set(v) { this.$store.commit('re_patient/update_waiting_progress',v) }
        },
        test_refers: {
            get() { return this.$store.state.re_patient.test_refers},
            set(v) { this.$store.commit('re_patient/update_test_refers',v) }
        },
        error_message: {
            get() { return this.$store.state.re_patient.error_message},
            set(v) { this.$store.commit('re_patient/update_error_message',v) }
        },
        dialog_success: {
            get() { return this.$store.state.re_patient.dialog_success},
            set(v) { this.$store.commit('re_patient/update_dialog_success',v) }
        },
        dialog_error: {
            get() { return this.$store.state.re_patient.dialog_error},
            set(v) { this.$store.commit('re_patient/update_dialog_error',v) }
        },
        selected_status_validasi: {
            get() { return this.$store.state.re_patient.selected_status_validasi},
            set(v) { this.$store.commit('re_patient/update_selected_status_validasi',v) }
            },
        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) }
        },

        order_id () {
            return this.$store.state.re_px.order_id
        },

        contain_val () {
            return this.$store.state.re_px.contain_val
        },

        lang () {
            return {code:this.$store.state.re_patient.selected_patient.M_LangCode, is_si:this.$store.state.re_patient.selected_patient.is_si}
        },

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

        sdate : {
            get () { return this.$store.state.re_patient.sdate },
            set (v) { this.$store.commit('re_patient/update_sdate', 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';    
		let info_req = this.$store.state.re_patient.info_req
		if ( info_req.note_fo != '' ) x = false
		if ( info_req.note_fo_ver != '' ) x = false
		if ( info_req.note_sampling != '' ) x = false
		if ( info_req.note_result_entry != '' ) x = false
            } catch (error) {
            }
            
            return x
        },
        info_req(){
            return this.$store.state.re_patient.info_req
        },

        btn_validation_enable() {
            let x = this.$store.state.re_px.pxs || []

            for (let i in x) {
                if (
                    x[i].is_result == 'Y' &&
                    x[i].verification == 'Y' &&
                    x[i].validation != 'Y'
                ) {
                    return true
                }
            }

            return false
        },

        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_unvalidation_enable() {
            let x = this.$store.state.re_px.pxs || []

            for (let i in x) {
                if (
                    x[i].is_result == 'Y' &&
                    (x[i].validation == 'Y' || x[i].validation_old == 'Y')
                ) {
                    return true
                }
            }

            return false
        },

        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
        },
    },

    methods : {
        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)
        },
        closeSuccess(){
            this.dialog_success = false
            this.dialog_confirm_local = false
        },
        uploadCpone(){
            this.$store.commit('re_patient/update_waiting_progress', true)
            this.$store.dispatch('re_patient/uploadCpone')
        },
        send_to_cpone(){
            let pxs = this.$store.state.re_px.pxs
            let test_refers = []
            for(let i in pxs){
                if(pxs[i].validation == 'Y'){
                    test_refers.push(pxs[i])
                }
            }
            if(test_refers.length > 0){
                this.$store.commit('re_patient/update_test_refers', test_refers)
                this.$store.commit('re_patient/update_dialog_confirm_local', true)
            }
            else{
                this.$store.commit('re_patient/update_error_message', 'Tidak ada pemeriksaan yang akan dikirim')
                this.$store.commit('re_patient/update_dialog_error', true)
            }
        },
        searchs() {
           console.log('search .... ')
            this.$store.dispatch('re_patient/search')
        },
        async send_to_adm() {
           await this.$store.dispatch('re_patient/send_to_adm')
        },
        keySearch(e) {
            if (e.which == 13) {
                this.searchs()
            }
        },
        async save_result() {
            await this.$store.dispatch('re_px/confirm')
        },

        histories() {
            this.$store.dispatch('re_history/history')
            this.$store.commit('re_history/update_dialog_history', true)
        },

        preview() {
            this.$store.commit('re_patient/update_rpt_url', this.$store.state.re_patient.selected_patient.T_OrderHeaderID)
            this.$store.commit('re_patient/update_print_dialog', 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"
        },

        changeDate(x) {
            this.sdate = 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)
        },

        async unvalidate_result() {
            await this.$store.dispatch('re_px/unvalidate')
        }
    },

    mounted () {
        this.$store.dispatch('re_px/search_group')
    }
}
</script>
