<template>
    <v-card   class="mb-1 pa-1">
        <v-layout row>
            <v-flex xs9>
                <v-layout>
                    <one-date-picker
                        label="Tanggal"
                        :date="sdate"
                        style="width:120px"
                        data="0"
                        @change="changeDate"
                    ></one-date-picker>
                    <v-text-field class="flex xs6 ma-1"
                          label=""
                          placeholder="No Reg / Nama"
                          single-line
                          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"
		    hide-no-data
                    filled
		    item-text="M_CompanyName"
		    item-value="M_CompanyID"
		    label=""
		    placeholder="Cari Kelompok Pelanggan"
		    return-object
		  ></v-autocomplete>  
                </v-layout>
            </v-flex>
            <v-flex xs1>
                <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 xs7 class="text-xs-right">
                <v-layout row wrap>
                    <v-flex xs2 class="text-xs-left">
                        <!-- <div class="mt-1">
                            <v-btn dark fab small color="success" class="ma-0 btn-legend">MR</v-btn>
                            <v-btn dark fab small color="red" class="ma-0 btn-legend">MR</v-btn>
                            Multi Rule</div> -->
                        <!-- <div class="mt-1">
                            <v-btn dark fab small color="success" class="ma-0 btn-legend">D</v-btn>
                            <v-btn dark fab small color="red" class="ma-0 btn-legend">D</v-btn>
                            Delta Check</div> -->
                        &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" color="red" @click="note" 
                           title="Catatan"
                           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
                            color="orange"
                            class="one-btn-icon white--text ma-1"
                            @click="preview"
                            title="Preview Hasil"
                            >
                           <v-icon>remove_red_eye</v-icon>
                        </v-btn>
                        

                        <v-btn
                            color="blue"
                            class="white--text ma-1"
                            @click="save_result"
                            :disabled="!btn_validation_enable"
                            :dark="btn_validation_enable"
                            v-show="true || btn_validation_show"
                            >
                            Validasi
                            <v-icon right dark>save_alt</v-icon>
                        </v-btn>

                        <v-btn
                            color="green"
                            class="white--text ma-1"
                            @click="send_to_adm"
                            :dark="contain_val"
                            v-show="contain_val"
                            >
                            Kirim ADM 
                            <v-icon right dark>send</v-icon>
                        </v-btn>

                        <v-btn
                            color="red"
                            class="white--text ma-1"
                            @click="unvalidate"
                            dark
                            v-show="!btn_validation_show && false"
                            >
                            UnValidasi
                            <v-icon right dark>save_alt</v-icon>
                        </v-btn>
                    </v-flex>
                </v-layout>
                

                
            </v-flex>
        </v-layout>
    </v-card>
</template>

<style scoped>
.btn-legend {
    height: 25px;
    width: 25px;
    border-radius: 0%;
}

button {
    height: 48px;
}

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

<script>
module.exports = {
    components : {
        'one-date-picker' : httpVueLoader('./oneDatePicker.vue')
    },
    data() {
       return {
         search_company: ''
       }
    },
    watch : {
	search_company(n,o) {
		this.$store.dispatch('company/search',n)
	}
    },
    computed : {
	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.s_date },
            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
        },

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

    methods : {
        searchs() {
            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()
            }
        },

        save_result() {
            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)
        },

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

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

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