<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="doOrderMethode()">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="dialogformmethode" persistent max-width="650">
            <v-card>
                <v-card-title>
                    <span class="headline">Form Methode Pemeriksaan {{xmethodeprioritys.Nat_TestName}}</span>
                </v-card-title>
                <v-card-text class="pt-0 pb-0">
                    <v-layout wrap>
                        <v-flex xs12>
                            <v-select item-text="Nat_MethodeName" readonly return-object :items="xmethodees" v-model="xmethode" label="Metode"></v-select>
                        </v-flex>
                        <v-flex xs12>
                            <v-text-field label="Prioritas" v-model="xpriority"></v-text-field>
                        </v-flex>
                        <v-flex xs12>
                            <span>Instrument : {{this.xinstrument}}</span>
                        </v-flex>
                        <v-flex xs12>
                            <v-btn class="mt-1 mb-2" v-for="(vst,idx) in xorderdays" :key="vst.id" @click="changeDay(idx)" small :color="vst.isday === 'N' ? 'yellow' : 'green'">{{vst.M_DayOfWeekName}}

                            </v-btn>
                        </v-flex>
                    </v-layout>
                    <v-layout row>
                    </v-layout>
                    <v-divider></v-divider>
                </v-card-text>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="blue darken-1" flat @click="dialogformmethode = false">Tutup</v-btn>
                    <v-btn v-if="xactpat === 'new'" color="blue darken-1" flat @click="saveNewMethode()">Simpan</v-btn>
                    <v-btn v-if="xactpat === 'edit'" color="blue darken-1" flat @click="saveEditMethode()">Simpan</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <v-card>
            <v-layout row>
                <v-flex xs12>
                    <v-subheader red--text text--lighten-1> DAFTAR METHODE PEMERIKSAAN {{xmethodeprioritys.Nat_TestName}}
                        <v-flex text-md-right>
                        </v-flex>
                    </v-subheader>
                    <v-divider></v-divider>

                    <v-layout row wrap>
                        <v-flex xs12 pa-2>
                            <v-data-table :headers="headers" :items="xmethodes" :loading="isLoading" hide-actions class="elevation-1">
                                <template slot="items" slot-scope="props">

                                    <td class="text-xs-left pa-2">{{ props.item.Nat_MethodeName}}</td>
                                    <td class="text-xs-left pa-2">{{ props.item.Nat_InstrumentName}}</td>
                                    <td class="text-xs-left pa-2">{{ props.item.M_DayOfWeekName}}
                                    </td>
                                    <td class="text-xs-left pa-2">{{ props.item.M_MethodePriorityNumber}}
                                    </td>
                                    <td class="text-xs-center pa-2">
                                        <v-icon class="ml-3" color="primary" @click="editMethode(props.item)">edit</v-icon>
                                    </td>
                                </template>
                            </v-data-table>

                        </v-flex>
                    </v-layout>

                    <v-layout row wrap>
                        <v-flex xs12 pa-2>
                        </v-flex>
                    </v-layout>

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

<style scoped>
</style>

