<template>
    <v-layout column pb-2>
        <v-dialog v-model="dialog_alert_msg" width="60%">


            <v-card>
                <v-card-title class="subtitle-1 white--text" style="background:#ee7777">
                    PEMBERITAHUAN !
                </v-card-title>

                <v-card-text v-html="alert_msg">

                </v-card-text>

                <v-divider></v-divider>

                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="grey" flat @click="dialog_alert_msg = false">
                        OK ... saya mengerti
                    </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <v-card class="pa-1 mb-0">
            <v-layout align-center row>
                <v-flex xs12>
                    <v-card tile class="pa-2" flat color="#64b2cd">
                        <h5 style="color:#fff" class="subtitle-1 font-weight-bold"></h5>
                    </v-card>
                </v-flex>
            </v-layout>
            <v-layout row>
                <v-flex pt-1 pl-2 pr-1 xs12>
                    <table>
                        <tr style="background:#64b2cd">
                            <th style="width:25%;font-weight:bold;font-size:12px;color:#fff" class="text-md-center pt-2 pb-2">PASIEN</th>
                            <th style="width:25%;font-weight:bold;font-size:12px;color:#fff" class="text-md-center pt-2 pb-2">PEMERIKSAAN</th>
                            <th style="width:25%;font-weight:bold;font-size:12px;color:#fff" class="text-md-center pt-2 pb-2">TOTAL</th>
                        </tr>
                        <tr v-if="s_patient.length === 0">
                            <td class="pa-2 text-xs-center caption" colspan="3">Belum ada order pemeriksaan</td>
                        </tr>
                        <tr v-if="s_patient.length > 0" style="font-size:12px" v-for="(t,i_patient) in s_patient" v-bind:key="t.M_PatientID">


                            <td class="text-md-left pl-3">
                                <p class="mb-1">{{ t.M_PatientName}}</p>
                            </td>
                            <td>
                                <v-layout row wrap>
                                    <v-flex xs12 v-for="(p, i) in t.test" v-bind:key="i.T_TestID">
                                        <!-- <v-btn style="min-width:160px;" class="mt-1 mb-2" small :color="is_checked(t.M_PatientID,p.T_TestID) === 'N' ? 'yellow' : 'green'"
                                            @click="changePosition(t,i,p)">{{ p.T_TestName }}</v-btn> -->

                                        <v-btn @click="changePosition(t,i,p)" v-if="is_checked(t.M_PatientID,p.T_TestID) === 'N'" small color="error">
                                            <v-icon left>close</v-icon>{{p.T_TestName}}</v-btn>
                                        <v-btn @click="changePosition(t,i,p)" v-if="is_checked(t.M_PatientID,p.T_TestID) === 'Y'" small color="success">
                                            <v-icon left>check</v-icon>{{p.T_TestName}}</v-btn>
                                    </v-flex>
                                </v-layout>
                            </td>
                            <td class="text-md-right mono font-weight-black pr-2">
                                <p class="mb-1">{{ one_money(t.subtotal)}}</p>
                            </td>
                        </tr>

                        <tfoot>
                            <tr>
                                <th style="background:#3c70a4;color:#fff" class="text-md-right pr-5 pt-2 pb-2"></th>
                                <th style="background:#3c70a4;color:#fff" class="text-md-left pl-3 pt-2 pb-2">SUB TOTAL</th>
                                <th style="background:#3c70a4;color:#fff" class="text-md-right pr-2 pt-2 pb-2">{{one_money(subtotal)}}</th>
                            </tr>
                            <tr>
                                <th style="background:#3c70a4;color:#fff" class="text-md-right pr-5 pt-2 pb-2"></th>
                                <th style="background:#3c70a4;color:#fff" class="text-md-left pl-3 pt-2 pb-2">BIAYA HOME SERVICE</th>
                                <th style="background:#3c70a4;color:#fff" class="text-md-right pr-2 pt-2 pb-2">{{one_money(hs_price)}}</th>
                            </tr>
                            <tr>
                                <th style="background:#3c70a4;color:#fff" class="text-md-right pr-5 pt-2 pb-2"></th>
                                <th style="background:#3c70a4;color:#fff" class="text-md-left pl-3 pt-2 pb-2">BIAYA MINIMAL TRANSAKSI</th>
                                <th style="background:#3c70a4;color:#fff" class="text-md-right pr-2 pt-2 pb-2">{{one_money(hs_priceminimal)}}</th>
                            </tr>
                            <tr>
                                <th style="background:#3c70a4;color:#fff" class="text-md-right pr-5 pt-2 pb-2"></th>
                                <th style="background:#3c70a4;color:#fff" class="text-md-left pl-3 pt-2 pb-2">BIAYA TAMBAHAN</th>
                                <th style="background:#3c70a4;color:#fff" class="text-md-right pr-2 pt-2 pb-2">{{one_money(hs_priceother)}}</th>
                            </tr>
                            <tr>
                                <th style="background:#3c70a4;color:#fff" class="text-md-right pr-5 pt-2 pb-2"></th>
                                <th style="background:#3c70a4;color:#fff" class="text-md-left pl-3 pt-2 pb-2">TOTAL</th>
                                <th style="background:#3c70a4;color:#fff" class="text-md-right pr-2 pt-2 pb-2">{{ one_money(total)}}</th>
                            </tr>
                        </tfoot>
                    </table>
                    <v-card flat>
                        <v-layout row>
                            <v-flex xs4>
                                <v-btn block v-if="btn_save_enabled && !loading_process" title="simpan order" @click="save_order()" class="mb-1 mt-2 ml-0 mr-0"
                                    dark color="#3c70a4">
                                    simpan
                                </v-btn>
                                <v-btn block v-if="!btn_save_enabled" title="simpan order" @click="check_msg_alert()" class="mb-1 mt-2 ml-0 mr-0 grey darken-1"
                                    dark>
                                    simpan
                                </v-btn>
                            </v-flex>
                        </v-layout>
                    </v-card>
                </v-flex>
            </v-layout>
        </v-card>
        <one-dialog-loading></one-dialog-loading>
    </v-layout>
