<template>
    <v-layout>
        <v-flex xs12>

            <v-card class="mb-2">
                <v-subheader red--text text--lighten-1>
                    Reflex Test
                    <v-flex text-md-right>
                    </v-flex>
                </v-subheader>
                <v-divider></v-divider>
                <v-layout row>
                    <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                        <v-data-table :headers="headers" :items="xnatmultirules" :loading="isLoading" :pagination.sync="pagination" class="elevation-1">
                            <template slot="items" slot-scope="props">
                                <td class="text-xs-left pa-2" v-html="props.item.nilai">
                                </td>
                                <td class="text-xs-left pa-2" v-html="props.item.Nat_MultiRuleRuleIsNotApplicable">
                                </td>
                                <td class="text-xs-left pa-2" v-html="props.item.low">
                                </td>
                                <td class="text-xs-left pa-2" v-html="props.item.normal">
                                </td>
                                <td class="text-xs-left pa-2" v-html="props.item.high">
                                </td>
                                <td class="text-xs-left pa-2" v-html="props.item.negative">
                                </td>
                                <td class="text-xs-left pa-2" v-html="props.item.borderline">
                                </td>
                                <td class="text-xs-left pa-2" v-html="props.item.positive">
                                </td>
                                <td class="text-xs-left pa-2">
                                    <v-icon class="ml-3" color="primary" @click="editFormMultiRule(props.item)">edit</v-icon>
                                </td>
                            </template>
                            <template>
                                <div class="text-xs-center">
                                    <v-pagination v-model="page" :length="15" :total-visible="7"></v-pagination>
                                </div>
                            </template>
                        </v-data-table>
                    </v-flex>
                </v-layout>
            </v-card>
            <template>
                <v-layout row justify-center>
                    <v-dialog v-model="dialogquantitative" persistent max-width="900px">
                        <v-card>
                            <v-card-title>
                                <span class="headline">Form Reflex Test : {{xnattest.Nat_TestName}} [ {{this.xnilai}} ]</span>
                            </v-card-title>
                            <v-card-text class="pt-0 pb-0">
                                <v-form ref="formmultirule" lazy-validation>
                                    <v-layout row wrap>
                                        <v-flex xs12 pa-2>
                                            <v-layout row>
                                                <v-flex xs6 pa-1>
                                                    <v-text-field v-model="xextremelow" label="Extreme Low" hide-details></v-text-field>
                                                </v-flex>
                                                <v-flex xs6 pa-1>
                                                    <v-text-field v-model="xlinieritas" label="Linieritas" hide-details></v-text-field>
                                                </v-flex>
                                            </v-layout>
                                        </v-flex>
                                        <v-flex xs12 pa-2>
                                            <v-layout row>
                                                <v-flex xs6 pa-1>
                                                    <v-text-field v-model="xextremehigh" label="Extreme High" hide-details></v-text-field>
                                                </v-flex>
                                                <v-flex xs6 pa-1>
                                                    <v-text-field v-model="xlimitdetection" label="Limit Detection" hide-details></v-text-field>
                                                </v-flex>
                                            </v-layout>
                                        </v-flex>
                                        <v-flex xs12 pa-2>
                                            <v-layout row>
                                                <v-flex xs6 pa-1>
                                                    <div>
                                                        <v-btn @click="changeBtnFlagNotApplicable(xisnotapplicable)" v-if="xisnotapplicable === 'N'" small color="error">
                                                            <v-icon left>close</v-icon> NOT APPLICABLE</v-btn>
                                                        <v-btn @click="changeBtnFlagNotApplicable(xisnotapplicable)" v-if="xisnotapplicable === 'Y'" small color="success">
                                                            <v-icon left>check</v-icon> NOT APPLICABLE</v-btn>
                                                    </div>
                                                </v-flex>
                                                <v-flex xs6 pa-1>
                                                </v-flex>
                                            </v-layout>
                                        </v-flex>
                                        <v-flex xs12 pa-2>
                                            <v-layout row>
                                                <v-flex xs4 pa-1>
                                                    <v-autocomplete label="Pemeriksaan" v-model="xorderautotest" :items="xorderautotests" :search-input.sync="search_test" auto-select-first
                                                        no-filter item-text="Nat_TestName" return-object no-data-text="Cari Pemeriksaan"
                                                        outline small>
                                                        <template slot="item" slot-scope="{ item }">
                                                            <v-list-tile-content>
                                                                <v-list-tile-title v-text="item.Nat_TestName"></v-list-tile-title>
                                                            </v-list-tile-content>
                                                        </template>
                                                    </v-autocomplete>
                                                </v-flex>
                                                <v-flex xs4 pa-1>
                                                    <v-select outline item-text="M_ValueName" class="mini-select" return-object :items="xvaluetypexes" v-model="xvaluetypex"
                                                        label="Nilai"></v-select>
                                                </v-flex>
                                                <v-flex xs4 pa-1>
                                                    <span @click="addTest()" class="icon-medium-fill-base xs1 white--text primary  ml-1 icon-add"></span>
                                                </v-flex>
                                            </v-layout>
                                        </v-flex>
                                    </v-layout>
                                    <v-layout row wrap>
                                        <v-flex xs12 pt-2 pb-1>
                                            <table style="
                                        font-family: arial, sans-serif;
                                        border-collapse: collapse;
                                        width: 100%;
                                        border: 0px;
                                    " class="px">
                                                <tr>
                                                    <td style="border: 1px solid #fff;
                                                        border-collapse: collapse;
                                                        padding-top: 2px;
                                                        padding-bottom: 2px;" width="40%" class="pa-2 grey lighten-1 white--text">TEST
                                                    </td>
                                                    <td style="border: 1px solid #fff;
                                                        border-collapse: collapse;
                                                        padding-top: 2px;
                                                        padding-bottom: 2px;" width="40%" class="pa-2 grey lighten-1 white--text">NILAI
                                                    </td>
                                                    <td style="border: 1px solid #fff;
                                                        border-collapse: collapse;
                                                        padding-top: 2px;
                                                        padding-bottom: 2px;" width="20%" class="pa-2 grey lighten-1 white--text">HAPUS
                                                    </td>
                                                </tr>
                                                <tr class="mini-input" v-for="(vpx,idx) in xordertests">
                                                    <td style="border: 1px solid #fff;
                                                        border-collapse: collapse;
                                                        padding-top: 2px;
                                                        padding-bottom: 2px;
                                                        padding: 8px;
                                                        text-align:left;
                                                        vertical-align:left;" align="left">{{vpx.Nat_TestName}}

                                                    </td>
                                                    <td style="border: 1px solid #fff;
                                                        border-collapse: collapse;
                                                        padding-top: 2px;
                                                        padding-bottom: 2px;
                                                        padding: 8px;" class="text-md-left pl-3">{{vpx.nilai}}</td>
                                                    <td style="border: 1px solid #fff;
                                                        border-collapse: collapse;
                                                        padding-top: 2px;
                                                        padding-bottom: 2px;
                                                        padding: 8px;" class="text-md-center pr-2"><span v-if="vpx.editable == 'N'">{{idx + 1}}</span>
                                                        <v-icon v-else-if="vpx.editable == 'Y'" color="#c75c3a" @click="deleteDataPx(idx)">delete</v-icon>
                                                    </td>
                                                </tr>
                                                <tfoot>
                                                </tfoot>
                                            </table>
                                        </v-flex>
                                    </v-layout>
                            </v-card-text>
                            <v-card-actions>
                                <v-spacer></v-spacer>
                                <v-btn color="blue darken-1" flat @click="updateDialogFormMultiRule()">Tutup</v-btn>
                                <v-btn v-if="this.xid === '0'" color="grey darken-1" flat @click="saveFormReflexTest()">Simpan</v-btn>
                                <v-btn v-if="this.xid !== '0'" color="grey darken-1" flat @click="updateFormReflexTest()">Simpan Perubahan</v-btn>
                            </v-card-actions>
                            </v-form>
                        </v-card>
                    </v-dialog>
                </v-layout>
            </template>
        </v-flex>
    </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-md-test-conclusion-dialog-normal-value': httpVueLoader(
                './oneMdTestConclusionDialogNormalValue.vue'),
            'one-dialog-alert': httpVueLoader('../../common/oneDialogAlert.vue')
        },
        methods: {
            changeBtnFlagNotApplicable(value) {
                var newval = (value === 'Y') ? 'N' : 'Y'
                this.xisnotapplicable = newval
                console.log(this.xisnotapplicable)
            },
            updateDialogFormMultiRule() {
                this.$store.commit("multirule/update_dialog_form_quantitative", false)
            },
            openFormMultiRule() {
                this.$refs.formmultirule.reset()
                this.$refs.formmultirule.resetValidation()
                this.$store.commit("multirule/update_dialog_form_quantitative", true)
            },
            addTest() {
                var oldArr = this.$store.state.multirule.ordertests
                var arrTest = this.$store.state.multirule.orderautotest
                var arrValue = this.$store.state.multirule.selected_valuetypex
                var arrAll = _.assignIn(arrTest, arrValue)
                oldArr.push(_.assignIn(arrTest, arrValue))
                this.searchtest = ''
                this.$store.commit("multirule/update_orderautotests", [])
                this.$store.commit("multirule/update_orderautotest", {})
                this.$store.commit("multirule/update_valuetypexes", [])
                this.$store.commit("multirule/update_selected_valuetypex", {})

            },
            deleteDataPx(idx) {
                var inx = parseInt(idx);
                var xrow = this.$store.state.multirule.ordertests[inx]
                if (parseInt(xrow.xid) !== 0) {
                    var xdeltest = this.$store.state.multirule.deleted_ordertests
                    xdeltest.push(xrow)
                    this.$store.commit("multirule/update_deleted_ordertests", xdeltest)
                }
                var oldArr = this.$store.state.multirule.ordertests
                oldArr.splice(inx, 1)
                this.$store.commit("multirule/update_ordertests", oldArr)
            },
            editFormMultiRule(data) {
                console.log('editform')
                console.log(data.nilai)
                this.xid = data.Nat_MultiRuleRuleID
                this.xmultiruleid = data.Nat_MultiRuleID
                this.xnilai = data.nilai
                this.xextremelow = this.xnattest.Nat_MultiRuleExtremeLow
                this.xextremehigh = this.xnattest.Nat_MultiRuleExtremeHigh
                this.xlinieritas = this.xnattest.Nat_MultiRuleLinieritas
                this.xlimitdetection = this.xnattest.Nat_MultiRuleLimitDetection
                this.xisnotapplicable = data.Nat_MultiRuleRuleIsNotApplicable
                this.$store.commit("multirule/update_ordertests", data.tests)
                this.$store.commit("multirule/update_dialog_form_quantitative", true)
            },
            saveFormReflexTest() {
                this.$store.dispatch("multirule/savereflextest", {
                    Nat_MultiRuleRuleNat_MultiRuleID: this.xmultiruleid,
                    Nat_MultiRuleNilai: this.xnilai,
                    testid: this.xnattest.Nat_TestID,
                    isquantitative: this.xnattest.Nat_TestIsQuantitative,
                    extremelow: this.xextremelow,
                    extremehigh: this.xextremehigh,
                    linieritas: this.xlinieritas,
                    limitdetection: this.xlimitdetection,
                    isnotapplicable: this.xisnotapplicable,
                    ordertests: this.xordertests
                })

            },
            updateFormReflexTest() {
                this.$store.dispatch("multirule/updatereflextest", {
                    id: this.xid,
                    multiruleid: this.xmultiruleid,
                    nilai: this.xnilai,
                    testid: this.xnattest.Nat_TestID,
                    isquantitative: this.xnattest.Nat_TestIsQuantitative,
                    extremelow: this.xextremelow,
                    extremehigh: this.xextremehigh,
                    linieritas: this.xlinieritas,
                    limitdetection: this.xlimitdetection,
                    isnotapplicable: this.xisnotapplicable,
                    ordertests: this.xordertests,
                    deleted_ordertests: this.$store.state.multirule.deleted_ordertests
                })

            },
            thr_search_test: _.debounce(function () {
                this.$store.dispatch("multirule/searchtest", this.search_test)
            }, 2000)
        },
        computed: {
            dialogquantitative() {
                return this.$store.state.multirule.dialog_form_quantitative
            },
            isLoading() {
                return this.$store.state.multirule.search_status == 1
            },
            xnattest() {
                return this.$store.state.multirule.selected_multirule
            },
            xnatmultirules() {
                return this.$store.state.multirule.natmultirules
            },
            xfiltermethode() {
                return this.$store.state.multirule.filter_methodes
            },
            xselected_filter_methode: {
                get() {
                    return this.$store.state.multirule.selected_filter_methode
                },
                set(val) {
                    this.$store.commit("form/update_selected_filter_methode", val)
                }
            },
            xtests() {
                return this.$store.state.multirule.tests
            },
            xtest: {
                get() {
                    return this.$store.state.multirule.test
                },
                set(val) {
                    this.$store.commit("multirule/update_test", val)
                }
            },
            xisnotapplicable: {
                get() {
                    return this.$store.state.multirule.isnotapplicable
                },
                set(val) {
                    this.$store.commit("multirule/update_isnotapplicable", val)
                }
            },
            xvaluexs() {
                return this.$store.state.multirule.valuexs
            },
            xvaluex: {
                get() {
                    return this.$store.state.multirule.valuex
                },
                set(val) {
                    this.$store.commit("multirule/update_valuex", val)
                }
            },
            xspilih: {
                get() {
                    return this.$store.state.multirule.spilih
                },
                set(val) {
                    this.$store.commit("multirule/update_spilih", val)
                }
            },
            xsmultiruleid: {
                get() {
                    return this.$store.state.multirule.smultiruleid
                },
                set(val) {
                    this.$store.commit("multirule/update_smultiruleid", val)
                }
            },
            xvaluetypees() {
                return this.$store.state.multirule.valuetypees
            },
            xvaluetype: {
                get() {
                    return this.$store.state.multirule.selected_valuetype
                },
                set(val) {
                    this.$store.commit("multirule/update_selected_valuetype", val)
                }
            },
            xnilai: {
                get() {
                    return this.$store.state.multirule.nilai
                },
                set(val) {
                    this.$store.commit("multirule/update_nilai", val)
                }
            },
            xorderautotests() {
                return this.$store.state.multirule.orderautotests
            },
            xorderautotest: {
                get() {
                    return this.$store.state.multirule.orderautotest
                },
                set(val) {
                    this.$store.commit("multirule/update_orderautotest", val)
                }
            },
            xordertests() {
                return this.$store.state.multirule.ordertests
            },
            xvaluetypexes() {
                return this.$store.state.multirule.valuetypexes
            },
            xvaluetypex: {
                get() {
                    return this.$store.state.multirule.selected_valuetypex
                },
                set(val) {
                    this.$store.commit("multirule/update_selected_valuetypex", val)
                }
            }
        },
        data() {
            return {
                msgalertconfirmation: "Perubahan yang telah dilakukan belum disimpan dong !",
                menufilterdatestart: false,
                menufilterdateend: false,
                date: new Date().toISOString().substr(0, 10),
                items: [],
                name: '',
                xelow: '',
                xehigh: '',
                xextremehigh: '',
                xextremelow: '',
                xlimitdetection: '',
                xlinieritas: '',
                search_test: '',
                xid: 0,
                xmultiruleid: 0,
                page: 1,
                headers: [{
                        text: "NILAI",
                        align: "center",
                        sortable: false,
                        width: "5%",
                        class: "pa-2 grey lighten-1 white--text"
                    },
                    {
                        text: "NOT APLICABLE",
                        align: "center",
                        sortable: false,
                        width: "5%",
                        class: "pa-2 grey lighten-1 white--text"
                    },
                    {
                        text: "LOW",
                        align: "center",
                        sortable: false,
                        width: "15%",
                        class: "pa-2 grey lighten-1 white--text"
                    },
                    {
                        text: "NORMAL",
                        align: "center",
                        sortable: false,
                        width: "15%",
                        class: "pa-2 grey lighten-1 white--text"
                    },
                    {
                        text: "HIGH",
                        align: "center",
                        sortable: false,
                        width: "15%",
                        class: "pa-2 grey lighten-1 white--text"
                    },
                    {
                        text: "NEGATIVE",
                        align: "center",
                        sortable: false,
                        width: "15%",
                        class: "pa-2 grey lighten-1 white--text"
                    },
                    {
                        text: "BORDERLINE",
                        align: "center",
                        sortable: false,
                        width: "15%",
                        class: "pa-2 grey lighten-1 white--text"
                    },
                    {
                        text: "POSITIVE",
                        align: "center",
                        sortable: false,
                        width: "15%",
                        class: "pa-2 grey lighten-1 white--text"
                    },
                    {
                        text: "AKSI",
                        align: "center",
                        sortable: false,
                        width: "5%",
                        class: "pa-2 grey lighten-1 white--text"
                    }

                ],
                headers_search: [{
                        text: "PASIEN",
                        align: "left",
                        sortable: false,
                        width: "20%",
                        class: "pa-2 grey lighten-1 white--text"
                    },
                    {
                        text: "BARCODE",
                        align: "center",
                        sortable: false,
                        width: "15%",
                        class: "pa-2 grey lighten-1 white--text"
                    },
                    {
                        text: "ALMARI",
                        align: "left",
                        sortable: false,
                        width: "25%",
                        class: "pa-2 grey lighten-1 white--text"
                    },
                    {
                        text: "RAK",
                        align: "center",
                        sortable: false,
                        width: "15%",
                        class: "pa-2 grey lighten-1 white--text"
                    },

                    {
                        text: "POSISI",
                        align: "center",
                        sortable: false,
                        width: "10%",
                        class: "pa-2 grey lighten-1 white--text"
                    },
                    {
                        text: "AKSI",
                        align: "center",
                        sortable: false,
                        width: "10%",
                        class: "pa-2 grey lighten-1 white--text"
                    }
                ],
                pagination: {
                    descending: false,
                    page: 1,
                    rowsPerPage: 5,
                    sortBy: 'trx_id ASC',
                    totalItems: this.$store.state.multirule.total_transactions
                }
            };
        },
        watch: {
            search_test(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.multirule.update_autocomplete_status == 1) return
                this.thr_search_test()
            }
        }
    }
</script>