<template>
    <v-layout v-if="xact !== 'new'" column>
        <v-dialog v-model="dialogconfirmationdeletepat" persistent max-width="290">
            <v-card>
                <v-card-title dark class="headline error pt-2 pb-2" primary-title style="color:white">
                    <h4 dark>Konfirmasi</h4>
                </v-card-title>
                <v-card-text>
                    {{msgconfirmationdeletepat}}
                </v-card-text>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn small color="error darken-1 text-sm-left" flat @click="doDeletePat()">Hapus</v-btn>
                    <v-btn small color="primary darken-1 text-sm-right" flat @click="dialogconfirmationdeletepat = false">Batal</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <v-dialog v-model="dialogconfirmationsend" persistent max-width="290">
            <v-card>
                <v-card-title dark class="headline success pt-2 pb-2" primary-title style="color:white">
                    <h4 dark>Konfirmasi</h4>
                </v-card-title>
                <v-card-text>
                    {{msgconfirmationsend}}
                </v-card-text>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn small color="success darken-1 text-sm-left" flat @click="doOrderCalculation()">Kirim</v-btn>
                    <v-btn small color="primary darken-1 text-sm-right" flat @click="dialogconfirmationsend = false">Batal</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <v-dialog v-model="dialogformdetail" persistent max-width="950">
            <v-card>
                <v-card-title>
                    <span class="headline">Form Rumus</span>
                </v-card-title>
                <v-card-text class="pt-0 pb-0">
                    <v-layout row>
                        <v-flex xs12 pr-3>
                            <v-layout wrap>
                                <v-flex xs12>
                                    <v-layout>
                                        <v-flex xs6 pa-1>
                                            <v-text-field v-model="xnamacalculation" label="Nama Rumus"></v-text-field>
                                            <p v-if="checkError('requirenamacalculation')" class="error pl-2 pr-2" style="color:#fff">Nama diisi dulu dong</p>
                                        </v-flex>
                                        <v-flex xs6 pa-1>
                                            <v-autocomplete label="Test Yang Akan Dihitung" v-model="xautotest" :items="xautotests" :search-input.sync="search_testheader" auto-select-first
                                                no-filter item-text="Nat_TestName" return-object no-data-text="Cari Pemeriksaan"
                                                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>
                                            <p v-if="checkError('requireautotest')" class="error pl-2 pr-2" style="color:#fff">Pemeriksaan detail dipilih dulu dong</p>
                                        </v-flex>
                                    </v-layout>
                                </v-flex>
                                <v-flex xs6>
                                    <v-select item-text="Nat_SexName" return-object :items="xsexes" v-model="xsex" label="Jenis Kelamin"></v-select>
                                </v-flex>
                                <v-flex xs12>
                                    <v-layout row>
                                        <v-flex xs10 pa-1>
                                            <v-autocomplete label="Tests" 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 xs-2 class="text-md-left">
                                            <v-btn @click="addTest()" color="grey en-3">+</v-btn>
                                        </v-flex>
                                    </v-layout>
                                </v-flex>
                                <v-flex class="border-bottom-dashed" xs12 pt-2 pl-4 pr-4 pb-4>
                                    <v-layout xs12 row wrap>
                                        <p v-if="checkError('requireordertest')" class="error pl-2 pr-2" style="color:#fff">Pemeriksaan dipilih dulu dong</p>
                                        <v-flex xs4 v-for="(vst,idx) in xordertests" :key="vst.Nat_TestID" class="pr-2 pb-1">
                                            <v-layout row>
                                                <v-flex @click="deleteDataPx(idx)" class="boxoutline" style="color: red;border: 1px solid red;justify-content: center;height: 45px;line-height: 45px;padding-left: 10px;background: #ffffff;font-size: 14px;font-weight: 500;border-radius: 1px"
                                                    xs11>
                                                    <span>{{vst.Nat_TestName}}</span>
                                                </v-flex>
                                            </v-layout>
                                        </v-flex>
                                    </v-layout>
                                </v-flex>                               
                                <v-flex xs12>
                                    <v-textarea v-model="xrumus" label="Formula"></v-textarea>
                                    <p v-if="checkError('requirerumus')" class="error pl-2 pr-2" style="color:#fff">Rumus nya diisi dulu dong</p>
                                </v-flex>
                            </v-layout>
                        </v-flex>
                    </v-layout>
                </v-card-text>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="blue darken-1" flat @click="dialogformdetail = false">Tutup</v-btn>
                    <v-btn v-if="xactpat === 'new'" color="blue darken-1" flat @click="saveNewCalculation()">Simpan</v-btn>
                    <v-btn v-if="xactpat === 'edit'" color="blue darken-1" flat @click="saveEditCalculation()">Simpan Perubahan</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        
        </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 = {
        data: () => ({
            oldlabel: '',
            search_test: '',
            search_testheader: '',
            headers: [

                {
                    text: "PEMERIKSAAN",
                    align: "center",
                    sortable: false,
                    value: "mr",
                    width: "25%",
                    class: "pa-1 blue lighten-3 white--text"
                },
                {
                    text: "KODE RUMUS",
                    align: "center",
                    sortable: false,
                    value: "lab",
                    width: "25%",
                    class: "pa-1 blue lighten-3 white--text"
                }
            ]
        }),
        computed: {
            dialogconfirmationdeletepat: {
                get() {
                    return this.$store.state.testcalculation.dialog_confirmation_delete_pat
                },
                set(val) {
                    this.$store.commit("testcalculation/update_dialog_confirmation_delete_pat", val)
                }
            },
            msgconfirmationdeletepat() {
                return this.$store.state.testcalculation.msg_confirmation_delete_pat
            },
            dialogconfirmationsend: {
                get() {
                    return this.$store.state.testcalculation.dialog_confirmation_send
                },
                set(val) {
                    this.$store.commit("testcalculation/update_dialog_confirmation_send", val)
                }
            },
            msgconfirmationsend() {
                return this.$store.state.testcalculation.msg_confirmation_send
            },
            xact() {
                return this.$store.state.testcalculation.act
            },
            xactpat() {
                return this.$store.state.testcalculation.act_pat
            },
            xactsend() {
                return this.$store.state.testcalculation.act_send
            },

            dialogformdetail: {
                get() {
                    return this.$store.state.testcalculation.dialog_form_detail
                },
                set(val) {
                    this.$store.commit("testcalculation/update_dialog_form_detail", val)
                }
            },
            isLoading() {
                return this.$store.state.testcalculation.search_status == 1
            },
            xdetails(p) {
                return this.$store.state.testcalculation.details
            },
            xnamacalculation: {
                get() {
                    return this.$store.state.testcalculation.namacalculation
                },
                set(val) {
                    this.$store.commit("testcalculation/update_namacalculation", val)
                }
            },
            xsexes() {
                return this.$store.state.testcalculation.sexes
            },
            xsex: {
                get() {
                    return this.$store.state.testcalculation.selected_sex
                },
                set(val) {
                    this.$store.commit("testcalculation/update_selected_sex", val)
                }
            },
            isage: {
                get() {
                    return this.$store.state.testcalculation.isage
                },
                set(val) {
                    this.$store.commit("testcalculation/update_isage", val)
                }
            },
            issex: {
                get() {
                    return this.$store.state.testcalculation.issex
                },
                set(val) {
                    this.$store.commit("testcalculation/update_issex", val)
                }
            },
            rumusx: {
                get() {
                    return this.$store.state.testcalculation.rumusx
                },
                set(val) {
                    this.$store.commit("testcalculation/update_rumusx", val)
                }
            },
            xisage: {
                get() {
                    return this.$store.state.testcalculation.xisage
                },
                set(val) {
                    this.$store.commit("testcalculation/update_xisage", val)
                }
            },
            xissex: {
                get() {
                    return this.$store.state.testcalculation.xissex
                },
                set(val) {
                    this.$store.commit("testcalculation/update_xissex", val)
                }
            },
            xrumus: {
                get() {
                    return this.$store.state.testcalculation.rumus
                },
                set(val) {
                    this.$store.commit("testcalculation/update_rumus", val)
                }
            },

            xautotests() {
                return this.$store.state.testcalculation.autotests
            },
            xautotest: {
                get() {
                    return this.$store.state.testcalculation.autotest
                },
                set(val) {
                    this.$store.commit("testcalculation/update_autotest", val)
                }
            },
            xorderautotests() {
                return this.$store.state.testcalculation.orderautotests
            },
            xorderautotest: {
                get() {
                    return this.$store.state.testcalculation.orderautotest
                },
                set(val) {
                    this.$store.commit("testcalculation/update_orderautotest", val)
                }
            },
            xordertests() {
                return this.$store.state.testcalculation.ordertests
            },
            xordertestfavorits() {
                return this.$store.state.testcalculation.ordertestfavorits
            },
            sub_total() {
                let tests = this.xordertests
                sub_total = 0
                tests.forEach(function (vpx, idx) {
                    let total = parseInt(vpx.total)
                    sub_total = sub_total + total
                })
                return sub_total
            },
            dp() {
                let mindp = this.$store.state.testcalculation.selected_testcalculation.M_MouMinDP
                let dp = mindp / 100 * this.sub_total
                return dp
            },
            isbill() {
                let isbill = this.$store.state.testcalculation.selected_testcalculation.M_MouIsBill
                return isbill
            },
            isagingonhold() {
                let isagingonhold = this.$store.state.testcalculation.selected_testcalculation.M_MouIsAgingOnHold
                return isagingonhold
            },
            agingonhold() {
                let agingonhold = this.$store.state.testcalculation.selected_testcalculation.M_MouIsAgingOnHoldNote
                return agingonhold
            }
        },
        methods: {
            changeBtnFlagAge(value) {
                var newval = (value === 'Y') ? 'N' : 'Y'
                this.isage = newval
            },
            xchangeBtnFlagAge(value) {
                var newval = (value === 'Y') ? 'N' : 'Y'
                this.xisage = newval
            },
            changeBtnFlagSex(value) {
                var newval = (value === 'Y') ? 'N' : 'Y'
                this.issex = newval
            },
            xchangeBtnFlagSex(value) {
                var newval = (value === 'Y') ? 'N' : 'Y'
                this.xissex = newval
            },
            changeBtnFlagDefaultAge(value) {
                var newval = (value === 'Y') ? 'N' : 'Y'
                this.isage = newval
            },
            createNewCalculation() {
                this.$store.commit("testcalculation/update_act_pat", 'new')
                this.$store.commit("testcalculation/update_isage", 'N')
                this.$store.commit("testcalculation/update_issex", 'N')
                this.$store.commit("testcalculation/update_rumusx", '')

                this.$store.commit("testcalculation/update_x_pat_id", 0)
                this.$store.commit("testcalculation/update_namacalculation", '')
                this.$store.commit("testcalculation/update_xisage", 'N')
                this.$store.commit("testcalculation/update_xissex", 'N')
                this.$store.commit("testcalculation/update_rumus", '')
                this.$store.commit("testcalculation/update_autotests", [])
                this.$store.commit("testcalculation/update_autotest", {})
                this.$store.commit("testcalculation/update_dialog_form_detail", true)
                this.$store.dispatch("testcalculation/searchtestfavorit", {
                    mouid: this.$store.state.testcalculation.selected_testcalculation.Nat_TestCalculationM_MouID
                })
            },
            checkError(value) {
                var errors = this.$store.state.testcalculation.errors
                if (errors.includes(value)) {
                    return true
                } else {
                    return false
                }
            },
            saveNewCalculation() {
                this.$store.commit("testcalculation/update_errors", [])
                var errors = this.$store.state.testcalculation.errors
                if (this.xnamacalculation === '') {
                    errors.push("requirenamacalculation")
                }
                if (this.xrumus === '') {
                    errors.push("requirerumus")
                }
                if (_.isEmpty(this.xautotest)) {
                    errors.push("requireautotest")
                }
                if (this.$store.state.testcalculation.ordertests.length == 0) {
                    errors.push("requireordertest")
                }
                if (errors.length === 0) {
                    var prm = {}
                    prm.T_TestCalculationName = this.xnamacalculation
                    prm.T_TestCalculationNat_TestID = this.$store.state.testcalculation.autotest.Nat_TestID
                    prm.T_TestCalculationFormula = this.xrumus
                    prm.T_TestCalculationNat_SexID = this.$store.state.testcalculation.selected_sex.Nat_SexID
                    prm.ordertests = this.xordertests
                    this.$store.dispatch("testcalculation/savenewdetail", prm)
                }
            },
            editCalculation() {
                this.$store.commit("testcalculation/update_act_pat", 'edit')
                this.$store.commit("testcalculation/update_dialog_form_detail", true)
            },
            saveEditCalculation() {
                this.$store.commit("testcalculation/update_errors", [])
                var errors = this.$store.state.testcalculation.errors
                if (this.xnamacalculation === '') {
                    errors.push("requirenamacalculation")
                }
                if (this.xrumus === '') {
                    errors.push("requirerumus")
                }
                if (_.isEmpty(this.xautotest)) {
                    errors.push("requireautotest")
                }
                if (this.$store.state.testcalculation.ordertests.length == 0) {
                    errors.push("requireordertest")
                }
                if (errors.length === 0) {
                    var prm = {}
                    prm.T_TestCalculationID = this.$store.state.testcalculation.x_pat_id
                    prm.T_TestCalculationName = this.xnamacalculation
                    prm.T_TestCalculationNat_TestID = this.$store.state.testcalculation.autotest.Nat_TestID
                    prm.T_TestCalculationFormula = this.xrumus
                    prm.T_TestCalculationNat_SexID = this.$store.state.testcalculation.selected_sex.Nat_SexID
                    prm.ordertests = this.xordertests
                    prm.deleted_ordertests = this.$store.state.testcalculation.deleted_ordertests
                    this.$store.dispatch("testcalculation/saveeditdetail", prm)
                }
            },
            thr_search_test: _.debounce(function () {
                this.$store.dispatch("testcalculation/searchtest", {
                    tes: this.search_test
                })
            }, 2000),
            thr_search_testheader: _.debounce(function () {
                this.$store.dispatch("testcalculation/searchtestheader", {
                    tes: this.search_testheader
                })
            }, 2000),
            deleteCalculation() {
                this.$store.commit("testcalculation/update_act_pat", 'delete')
                this.$store.commit("testcalculation/update_x_pat_id", this.$store.state.testcalculation.x_pat_id)
                this.$store.commit("testcalculation/update_errors", [])
                var errors = this.$store.state.testcalculation.errors
                var msg = ''
                msg = "Yakin, akan menghapus data rumus " + this.$store.state.testcalculation.selected_testcalculation
                    .T_TestCalculationName + " ?"
                this.$store.commit("testcalculation/update_msg_confirmation_delete_pat", msg)
                this.$store.commit("testcalculation/update_dialog_confirmation_delete_pat", true)
            },
            doDeletePat() {
                var prm = {}
                prm.T_TestCalculationID = this.$store.state.testcalculation.selected_testcalculation
                    .T_TestCalculationID
                prm.T_TestCalculationName = this.$store.state.testcalculation.selected_testcalculation
                    .T_TestCalculationName
                this.$store.dispatch("testcalculation/deletedetail", prm)
            },
            sendOrderCalculation(value) {
                let msg = "Yakin, akan mengirim permintaan nomor " + this.$store.state.testcalculation.selected_testcalculation
                    .Nat_TestCalculationNumber + " ?"
                this.$store.commit("testcalculation/update_msg_confirmation_send", msg)
                this.$store.commit("testcalculation/update_dialog_confirmation_send", true)
            },
            doOrderCalculation() {
                var prm = {}
                prm.Nat_TestCalculationID = this.$store.state.testcalculation.selected_testcalculation
                    .Nat_TestCalculationID
                prm.Nat_TestCalculationNumber = this.$store.state.testcalculation.selected_testcalculation
                    .Nat_TestCalculationNumber
                this.$store.dispatch("testcalculation/sendorder", prm)
            },
            convertMoney(money) {
                return one_money(money)
            },
            addTest() {
                this.$store.dispatch("testcalculation/getnumberx", {
                    Nat_TestID: this.$store.state.testcalculation.orderautotest.Nat_TestID,
                    Nat_TestName: this.$store.state.testcalculation.orderautotest.Nat_TestName
                })
                this.searchtest = ''
            },
            addTestFavorit(val) {
                var oldArr = this.$store.state.testcalculation.ordertests
                oldArr.push(val)
            },
            deleteDataPx(idx) {
                this.$store.dispatch("testcalculation/deletenumberx",{id:idx})
                var inx = parseInt(idx);
                var xrow = this.$store.state.testcalculation.ordertests[inx]
                if (parseInt(xrow.xid) !== 0) {
                    var xdeltest = this.$store.state.testcalculation.deleted_ordertests
                    xdeltest.push(xrow)
                    this.$store.commit("testcalculation/update_deleted_ordertests", xdeltest)
                }
                var oldArr = this.$store.state.testcalculation.ordertests
                oldArr.splice(inx, 1)
                this.$store.commit("testcalculation/update_ordertests", oldArr)
            },
        },
        watch: {
            search_city(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.testcalculation.update_autocomplete_status == 1) return
                this.thr_search_city()
            },
            search_test(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.testcalculation.update_autocomplete_status == 1) return
                this.thr_search_test()
            },
            search_testheader(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.testcalculation.update_autocomplete_status == 1) return
                this.thr_search_testheader()
            }
        }
    }
</script>