</template>

<style scoped>
    table {
        font-family: arial, sans-serif;
        border-collapse: collapse;
        width: 100%;
        background: white;
        border: 0px;
    }

    th,
    td {
        border: 1px solid black;
        border-collapse: collapse;
        padding-top: 2px;
        padding-bottom: 2px;
    }

    table>tr>td {
        padding: 8px;
    }

    table>tr>td:first {
        padding-left: 15px !important;
    }

    .vintage-text {
        text-shadow: 0px -2px 0px #fff, 0px 2px 3px #fff;
    }

    .v-input--selection-controls {
        margin-top: 0px;
        padding-top: 0px;
    }
</style>
<script>
    module.exports = {
        components: {
            'one-date-picker': httpVueLoader('../../common/oneDatePicker.vue'),
            'one-dialog-print': httpVueLoader('../../common/oneDialogPrintX.vue'),
            'one-dialog-loading': httpVueLoader('../../common/oneDialogLoading.vue')
        },
        data() {
            return {
                query: "",
                items: [],
                dialog_alert_msg: false,
                alert_msg: '',
                openprint: false,
                urlprint: '',
                printtitle: '',
                printwidth: 600,
                formatreport: 'pdf',
                page: 1,
                reflextestname: '',
                reflextestcode: '',
                search_reflextest: '',
                search_testmandatory: '',
                reflextestnameRules: [
                    v => !!v || 'Argument 1 harus dipilih'
                ],
                kondisitestRules: [
                    v => !!v || 'Kondisi harus dipilih'
                ],
                relasiRules: [
                    v => !!v || 'Relasi harus dipilih'
                ],
                headers: [{
                        text: "PASIEN",
                        align: "LEFT",
                        sortable: false,
                        value: "name",
                        width: "20%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "PEMERIKSAAN",
                        align: "LEFT",
                        sortable: false,
                        value: "lab",
                        width: "40%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "TOTAL",
                        align: "LEFT",
                        sortable: false,
                        value: "lab",
                        width: "20%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "AKSI",
                        align: "LEFT",
                        sortable: false,
                        value: "status",
                        width: "20%",
                        class: "pa-2 blue lighten-3 white--text"
                    }
                ],
                pagination: {
                    descending: false,
                    page: 1,
                    rowsPerPage: 100,
                    sortBy: 'trx_id ASC',
                    totalItems: this.$store.state.review.total_transactions
                },
                statuss: [{
                        waktu: '16-07-2019 10:00',
                        status: 'Order Dibuat',
                        reflextest: 'Nagita Slavina'
                    },
                    {
                        waktu: '16-07-2019 11:00',
                        status: 'Order Dikirim',
                        reflextest: 'Nagita Slavina'
                    },
                    {
                        waktu: '16-07-2019 12:00',
                        status: 'Order dikonfirmasi',
                        reflextest: 'Chacha Frederica'
                    },
                    {
                        waktu: '16-07-2019 13:00',
                        status: 'Telah dibuat Surat Jalan',
                        reflextest: 'Chacha Frederica'
                    },
                    {
                        waktu: '16-07-2019 14:00',
                        status: 'Kurir akan mengambil sampel',
                        reflextest: 'Satria Subiantoro'
                    }
                ],
                color: "success",
                validreflextest: false,
                xid: 0,
                isinput: "N",
                countprice: 0,
                reflextestdetailid: 0,
                reflextestdetailname: "",
                reflextestlang: "",
                reflextesttext: "",
                isadvance: "N",
                reflextestformatadvance: "",
                issenttomultirule: "N",
                menustartdate: false,
                menuenddate: false,
                date: new Date().toISOString().substr(0, 10),

                dialogdeletealertreflextest: false,
                dialogverifyalertreflextest: false,
                dialogreleasealertreflextest: false,
                alertverify: false,
                dialogunreleasealertreflextest: false,
                dialogunverifyalertreflextest: false,
                msgalertreflextest: ""
            };
        },
        mounted() {
            this.$store.dispatch("patient/getconfig")
            // this.$store.dispatch("review/selectcondition")
            // this.$store.dispatch("review/selectconditionmandatory")
        },
        computed: {
            tab_active() {
                return this.$store.state.tab_active;
            },
            s_patient() {
                return this.$store.state.patient.s_patient
            },
            s_test() {
                return this.$store.state.review.s_test
            },
            hs_config() {
                return this.$store.state.patient.hs_config
            },
            hs_price() {
                return this.$store.state.patient.hs_price
            },
            hs_priceminimal: {
                get() {
                    return this.$store.state.patient.hs_priceminimal
                },
                set(val) {
                    this.$store.commit("patient/update_hs_priceminimal", val)
                }
            },
            hs_priceother: {
                get() {
                    return this.$store.state.patient.hs_priceother
                },
                set(val) {
                    this.$store.commit("patient/update_hs_priceother", val)
                }
            },
            subtotal() {
                let tests = this.s_patient
                subtotal = 0
                testvisit = 0
                testcovid = 0
                tests.forEach(function (t, idx) {
                    let price = t.subtotal
                    let visit = t.testvisit
                    let covid = t.testcovid
                    subtotal += price
                    testvisit += visit
                    testcovid += covid
                })
                let minimalorder = this.hs_config.HS_ConfigMinOrder

                if(subtotal < minimalorder){
                    this.hs_priceminimal = minimalorder - subtotal
                }else{
                    this.hs_priceminimal =  0
                }
                if(testcovid > 0 && testvisit === 0){
                    let covid = this.hs_config.HS_ConfigAPDFee
                    this.hs_priceother = covid
                }else if(testcovid === 0 && testvisit > 0){
                    let visit = (this.hs_config.HS_ConfigSecondVisitPercent/100) * this.hs_price
                    this.hs_priceother = visit

                }else if(testcovid > 0 && testvisit > 0){
                    let covid = this.hs_config.HS_ConfigAPDFee
                    let visit = (this.hs_config.HS_ConfigSecondVisitPercent/100) * this.hs_price
                    this.hs_priceother = covid
                }else if(testcovid === 0 && testvisit === 0){
                    this.hs_priceother = 0
                }
                return subtotal
            },
            total() {
                return parseInt(this.subtotal) + parseInt(this.hs_price) + parseInt(this.hs_priceminimal) + parseInt(this.hs_priceother)
            },
            loading_process() {
                return this.$store.state.order.loading_process
            },
            btn_save_enabled() {

                var deliveries = this.$store.state.delivery.data_deliveries
                var checked_deliveries = _.filter(deliveries, function (o) {
                    return o.chex === 'Y'
                })
                var hs_time = this.$store.state.patient.selected_hs_time.M_RegTimeName
                var hs_hp = this.$store.state.patient.hs_hp

                if (checked_deliveries.length === 0)
                    return false

             /*   var validBDate = true
                var bDate = moment(this.$store.state.patient.hs_date, 'DDMMYYYY', true)
                var bvalidDate = bDate.isValid()
                console.log(bvalidDate)
                if (!bvalidDate) {
                    validBDate = bvalidDate

                } else {
                    var xnow = moment(new Date(), 'DDMMYYYY')
                    validBDate = moment(this.$store.state.patient.hs_date, 'DDMMYYYY').isAfter(xnow)

                }
                if (!validBDate)
                    return false
*/
                if (parseInt(this.hs_price) === 0 || this.hs_price === null)
                    return false

                if (hs_hp === '' || hs_hp === '-' || hs_hp === ' ')
                    return false

                if(!hs_time)    
                return false
                return true;

                
            },
            dialogreflextest() {
                return this.$store.state.review.dialog_form_reflextest
            },
            xerrors() {
                return this.$store.state.review.errors
            },
            isLoading() {
                return this.$store.state.review.search_status == 1
            },
            xkondisiheaders() {
                return this.$store.state.review.kondisiheaders
            },
            xkondisiheader: {
                get() {
                    return this.$store.state.review.selected_kondisiheader
                },
                set(val) {
                    this.$store.commit("review/update_selected_kondisiheader", val)
                }
            },
            xkondisitests() {
                return this.$store.state.review.kondisitests
            },
            xkondisitest: {
                get() {
                    return this.$store.state.review.selected_kondisitest
                },
                set(val) {
                    this.$store.commit("review/update_selected_kondisitest", val)
                }
            },
            xkondisimandatorys() {
                return this.$store.state.review.kondisimandatorys
            },
            xkondisimandatory: {
                get() {
                    return this.$store.state.review.selected_kondisimandatory
                },
                set(val) {
                    this.$store.commit("review/update_selected_kondisimandatory", val)
                }
            },
            xrelasis() {
                return this.$store.state.review.relasis
            },
            xrelasi: {
                get() {
                    return this.$store.state.review.selected_relasi
                },
                set(val) {
                    this.$store.commit("review/update_selected_relasi", val)
                }
            },
            xorderautotests() {
                return this.$store.state.review.orderautotests
            },
            xorderautotest: {
                get() {
                    return this.$store.state.review.orderautotest
                },
                set(val) {
                    this.$store.commit("review/update_orderautotest", val)
                    this.$store.dispatch("review/selectconditionmandatory", {
                        isquantitative: val.Nat_TestIsQuantitative
                    })
                }
            },
            xordertests() {
                return this.$store.state.review.ordertests
            },
            startComputedDateFormatted() {
                return this.formatDate(this.xstartdate)
            },
            xstartdate: {
                get() {
                    return this.$store.state.review.startdate
                },
                set(val) {
                    this.$store.commit("review/update_startdate", val)
                }
            },
            endComputedDateFormatted() {
                return this.formatDate(this.xenddate)
            },
            xenddate: {
                get() {
                    return this.$store.state.review.enddate
                },
                set(val) {
                    this.$store.commit("review/update_enddate", val)
                }
            },
            xbases() {
                return this.$store.state.review.bases
            },
            xbase: {
                get() {
                    return this.$store.state.review.base
                },
                set(val) {
                    this.$store.commit("review/update_base", val)
                }

            },
            xomzettypes() {
                return this.$store.state.review.omzettypes
            },
            xomzettype: {
                get() {
                    return this.$store.state.review.omzettype
                },
                set(val) {
                    this.$store.commit("review/update_omzettype", val)
                }

            },
            xreflextesttypes() {
                return this.$store.state.review.reflextesttypes
            },
            xreflextesttype: {
                get() {
                    return this.$store.state.review.reflextesttype
                },
                set(val) {
                    this.$store.commit("review/update_reflextesttype", val)
                }

            },
            xagingtypes() {
                return this.$store.state.review.agingtypes
            },
            xagingtype: {
                get() {
                    return this.$store.state.review.agingtype
                },
                set(val) {
                    this.$store.commit("review/update_agingtype", val)
                }

            },
            xstatuss() {
                return this.$store.state.review.statuss
            },
            dialogstatusorder() {
                return this.$store.state.review.dialog_status_order
            },
            xautoreflextests() {
                return this.$store.state.review.autoreflextests
            },
            xautoreflextest: {
                get() {
                    return this.$store.state.review.autoreflextest
                },
                set(val) {
                    this.$store.commit("review/update_autoreflextest", val)
                    this.$store.dispatch("review/selectconditiontest", {
                        isquantitative: val.Nat_TestIsQuantitative
                    })
                }
            }
        },
        methods: {
            is_checked(M_PatientID, T_TestID) {
                return this.$store.state.checked_test[M_PatientID][T_TestID];
            },
            checkError(value) {
                var errors = this.$store.state.review.errors
                if (errors.includes(value)) {
                    return true
                } else {
                    return false
                }
            },
            save_order() {
                if (!window.one_token()) {
                    this.$store.commit('update_message_error',
                        'Maaf, koneksi Anda sempat terputus silahkan Log Out dan Login kembali')
                    this.$store.commit('update_dialog_error', true)
                    return
                }
                // Loading
                this.$store.commit('order/update_loading_process', true)
                this.$store.commit('update_dialog_loading', true)
                this.$store.dispatch("order/save")
                console.log(ctest)

                return
            },
            check_msg_alert() {
                // console.log(this.$store.state.patient.selected_patient.M_PatientID)
                var msg = ""
           /*      var validBDate = true
                var bDate = moment(this.$store.state.patient.hs_date, 'DDMMYYYY', true)
                var bvalidDate = bDate.isValid()
                console.log(bvalidDate)
               if (!bvalidDate) {
                    validBDate = bvalidDate
                    msg = msg + "<p class='caption mono mb-1'>- Tanggal home service belum diisi </p>"
                } else {
                    var xnow = moment(new Date(), 'DDMMYYYY')
                    validBDate = moment(this.$store.state.patient.hs_date, 'DDMMYYYY').isAfter(xnow)
                    if (!validBDate)
                        msg = msg + "<p class='caption mono mb-1'>- Tanggal home service harus > hari ini </p>"
                }
*/
                if (!this.$store.state.patient.selected_patient.M_PatientID)
                    msg = msg +
                    "<p class='caption mono mb-1'>- Ibarat ibu-ibu kalo belok tanpa lampu sein, bingung dong kalo belum dipilih pasien</p>"


                var xDate = moment(this.$store.state.patient.selected_patient.M_PatientDOB, 'YYYY-MM-DD', true)
                var xDatenow = moment(new Date(), 'YYYY-MM-DD')
                var validDOB = xDate.isSameOrBefore(xDatenow)
                if (!validDOB)
                    msg = msg + "<p class='caption mono mb-1'>- Apa pasien datang dari masa depan ? </p>"


                var deliveries = this.$store.state.delivery.data_deliveries
                var hs_time = this.$store.state.patient.selected_hs_time.M_RegTimeName
                var hs_hp = this.$store.state.patient.hs_hp

                if (!hs_time)
                    msg = msg + "<p class='caption mono mb-1'>- Ayo pilih dulu jam Home Service nya ? </p>" 

                if (hs_hp === '' || hs_hp === '-' || hs_hp === ' ')
                    msg = msg + "<p class='caption mono mb-1'>- Jangan lupa nomor HP nya .... </p>" 

                var checked_deliveries = _.filter(deliveries, function (o) {
                    return o.chex === 'Y'
                })

                if (checked_deliveries.length === 0)
                    msg = msg +
                    "<p class='caption mono mb-1'>- Jangan lupa ya ... pengiriman hasil dipilih salah satu</p>"
                
                if (parseInt(this.hs_price) === 0 || this.hs_price === null)
                    msg = msg +
                    "<p class='caption mono mb-1'>- Jangan lupa ya ... cek alamat & biaya Home Service</p>"
                console.log('hs_price')   
                console.log(this.hs_price)
                this.alert_msg = msg
                this.dialog_alert_msg = true
            },
            one_money(p) {
                return window.one_money(p)
            },
            updateDialogFormReflexTest() {
                this.$store.commit("review/update_dialog_form_reflextest", false)
            },
            updateDialogStatusOrder() {
                this.$store.commit("review/update_dialog_status_order", false)
            },
            setStatusOrder(val) {
                this.$store.commit("review/update_reflextests", {})
                this.$store.commit("review/update_dialog_status_order", true)
                this.$store.commit("review/update_statuss", val.statuss)
            },
            changePosition(t, i, p) {
                let sel = this.s_patient
                let idx = _.findIndex(sel, function (p) {
                    return p.M_PatientID == t.M_PatientID
                });
                let xc = this.$store.state.checked_test;

                if (xc[t.M_PatientID][p.T_TestID] == 'Y') {
                    xc[t.M_PatientID][p.T_TestID] = 'N';
                    let price = sel[idx].test[i].T_PriceAmount - sel[idx].test[i].T_PriceDisc / 100 * sel[idx].test[
                            i].T_PriceAmount -
                        sel[idx].test[i].T_PriceDiscRp
                    let covid = parseInt(sel[idx].test[i].testcovid) 
                    let visit = parseInt(sel[idx].test[i].testvisit)   
                    sel[idx].subtotal = parseInt(sel[idx].subtotal) - parseInt(price)
                    sel[idx].testcovid = parseInt(sel[idx].testcovid) - covid
                    sel[idx].testvisit = parseInt(sel[idx].testvisit) - visit

                    let s_test = this.$store.state.review.s_test
                    s_test.forEach(function (v, idx) {
                        if (v.patientid == t.patientidhs && v.testid == p.T_TestID) {
                            s_test.splice(idx, 1)
                        }
                    })
                    this.$store.commit("review/update_s_test", s_test)
                    console.log(s_test)
                } else {
                    xc[t.M_PatientID][p.T_TestID] = 'Y';
                    let price = sel[idx].test[i].T_PriceAmount - sel[idx].test[i].T_PriceDisc / 100 * sel[idx].test[
                            i].T_PriceAmount -
                        sel[idx].test[i].T_PriceDiscRp
                    let covid = parseInt(sel[idx].test[i].testcovid) 
                    let visit = parseInt(sel[idx].test[i].testvisit) 
                    sel[idx].subtotal = parseInt(sel[idx].subtotal) + parseInt(price)
                    sel[idx].testcovid = parseInt(sel[idx].testcovid) + covid
                    sel[idx].testvisit = parseInt(sel[idx].testvisit) + visit

                    let s_test = this.$store.state.review.s_test
                    s_test.push({
                        patientid: t.patientidhs,
                        testid: p.T_TestID,
                        T_TestID: p.T_TestID,
                        T_TestName: p.T_TestName,
                        T_PriceAmount: p.T_PriceAmount,
                        T_PriceDisc: p.T_PriceDisc,
                        T_PriceDiscRp: p.T_PriceDiscRp,
                        T_PriceSubTotal: p.T_PriceSubTotal,
                        T_PriceTotal: p.T_PriceTotal,
                        testvisit: p.testvisit,
                        testcovid: p.testcovid
                    })
                    this.$store.commit("review/update_s_test", s_test)
                    console.log(s_test)
                }
                this.$store.state.checked_test = xc;

                // sel[idx].test[i].IsChecked = xc[idx][i]
                console.log(this.s_patient)

            },
            doPriceList(val) {
                console.log(location)
                var id = val.id
                location.replace("/one-ui/test/vuex/one-md-price/" + "?id=" + id)
            },
            doPrice() {
                console.log(location)
                var id = this.xid
                location.replace("/one-ui/test/vuex/one-md-price/" + "?id=" + id)
            },
            addTest() {
                var oldArr = this.$store.state.review.ordertests
                var arrTest = this.$store.state.review.orderautotest
                var arrValue = this.$store.state.review.selected_kondisimandatory
                var arrAll = _.assignIn(arrTest, arrValue)
                oldArr.push(_.assignIn(arrTest, arrValue))
                this.search_testmandatory = ''
                this.$store.commit("review/update_orderautotests", [])
                this.$store.commit("review/update_orderautotest", {})
                this.$store.commit("review/update_selected_kondisimandatory", {})

            },
            deleteDataPx(idx) {
                var inx = parseInt(idx);
                var xrow = this.$store.state.review.ordertests[inx]
                if (parseInt(xrow.xid) !== 0) {
                    var xdeltest = this.$store.state.review.deleted_ordertests
                    xdeltest.push(xrow)
                    this.$store.commit("review/update_deleted_ordertests", xdeltest)
                }
                var oldArr = this.$store.state.review.ordertests
                oldArr.splice(inx, 1)
                this.$store.commit("review/update_ordertests", oldArr)
            },
            thr_search_reflextest: _.debounce(function () {
                this.$store.dispatch("review/searchreflextest", {
                    tes: this.search_reflextest
                })
            }, 2000),
            thr_search_testmandatory: _.debounce(function () {
                this.$store.dispatch("review/searchmandatorytest", {
                    tes: this.search_testmandatory
                })
            }, 2000),
            doPrint() {
                this.printwidth = 1028
                this.printtitle = ""
                let reflextest = one_reflextest()
                var id = this.xid
                var d = new Date();
                var n = d.getTime()
                var rptname = 'test_price'
                var formatrpt = this.formatreport

                this.urlprint = "/birt/run?__report=report/one/md/" + rptname + ".rptdesign&__format=" +
                    formatrpt + "&PReflexTestID=" + id + "&reflextestdetailname=" + review.M_ReflexTestReflexTestname +
                    "&tm=" + n

                this.openprint = true
            },
            closePrint() {
                this.openprint = false
            },
            openFormReflexTest(val) {
                this.$store.commit("review/update_errors", [])
                this.$store.commit("review/update_ordertests", [])
                this.xid = val
                this.reflextestname = ''
                this.reflextestcode = ''
                this.$store.commit("review/update_autoreflextests", [])
                this.$store.commit("review/update_autoreflextest", {})
                this.$store.commit("review/update_selected_kondisitest", {})
                this.$store.commit("review/update_dialog_form_reflextest", true)
            },
            editFormReflexTest(data) {
                this.$store.commit("review/update_errors", [])
                this.xid = data.id
                this.$store.commit("review/update_autoreflextests", [{
                    Nat_TestID: data.Nat_TestID,
                    Nat_TestName: data.Nat_TestName
                }])
                this.$store.commit("review/update_autoreflextest", {
                    Nat_TestID: data.Nat_TestID,
                    Nat_TestName: data.Nat_TestName
                })
                this.$store.dispatch("review/selectconditionheader", {
                    isquantitative: data.headerIsQuantitative
                })
                this.$store.commit("review/update_selected_kondisiheader", {
                    Nat_ConditionID: data.kondisiheaderid,
                    Nat_ConditionName: data.kondisiheader
                })
                this.$store.dispatch("review/selectconditiontest", {
                    isquantitative: data.testIsQuantitative
                })
                this.$store.commit("review/update_selected_kondisitest", {
                    Nat_ConditionID: data.Nat_ConditionID,
                    Nat_ConditionName: data.Nat_ConditionName
                })
                this.$store.commit("review/update_ordertests", data.mandatorys)
                this.$store.commit("review/update_act", 'edit')
                this.$store.commit("review/update_dialog_form_reflextest", true)
            },
            checkError(value) {
                var errors = this.$store.state.review.errors
                if (errors.includes(value)) {
                    return true
                } else {
                    return false
                }
            },
            saveFormReflexTest() {
                this.$store.commit("review/update_errors", [])
                var errors = this.$store.state.review.errors
                if (_.isEmpty(this.xautoreflextest)) {
                    errors.push("requiretest")
                }
                if (_.isEmpty(this.xkondisitest)) {
                    errors.push("requirecondition")
                }
                if (errors.length === 0) {
                    this.$store.dispatch("review/save", {
                        xid: this.xid,
                        multiruleid: 0,
                        nattestid: this.$store.state.review.autoreview.Nat_TestID,
                        nattestname: this.$store.state.review.autoreview.Nat_TestName,
                        conditionid: this.$store.state.review.selected_kondisitest.Nat_ConditionID,
                        conditionname: this.$store.state.review.selected_kondisitest.Nat_ConditionName,
                        ordertests: this.xordertests,
                        deleted_ordertests: this.$store.state.review.deleted_ordertests
                    })
                }

            },
            set_date2(x) {
                this.startdate = x.new_date
            },
            set_date3(x) {
                this.enddate = x.new_date
            },
            formatDate(date) {
                if (!date) return null

                const [year, month, day] = date.split('-')
                return `${day}-${month}-${year}`
            },
            deFormatedDate(date) {
                if (!date) return null

                const [day, month, year] = date.split('-')
                return `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`
            },
            updateAlert_success(val) {
                this.$store.commit("review/update_alert_success", val)
            },
            deleteFormReflexTest(data) {
                this.xid = data.id

                this.msgalertreflextest = "Yakin, mau hapus reflextest ini?"
                this.dialogdeletealertreflextest = true
            },
            closeDeleteAlertReflexTest() {
                this.$store.dispatch("review/delete", {
                    xid: this.xid,
                    multiruleid: 0,
                    nattestid: this.$store.state.review.autoreview.Nat_TestID,
                    nattestname: this.$store.state.review.autoreview.Nat_TestName,
                    conditionid: this.$store.state.review.selected_kondisitest.Nat_ConditionID,
                    conditionname: this.$store.state.review.selected_kondisitest.Nat_ConditionName
                })
                this.dialogdeletealertreflextest = false
            }
        },
        watch: {
            search_reflextest(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.review.update_autocomplete_status == 1) return
                this.thr_search_reflextest()
            },
            search_testmandatory(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.review.update_autocomplete_status == 1) return
                this.thr_search_testmandatory()
            }
        }
    }
</script>