<template>
    <v-layout row justify-center>
        <v-dialog v-model="promisedialog" persistent max-width="30%">
            <v-card>
                <v-card-title class="headline darken-1 pt-2 pb-2" primary-title>
                    Konfirmasi
                </v-card-title>
                <v-card-text class="pt-2 pb-2">
                    <v-layout row>
                        <v-flex xs12 d-flex>
                            <v-layout row>
                                <v-flex pb-1 v-html="msgdialogpromise" xs12>

                                </v-flex>
                            </v-layout>
                        </v-flex>
                    </v-layout>
                </v-card-text>
                <v-divider></v-divider>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="black" flat @click="promisedialog = false">
                        Tutup
                    </v-btn>
                    <v-btn color="purple" flat @click="closeDialogPromise()">
                        Lanjutkan
                    </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <v-dialog v-model="testdialog" persistent max-width="50%">
            <v-card>
                <v-card-title dark class="headline purple pt-2 pb-2" primary-title style="color:white">
                    <h4>Pemeriksaan</h4>
                </v-card-title>
                <v-card-text class="pt-2 pb-2">
                    <v-layout pa-2 row wrap>
                        <v-flex xs12 pl-1>
                            <v-card elevation="1">
                                <v-flex xs12 mb-2 pa-2>
                                    <v-layout row>
                                        <v-flex pt-2 xs4>
                                            <h4 class="mb-1">Tambah Pemeriksaan</h4>
                                        </v-flex>
                                        <v-flex style="text-align:right" pt-1 xs8>
                                            <v-progress-circular v-if="getdatastatus === 1" indeterminate
                                                color="primary"></v-progress-circular>
                                        </v-flex>
                                    </v-layout>
                                    <v-layout row>
                                        <v-flex pr-1 xs8>
                                            <v-autocomplete label="Pemeriksaan" v-model="vtest" :items="vtests"
                                                :search-input.sync="search_test" auto-select-first no-filter
                                                item-text="pxname" return-object :loading="isLoading"
                                                no-data-text="Cari Pemeriksaan ...">
                                                <template slot="item" slot-scope="{ item }">
                                                    <v-list-tile-content>
                                                        <v-list-tile-title v-text="item.pxname"></v-list-tile-title>
                                                    </v-list-tile-content>
                                                </template>
                                            </v-autocomplete>
                                        </v-flex>
                                        <v-flex class="xs4 offset-2 text-sm-right">
                                            <v-btn color="primary" :disabled="parseInt(selectedpatient.F_BillDetailID) > 0" @click="addTestToAll()">
                                                Tambahkan
                                            </v-btn>
                                        </v-flex>
                                    </v-layout>

                                </v-flex>
                            </v-card>
                            <v-card>
                                <v-flex xs12 pa-2>
                                    <v-layout row>
                                        <v-flex pa-2 xs12>
                                            <v-flex text-md-right>
                                                <v-select v-if="showdropdowncito === 'Y'" item-text="name" return-object
                                                    :items="citos" v-model="selected_cito" label="Pilihan Cito"></v-select>
                                            </v-flex>
                                        </v-flex>
                                    </v-layout>
                                    <v-layout row>
                                        <v-flex pa-2 xs12>
                                            <v-data-table :headers="headers" :items="testsall" :loading="isLoading"
                                                hide-actions>
                                                <template slot="items" slot-scope="props">
                                                    <tr>
                                                        <td class="text-xs-center pa-2 pl-2">
                                                            <v-checkbox class="text-xs-center" primary hide-details
                                                            :disabled="parseInt(selectedpatient.F_BillDetailID) > 0"   @change="changeTestAll(props.item, props.item.status)"
                                                                v-model="props.item.status"></v-checkbox>
                                                        </td>
                                                        <td class="text-xs-center pa-2">
                                                            <v-checkbox class="text-xs-center" primary hide-details
                                                            :disabled="parseInt(selectedpatient.F_BillDetailID) > 0"   @change="changeTestCito(props.item)"
                                                                v-model="props.item.cito"></v-checkbox>
                                                        </td>
                                                        <td class="text-xs-left pa-2">
                                                            {{ props.item.pxname }}
                                                            <v-select
                                                                v-if="(props.item.xid !== 0 || props.item.xid !== '0') && props.item.status === false"
                                                                item-text="label" return-object :items="charges"
                                                                hide-details v-model="props.item.charge"
                                                                @change="changeCharge(props.item, props.item.charge)"
                                                                label="Pilihan potongan admin"></v-select>
                                                        </td>
                                                        <td class="text-xs-center pa-2">{{ convertMoney(props.item.bruto) }}
                                                        </td>
                                                        <td class="text-xs-center pa-2">{{ convertMoney(props.item.discount)
                                                        }}</td>
                                                        <td class="text-xs-center pa-2">
                                                            <span
                                                                v-if="(props.item.xid !== 0 || props.item.xid !== '0') && props.item.status">{{
                                                                    convertMoney(props.item.total) }}</span>
                                                            <span
                                                                v-if="(props.item.xid !== 0 || props.item.xid !== '0') && props.item.status === false"
                                                                style="text-decoration: line-through;">{{
                                                                    convertMoney(props.item.total) }}</span>
                                                            <span
                                                                v-if="(props.item.xid !== 0 || props.item.xid !== '0') && props.item.status === false">{{ totalRetur(props.item) }}</span>
                                                        </td>
                                                    </tr>
                                                </template>
                                            </v-data-table>
                                        </v-flex>
                                    </v-layout>
                                </v-flex>
                            </v-card>
                        </v-flex>
                    </v-layout>
                </v-card-text>
                <v-divider></v-divider>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn flat @click="closeDialogOrder">
                        Tutup
                    </v-btn>
                    <v-btn v-if="getdatastatus !== 1" style="margin-right:0" @click="checkPromiseOrderAdd()" flat
                        color="primary" dark>Simpan</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <one-dialog-error :status="opendialogerror" :msg="msgerror"
            @close-dialog-error="opendialogerror = false"></one-dialog-error>
    </v-layout>