<script>
    module.exports = {
        data: () => ({
            oldlabel: '',
            search_day: '',
            xpriority: '',
            xinstrument:'',
            headers: [

                {
                    text: "METODE",
                    align: "left",
                    sortable: false,
                    value: "mr",
                    width: "20%",
                    class: "pa-1 blue lighten-3 white--text"
                },
                {
                    text: "INSTRUMENT",
                    align: "left",
                    sortable: false,
                    value: "mr",
                    width: "20%",
                    class: "pa-1 blue lighten-3 white--text"
                },
                {
                    text: "HARI",
                    align: "left",
                    sortable: false,
                    value: "lab",
                    width: "30%",
                    class: "pa-1 blue lighten-3 white--text"
                },
                {
                    text: "PRIORITAS",
                    align: "left",
                    sortable: false,
                    value: "lab",
                    width: "10%",
                    class: "pa-1 blue lighten-3 white--text"
                },
                {
                    text: "AKSI",
                    align: "center",
                    sortable: false,
                    value: "action",
                    width: "10%",
                    class: "pa-1 blue lighten-3 white--text"
                }
            ]
        }),
        computed: {
            dialogconfirmationdeletepat: {
                get() {
                    return this.$store.state.methodepriority.dialog_confirmation_delete_meth
                },
                set(val) {
                    this.$store.commit("methodepriority/update_dialog_confirmation_delete_meth", val)
                }
            },
            xmethodeprioritys() {
                return this.$store.state.methodepriority.selected_methodepriority
            },
            msgconfirmationdeletepat() {
                return this.$store.state.methodepriority.msg_confirmation_delete_meth
            },
            dialogconfirmationsend: {
                get() {
                    return this.$store.state.methodepriority.dialog_confirmation_send
                },
                set(val) {
                    this.$store.commit("methodepriority/update_dialog_confirmation_send", val)
                }
            },
            msgconfirmationsend() {
                return this.$store.state.methodepriority.msg_confirmation_send
            },
            xact() {
                return this.$store.state.methodepriority.act
            },
            xactpat() {
                return this.$store.state.methodepriority.act_meth
            },
            xactsend() {
                return this.$store.state.methodepriority.act_send
            },

            dialogformmethode: {
                get() {
                    return this.$store.state.methodepriority.dialog_form_methode
                },
                set(val) {
                    this.$store.commit("methodepriority/update_dialog_form_methode", val)
                }
            },
            isLoading() {
                return this.$store.state.methodepriority.search_status == 1
            },
            xmethodes(p) {
                return this.$store.state.methodepriority.methodes
            },
            xnoref: {
                get() {
                    return this.$store.state.methodepriority.noref
                },
                set(val) {
                    this.$store.commit("methodepriority/update_noref", val)
                }
            },
            namamethode: {
                get() {
                    return this.$store.state.methodepriority.namamethode
                },
                set(val) {
                    this.$store.commit("methodepriority/update_nama_methode", val)
                }
            },
            xdob: {
                get() {
                    return this.$store.state.methodepriority.dob
                },
                set(val) {
                    this.$store.commit("methodepriority/update_dob", val)
                }
            },
            xmethodees() {
                return this.$store.state.methodepriority.methodees
            },
            xmethode: {
                get() {
                    return this.$store.state.methodepriority.selected_methode
                },
                set(val) {
                    this.$store.commit("methodepriority/update_selected_methode", val)
                }
            },
            xorderautodays() {
                return this.$store.state.methodepriority.orderautodays
            },
            xorderautoday: {
                get() {
                    return this.$store.state.methodepriority.orderautoday
                },
                set(val) {
                    this.$store.commit("methodepriority/update_orderautoday", val)
                }
            },
            xorderdays() {
               
                xdays = this.$store.state.methodepriority.orderdays;
                console.log('xdays',xdays)
                return xdays
            },
            sub_total() {
                let days = this.xorderdays
                sub_total = 0
                days.forEach(function (vpx, idx) {
                    let total = parseInt(vpx.total)
                    sub_total = sub_total + total
                })
                return sub_total
            },
            dp() {
                let mindp = this.$store.state.methodepriority.selected_methodepriority.M_MouMinDP
                let dp = mindp / 100 * this.sub_total
                return dp
            },
            isbill() {
                let isbill = this.$store.state.methodepriority.selected_methodepriority.M_MouIsBill
                return isbill
            },
            isagingonhold() {
                let isagingonhold = this.$store.state.methodepriority.selected_methodepriority.M_MouIsAgingOnHold
                return isagingonhold
            },
            agingonhold() {
                let agingonhold = this.$store.state.methodepriority.selected_methodepriority.M_MouIsAgingOnHoldNote
                return agingonhold
            }
        },
        methods: {
            createNewMethode() {
                this.$store.commit("methodepriority/update_act_meth", 'new')
                this.xnoref = ''
                this.namamethode = ''
                this.$store.commit("methodepriority/update_selected_methode", [])
                this.xdob = ''
                this.$store.commit("methodepriority/update_orderdays", [])
                this.$store.commit("methodepriority/update_dialog_form_methode", true)
            },
            checkError(value) {
                var errors = this.$store.state.methodepriority.errors
                if (errors.includes(value)) {
                    return true
                } else {
                    return false
                }
            },
            changeDay(idx) {
                var arrOrders = this.$store.state.methodepriority.orderdays
                arrOrders[idx].isday = arrOrders[idx].isday === 'N' ? 'Y' : 'N'

                this.$store.commit("methodepriority/update_orderdays", arrOrders)
            },
            saveNewMethode() {
                var prm = {}
                prm.M_MethodePriorityNat_TestID = this.$store.state.methodepriority.selected_methodepriority
                    .Nat_TestID
                prm.Nat_MethodeName = this.$store.state.methodepriority.selected_methodepriority.Nat_MethodeName
                prm.M_MethodePriorityNat_MethodeID = this.$store.state.methodepriority.selected_methode.Nat_MethodeID
                prm.M_MethodePriorityNumber = this.xpriority
                prm.orderdays = this.xorderdays
                this.$store.dispatch("methodepriority/savenewmethode", prm)

            },
            editMethode(value) {
                this.$store.commit("methodepriority/update_act_meth", 'edit')
                this.$store.commit("methodepriority/update_x_meth_id", value.M_MethodePriorityID)
                this.xpriority = value.M_MethodePriorityNumber
                this.xinstrument = value.Nat_InstrumentName
                this.$store.commit("methodepriority/update_selected_methode", {
                    Nat_MethodeID: value.Nat_MethodeID,
                    Nat_MethodeName: value.Nat_MethodeName
                })
                this.isday = value.isday === 'N' ? false : true
                if ( value.M_DayOfWeekName == "No Local Setting") {
                   value.days.forEach(function(d,idx)  {
                      value.days[idx].isday = 'Y';
                   });
                }
                this.$store.commit("methodepriority/update_orderdays", value.days)
                this.$store.commit("methodepriority/update_dialog_form_methode", true)
            },
            saveEditMethode() {
                var prm = {}
                prm.M_MethodePriorityID = this.$store.state.methodepriority.x_meth_id
                prm.M_MethodePriorityNat_TestID = this.$store.state.methodepriority.selected_methodepriority
                    .Nat_TestID
                prm.Nat_MethodeName = this.$store.state.methodepriority.selected_methode.Nat_MethodeName
                prm.M_MethodePriorityNat_MethodeID = this.$store.state.methodepriority.selected_methode.Nat_MethodeID
                prm.M_MethodePriorityNumber = this.xpriority
                prm.orderdays = this.xorderdays
                this.$store.dispatch("methodepriority/saveeditmethode", prm)
            },
            thr_search_day: _.debounce(function () {
                this.$store.dispatch("methodepriority/searchday", {
                    day: this.search_day,
                    mouid: this.$store.state.methodepriority.selected_methodepriority.Nat_TestM_MouID
                })
            }, 2000),
            deleteMethode(value) {
                this.$store.commit("methodepriority/update_act_meth", 'delete')
                this.$store.commit("methodepriority/update_x_meth_id", value.M_MethodePriorityID)
                this.$store.commit("methodepriority/update_errors", [])
                var errors = this.$store.state.methodepriority.errors
                var msg = ''
                msg = "Yakin, akan menghapus data methode " + value.M_MethodePriorityName + " ?"
                this.$store.commit("methodepriority/update_msg_confirmation_delete_meth", msg)
                this.$store.commit("methodepriority/update_dialog_confirmation_delete_meth", true)
            },
            doDeletePat() {
                var prm = {}
                prm.M_MethodePriorityNat_TestID = this.$store.state.methodepriority.selected_methodepriority
                    .Nat_TestID
                prm.M_MethodePriorityID = this.$store.state.methodepriority.x_meth_id
                prm.M_MethodePriorityName = this.namamethode
                prm.M_CompanyName = this.$store.state.methodepriority.selected_methodepriority.M_CompanyName
                this.$store.dispatch("methodepriority/deletemethode", prm)
            },
            sendOrderMethode(value) {
                let msg = "Yakin, akan mengirim permintaan nomor " + this.$store.state.methodepriority.selected_methodepriority
                    .Nat_TestNumber + " ?"
                this.$store.commit("methodepriority/update_msg_confirmation_send", msg)
                this.$store.commit("methodepriority/update_dialog_confirmation_send", true)
            },
            doOrderMethode() {
                var prm = {}
                prm.Nat_TestID = this.$store.state.methodepriority.selected_methodepriority
                    .Nat_TestID
                prm.Nat_TestNumber = this.$store.state.methodepriority.selected_methodepriority
                    .Nat_TestNumber
                this.$store.dispatch("methodepriority/sendorder", prm)
            },
            convertMoney(money) {
                return one_money(money)
            },
            addTest() {
                var oldArr = this.$store.state.methodepriority.orderdays
                oldArr.push(this.$store.state.methodepriority.orderautoday)
                this.searchday = ''
                this.$store.commit("methodepriority/update_orderautodays", [])
                this.$store.commit("methodepriority/update_orderautoday", {})
            },
            deleteDataPx(idx) {
                var inx = parseInt(idx);
                var xrow = this.$store.state.methodepriority.orderdays[inx]
                if (parseInt(xrow.xid) !== 0) {
                    var xdelday = this.$store.state.methodepriority.deleted_orderdays
                    xdelday.push(xrow)
                    this.$store.commit("methodepriority/update_deleted_orderdays", xdelday)
                }
                var oldArr = this.$store.state.methodepriority.orderdays
                oldArr.splice(inx, 1)
                this.$store.commit("methodepriority/update_orderdays", oldArr)
            },
        },
        watch: {
            search_city(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.methodepriority.update_autocomplete_status == 1) return
                this.thr_search_city()
            },
            search_day(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.methodepriority.update_autocomplete_status == 1) return
                this.thr_search_day()
            }
        }
    }
</script>