</template>

<style scoped>
table {
    font-family: arial, sans-serif;
    btest-collapse: collapse;
    width: 100%;
    background: white;
    btest: 1px solid #ddd !important;
}

th,
td {


    padding: .75rem 1.25rem;
    btest: 1px solid #ddd !important;
}

table>tr>td {
    padding: .75rem 1.25rem;
    btest: 1px solid #ddd !important;
}

table>tr>td:first {
    padding-left: 15px !important;
}
</style>
<script>
module.exports = {
    components: {
        'one-dialog-error': httpVueLoader('../../common/oneDialogErrorFajri.vue')
    },
    data() {
        return {
            printwidth: 600,
            search_test: '',
            is_loading: false,
            isLoading: false,
            error_promise: false,
            error_promise_dialog: false,
            selected_promise: {},
            opendialogerror: false,
            msgerror: "",
            promiseright: '-',
            charges: [{ value: 0, label: '0%' }, { value: 5, label: '5%' }, { value: 10, label: '10%' }, { value: 25, label: '25%' }],
            headers: [
                {
                    text: "STATUS",
                    align: "left",
                    sortable: false,
                    value: "status",
                    width: "10%",
                    class: "pa-2 blue lighten-3 white--text"
                },
                {
                    text: "CITO",
                    align: "left",
                    sortable: false,
                    value: "cito",
                    width: "10%",
                    class: "pa-2 blue lighten-3 white--text"
                },
                {
                    text: "PEMERIKSAAN",
                    align: "left",
                    sortable: false,
                    value: "testname",
                    width: "30%",
                    class: "pa-2 blue lighten-3 white--text"
                },

                {
                    text: "BRUTO",
                    align: "center",
                    sortable: false,
                    value: "bruto",
                    width: "20%",
                    class: "pa-2 blue lighten-3 white--text"
                },
                {
                    text: "DISKON",
                    align: "center",
                    sortable: false,
                    value: "discount",
                    width: "20%",
                    class: "pa-2 blue lighten-3 white--text"
                },
                {
                    text: "TOTAL",
                    align: "center",
                    sortable: false,
                    value: "total",
                    width: "20%",
                    class: "pa-2 blue lighten-3 white--text"
                }
            ],
            headerverifications: [
                {
                    text: "STATUS",
                    align: "center",
                    sortable: false,
                    value: "status",
                    width: "7%",
                    class: "pa-2 blue lighten-3 white--text"
                },
                {
                    text: "CITO",
                    align: "center",
                    sortable: false,
                    value: "cito",
                    width: "7%",
                    class: "pa-2 blue lighten-3 white--text"
                },
                {
                    text: "PEMERIKSAAN",
                    align: "left",
                    sortable: false,
                    value: "testname",
                    width: "15%",
                    class: "pa-2 blue lighten-3 white--text"
                },

                {
                    text: "BRUTO",
                    align: "center",
                    sortable: false,
                    value: "bruto",
                    width: "10%",
                    class: "pa-2 blue lighten-3 white--text"
                },
                {
                    text: "DISKON",
                    align: "center",
                    sortable: false,
                    value: "discount",
                    width: "10%",
                    class: "pa-2 blue lighten-3 white--text"
                },
                {
                    text: "TOTAL",
                    align: "center",
                    sortable: false,
                    value: "total",
                    width: "15%",
                    class: "pa-2 blue lighten-3 white--text"
                }
            ]
        }
    },
    computed: {
        testdialog: {
            get() {
                return this.$store.state.test.test_dialog
            },
            set(val) {
                this.$store.commit("test/update_test_dialog", val)
            }
        },
        selectedpatient(){
            return this.$store.state.patient.selected_patient
        },
        savestatusverification() {
            return this.$store.state.test.save_status_verification
        },
        vmsgsave() {
            return this.$store.state.test.save_message
        },
        vstatussave() {
            return this.$store.state.test.save_status
        },
        testsregistration() {
            return this.$store.state.test.test_registration
        },
        testsverification() {
            return this.$store.state.test.test_verification
        },
        testsall() {
            return this.$store.state.test.test_all
        },
        vtests() {
            return this.$store.state.test.tests
        },
        vtest: {
            get() {
                return this.$store.state.test.test
            },
            set(val) {
                this.$store.commit("test/update_test", val)
                console.log(val.promise)
                this.promiseright = val.promise
                this.$store.commit("test/update_show_promise_right", true)
            }
        },
        vcito: {
            get() {
                return this.$store.state.test.cito
            },
            set(val) {
                this.$store.commit("test/update_cito", val)
            }
        },
        citos() {
            return this.$store.state.test.citos
        },
        selected_cito: {
            get() {
                return this.$store.state.test.selected_cito
            },
            set(val) {
                this.$store.commit("test/update_selected_cito", val)
            }
        },
        xpromises() {
            return this.$store.state.test.promises
        },
        xpromise: {
            get() {
                return this.$store.state.test.promise
            },
            set(val) {
                this.$store.commit("test/update_promise", val)
            }
        },
        promisedialog: {
            get() {
                return this.$store.state.test.promise_dialog
            },
            set(val) {
                this.$store.commit("test/update_promise_dialog", val)
            }
        },
        msgdialogpromise: {
            get() {
                return this.$store.state.test.msg_promise_dialog
            },
            set(val) {
                this.$store.commit("test/update_msg_promise_dialog", val)
            }
        },
        showpromiseleft: {
            get() {
                return this.$store.state.test.show_promise_left
            },
            set(val) {
                this.$store.commit("test/update_show_promise_left", val)
            }
        },
        showpromiseright: {
            get() {
                return this.$store.state.test.show_promise_right
            },
            set(val) {
                this.$store.commit("test/update_show_promise_right", val)
            }
        },
        promiseleft: {
            get() {
                return this.$store.state.test.promise_left
            },
            set(val) {
                this.$store.commit("test/update_promise_left", val)
            }
        },
        getdatastatus: {
            get() {
                return this.$store.state.test.save_status
            },
            set(val) {
                this.$store.commit("test/update_save_status", val)
            }
        },
        showdropdowncito: {
            get() {
                return this.$store.state.test.show_cito_dropdown
            },
            set(val) {
                this.$store.commit("test/update_show_cito_dropdown", val)
            }
        },
    },
    methods: {
        showExist() {
            this.opendialogerror = true
            this.msgerror = "Pemeriksaan Sudah Ada"
        },
        totalRetur(row) {
            var charge = parseFloat(row.charge.value)
            var charge_amount = (charge / 100) * parseFloat(row.total)
            var totalaftercharge = parseFloat(row.total) - (charge_amount)
            return one_money(totalaftercharge)
        },
        convertMoney(money) {
            return one_money(money)
        },
        saveTestAdditionVerification() {
            let arrtest = this.$store.state.test.test_verification
            var prm = {
                orderid: this.$store.state.patient.selected_patient.orderid,
                tests: arrtest
            }
            this.$store.dispatch("test/savetestadditionalverification", prm)
        },
        changePromise(value) {
            let arrtest = this.$store.state.test.test_verification
            var idx = _.findIndex(arrtest, item => item.idx === value.idx)
            arrtest[idx]['promise'] = value.promise
            this.$store.commit("test/update_test_verification", arrtest)
        },
        closeDialogOrder() {
            if (this.savestatus !== 2) {
                this.$store.commit("test/update_test_dialog", false)
            }
            else {
                let arrpatient = this.$store.state.patient.patients
                var idx = _.findIndex(arrpatient, item => item.idx === this.$store.state.patient.selected_patient.idx)
                this.$store.dispatch("patient/search", {
                    search: this.$store.state.patient.search,
                    status: this.$store.state.patient.selected_status.value,
                    lastidx: idx
                })
                this.$store.commit("test/update_test_dialog", false)
            }
        },
        changeCharge(row, value) {
            var oldArr = this.$store.state.test.test_all
            var idx = _.findIndex(oldArr, row)
            oldArr[idx].active = 'Y'
            oldArr[idx].charge = value
            this.$store.commit("test/update_test_all", oldArr)
        },
        changeTestAll(row, value) {
            var oldArr = this.$store.state.test.test_all
            var idx = _.findIndex(oldArr, row)
            let nowstatus = row.status
            if (nowstatus == false && row.xid != '0') {
                this.$store.dispatch("test/checkstatus", row)
            }
            else {
                oldArr[idx].active = 'Y'
                oldArr[idx].status = nowstatus
                this.$store.commit("test/update_test_all", oldArr)
            }
        },
        changeTestCito(value) {
            this.$store.commit("test/update_selected_test", value)
            var prm = value
            prm.orderid = this.$store.state.patient.selected_patient.orderid
            this.$store.dispatch("test/getnewprice", prm)
        },
        thr_search_test: _.debounce(function () {
            var oldArr = this.$store.state.test.test_all
            this.promiseright = '-'

            var prm = {
                orderid: this.$store.state.patient.selected_patient.orderid,
                search: this.search_test,
                cito: this.$store.state.test.cito
            }
            this.$store.dispatch("test/searchtest", prm)
        }, 2000),
        addTestToAll() {
            var xtestall = this.$store.state.test.test_all
            var all_nattest = []
            var xchecked_test = _.filter(xtestall, function (o) { return o.status })
            xchecked_test = xchecked_test
            xchecked_test.forEach(function (test_a) {
                //console.log(test_a)
                //debugger

                test_a.nat_test.forEach(function (x_test_a) {
                    all_nattest.push(x_test_a)
                })
            })
            var xtestadditional = this.$store.state.test.test
            var is_exist = false
            var xnattest_now = xtestadditional.nat_test
            //debugger

            //console.log(xnattest_now)
            //debugger
            xnattest_now.forEach(function (nattest_a) {
                var idx_check = _.findIndex(all_nattest, function (o) { return parseInt(o) === parseInt(nattest_a) })
                if (idx_check !== -1)
                    is_exist = true
            })
            if (!is_exist) {
                console.log(xtestadditional.type)
                if (xtestadditional.type !== 'PXR' && xtestadditional.type !== 'PR') {
                    xtestall.push(xtestadditional)
                }
                else if (xtestadditional.type === 'PXR' || xtestadditional.type === 'PR') {
                    var child_test = xtestadditional.child_test
                    console.log(child_test)
                    child_test.forEach(function (entry) {
                        var new_test = {}
                        new_test.xid = '0'
                        new_test.nat_testid = entry.Nat_TestID
                        new_test.nat_test = entry.nat_test
                        new_test.is_packet = entry.is_packet
                        new_test.packet_id = entry.packet_id
                        new_test.type = entry.px_type
                        new_test.pxid = entry.T_TestID
                        new_test.pxcode = entry.pxcode
                        new_test.pxsascode = entry.T_TestSasCode
                        new_test.pxname = entry.T_TestSasCode + " " + entry.T_TestName
                        new_test.test_name = entry.T_TestName
                        new_test.isresult = entry.isresult
                        new_test.bruto = entry.T_PriceAmount
                        new_test.discountpersen = entry.T_PriceDisc
                        new_test.discountrp = entry.T_PriceDiscRp
                        new_test.discount = ((entry.T_PriceDisc / 100) * entry.T_PriceAmount) + entry.T_PriceDiscRp
                        //new_test.discount =  ( ( entry.T_PriceDisc / 100 ) * entry.T_PriceAmount ) -  entry.T_PriceDiscRp
                        new_test.total = entry.T_PriceAmount - new_test.discount
                        new_test.status = true
                        new_test.cito = false
                        new_test.cito_before = "N"
                        new_test.promise = ""
                        xtestall.push(new_test)
                    })
                }

                this.$store.commit("test/update_test_all", xtestall)
                this.$store.commit("test/update_test", {})
            }


        },
        addTestToAllxx() {

            var xtestall = this.$store.state.test.test_all
            var xtestadditional = this.$store.state.test.test
            //nat_testid tambaha/n
            let n_id = xtestadditional.nat_testid
            // all child_test 
            let x_idx = _.findIndex(xtestall, function (p) {
                console.log('n_id', n_id)
                console.log('px_nat_testid', p.nat_testid)
                return n_id == parseInt(p.nat_testid)
            });
            let flag_exists = false
            if (x_idx > -1) flag_exists = true
            if (!flag_exists) {
                if (xtestadditional.child_test != '') {
                    let add_ct = JSON.parse(xtestadditional.child_test)
                    for (let idx = 0; idx < add_ct.length; idx++) {
                        let add_ct_id = add_ct[idx].pxnat_testid
                        let x_idx = _.findIndex(xtestall, function (p) {
                            console.log('ct id', add_ct_id)
                            console.log('px_nat_testid', p.nat_testid)
                            return add_ct_id == parseInt(p.nat_testid)
                        });
                        if (x_idx > -1) {
                            flag_exists = true
                            break
                        }
                    }
                }

            }
            if (flag_exists) {
                this.showExist()
                return
            }
            var xnew = {
                xid: 0,
                type: xtestadditional.type,
                status: 'Y',
                cito: false,
                cito_before: 'N',
                testid: xtestadditional.pxid,
                testcode: xtestadditional.pxcode,
                testsascode: xtestadditional.pxsascode,
                testname: xtestadditional.pxname,
                isresult: xtestadditional.isresult,
                bruto: xtestadditional.bruto,
                discount: xtestadditional.discounttotal,
                discountpersen: xtestadditional.discount,
                discountrp: xtestadditional.discountrp,
                charge: { value: 0, label: '0%' },
                total: xtestadditional.total,
                nat_test: xtestadditional.nat_test,
                child_test: xtestadditional.child_test,
                is_packet: xtestadditional.is_packet,
                packet_id: xtestadditional.packet_id
            }

            if (xnew.type === 'PN' || xnew.type === 'PX') {
                xtestall.push(xnew)
                this.$store.commit("test/update_test_all", xtestall)
                this.$store.commit("test/update_test", {})
            }

            if (xnew.type === 'PXR') {
                xnew.orderid = this.$store.state.patient.selected_patient.orderid
                var params = { all: xtestall, new: xnew }
                this.$store.dispatch("test/getchildrenprofil", params)
            }


            if (xnew.type === 'SINGLE-PR') {
                var child_test = JSON.parse(xtestadditional.child_test)
                console.log(child_test)
                child_test.forEach(item => {
                    var xxnew = {
                        xid: 0,
                        type: 'SINGLE',
                        status: 'Y',
                        cito: false,
                        cito_before: 'N',
                        testid: item.pxid,
                        testcode: item.pxcode,
                        testsascode: item.pxsascode,
                        testname: item.pxname,
                        isresult: item.isresult,
                        bruto: item.bruto,
                        discount: item.discounttotal,
                        discountpersen: item.discount,
                        discountrp: item.discountrp,
                        charge: { value: 0, label: '0%' },
                        total: item.total
                    }
                    xtestall.push(xxnew)
                })

                this.$store.commit("test/update_test_all", xtestall)
                this.$store.commit("test/update_test", {})
            }

        },
        saveTestAdditional() {
            var prm = {
                orderid: this.$store.state.patient.selected_patient.orderid,
                testall: this.$store.state.test.test_all,
                selected_cito: this.$store.state.test.selected_cito
            }
            this.$store.dispatch("test/savetestadditional", prm)
        },
        checkPromiseOrderAdd() {
            this.$store.commit("test/update_act", 'additional')
            var prm = {
                orderid: this.$store.state.patient.selected_patient.orderid,
                tests: this.$store.state.test.test_all
            }
            this.$store.dispatch("test/checkpromisetests", prm)
        },
        checkPromiseOrderVerification() {
            this.$store.commit("test/update_act", 'verification')
            var prm = {
                orderid: this.$store.state.patient.selected_patient.orderid,
                tests: this.$store.state.test.test_verification
            }
            this.$store.dispatch("test/checkpromisetests", prm)
        },

        openDialogPromise(value) {
            this.error_promise_dialog = false
            this.selected_promise = {}
            this.$store.commit("test/update_selected_detail_verification", value)
            this.promisedialog = true
        },
        addPromiseTodetail() {
            if (this.selected_promise) {
                let arrtest = this.$store.state.test.test_verification
                console.log(arrtest)
                var idx = _.findIndex(arrtest, item => item.idx === this.$store.state.test.selected_detail_verification.idx)
                arrtest[idx]['promise'] = this.selected_promise.id
                arrtest[idx]['promisename'] = this.selected_promise.name
                this.$store.commit("test/update_test_verification", arrtest)
                this.promisedialog = false
            }
            else {
                this.error_promise_dialog = true
            }

        },
        closeDialogPromise() {
            var act = this.$store.state.test.act
            this.promisedialog = false
            if (act === 'additional')
                this.saveTestAdditional()
            else
                this.saveTestAdditionVerification()
        }
    },
    watch: {
        search_test(val, old) {
            if (val == old) return
            if (!val) return
            if (val == '') return
            if (val.length < 1) return
            if (this.$store.state.test.autocomplete_status == 1) return
            this.thr_search_test()
        }
    }
}
</script>
